sunxi_sid.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. .owner = THIS_MODULE,
  40. };
  41. struct sunxi_sid_cfg {
  42. u32 value_offset;
  43. u32 size;
  44. bool need_register_readout;
  45. };
  46. struct sunxi_sid {
  47. void __iomem *base;
  48. u32 value_offset;
  49. };
  50. /* We read the entire key, due to a 32 bit read alignment requirement. Since we
  51. * want to return the requested byte, this results in somewhat slower code and
  52. * uses 4 times more reads as needed but keeps code simpler. Since the SID is
  53. * only very rarely probed, this is not really an issue.
  54. */
  55. static u8 sunxi_sid_read_byte(const struct sunxi_sid *sid,
  56. const unsigned int offset)
  57. {
  58. u32 sid_key;
  59. sid_key = ioread32be(sid->base + round_down(offset, 4));
  60. sid_key >>= (offset % 4) * 8;
  61. return sid_key; /* Only return the last byte */
  62. }
  63. static int sunxi_sid_read(void *context, unsigned int offset,
  64. void *val, size_t bytes)
  65. {
  66. struct sunxi_sid *sid = context;
  67. u8 *buf = val;
  68. /* Offset the read operation to the real position of SID */
  69. offset += sid->value_offset;
  70. while (bytes--)
  71. *buf++ = sunxi_sid_read_byte(sid, offset++);
  72. return 0;
  73. }
  74. static int sun8i_sid_register_readout(const struct sunxi_sid *sid,
  75. const unsigned int word)
  76. {
  77. u32 reg_val;
  78. int ret;
  79. /* Set word, lock access, and set read command */
  80. reg_val = (word & 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. writel(0, sid->base + SUN8I_SID_PRCTL);
  89. return 0;
  90. }
  91. static int sunxi_sid_probe(struct platform_device *pdev)
  92. {
  93. struct device *dev = &pdev->dev;
  94. struct resource *res;
  95. struct nvmem_device *nvmem;
  96. struct sunxi_sid *sid;
  97. int ret, i, 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. if (cfg->need_register_readout) {
  113. /*
  114. * H3's SID controller have a bug that the value at 0x200
  115. * offset is not the correct value when the hardware is reseted.
  116. * However, after doing a register-based read operation, the
  117. * value become right.
  118. * Do a full read operation here, but ignore its value
  119. * (as it's more fast to read by direct MMIO value than
  120. * with registers)
  121. */
  122. for (i = 0; i < (size >> 2); i++) {
  123. ret = sun8i_sid_register_readout(sid, i);
  124. if (ret)
  125. return ret;
  126. }
  127. }
  128. econfig.size = size;
  129. econfig.dev = dev;
  130. econfig.reg_read = sunxi_sid_read;
  131. econfig.priv = sid;
  132. nvmem = nvmem_register(&econfig);
  133. if (IS_ERR(nvmem))
  134. return PTR_ERR(nvmem);
  135. randomness = kzalloc(sizeof(u8) * (size), GFP_KERNEL);
  136. if (!randomness) {
  137. ret = -EINVAL;
  138. goto err_unreg_nvmem;
  139. }
  140. for (i = 0; i < size; i++)
  141. randomness[i] = sunxi_sid_read_byte(sid, i);
  142. add_device_randomness(randomness, size);
  143. kfree(randomness);
  144. platform_set_drvdata(pdev, nvmem);
  145. return 0;
  146. err_unreg_nvmem:
  147. nvmem_unregister(nvmem);
  148. return ret;
  149. }
  150. static int sunxi_sid_remove(struct platform_device *pdev)
  151. {
  152. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  153. return nvmem_unregister(nvmem);
  154. }
  155. static const struct sunxi_sid_cfg sun4i_a10_cfg = {
  156. .size = 0x10,
  157. };
  158. static const struct sunxi_sid_cfg sun7i_a20_cfg = {
  159. .size = 0x200,
  160. };
  161. static const struct sunxi_sid_cfg sun8i_h3_cfg = {
  162. .value_offset = 0x200,
  163. .size = 0x100,
  164. .need_register_readout = true,
  165. };
  166. static const struct of_device_id sunxi_sid_of_match[] = {
  167. { .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg },
  168. { .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg },
  169. { .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg },
  170. {/* sentinel */},
  171. };
  172. MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
  173. static struct platform_driver sunxi_sid_driver = {
  174. .probe = sunxi_sid_probe,
  175. .remove = sunxi_sid_remove,
  176. .driver = {
  177. .name = "eeprom-sunxi-sid",
  178. .of_match_table = sunxi_sid_of_match,
  179. },
  180. };
  181. module_platform_driver(sunxi_sid_driver);
  182. MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
  183. MODULE_DESCRIPTION("Allwinner sunxi security id driver");
  184. MODULE_LICENSE("GPL");