sunxi_sid.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Allwinner sunXi SoCs Security ID support.
  4. *
  5. * Copyright (c) 2013 Oliver Schinagl <[email protected]>
  6. * Copyright (C) 2014 Maxime Ripard <[email protected]>
  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. u32 word;
  39. /* .stride = 4 so offset is guaranteed to be aligned */
  40. __ioread32_copy(val, sid->base + sid->value_offset + offset, bytes / 4);
  41. val += round_down(bytes, 4);
  42. offset += round_down(bytes, 4);
  43. bytes = bytes % 4;
  44. if (!bytes)
  45. return 0;
  46. /* Handle any trailing bytes */
  47. word = readl_relaxed(sid->base + sid->value_offset + offset);
  48. memcpy(val, &word, bytes);
  49. return 0;
  50. }
  51. static int sun8i_sid_register_readout(const struct sunxi_sid *sid,
  52. const unsigned int offset,
  53. u32 *out)
  54. {
  55. u32 reg_val;
  56. int ret;
  57. /* Set word, lock access, and set read command */
  58. reg_val = (offset & SUN8I_SID_OFFSET_MASK)
  59. << SUN8I_SID_OFFSET_SHIFT;
  60. reg_val |= SUN8I_SID_OP_LOCK | SUN8I_SID_READ;
  61. writel(reg_val, sid->base + SUN8I_SID_PRCTL);
  62. ret = readl_poll_timeout(sid->base + SUN8I_SID_PRCTL, reg_val,
  63. !(reg_val & SUN8I_SID_READ), 100, 250000);
  64. if (ret)
  65. return ret;
  66. if (out)
  67. *out = readl(sid->base + SUN8I_SID_RDKEY);
  68. writel(0, sid->base + SUN8I_SID_PRCTL);
  69. return 0;
  70. }
  71. /*
  72. * On Allwinner H3, the value on the 0x200 offset of the SID controller seems
  73. * to be not reliable at all.
  74. * Read by the registers instead.
  75. */
  76. static int sun8i_sid_read_by_reg(void *context, unsigned int offset,
  77. void *val, size_t bytes)
  78. {
  79. struct sunxi_sid *sid = context;
  80. u32 word;
  81. int ret;
  82. /* .stride = 4 so offset is guaranteed to be aligned */
  83. while (bytes >= 4) {
  84. ret = sun8i_sid_register_readout(sid, offset, val);
  85. if (ret)
  86. return ret;
  87. val += 4;
  88. offset += 4;
  89. bytes -= 4;
  90. }
  91. if (!bytes)
  92. return 0;
  93. /* Handle any trailing bytes */
  94. ret = sun8i_sid_register_readout(sid, offset, &word);
  95. if (ret)
  96. return ret;
  97. memcpy(val, &word, bytes);
  98. return 0;
  99. }
  100. static int sunxi_sid_probe(struct platform_device *pdev)
  101. {
  102. struct device *dev = &pdev->dev;
  103. struct resource *res;
  104. struct nvmem_config *nvmem_cfg;
  105. struct nvmem_device *nvmem;
  106. struct sunxi_sid *sid;
  107. int size;
  108. char *randomness;
  109. const struct sunxi_sid_cfg *cfg;
  110. sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
  111. if (!sid)
  112. return -ENOMEM;
  113. cfg = of_device_get_match_data(dev);
  114. if (!cfg)
  115. return -EINVAL;
  116. sid->value_offset = cfg->value_offset;
  117. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  118. sid->base = devm_ioremap_resource(dev, res);
  119. if (IS_ERR(sid->base))
  120. return PTR_ERR(sid->base);
  121. size = cfg->size;
  122. nvmem_cfg = devm_kzalloc(dev, sizeof(*nvmem_cfg), GFP_KERNEL);
  123. if (!nvmem_cfg)
  124. return -ENOMEM;
  125. nvmem_cfg->dev = dev;
  126. nvmem_cfg->name = "sunxi-sid";
  127. nvmem_cfg->type = NVMEM_TYPE_OTP;
  128. nvmem_cfg->read_only = true;
  129. nvmem_cfg->size = cfg->size;
  130. nvmem_cfg->word_size = 1;
  131. nvmem_cfg->stride = 4;
  132. nvmem_cfg->priv = sid;
  133. if (cfg->need_register_readout)
  134. nvmem_cfg->reg_read = sun8i_sid_read_by_reg;
  135. else
  136. nvmem_cfg->reg_read = sunxi_sid_read;
  137. nvmem = devm_nvmem_register(dev, nvmem_cfg);
  138. if (IS_ERR(nvmem))
  139. return PTR_ERR(nvmem);
  140. randomness = kzalloc(size, GFP_KERNEL);
  141. if (!randomness)
  142. return -ENOMEM;
  143. nvmem_cfg->reg_read(sid, 0, randomness, size);
  144. add_device_randomness(randomness, size);
  145. kfree(randomness);
  146. platform_set_drvdata(pdev, nvmem);
  147. return 0;
  148. }
  149. static const struct sunxi_sid_cfg sun4i_a10_cfg = {
  150. .size = 0x10,
  151. };
  152. static const struct sunxi_sid_cfg sun7i_a20_cfg = {
  153. .size = 0x200,
  154. };
  155. static const struct sunxi_sid_cfg sun8i_h3_cfg = {
  156. .value_offset = 0x200,
  157. .size = 0x100,
  158. .need_register_readout = true,
  159. };
  160. static const struct sunxi_sid_cfg sun20i_d1_cfg = {
  161. .value_offset = 0x200,
  162. .size = 0x100,
  163. };
  164. static const struct sunxi_sid_cfg sun50i_a64_cfg = {
  165. .value_offset = 0x200,
  166. .size = 0x100,
  167. .need_register_readout = true,
  168. };
  169. static const struct sunxi_sid_cfg sun50i_h6_cfg = {
  170. .value_offset = 0x200,
  171. .size = 0x200,
  172. };
  173. static const struct of_device_id sunxi_sid_of_match[] = {
  174. { .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg },
  175. { .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg },
  176. { .compatible = "allwinner,sun8i-a83t-sid", .data = &sun50i_a64_cfg },
  177. { .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg },
  178. { .compatible = "allwinner,sun20i-d1-sid", .data = &sun20i_d1_cfg },
  179. { .compatible = "allwinner,sun50i-a64-sid", .data = &sun50i_a64_cfg },
  180. { .compatible = "allwinner,sun50i-h5-sid", .data = &sun50i_a64_cfg },
  181. { .compatible = "allwinner,sun50i-h6-sid", .data = &sun50i_h6_cfg },
  182. {/* sentinel */},
  183. };
  184. MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
  185. static struct platform_driver sunxi_sid_driver = {
  186. .probe = sunxi_sid_probe,
  187. .driver = {
  188. .name = "eeprom-sunxi-sid",
  189. .of_match_table = sunxi_sid_of_match,
  190. },
  191. };
  192. module_platform_driver(sunxi_sid_driver);
  193. MODULE_AUTHOR("Oliver Schinagl <[email protected]>");
  194. MODULE_DESCRIPTION("Allwinner sunxi security id driver");
  195. MODULE_LICENSE("GPL");