sunxi_sid.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. */
  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>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. #include <linux/random.h>
  27. static struct nvmem_config econfig = {
  28. .name = "sunxi-sid",
  29. .read_only = true,
  30. .owner = THIS_MODULE,
  31. };
  32. struct sunxi_sid {
  33. void __iomem *base;
  34. };
  35. /* We read the entire key, due to a 32 bit read alignment requirement. Since we
  36. * want to return the requested byte, this results in somewhat slower code and
  37. * uses 4 times more reads as needed but keeps code simpler. Since the SID is
  38. * only very rarely probed, this is not really an issue.
  39. */
  40. static u8 sunxi_sid_read_byte(const struct sunxi_sid *sid,
  41. const unsigned int offset)
  42. {
  43. u32 sid_key;
  44. sid_key = ioread32be(sid->base + round_down(offset, 4));
  45. sid_key >>= (offset % 4) * 8;
  46. return sid_key; /* Only return the last byte */
  47. }
  48. static int sunxi_sid_read(void *context,
  49. const void *reg, size_t reg_size,
  50. void *val, size_t val_size)
  51. {
  52. struct sunxi_sid *sid = context;
  53. unsigned int offset = *(u32 *)reg;
  54. u8 *buf = val;
  55. while (val_size) {
  56. *buf++ = sunxi_sid_read_byte(sid, offset);
  57. val_size--;
  58. offset++;
  59. }
  60. return 0;
  61. }
  62. static int sunxi_sid_write(void *context, const void *data, size_t count)
  63. {
  64. /* Unimplemented, dummy to keep regmap core happy */
  65. return 0;
  66. }
  67. static struct regmap_bus sunxi_sid_bus = {
  68. .read = sunxi_sid_read,
  69. .write = sunxi_sid_write,
  70. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  71. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  72. };
  73. static bool sunxi_sid_writeable_reg(struct device *dev, unsigned int reg)
  74. {
  75. return false;
  76. }
  77. static struct regmap_config sunxi_sid_regmap_config = {
  78. .reg_bits = 32,
  79. .val_bits = 8,
  80. .reg_stride = 1,
  81. .writeable_reg = sunxi_sid_writeable_reg,
  82. };
  83. static int sunxi_sid_probe(struct platform_device *pdev)
  84. {
  85. struct device *dev = &pdev->dev;
  86. struct resource *res;
  87. struct nvmem_device *nvmem;
  88. struct regmap *regmap;
  89. struct sunxi_sid *sid;
  90. int ret, i, size;
  91. char *randomness;
  92. sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
  93. if (!sid)
  94. return -ENOMEM;
  95. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. sid->base = devm_ioremap_resource(dev, res);
  97. if (IS_ERR(sid->base))
  98. return PTR_ERR(sid->base);
  99. size = resource_size(res) - 1;
  100. sunxi_sid_regmap_config.max_register = size;
  101. regmap = devm_regmap_init(dev, &sunxi_sid_bus, sid,
  102. &sunxi_sid_regmap_config);
  103. if (IS_ERR(regmap)) {
  104. dev_err(dev, "regmap init failed\n");
  105. return PTR_ERR(regmap);
  106. }
  107. econfig.dev = dev;
  108. nvmem = nvmem_register(&econfig);
  109. if (IS_ERR(nvmem))
  110. return PTR_ERR(nvmem);
  111. randomness = kzalloc(sizeof(u8) * size, GFP_KERNEL);
  112. if (!randomness) {
  113. ret = -EINVAL;
  114. goto err_unreg_nvmem;
  115. }
  116. for (i = 0; i < size; i++)
  117. randomness[i] = sunxi_sid_read_byte(sid, i);
  118. add_device_randomness(randomness, size);
  119. kfree(randomness);
  120. platform_set_drvdata(pdev, nvmem);
  121. return 0;
  122. err_unreg_nvmem:
  123. nvmem_unregister(nvmem);
  124. return ret;
  125. }
  126. static int sunxi_sid_remove(struct platform_device *pdev)
  127. {
  128. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  129. return nvmem_unregister(nvmem);
  130. }
  131. static const struct of_device_id sunxi_sid_of_match[] = {
  132. { .compatible = "allwinner,sun4i-a10-sid" },
  133. { .compatible = "allwinner,sun7i-a20-sid" },
  134. {/* sentinel */},
  135. };
  136. MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
  137. static struct platform_driver sunxi_sid_driver = {
  138. .probe = sunxi_sid_probe,
  139. .remove = sunxi_sid_remove,
  140. .driver = {
  141. .name = "eeprom-sunxi-sid",
  142. .of_match_table = sunxi_sid_of_match,
  143. },
  144. };
  145. module_platform_driver(sunxi_sid_driver);
  146. MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
  147. MODULE_DESCRIPTION("Allwinner sunxi security id driver");
  148. MODULE_LICENSE("GPL");