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 | .owner = THIS_MODULE, |
| 44 | }; |
| 45 | |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 46 | struct sunxi_sid_cfg { |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 47 | u32 value_offset; |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 48 | u32 size; |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 49 | bool need_register_readout; |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 52 | struct sunxi_sid { |
| 53 | void __iomem *base; |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 54 | u32 value_offset; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /* We read the entire key, due to a 32 bit read alignment requirement. Since we |
| 58 | * want to return the requested byte, this results in somewhat slower code and |
| 59 | * uses 4 times more reads as needed but keeps code simpler. Since the SID is |
| 60 | * only very rarely probed, this is not really an issue. |
| 61 | */ |
| 62 | static u8 sunxi_sid_read_byte(const struct sunxi_sid *sid, |
| 63 | const unsigned int offset) |
| 64 | { |
| 65 | u32 sid_key; |
| 66 | |
| 67 | sid_key = ioread32be(sid->base + round_down(offset, 4)); |
| 68 | sid_key >>= (offset % 4) * 8; |
| 69 | |
| 70 | return sid_key; /* Only return the last byte */ |
| 71 | } |
| 72 | |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 73 | static int sunxi_sid_read(void *context, unsigned int offset, |
| 74 | void *val, size_t bytes) |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 75 | { |
| 76 | struct sunxi_sid *sid = context; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 77 | u8 *buf = val; |
| 78 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 79 | /* Offset the read operation to the real position of SID */ |
| 80 | offset += sid->value_offset; |
| 81 | |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 82 | while (bytes--) |
| 83 | *buf++ = sunxi_sid_read_byte(sid, offset++); |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 88 | static int sun8i_sid_register_readout(const struct sunxi_sid *sid, |
| 89 | const unsigned int word) |
| 90 | { |
| 91 | u32 reg_val; |
| 92 | int ret; |
| 93 | |
| 94 | /* Set word, lock access, and set read command */ |
| 95 | reg_val = (word & 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 | writel(0, sid->base + SUN8I_SID_PRCTL); |
| 106 | return 0; |
| 107 | } |
| 108 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 109 | static int sunxi_sid_probe(struct platform_device *pdev) |
| 110 | { |
| 111 | struct device *dev = &pdev->dev; |
| 112 | struct resource *res; |
| 113 | struct nvmem_device *nvmem; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 114 | struct sunxi_sid *sid; |
Maxime Ripard | fb72707 | 2015-09-30 13:36:31 +0100 | [diff] [blame] | 115 | int ret, i, size; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 116 | char *randomness; |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 117 | const struct sunxi_sid_cfg *cfg; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 118 | |
| 119 | sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL); |
| 120 | if (!sid) |
| 121 | return -ENOMEM; |
| 122 | |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 123 | cfg = of_device_get_match_data(dev); |
| 124 | if (!cfg) |
| 125 | return -EINVAL; |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 126 | sid->value_offset = cfg->value_offset; |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 127 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 128 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 129 | sid->base = devm_ioremap_resource(dev, res); |
| 130 | if (IS_ERR(sid->base)) |
| 131 | return PTR_ERR(sid->base); |
| 132 | |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 133 | size = cfg->size; |
| 134 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 135 | if (cfg->need_register_readout) { |
| 136 | /* |
| 137 | * H3's SID controller have a bug that the value at 0x200 |
| 138 | * offset is not the correct value when the hardware is reseted. |
| 139 | * However, after doing a register-based read operation, the |
| 140 | * value become right. |
| 141 | * Do a full read operation here, but ignore its value |
| 142 | * (as it's more fast to read by direct MMIO value than |
| 143 | * with registers) |
| 144 | */ |
| 145 | for (i = 0; i < (size >> 2); i++) { |
| 146 | ret = sun8i_sid_register_readout(sid, i); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | } |
| 150 | } |
| 151 | |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 152 | econfig.size = size; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 153 | econfig.dev = dev; |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 154 | econfig.reg_read = sunxi_sid_read; |
| 155 | econfig.priv = sid; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 156 | nvmem = nvmem_register(&econfig); |
| 157 | if (IS_ERR(nvmem)) |
| 158 | return PTR_ERR(nvmem); |
| 159 | |
Caesar Wang | d16abd3 | 2015-12-14 09:43:21 +0000 | [diff] [blame] | 160 | randomness = kzalloc(sizeof(u8) * (size), GFP_KERNEL); |
Maxime Ripard | fb72707 | 2015-09-30 13:36:31 +0100 | [diff] [blame] | 161 | if (!randomness) { |
| 162 | ret = -EINVAL; |
| 163 | goto err_unreg_nvmem; |
| 164 | } |
| 165 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 166 | for (i = 0; i < size; i++) |
| 167 | randomness[i] = sunxi_sid_read_byte(sid, i); |
| 168 | |
| 169 | add_device_randomness(randomness, size); |
| 170 | kfree(randomness); |
| 171 | |
| 172 | platform_set_drvdata(pdev, nvmem); |
| 173 | |
| 174 | return 0; |
Maxime Ripard | fb72707 | 2015-09-30 13:36:31 +0100 | [diff] [blame] | 175 | |
| 176 | err_unreg_nvmem: |
| 177 | nvmem_unregister(nvmem); |
| 178 | return ret; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static int sunxi_sid_remove(struct platform_device *pdev) |
| 182 | { |
| 183 | struct nvmem_device *nvmem = platform_get_drvdata(pdev); |
| 184 | |
| 185 | return nvmem_unregister(nvmem); |
| 186 | } |
| 187 | |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 188 | static const struct sunxi_sid_cfg sun4i_a10_cfg = { |
| 189 | .size = 0x10, |
| 190 | }; |
| 191 | |
| 192 | static const struct sunxi_sid_cfg sun7i_a20_cfg = { |
| 193 | .size = 0x200, |
| 194 | }; |
| 195 | |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 196 | static const struct sunxi_sid_cfg sun8i_h3_cfg = { |
| 197 | .value_offset = 0x200, |
| 198 | .size = 0x100, |
| 199 | .need_register_readout = true, |
| 200 | }; |
| 201 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 202 | static const struct of_device_id sunxi_sid_of_match[] = { |
Icenowy Zheng | 4a72cda | 2017-03-31 13:44:47 +0100 | [diff] [blame] | 203 | { .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg }, |
| 204 | { .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg }, |
Icenowy Zheng | 1a96364 | 2017-03-31 13:44:48 +0100 | [diff] [blame] | 205 | { .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg }, |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 206 | {/* sentinel */}, |
| 207 | }; |
| 208 | MODULE_DEVICE_TABLE(of, sunxi_sid_of_match); |
| 209 | |
| 210 | static struct platform_driver sunxi_sid_driver = { |
| 211 | .probe = sunxi_sid_probe, |
| 212 | .remove = sunxi_sid_remove, |
| 213 | .driver = { |
| 214 | .name = "eeprom-sunxi-sid", |
| 215 | .of_match_table = sunxi_sid_of_match, |
| 216 | }, |
| 217 | }; |
| 218 | module_platform_driver(sunxi_sid_driver); |
| 219 | |
| 220 | MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>"); |
| 221 | MODULE_DESCRIPTION("Allwinner sunxi security id driver"); |
| 222 | MODULE_LICENSE("GPL"); |