sunxi_sid.c 6.2 KB

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