Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 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. |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 16 | */ |
| 17 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 18 | #include <linux/device.h> |
| 19 | #include <linux/io.h> |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 20 | #include <linux/iopoll.h> |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/nvmem-provider.h> |
| 23 | #include <linux/of.h> |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 24 | #include <linux/of_device.h> |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/random.h> |
| 28 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 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 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 38 | static struct nvmem_config econfig = { |
| 39 | .name = "sunxi-sid", |
| 40 | .read_only = true, |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 41 | .stride = 4, |
| 42 | .word_size = 1, |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 45 | struct sunxi_sid_cfg { |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 46 | u32 value_offset; |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 47 | u32 size; |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 48 | bool need_register_readout; |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 51 | struct sunxi_sid { |
| 52 | void __iomem *base; |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 53 | u32 value_offset; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 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 | */ |
| 61 | static 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 | |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 72 | static int sunxi_sid_read(void *context, unsigned int offset, |
| 73 | void *val, size_t bytes) |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 74 | { |
| 75 | struct sunxi_sid *sid = context; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 76 | u8 *buf = val; |
| 77 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 78 | /* Offset the read operation to the real position of SID */ |
| 79 | offset += sid->value_offset; |
| 80 | |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 81 | while (bytes--) |
| 82 | *buf++ = sunxi_sid_read_byte(sid, offset++); |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 87 | static int sun8i_sid_register_readout(const struct sunxi_sid *sid, |
Icenowy Zheng | 0ab09d6 | 2018-03-09 14:47:17 +0000 | [diff] [blame] | 88 | const unsigned int offset, |
| 89 | u32 *out) |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 90 | { |
| 91 | u32 reg_val; |
| 92 | int ret; |
| 93 | |
| 94 | /* Set word, lock access, and set read command */ |
Icenowy Zheng | 0ab09d6 | 2018-03-09 14:47:17 +0000 | [diff] [blame] | 95 | reg_val = (offset & SUN8I_SID_OFFSET_MASK) |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 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 | |
Icenowy Zheng | 0ab09d6 | 2018-03-09 14:47:17 +0000 | [diff] [blame] | 105 | if (out) |
| 106 | *out = readl(sid->base + SUN8I_SID_RDKEY); |
| 107 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 108 | writel(0, sid->base + SUN8I_SID_PRCTL); |
Icenowy Zheng | 0ab09d6 | 2018-03-09 14:47:17 +0000 | [diff] [blame] | 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 | */ |
| 118 | static 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 | |
| 135 | static 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 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 151 | static int sunxi_sid_probe(struct platform_device *pdev) |
| 152 | { |
| 153 | struct device *dev = &pdev->dev; |
| 154 | struct resource *res; |
| 155 | struct nvmem_device *nvmem; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 156 | struct sunxi_sid *sid; |
Srinivas Kandagatla | 4876bfe | 2018-09-21 06:40:25 -0700 | [diff] [blame] | 157 | int i, size; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 158 | char *randomness; |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 159 | const struct sunxi_sid_cfg *cfg; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 160 | |
| 161 | sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL); |
| 162 | if (!sid) |
| 163 | return -ENOMEM; |
| 164 | |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 165 | cfg = of_device_get_match_data(dev); |
| 166 | if (!cfg) |
| 167 | return -EINVAL; |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 168 | sid->value_offset = cfg->value_offset; |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 169 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 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 | |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 175 | size = cfg->size; |
| 176 | |
| 177 | econfig.size = size; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 178 | econfig.dev = dev; |
Icenowy Zheng | 0ab09d6 | 2018-03-09 14:47:17 +0000 | [diff] [blame] | 179 | if (cfg->need_register_readout) |
| 180 | econfig.reg_read = sun8i_sid_read_by_reg; |
| 181 | else |
| 182 | econfig.reg_read = sunxi_sid_read; |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 183 | econfig.priv = sid; |
Bartosz Golaszewski | 6eed8dd | 2018-09-21 06:40:10 -0700 | [diff] [blame] | 184 | nvmem = devm_nvmem_register(dev, &econfig); |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 185 | if (IS_ERR(nvmem)) |
| 186 | return PTR_ERR(nvmem); |
| 187 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 188 | randomness = kzalloc(size, GFP_KERNEL); |
Bartosz Golaszewski | 6eed8dd | 2018-09-21 06:40:10 -0700 | [diff] [blame] | 189 | if (!randomness) |
| 190 | return -ENOMEM; |
Maxime Ripard | fb72707 | 2015-09-30 13:36:31 +0100 | [diff] [blame] | 191 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 192 | for (i = 0; i < size; i++) |
Icenowy Zheng | 0ab09d6 | 2018-03-09 14:47:17 +0000 | [diff] [blame] | 193 | econfig.reg_read(sid, i, &randomness[i], 1); |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 194 | |
| 195 | add_device_randomness(randomness, size); |
| 196 | kfree(randomness); |
| 197 | |
| 198 | platform_set_drvdata(pdev, nvmem); |
| 199 | |
| 200 | return 0; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 203 | static const struct sunxi_sid_cfg sun4i_a10_cfg = { |
| 204 | .size = 0x10, |
| 205 | }; |
| 206 | |
| 207 | static const struct sunxi_sid_cfg sun7i_a20_cfg = { |
| 208 | .size = 0x200, |
| 209 | }; |
| 210 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 211 | static const struct sunxi_sid_cfg sun8i_h3_cfg = { |
| 212 | .value_offset = 0x200, |
| 213 | .size = 0x100, |
| 214 | .need_register_readout = true, |
| 215 | }; |
| 216 | |
Icenowy Zheng | b7fe57b | 2017-10-24 10:54:34 +0100 | [diff] [blame] | 217 | static const struct sunxi_sid_cfg sun50i_a64_cfg = { |
| 218 | .value_offset = 0x200, |
| 219 | .size = 0x100, |
| 220 | }; |
| 221 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 222 | static const struct of_device_id sunxi_sid_of_match[] = { |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 223 | { .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg }, |
| 224 | { .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg }, |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 225 | { .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg }, |
Icenowy Zheng | b7fe57b | 2017-10-24 10:54:34 +0100 | [diff] [blame] | 226 | { .compatible = "allwinner,sun50i-a64-sid", .data = &sun50i_a64_cfg }, |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 227 | {/* sentinel */}, |
| 228 | }; |
| 229 | MODULE_DEVICE_TABLE(of, sunxi_sid_of_match); |
| 230 | |
| 231 | static struct platform_driver sunxi_sid_driver = { |
| 232 | .probe = sunxi_sid_probe, |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 233 | .driver = { |
| 234 | .name = "eeprom-sunxi-sid", |
| 235 | .of_match_table = sunxi_sid_of_match, |
| 236 | }, |
| 237 | }; |
| 238 | module_platform_driver(sunxi_sid_driver); |
| 239 | |
| 240 | MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>"); |
| 241 | MODULE_DESCRIPTION("Allwinner sunxi security id driver"); |
| 242 | MODULE_LICENSE("GPL"); |