2 * Allwinner sunXi SoCs Security ID support.
4 * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
5 * Copyright (C) 2014 Maxime Ripard <maxime.ripard@free-electrons.com>
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/regmap.h>
25 #include <linux/slab.h>
26 #include <linux/random.h>
28 static struct nvmem_config econfig
= {
38 /* We read the entire key, due to a 32 bit read alignment requirement. Since we
39 * want to return the requested byte, this results in somewhat slower code and
40 * uses 4 times more reads as needed but keeps code simpler. Since the SID is
41 * only very rarely probed, this is not really an issue.
43 static u8
sunxi_sid_read_byte(const struct sunxi_sid
*sid
,
44 const unsigned int offset
)
48 sid_key
= ioread32be(sid
->base
+ round_down(offset
, 4));
49 sid_key
>>= (offset
% 4) * 8;
51 return sid_key
; /* Only return the last byte */
54 static int sunxi_sid_read(void *context
,
55 const void *reg
, size_t reg_size
,
56 void *val
, size_t val_size
)
58 struct sunxi_sid
*sid
= context
;
59 unsigned int offset
= *(u32
*)reg
;
63 *buf
++ = sunxi_sid_read_byte(sid
, offset
);
71 static int sunxi_sid_write(void *context
, const void *data
, size_t count
)
73 /* Unimplemented, dummy to keep regmap core happy */
77 static struct regmap_bus sunxi_sid_bus
= {
78 .read
= sunxi_sid_read
,
79 .write
= sunxi_sid_write
,
80 .reg_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
81 .val_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
84 static bool sunxi_sid_writeable_reg(struct device
*dev
, unsigned int reg
)
89 static struct regmap_config sunxi_sid_regmap_config
= {
93 .writeable_reg
= sunxi_sid_writeable_reg
,
96 static int sunxi_sid_probe(struct platform_device
*pdev
)
98 struct device
*dev
= &pdev
->dev
;
100 struct nvmem_device
*nvmem
;
101 struct regmap
*regmap
;
102 struct sunxi_sid
*sid
;
106 sid
= devm_kzalloc(dev
, sizeof(*sid
), GFP_KERNEL
);
110 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
111 sid
->base
= devm_ioremap_resource(dev
, res
);
112 if (IS_ERR(sid
->base
))
113 return PTR_ERR(sid
->base
);
115 size
= resource_size(res
) - 1;
116 sunxi_sid_regmap_config
.max_register
= size
;
118 regmap
= devm_regmap_init(dev
, &sunxi_sid_bus
, sid
,
119 &sunxi_sid_regmap_config
);
120 if (IS_ERR(regmap
)) {
121 dev_err(dev
, "regmap init failed\n");
122 return PTR_ERR(regmap
);
126 nvmem
= nvmem_register(&econfig
);
128 return PTR_ERR(nvmem
);
130 randomness
= kzalloc(sizeof(u8
) * (size
), GFP_KERNEL
);
133 goto err_unreg_nvmem
;
136 for (i
= 0; i
< size
; i
++)
137 randomness
[i
] = sunxi_sid_read_byte(sid
, i
);
139 add_device_randomness(randomness
, size
);
142 platform_set_drvdata(pdev
, nvmem
);
147 nvmem_unregister(nvmem
);
151 static int sunxi_sid_remove(struct platform_device
*pdev
)
153 struct nvmem_device
*nvmem
= platform_get_drvdata(pdev
);
155 return nvmem_unregister(nvmem
);
158 static const struct of_device_id sunxi_sid_of_match
[] = {
159 { .compatible
= "allwinner,sun4i-a10-sid" },
160 { .compatible
= "allwinner,sun7i-a20-sid" },
163 MODULE_DEVICE_TABLE(of
, sunxi_sid_of_match
);
165 static struct platform_driver sunxi_sid_driver
= {
166 .probe
= sunxi_sid_probe
,
167 .remove
= sunxi_sid_remove
,
169 .name
= "eeprom-sunxi-sid",
170 .of_match_table
= sunxi_sid_of_match
,
173 module_platform_driver(sunxi_sid_driver
);
175 MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
176 MODULE_DESCRIPTION("Allwinner sunxi security id driver");
177 MODULE_LICENSE("GPL");