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.
20 #include <linux/device.h>
22 #include <linux/module.h>
23 #include <linux/nvmem-provider.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28 #include <linux/random.h>
31 static struct nvmem_config econfig
= {
41 /* We read the entire key, due to a 32 bit read alignment requirement. Since we
42 * want to return the requested byte, this results in somewhat slower code and
43 * uses 4 times more reads as needed but keeps code simpler. Since the SID is
44 * only very rarely probed, this is not really an issue.
46 static u8
sunxi_sid_read_byte(const struct sunxi_sid
*sid
,
47 const unsigned int offset
)
51 sid_key
= ioread32be(sid
->base
+ round_down(offset
, 4));
52 sid_key
>>= (offset
% 4) * 8;
54 return sid_key
; /* Only return the last byte */
57 static int sunxi_sid_read(void *context
,
58 const void *reg
, size_t reg_size
,
59 void *val
, size_t val_size
)
61 struct sunxi_sid
*sid
= context
;
62 unsigned int offset
= *(u32
*)reg
;
66 *buf
++ = sunxi_sid_read_byte(sid
, offset
);
74 static int sunxi_sid_write(void *context
, const void *data
, size_t count
)
76 /* Unimplemented, dummy to keep regmap core happy */
80 static struct regmap_bus sunxi_sid_bus
= {
81 .read
= sunxi_sid_read
,
82 .write
= sunxi_sid_write
,
83 .reg_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
84 .val_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
87 static bool sunxi_sid_writeable_reg(struct device
*dev
, unsigned int reg
)
92 static struct regmap_config sunxi_sid_regmap_config
= {
96 .writeable_reg
= sunxi_sid_writeable_reg
,
99 static int sunxi_sid_probe(struct platform_device
*pdev
)
101 struct device
*dev
= &pdev
->dev
;
102 struct resource
*res
;
103 struct nvmem_device
*nvmem
;
104 struct regmap
*regmap
;
105 struct sunxi_sid
*sid
;
109 sid
= devm_kzalloc(dev
, sizeof(*sid
), GFP_KERNEL
);
113 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
114 sid
->base
= devm_ioremap_resource(dev
, res
);
115 if (IS_ERR(sid
->base
))
116 return PTR_ERR(sid
->base
);
118 size
= resource_size(res
) - 1;
119 sunxi_sid_regmap_config
.max_register
= size
;
121 regmap
= devm_regmap_init(dev
, &sunxi_sid_bus
, sid
,
122 &sunxi_sid_regmap_config
);
123 if (IS_ERR(regmap
)) {
124 dev_err(dev
, "regmap init failed\n");
125 return PTR_ERR(regmap
);
129 nvmem
= nvmem_register(&econfig
);
131 return PTR_ERR(nvmem
);
133 randomness
= kzalloc(sizeof(u8
) * size
, GFP_KERNEL
);
136 goto err_unreg_nvmem
;
139 for (i
= 0; i
< size
; i
++)
140 randomness
[i
] = sunxi_sid_read_byte(sid
, i
);
142 add_device_randomness(randomness
, size
);
145 platform_set_drvdata(pdev
, nvmem
);
150 nvmem_unregister(nvmem
);
154 static int sunxi_sid_remove(struct platform_device
*pdev
)
156 struct nvmem_device
*nvmem
= platform_get_drvdata(pdev
);
158 return nvmem_unregister(nvmem
);
161 static const struct of_device_id sunxi_sid_of_match
[] = {
162 { .compatible
= "allwinner,sun4i-a10-sid" },
163 { .compatible
= "allwinner,sun7i-a20-sid" },
166 MODULE_DEVICE_TABLE(of
, sunxi_sid_of_match
);
168 static struct platform_driver sunxi_sid_driver
= {
169 .probe
= sunxi_sid_probe
,
170 .remove
= sunxi_sid_remove
,
172 .name
= "eeprom-sunxi-sid",
173 .of_match_table
= sunxi_sid_of_match
,
176 module_platform_driver(sunxi_sid_driver
);
178 MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
179 MODULE_DESCRIPTION("Allwinner sunxi security id driver");
180 MODULE_LICENSE("GPL");