sunxi_sid.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/module.h>
  20. #include <linux/nvmem-provider.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/random.h>
  25. static struct nvmem_config econfig = {
  26. .name = "sunxi-sid",
  27. .read_only = true,
  28. .stride = 4,
  29. .word_size = 1,
  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, unsigned int offset,
  49. void *val, size_t bytes)
  50. {
  51. struct sunxi_sid *sid = context;
  52. u8 *buf = val;
  53. while (bytes--)
  54. *buf++ = sunxi_sid_read_byte(sid, offset++);
  55. return 0;
  56. }
  57. static int sunxi_sid_probe(struct platform_device *pdev)
  58. {
  59. struct device *dev = &pdev->dev;
  60. struct resource *res;
  61. struct nvmem_device *nvmem;
  62. struct sunxi_sid *sid;
  63. int ret, i, size;
  64. char *randomness;
  65. sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
  66. if (!sid)
  67. return -ENOMEM;
  68. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  69. sid->base = devm_ioremap_resource(dev, res);
  70. if (IS_ERR(sid->base))
  71. return PTR_ERR(sid->base);
  72. size = resource_size(res) - 1;
  73. econfig.size = resource_size(res);
  74. econfig.dev = dev;
  75. econfig.reg_read = sunxi_sid_read;
  76. econfig.priv = sid;
  77. nvmem = nvmem_register(&econfig);
  78. if (IS_ERR(nvmem))
  79. return PTR_ERR(nvmem);
  80. randomness = kzalloc(sizeof(u8) * (size), GFP_KERNEL);
  81. if (!randomness) {
  82. ret = -EINVAL;
  83. goto err_unreg_nvmem;
  84. }
  85. for (i = 0; i < size; i++)
  86. randomness[i] = sunxi_sid_read_byte(sid, i);
  87. add_device_randomness(randomness, size);
  88. kfree(randomness);
  89. platform_set_drvdata(pdev, nvmem);
  90. return 0;
  91. err_unreg_nvmem:
  92. nvmem_unregister(nvmem);
  93. return ret;
  94. }
  95. static int sunxi_sid_remove(struct platform_device *pdev)
  96. {
  97. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  98. return nvmem_unregister(nvmem);
  99. }
  100. static const struct of_device_id sunxi_sid_of_match[] = {
  101. { .compatible = "allwinner,sun4i-a10-sid" },
  102. { .compatible = "allwinner,sun7i-a20-sid" },
  103. {/* sentinel */},
  104. };
  105. MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
  106. static struct platform_driver sunxi_sid_driver = {
  107. .probe = sunxi_sid_probe,
  108. .remove = sunxi_sid_remove,
  109. .driver = {
  110. .name = "eeprom-sunxi-sid",
  111. .of_match_table = sunxi_sid_of_match,
  112. },
  113. };
  114. module_platform_driver(sunxi_sid_driver);
  115. MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
  116. MODULE_DESCRIPTION("Allwinner sunxi security id driver");
  117. MODULE_LICENSE("GPL");