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/of_device.h> 16 #include <linux/platform_device.h> 17 #include <linux/slab.h> 18 #include <linux/random.h> 19 20 /* Registers and special values for doing register-based SID readout on H3 */ 21 #define SUN8I_SID_PRCTL 0x40 22 #define SUN8I_SID_RDKEY 0x60 23 24 #define SUN8I_SID_OFFSET_MASK 0x1FF 25 #define SUN8I_SID_OFFSET_SHIFT 16 26 #define SUN8I_SID_OP_LOCK (0xAC << 8) 27 #define SUN8I_SID_READ BIT(1) 28 29 struct sunxi_sid_cfg { 30 u32 value_offset; 31 u32 size; 32 bool need_register_readout; 33 }; 34 35 struct sunxi_sid { 36 void __iomem *base; 37 u32 value_offset; 38 }; 39 40 static int sunxi_sid_read(void *context, unsigned int offset, 41 void *val, size_t bytes) 42 { 43 struct sunxi_sid *sid = context; 44 u32 word; 45 46 /* .stride = 4 so offset is guaranteed to be aligned */ 47 __ioread32_copy(val, sid->base + sid->value_offset + offset, bytes / 4); 48 49 val += round_down(bytes, 4); 50 offset += round_down(bytes, 4); 51 bytes = bytes % 4; 52 53 if (!bytes) 54 return 0; 55 56 /* Handle any trailing bytes */ 57 word = readl_relaxed(sid->base + sid->value_offset + offset); 58 memcpy(val, &word, bytes); 59 60 return 0; 61 } 62 63 static int sun8i_sid_register_readout(const struct sunxi_sid *sid, 64 const unsigned int offset, 65 u32 *out) 66 { 67 u32 reg_val; 68 int ret; 69 70 /* Set word, lock access, and set read command */ 71 reg_val = (offset & SUN8I_SID_OFFSET_MASK) 72 << SUN8I_SID_OFFSET_SHIFT; 73 reg_val |= SUN8I_SID_OP_LOCK | SUN8I_SID_READ; 74 writel(reg_val, sid->base + SUN8I_SID_PRCTL); 75 76 ret = readl_poll_timeout(sid->base + SUN8I_SID_PRCTL, reg_val, 77 !(reg_val & SUN8I_SID_READ), 100, 250000); 78 if (ret) 79 return ret; 80 81 if (out) 82 *out = readl(sid->base + SUN8I_SID_RDKEY); 83 84 writel(0, sid->base + SUN8I_SID_PRCTL); 85 86 return 0; 87 } 88 89 /* 90 * On Allwinner H3, the value on the 0x200 offset of the SID controller seems 91 * to be not reliable at all. 92 * Read by the registers instead. 93 */ 94 static int sun8i_sid_read_by_reg(void *context, unsigned int offset, 95 void *val, size_t bytes) 96 { 97 struct sunxi_sid *sid = context; 98 u32 word; 99 int ret; 100 101 /* .stride = 4 so offset is guaranteed to be aligned */ 102 while (bytes >= 4) { 103 ret = sun8i_sid_register_readout(sid, offset, val); 104 if (ret) 105 return ret; 106 107 val += 4; 108 offset += 4; 109 bytes -= 4; 110 } 111 112 if (!bytes) 113 return 0; 114 115 /* Handle any trailing bytes */ 116 ret = sun8i_sid_register_readout(sid, offset, &word); 117 if (ret) 118 return ret; 119 120 memcpy(val, &word, bytes); 121 122 return 0; 123 } 124 125 static int sunxi_sid_probe(struct platform_device *pdev) 126 { 127 struct device *dev = &pdev->dev; 128 struct resource *res; 129 struct nvmem_config *nvmem_cfg; 130 struct nvmem_device *nvmem; 131 struct sunxi_sid *sid; 132 int size; 133 char *randomness; 134 const struct sunxi_sid_cfg *cfg; 135 136 sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL); 137 if (!sid) 138 return -ENOMEM; 139 140 cfg = of_device_get_match_data(dev); 141 if (!cfg) 142 return -EINVAL; 143 sid->value_offset = cfg->value_offset; 144 145 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 146 sid->base = devm_ioremap_resource(dev, res); 147 if (IS_ERR(sid->base)) 148 return PTR_ERR(sid->base); 149 150 size = cfg->size; 151 152 nvmem_cfg = devm_kzalloc(dev, sizeof(*nvmem_cfg), GFP_KERNEL); 153 if (!nvmem_cfg) 154 return -ENOMEM; 155 156 nvmem_cfg->dev = dev; 157 nvmem_cfg->name = "sunxi-sid"; 158 nvmem_cfg->type = NVMEM_TYPE_OTP; 159 nvmem_cfg->read_only = true; 160 nvmem_cfg->size = cfg->size; 161 nvmem_cfg->word_size = 1; 162 nvmem_cfg->stride = 4; 163 nvmem_cfg->priv = sid; 164 if (cfg->need_register_readout) 165 nvmem_cfg->reg_read = sun8i_sid_read_by_reg; 166 else 167 nvmem_cfg->reg_read = sunxi_sid_read; 168 169 nvmem = devm_nvmem_register(dev, nvmem_cfg); 170 if (IS_ERR(nvmem)) 171 return PTR_ERR(nvmem); 172 173 randomness = kzalloc(size, GFP_KERNEL); 174 if (!randomness) 175 return -ENOMEM; 176 177 nvmem_cfg->reg_read(sid, 0, randomness, size); 178 add_device_randomness(randomness, size); 179 kfree(randomness); 180 181 platform_set_drvdata(pdev, nvmem); 182 183 return 0; 184 } 185 186 static const struct sunxi_sid_cfg sun4i_a10_cfg = { 187 .size = 0x10, 188 }; 189 190 static const struct sunxi_sid_cfg sun7i_a20_cfg = { 191 .size = 0x200, 192 }; 193 194 static const struct sunxi_sid_cfg sun8i_h3_cfg = { 195 .value_offset = 0x200, 196 .size = 0x100, 197 .need_register_readout = true, 198 }; 199 200 static const struct sunxi_sid_cfg sun50i_a64_cfg = { 201 .value_offset = 0x200, 202 .size = 0x100, 203 }; 204 205 static const struct sunxi_sid_cfg sun50i_h6_cfg = { 206 .value_offset = 0x200, 207 .size = 0x200, 208 }; 209 210 static const struct of_device_id sunxi_sid_of_match[] = { 211 { .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg }, 212 { .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg }, 213 { .compatible = "allwinner,sun8i-a83t-sid", .data = &sun50i_a64_cfg }, 214 { .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg }, 215 { .compatible = "allwinner,sun20i-d1-sid", .data = &sun50i_a64_cfg }, 216 { .compatible = "allwinner,sun50i-a64-sid", .data = &sun50i_a64_cfg }, 217 { .compatible = "allwinner,sun50i-h5-sid", .data = &sun50i_a64_cfg }, 218 { .compatible = "allwinner,sun50i-h6-sid", .data = &sun50i_h6_cfg }, 219 {/* sentinel */}, 220 }; 221 MODULE_DEVICE_TABLE(of, sunxi_sid_of_match); 222 223 static struct platform_driver sunxi_sid_driver = { 224 .probe = sunxi_sid_probe, 225 .driver = { 226 .name = "eeprom-sunxi-sid", 227 .of_match_table = sunxi_sid_of_match, 228 }, 229 }; 230 module_platform_driver(sunxi_sid_driver); 231 232 MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>"); 233 MODULE_DESCRIPTION("Allwinner sunxi security id driver"); 234 MODULE_LICENSE("GPL"); 235