]>
Commit | Line | Data |
---|---|---|
9fd379e9 OS |
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> | |
9fd379e9 OS |
24 | #include <linux/io.h> |
25 | #include <linux/kernel.h> | |
26 | #include <linux/kobject.h> | |
27 | #include <linux/module.h> | |
28 | #include <linux/of_device.h> | |
29 | #include <linux/platform_device.h> | |
30 | #include <linux/random.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/stat.h> | |
33 | #include <linux/sysfs.h> | |
34 | #include <linux/types.h> | |
35 | ||
36 | #define DRV_NAME "sunxi-sid" | |
37 | ||
38 | struct sunxi_sid_data { | |
39 | void __iomem *reg_base; | |
40 | unsigned int keysize; | |
41 | }; | |
42 | ||
43 | /* We read the entire key, due to a 32 bit read alignment requirement. Since we | |
44 | * want to return the requested byte, this results in somewhat slower code and | |
45 | * uses 4 times more reads as needed but keeps code simpler. Since the SID is | |
46 | * only very rarely probed, this is not really an issue. | |
47 | */ | |
48 | static u8 sunxi_sid_read_byte(const struct sunxi_sid_data *sid_data, | |
49 | const unsigned int offset) | |
50 | { | |
51 | u32 sid_key; | |
52 | ||
53 | if (offset >= sid_data->keysize) | |
54 | return 0; | |
55 | ||
56 | sid_key = ioread32be(sid_data->reg_base + round_down(offset, 4)); | |
57 | sid_key >>= (offset % 4) * 8; | |
58 | ||
59 | return sid_key; /* Only return the last byte */ | |
60 | } | |
61 | ||
62 | static ssize_t sid_read(struct file *fd, struct kobject *kobj, | |
63 | struct bin_attribute *attr, char *buf, | |
64 | loff_t pos, size_t size) | |
65 | { | |
66 | struct platform_device *pdev; | |
67 | struct sunxi_sid_data *sid_data; | |
68 | int i; | |
69 | ||
70 | pdev = to_platform_device(kobj_to_dev(kobj)); | |
71 | sid_data = platform_get_drvdata(pdev); | |
72 | ||
73 | if (pos < 0 || pos >= sid_data->keysize) | |
74 | return 0; | |
75 | if (size > sid_data->keysize - pos) | |
76 | size = sid_data->keysize - pos; | |
77 | ||
78 | for (i = 0; i < size; i++) | |
79 | buf[i] = sunxi_sid_read_byte(sid_data, pos + i); | |
80 | ||
81 | return i; | |
82 | } | |
83 | ||
84 | static struct bin_attribute sid_bin_attr = { | |
85 | .attr = { .name = "eeprom", .mode = S_IRUGO, }, | |
86 | .read = sid_read, | |
87 | }; | |
88 | ||
89 | static int sunxi_sid_remove(struct platform_device *pdev) | |
90 | { | |
91 | device_remove_bin_file(&pdev->dev, &sid_bin_attr); | |
92 | dev_dbg(&pdev->dev, "driver unloaded\n"); | |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
97 | static const struct of_device_id sunxi_sid_of_match[] = { | |
1e700846 | 98 | { .compatible = "allwinner,sun4i-a10-sid", .data = (void *)16}, |
9fd379e9 OS |
99 | { .compatible = "allwinner,sun7i-a20-sid", .data = (void *)512}, |
100 | {/* sentinel */}, | |
101 | }; | |
102 | MODULE_DEVICE_TABLE(of, sunxi_sid_of_match); | |
103 | ||
104 | static int sunxi_sid_probe(struct platform_device *pdev) | |
105 | { | |
106 | struct sunxi_sid_data *sid_data; | |
107 | struct resource *res; | |
108 | const struct of_device_id *of_dev_id; | |
109 | u8 *entropy; | |
110 | unsigned int i; | |
111 | ||
112 | sid_data = devm_kzalloc(&pdev->dev, sizeof(struct sunxi_sid_data), | |
113 | GFP_KERNEL); | |
114 | if (!sid_data) | |
115 | return -ENOMEM; | |
116 | ||
117 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
118 | sid_data->reg_base = devm_ioremap_resource(&pdev->dev, res); | |
119 | if (IS_ERR(sid_data->reg_base)) | |
120 | return PTR_ERR(sid_data->reg_base); | |
121 | ||
122 | of_dev_id = of_match_device(sunxi_sid_of_match, &pdev->dev); | |
123 | if (!of_dev_id) | |
124 | return -ENODEV; | |
125 | sid_data->keysize = (int)of_dev_id->data; | |
126 | ||
127 | platform_set_drvdata(pdev, sid_data); | |
128 | ||
129 | sid_bin_attr.size = sid_data->keysize; | |
130 | if (device_create_bin_file(&pdev->dev, &sid_bin_attr)) | |
131 | return -ENODEV; | |
132 | ||
133 | entropy = kzalloc(sizeof(u8) * sid_data->keysize, GFP_KERNEL); | |
134 | for (i = 0; i < sid_data->keysize; i++) | |
135 | entropy[i] = sunxi_sid_read_byte(sid_data, i); | |
136 | add_device_randomness(entropy, sid_data->keysize); | |
137 | kfree(entropy); | |
138 | ||
139 | dev_dbg(&pdev->dev, "loaded\n"); | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | static struct platform_driver sunxi_sid_driver = { | |
145 | .probe = sunxi_sid_probe, | |
146 | .remove = sunxi_sid_remove, | |
147 | .driver = { | |
148 | .name = DRV_NAME, | |
9fd379e9 OS |
149 | .of_match_table = sunxi_sid_of_match, |
150 | }, | |
151 | }; | |
152 | module_platform_driver(sunxi_sid_driver); | |
153 | ||
154 | MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>"); | |
155 | MODULE_DESCRIPTION("Allwinner sunxi security id driver"); | |
156 | MODULE_LICENSE("GPL"); |