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-
19 #include <linux/compiler.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/export.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>
36 #define DRV_NAME "sunxi-sid"
38 struct sunxi_sid_data
{
39 void __iomem
*reg_base
;
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.
48 static u8
sunxi_sid_read_byte(const struct sunxi_sid_data
*sid_data
,
49 const unsigned int offset
)
53 if (offset
>= sid_data
->keysize
)
56 sid_key
= ioread32be(sid_data
->reg_base
+ round_down(offset
, 4));
57 sid_key
>>= (offset
% 4) * 8;
59 return sid_key
; /* Only return the last byte */
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
)
66 struct platform_device
*pdev
;
67 struct sunxi_sid_data
*sid_data
;
70 pdev
= to_platform_device(kobj_to_dev(kobj
));
71 sid_data
= platform_get_drvdata(pdev
);
73 if (pos
< 0 || pos
>= sid_data
->keysize
)
75 if (size
> sid_data
->keysize
- pos
)
76 size
= sid_data
->keysize
- pos
;
78 for (i
= 0; i
< size
; i
++)
79 buf
[i
] = sunxi_sid_read_byte(sid_data
, pos
+ i
);
84 static struct bin_attribute sid_bin_attr
= {
85 .attr
= { .name
= "eeprom", .mode
= S_IRUGO
, },
89 static int sunxi_sid_remove(struct platform_device
*pdev
)
91 device_remove_bin_file(&pdev
->dev
, &sid_bin_attr
);
92 dev_dbg(&pdev
->dev
, "driver unloaded\n");
97 static const struct of_device_id sunxi_sid_of_match
[] = {
98 { .compatible
= "allwinner,sun4i-a10-sid", .data
= (void *)16},
99 { .compatible
= "allwinner,sun7i-a20-sid", .data
= (void *)512},
102 MODULE_DEVICE_TABLE(of
, sunxi_sid_of_match
);
104 static int sunxi_sid_probe(struct platform_device
*pdev
)
106 struct sunxi_sid_data
*sid_data
;
107 struct resource
*res
;
108 const struct of_device_id
*of_dev_id
;
112 sid_data
= devm_kzalloc(&pdev
->dev
, sizeof(struct sunxi_sid_data
),
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
);
122 of_dev_id
= of_match_device(sunxi_sid_of_match
, &pdev
->dev
);
125 sid_data
->keysize
= (int)of_dev_id
->data
;
127 platform_set_drvdata(pdev
, sid_data
);
129 sid_bin_attr
.size
= sid_data
->keysize
;
130 if (device_create_bin_file(&pdev
->dev
, &sid_bin_attr
))
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
);
139 dev_dbg(&pdev
->dev
, "loaded\n");
144 static struct platform_driver sunxi_sid_driver
= {
145 .probe
= sunxi_sid_probe
,
146 .remove
= sunxi_sid_remove
,
149 .of_match_table
= sunxi_sid_of_match
,
152 module_platform_driver(sunxi_sid_driver
);
154 MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
155 MODULE_DESCRIPTION("Allwinner sunxi security id driver");
156 MODULE_LICENSE("GPL");