sunxi_sid.c 5.2 KB

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