e137e75a89e584e245e70f45e57abdcfeca67bf0
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / misc / eeprom / sunxi_sid.c
1 /*
2  * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
3  * http://www.linux-sunxi.org
4  *
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.
9  *
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.
14  *
15  * This driver exposes the Allwinner security ID, efuses exported in byte-
16  * sized chunks.
17  */
18
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>
36
37 #define DRV_NAME "sunxi-sid"
38
39 struct sunxi_sid_data {
40         void __iomem *reg_base;
41         unsigned int keysize;
42 };
43
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.
48  */
49 static u8 sunxi_sid_read_byte(const struct sunxi_sid_data *sid_data,
50                               const unsigned int offset)
51 {
52         u32 sid_key;
53
54         if (offset >= sid_data->keysize)
55                 return 0;
56
57         sid_key = ioread32be(sid_data->reg_base + round_down(offset, 4));
58         sid_key >>= (offset % 4) * 8;
59
60         return sid_key; /* Only return the last byte */
61 }
62
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)
66 {
67         struct platform_device *pdev;
68         struct sunxi_sid_data *sid_data;
69         int i;
70
71         pdev = to_platform_device(kobj_to_dev(kobj));
72         sid_data = platform_get_drvdata(pdev);
73
74         if (pos < 0 || pos >= sid_data->keysize)
75                 return 0;
76         if (size > sid_data->keysize - pos)
77                 size = sid_data->keysize - pos;
78
79         for (i = 0; i < size; i++)
80                 buf[i] = sunxi_sid_read_byte(sid_data, pos + i);
81
82         return i;
83 }
84
85 static struct bin_attribute sid_bin_attr = {
86         .attr = { .name = "eeprom", .mode = S_IRUGO, },
87         .read = sid_read,
88 };
89
90 static int sunxi_sid_remove(struct platform_device *pdev)
91 {
92         device_remove_bin_file(&pdev->dev, &sid_bin_attr);
93         dev_dbg(&pdev->dev, "driver unloaded\n");
94
95         return 0;
96 }
97
98 static const struct of_device_id sunxi_sid_of_match[] = {
99         { .compatible = "allwinner,sun4i-a10-sid", .data = (void *)16},
100         { .compatible = "allwinner,sun7i-a20-sid", .data = (void *)512},
101
102         /* Deprecated */
103         { .compatible = "allwinner,sun4i-sid", .data = (void *)16},
104         {/* sentinel */},
105 };
106 MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
107
108 static int sunxi_sid_probe(struct platform_device *pdev)
109 {
110         struct sunxi_sid_data *sid_data;
111         struct resource *res;
112         const struct of_device_id *of_dev_id;
113         u8 *entropy;
114         unsigned int i;
115
116         sid_data = devm_kzalloc(&pdev->dev, sizeof(struct sunxi_sid_data),
117                                 GFP_KERNEL);
118         if (!sid_data)
119                 return -ENOMEM;
120
121         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122         sid_data->reg_base = devm_ioremap_resource(&pdev->dev, res);
123         if (IS_ERR(sid_data->reg_base))
124                 return PTR_ERR(sid_data->reg_base);
125
126         of_dev_id = of_match_device(sunxi_sid_of_match, &pdev->dev);
127         if (!of_dev_id)
128                 return -ENODEV;
129         sid_data->keysize = (int)of_dev_id->data;
130
131         platform_set_drvdata(pdev, sid_data);
132
133         sid_bin_attr.size = sid_data->keysize;
134         if (device_create_bin_file(&pdev->dev, &sid_bin_attr))
135                 return -ENODEV;
136
137         entropy = kzalloc(sizeof(u8) * sid_data->keysize, GFP_KERNEL);
138         for (i = 0; i < sid_data->keysize; i++)
139                 entropy[i] = sunxi_sid_read_byte(sid_data, i);
140         add_device_randomness(entropy, sid_data->keysize);
141         kfree(entropy);
142
143         dev_dbg(&pdev->dev, "loaded\n");
144
145         return 0;
146 }
147
148 static struct platform_driver sunxi_sid_driver = {
149         .probe = sunxi_sid_probe,
150         .remove = sunxi_sid_remove,
151         .driver = {
152                 .name = DRV_NAME,
153                 .owner = THIS_MODULE,
154                 .of_match_table = sunxi_sid_of_match,
155         },
156 };
157 module_platform_driver(sunxi_sid_driver);
158
159 MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
160 MODULE_DESCRIPTION("Allwinner sunxi security id driver");
161 MODULE_LICENSE("GPL");