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> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/nvmem-provider.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/platform_device.h> |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/random.h> |
| 26 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 27 | static struct nvmem_config econfig = { |
| 28 | .name = "sunxi-sid", |
| 29 | .read_only = true, |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 30 | .stride = 4, |
| 31 | .word_size = 1, |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 32 | .owner = THIS_MODULE, |
| 33 | }; |
| 34 | |
| 35 | struct sunxi_sid { |
| 36 | void __iomem *base; |
| 37 | }; |
| 38 | |
| 39 | /* We read the entire key, due to a 32 bit read alignment requirement. Since we |
| 40 | * want to return the requested byte, this results in somewhat slower code and |
| 41 | * uses 4 times more reads as needed but keeps code simpler. Since the SID is |
| 42 | * only very rarely probed, this is not really an issue. |
| 43 | */ |
| 44 | static u8 sunxi_sid_read_byte(const struct sunxi_sid *sid, |
| 45 | const unsigned int offset) |
| 46 | { |
| 47 | u32 sid_key; |
| 48 | |
| 49 | sid_key = ioread32be(sid->base + round_down(offset, 4)); |
| 50 | sid_key >>= (offset % 4) * 8; |
| 51 | |
| 52 | return sid_key; /* Only return the last byte */ |
| 53 | } |
| 54 | |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 55 | static int sunxi_sid_read(void *context, unsigned int offset, |
| 56 | void *val, size_t bytes) |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 57 | { |
| 58 | struct sunxi_sid *sid = context; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 59 | u8 *buf = val; |
| 60 | |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 61 | while (bytes--) |
| 62 | *buf++ = sunxi_sid_read_byte(sid, offset++); |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 67 | static int sunxi_sid_probe(struct platform_device *pdev) |
| 68 | { |
| 69 | struct device *dev = &pdev->dev; |
| 70 | struct resource *res; |
| 71 | struct nvmem_device *nvmem; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 72 | struct sunxi_sid *sid; |
Maxime Ripard | fb72707 | 2015-09-30 13:36:31 +0100 | [diff] [blame] | 73 | int ret, i, size; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 74 | char *randomness; |
| 75 | |
| 76 | sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL); |
| 77 | if (!sid) |
| 78 | return -ENOMEM; |
| 79 | |
| 80 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 81 | sid->base = devm_ioremap_resource(dev, res); |
| 82 | if (IS_ERR(sid->base)) |
| 83 | return PTR_ERR(sid->base); |
| 84 | |
| 85 | size = resource_size(res) - 1; |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 86 | econfig.size = resource_size(res); |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 87 | econfig.dev = dev; |
Srinivas Kandagatla | 9c7b16e | 2016-04-24 20:28:10 +0100 | [diff] [blame] | 88 | econfig.reg_read = sunxi_sid_read; |
| 89 | econfig.priv = sid; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 90 | nvmem = nvmem_register(&econfig); |
| 91 | if (IS_ERR(nvmem)) |
| 92 | return PTR_ERR(nvmem); |
| 93 | |
Caesar Wang | d16abd3 | 2015-12-14 09:43:21 +0000 | [diff] [blame] | 94 | randomness = kzalloc(sizeof(u8) * (size), GFP_KERNEL); |
Maxime Ripard | fb72707 | 2015-09-30 13:36:31 +0100 | [diff] [blame] | 95 | if (!randomness) { |
| 96 | ret = -EINVAL; |
| 97 | goto err_unreg_nvmem; |
| 98 | } |
| 99 | |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 100 | for (i = 0; i < size; i++) |
| 101 | randomness[i] = sunxi_sid_read_byte(sid, i); |
| 102 | |
| 103 | add_device_randomness(randomness, size); |
| 104 | kfree(randomness); |
| 105 | |
| 106 | platform_set_drvdata(pdev, nvmem); |
| 107 | |
| 108 | return 0; |
Maxime Ripard | fb72707 | 2015-09-30 13:36:31 +0100 | [diff] [blame] | 109 | |
| 110 | err_unreg_nvmem: |
| 111 | nvmem_unregister(nvmem); |
| 112 | return ret; |
Maxime Ripard | 3d0b16a | 2015-07-27 12:17:09 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static int sunxi_sid_remove(struct platform_device *pdev) |
| 116 | { |
| 117 | struct nvmem_device *nvmem = platform_get_drvdata(pdev); |
| 118 | |
| 119 | return nvmem_unregister(nvmem); |
| 120 | } |
| 121 | |
| 122 | static const struct of_device_id sunxi_sid_of_match[] = { |
| 123 | { .compatible = "allwinner,sun4i-a10-sid" }, |
| 124 | { .compatible = "allwinner,sun7i-a20-sid" }, |
| 125 | {/* sentinel */}, |
| 126 | }; |
| 127 | MODULE_DEVICE_TABLE(of, sunxi_sid_of_match); |
| 128 | |
| 129 | static struct platform_driver sunxi_sid_driver = { |
| 130 | .probe = sunxi_sid_probe, |
| 131 | .remove = sunxi_sid_remove, |
| 132 | .driver = { |
| 133 | .name = "eeprom-sunxi-sid", |
| 134 | .of_match_table = sunxi_sid_of_match, |
| 135 | }, |
| 136 | }; |
| 137 | module_platform_driver(sunxi_sid_driver); |
| 138 | |
| 139 | MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>"); |
| 140 | MODULE_DESCRIPTION("Allwinner sunxi security id driver"); |
| 141 | MODULE_LICENSE("GPL"); |