Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Allwinner sunXi SoCs Security ID support.
  4 *
  5 * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
  6 * Copyright (C) 2014 Maxime Ripard <maxime.ripard@free-electrons.com>
 
 
 
 
 
 
 
 
 
 
  7 */
  8
  9#include <linux/device.h>
 10#include <linux/io.h>
 11#include <linux/iopoll.h>
 12#include <linux/module.h>
 13#include <linux/nvmem-provider.h>
 14#include <linux/of.h>
 
 15#include <linux/platform_device.h>
 16#include <linux/slab.h>
 17#include <linux/random.h>
 18
 19/* Registers and special values for doing register-based SID readout on H3 */
 20#define SUN8I_SID_PRCTL		0x40
 21#define SUN8I_SID_RDKEY		0x60
 22
 23#define SUN8I_SID_OFFSET_MASK	0x1FF
 24#define SUN8I_SID_OFFSET_SHIFT	16
 25#define SUN8I_SID_OP_LOCK	(0xAC << 8)
 26#define SUN8I_SID_READ		BIT(1)
 27
 
 
 
 
 
 
 
 28struct sunxi_sid_cfg {
 29	u32	value_offset;
 30	u32	size;
 31	bool	need_register_readout;
 32};
 33
 34struct sunxi_sid {
 35	void __iomem		*base;
 36	u32			value_offset;
 37};
 38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 39static int sunxi_sid_read(void *context, unsigned int offset,
 40			  void *val, size_t bytes)
 41{
 42	struct sunxi_sid *sid = context;
 43	u32 word;
 44
 45	/* .stride = 4 so offset is guaranteed to be aligned */
 46	__ioread32_copy(val, sid->base + sid->value_offset + offset, bytes / 4);
 47
 48	val += round_down(bytes, 4);
 49	offset += round_down(bytes, 4);
 50	bytes = bytes % 4;
 51
 52	if (!bytes)
 53		return 0;
 54
 55	/* Handle any trailing bytes */
 56	word = readl_relaxed(sid->base + sid->value_offset + offset);
 57	memcpy(val, &word, bytes);
 58
 59	return 0;
 60}
 61
 62static int sun8i_sid_register_readout(const struct sunxi_sid *sid,
 63				      const unsigned int offset,
 64				      u32 *out)
 65{
 66	u32 reg_val;
 67	int ret;
 68
 69	/* Set word, lock access, and set read command */
 70	reg_val = (offset & SUN8I_SID_OFFSET_MASK)
 71		  << SUN8I_SID_OFFSET_SHIFT;
 72	reg_val |= SUN8I_SID_OP_LOCK | SUN8I_SID_READ;
 73	writel(reg_val, sid->base + SUN8I_SID_PRCTL);
 74
 75	ret = readl_poll_timeout(sid->base + SUN8I_SID_PRCTL, reg_val,
 76				 !(reg_val & SUN8I_SID_READ), 100, 250000);
 77	if (ret)
 78		return ret;
 79
 80	if (out)
 81		*out = readl(sid->base + SUN8I_SID_RDKEY);
 82
 83	writel(0, sid->base + SUN8I_SID_PRCTL);
 84
 85	return 0;
 86}
 87
 88/*
 89 * On Allwinner H3, the value on the 0x200 offset of the SID controller seems
 90 * to be not reliable at all.
 91 * Read by the registers instead.
 92 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 93static int sun8i_sid_read_by_reg(void *context, unsigned int offset,
 94				 void *val, size_t bytes)
 95{
 96	struct sunxi_sid *sid = context;
 97	u32 word;
 98	int ret;
 99
100	/* .stride = 4 so offset is guaranteed to be aligned */
101	while (bytes >= 4) {
102		ret = sun8i_sid_register_readout(sid, offset, val);
103		if (ret)
104			return ret;
105
106		val += 4;
107		offset += 4;
108		bytes -= 4;
109	}
110
111	if (!bytes)
112		return 0;
113
114	/* Handle any trailing bytes */
115	ret = sun8i_sid_register_readout(sid, offset, &word);
116	if (ret)
117		return ret;
118
119	memcpy(val, &word, bytes);
120
121	return 0;
122}
123
124static int sunxi_sid_probe(struct platform_device *pdev)
125{
126	struct device *dev = &pdev->dev;
127	struct nvmem_config *nvmem_cfg;
128	struct nvmem_device *nvmem;
129	struct sunxi_sid *sid;
130	int size;
131	char *randomness;
132	const struct sunxi_sid_cfg *cfg;
133
134	sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
135	if (!sid)
136		return -ENOMEM;
137
138	cfg = of_device_get_match_data(dev);
139	if (!cfg)
140		return -EINVAL;
141	sid->value_offset = cfg->value_offset;
142
143	sid->base = devm_platform_ioremap_resource(pdev, 0);
 
144	if (IS_ERR(sid->base))
145		return PTR_ERR(sid->base);
146
147	size = cfg->size;
148
149	nvmem_cfg = devm_kzalloc(dev, sizeof(*nvmem_cfg), GFP_KERNEL);
150	if (!nvmem_cfg)
151		return -ENOMEM;
152
153	nvmem_cfg->dev = dev;
154	nvmem_cfg->name = "sunxi-sid";
155	nvmem_cfg->type = NVMEM_TYPE_OTP;
156	nvmem_cfg->add_legacy_fixed_of_cells = true;
157	nvmem_cfg->read_only = true;
158	nvmem_cfg->size = cfg->size;
159	nvmem_cfg->word_size = 1;
160	nvmem_cfg->stride = 4;
161	nvmem_cfg->priv = sid;
162	if (cfg->need_register_readout)
163		nvmem_cfg->reg_read = sun8i_sid_read_by_reg;
164	else
165		nvmem_cfg->reg_read = sunxi_sid_read;
166
167	nvmem = devm_nvmem_register(dev, nvmem_cfg);
168	if (IS_ERR(nvmem))
169		return PTR_ERR(nvmem);
170
171	randomness = kzalloc(size, GFP_KERNEL);
172	if (!randomness)
173		return -ENOMEM;
 
 
 
 
 
174
175	nvmem_cfg->reg_read(sid, 0, randomness, size);
176	add_device_randomness(randomness, size);
177	kfree(randomness);
178
179	platform_set_drvdata(pdev, nvmem);
180
181	return 0;
 
 
 
 
 
 
 
 
 
 
 
182}
183
184static const struct sunxi_sid_cfg sun4i_a10_cfg = {
185	.size = 0x10,
186};
187
188static const struct sunxi_sid_cfg sun7i_a20_cfg = {
189	.size = 0x200,
190};
191
192static const struct sunxi_sid_cfg sun8i_h3_cfg = {
193	.value_offset = 0x200,
194	.size = 0x100,
195	.need_register_readout = true,
196};
197
198static const struct sunxi_sid_cfg sun50i_a64_cfg = {
199	.value_offset = 0x200,
200	.size = 0x100,
201};
202
203static const struct sunxi_sid_cfg sun50i_h6_cfg = {
204	.value_offset = 0x200,
205	.size = 0x200,
206};
207
208static const struct of_device_id sunxi_sid_of_match[] = {
209	{ .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg },
210	{ .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg },
211	{ .compatible = "allwinner,sun8i-a83t-sid", .data = &sun50i_a64_cfg },
212	{ .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg },
213	{ .compatible = "allwinner,sun20i-d1-sid", .data = &sun50i_a64_cfg },
214	{ .compatible = "allwinner,sun50i-a64-sid", .data = &sun50i_a64_cfg },
215	{ .compatible = "allwinner,sun50i-h5-sid", .data = &sun50i_a64_cfg },
216	{ .compatible = "allwinner,sun50i-h6-sid", .data = &sun50i_h6_cfg },
217	{/* sentinel */},
218};
219MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
220
221static struct platform_driver sunxi_sid_driver = {
222	.probe = sunxi_sid_probe,
 
223	.driver = {
224		.name = "eeprom-sunxi-sid",
225		.of_match_table = sunxi_sid_of_match,
226	},
227};
228module_platform_driver(sunxi_sid_driver);
229
230MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
231MODULE_DESCRIPTION("Allwinner sunxi security id driver");
232MODULE_LICENSE("GPL");
v4.17
 
  1/*
  2 * Allwinner sunXi SoCs Security ID support.
  3 *
  4 * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
  5 * Copyright (C) 2014 Maxime Ripard <maxime.ripard@free-electrons.com>
  6 *
  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.
 11 *
 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.
 16 */
 17
 18#include <linux/device.h>
 19#include <linux/io.h>
 20#include <linux/iopoll.h>
 21#include <linux/module.h>
 22#include <linux/nvmem-provider.h>
 23#include <linux/of.h>
 24#include <linux/of_device.h>
 25#include <linux/platform_device.h>
 26#include <linux/slab.h>
 27#include <linux/random.h>
 28
 29/* Registers and special values for doing register-based SID readout on H3 */
 30#define SUN8I_SID_PRCTL		0x40
 31#define SUN8I_SID_RDKEY		0x60
 32
 33#define SUN8I_SID_OFFSET_MASK	0x1FF
 34#define SUN8I_SID_OFFSET_SHIFT	16
 35#define SUN8I_SID_OP_LOCK	(0xAC << 8)
 36#define SUN8I_SID_READ		BIT(1)
 37
 38static struct nvmem_config econfig = {
 39	.name = "sunxi-sid",
 40	.read_only = true,
 41	.stride = 4,
 42	.word_size = 1,
 43};
 44
 45struct sunxi_sid_cfg {
 46	u32	value_offset;
 47	u32	size;
 48	bool	need_register_readout;
 49};
 50
 51struct sunxi_sid {
 52	void __iomem		*base;
 53	u32			value_offset;
 54};
 55
 56/* We read the entire key, due to a 32 bit read alignment requirement. Since we
 57 * want to return the requested byte, this results in somewhat slower code and
 58 * uses 4 times more reads as needed but keeps code simpler. Since the SID is
 59 * only very rarely probed, this is not really an issue.
 60 */
 61static u8 sunxi_sid_read_byte(const struct sunxi_sid *sid,
 62			      const unsigned int offset)
 63{
 64	u32 sid_key;
 65
 66	sid_key = ioread32be(sid->base + round_down(offset, 4));
 67	sid_key >>= (offset % 4) * 8;
 68
 69	return sid_key; /* Only return the last byte */
 70}
 71
 72static int sunxi_sid_read(void *context, unsigned int offset,
 73			  void *val, size_t bytes)
 74{
 75	struct sunxi_sid *sid = context;
 76	u8 *buf = val;
 77
 78	/* Offset the read operation to the real position of SID */
 79	offset += sid->value_offset;
 80
 81	while (bytes--)
 82		*buf++ = sunxi_sid_read_byte(sid, offset++);
 
 
 
 
 
 
 
 
 83
 84	return 0;
 85}
 86
 87static int sun8i_sid_register_readout(const struct sunxi_sid *sid,
 88				      const unsigned int offset,
 89				      u32 *out)
 90{
 91	u32 reg_val;
 92	int ret;
 93
 94	/* Set word, lock access, and set read command */
 95	reg_val = (offset & SUN8I_SID_OFFSET_MASK)
 96		  << SUN8I_SID_OFFSET_SHIFT;
 97	reg_val |= SUN8I_SID_OP_LOCK | SUN8I_SID_READ;
 98	writel(reg_val, sid->base + SUN8I_SID_PRCTL);
 99
100	ret = readl_poll_timeout(sid->base + SUN8I_SID_PRCTL, reg_val,
101				 !(reg_val & SUN8I_SID_READ), 100, 250000);
102	if (ret)
103		return ret;
104
105	if (out)
106		*out = readl(sid->base + SUN8I_SID_RDKEY);
107
108	writel(0, sid->base + SUN8I_SID_PRCTL);
109
110	return 0;
111}
112
113/*
114 * On Allwinner H3, the value on the 0x200 offset of the SID controller seems
115 * to be not reliable at all.
116 * Read by the registers instead.
117 */
118static int sun8i_sid_read_byte_by_reg(const struct sunxi_sid *sid,
119				      const unsigned int offset,
120				      u8 *out)
121{
122	u32 word;
123	int ret;
124
125	ret = sun8i_sid_register_readout(sid, offset & ~0x03, &word);
126
127	if (ret)
128		return ret;
129
130	*out = (word >> ((offset & 0x3) * 8)) & 0xff;
131
132	return 0;
133}
134
135static int sun8i_sid_read_by_reg(void *context, unsigned int offset,
136				 void *val, size_t bytes)
137{
138	struct sunxi_sid *sid = context;
139	u8 *buf = val;
140	int ret;
141
142	while (bytes--) {
143		ret = sun8i_sid_read_byte_by_reg(sid, offset++, buf++);
 
144		if (ret)
145			return ret;
 
 
 
 
146	}
147
 
 
 
 
 
 
 
 
 
 
148	return 0;
149}
150
151static int sunxi_sid_probe(struct platform_device *pdev)
152{
153	struct device *dev = &pdev->dev;
154	struct resource *res;
155	struct nvmem_device *nvmem;
156	struct sunxi_sid *sid;
157	int ret, i, size;
158	char *randomness;
159	const struct sunxi_sid_cfg *cfg;
160
161	sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
162	if (!sid)
163		return -ENOMEM;
164
165	cfg = of_device_get_match_data(dev);
166	if (!cfg)
167		return -EINVAL;
168	sid->value_offset = cfg->value_offset;
169
170	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
171	sid->base = devm_ioremap_resource(dev, res);
172	if (IS_ERR(sid->base))
173		return PTR_ERR(sid->base);
174
175	size = cfg->size;
176
177	econfig.size = size;
178	econfig.dev = dev;
 
 
 
 
 
 
 
 
 
 
 
179	if (cfg->need_register_readout)
180		econfig.reg_read = sun8i_sid_read_by_reg;
181	else
182		econfig.reg_read = sunxi_sid_read;
183	econfig.priv = sid;
184	nvmem = nvmem_register(&econfig);
185	if (IS_ERR(nvmem))
186		return PTR_ERR(nvmem);
187
188	randomness = kzalloc(sizeof(u8) * (size), GFP_KERNEL);
189	if (!randomness) {
190		ret = -EINVAL;
191		goto err_unreg_nvmem;
192	}
193
194	for (i = 0; i < size; i++)
195		econfig.reg_read(sid, i, &randomness[i], 1);
196
 
197	add_device_randomness(randomness, size);
198	kfree(randomness);
199
200	platform_set_drvdata(pdev, nvmem);
201
202	return 0;
203
204err_unreg_nvmem:
205	nvmem_unregister(nvmem);
206	return ret;
207}
208
209static int sunxi_sid_remove(struct platform_device *pdev)
210{
211	struct nvmem_device *nvmem = platform_get_drvdata(pdev);
212
213	return nvmem_unregister(nvmem);
214}
215
216static const struct sunxi_sid_cfg sun4i_a10_cfg = {
217	.size = 0x10,
218};
219
220static const struct sunxi_sid_cfg sun7i_a20_cfg = {
221	.size = 0x200,
222};
223
224static const struct sunxi_sid_cfg sun8i_h3_cfg = {
225	.value_offset = 0x200,
226	.size = 0x100,
227	.need_register_readout = true,
228};
229
230static const struct sunxi_sid_cfg sun50i_a64_cfg = {
231	.value_offset = 0x200,
232	.size = 0x100,
233};
234
 
 
 
 
 
235static const struct of_device_id sunxi_sid_of_match[] = {
236	{ .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg },
237	{ .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg },
 
238	{ .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg },
 
239	{ .compatible = "allwinner,sun50i-a64-sid", .data = &sun50i_a64_cfg },
 
 
240	{/* sentinel */},
241};
242MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
243
244static struct platform_driver sunxi_sid_driver = {
245	.probe = sunxi_sid_probe,
246	.remove = sunxi_sid_remove,
247	.driver = {
248		.name = "eeprom-sunxi-sid",
249		.of_match_table = sunxi_sid_of_match,
250	},
251};
252module_platform_driver(sunxi_sid_driver);
253
254MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
255MODULE_DESCRIPTION("Allwinner sunxi security id driver");
256MODULE_LICENSE("GPL");