sunxi_sid.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
  3. * http://www.linux-sunxi.org
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * This driver exposes the Allwinner security ID, efuses exported in byte-
  16. * sized chunks.
  17. */
  18. #include <linux/compiler.h>
  19. #include <linux/device.h>
  20. #include <linux/err.h>
  21. #include <linux/export.h>
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/kobject.h>
  27. #include <linux/module.h>
  28. #include <linux/of_device.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/random.h>
  31. #include <linux/slab.h>
  32. #include <linux/stat.h>
  33. #include <linux/sysfs.h>
  34. #include <linux/types.h>
  35. #define DRV_NAME "sunxi-sid"
  36. struct sunxi_sid_data {
  37. void __iomem *reg_base;
  38. unsigned int keysize;
  39. };
  40. /* We read the entire key, due to a 32 bit read alignment requirement. Since we
  41. * want to return the requested byte, this results in somewhat slower code and
  42. * uses 4 times more reads as needed but keeps code simpler. Since the SID is
  43. * only very rarely probed, this is not really an issue.
  44. */
  45. static u8 sunxi_sid_read_byte(const struct sunxi_sid_data *sid_data,
  46. const unsigned int offset)
  47. {
  48. u32 sid_key;
  49. if (offset >= sid_data->keysize)
  50. return 0;
  51. sid_key = ioread32be(sid_data->reg_base + round_down(offset, 4));
  52. sid_key >>= (offset % 4) * 8;
  53. return sid_key; /* Only return the last byte */
  54. }
  55. static ssize_t sid_read(struct file *fd, struct kobject *kobj,
  56. struct bin_attribute *attr, char *buf,
  57. loff_t pos, size_t size)
  58. {
  59. struct platform_device *pdev;
  60. struct sunxi_sid_data *sid_data;
  61. int i;
  62. pdev = to_platform_device(kobj_to_dev(kobj));
  63. sid_data = platform_get_drvdata(pdev);
  64. if (pos < 0 || pos >= sid_data->keysize)
  65. return 0;
  66. if (size > sid_data->keysize - pos)
  67. size = sid_data->keysize - pos;
  68. for (i = 0; i < size; i++)
  69. buf[i] = sunxi_sid_read_byte(sid_data, pos + i);
  70. return i;
  71. }
  72. static struct bin_attribute sid_bin_attr = {
  73. .attr = { .name = "eeprom", .mode = S_IRUGO, },
  74. .read = sid_read,
  75. };
  76. static int sunxi_sid_remove(struct platform_device *pdev)
  77. {
  78. device_remove_bin_file(&pdev->dev, &sid_bin_attr);
  79. dev_dbg(&pdev->dev, "driver unloaded\n");
  80. return 0;
  81. }
  82. static const struct of_device_id sunxi_sid_of_match[] = {
  83. { .compatible = "allwinner,sun4i-sid", .data = (void *)16},
  84. { .compatible = "allwinner,sun7i-a20-sid", .data = (void *)512},
  85. {/* sentinel */},
  86. };
  87. MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
  88. static int sunxi_sid_probe(struct platform_device *pdev)
  89. {
  90. struct sunxi_sid_data *sid_data;
  91. struct resource *res;
  92. const struct of_device_id *of_dev_id;
  93. u8 *entropy;
  94. unsigned int i;
  95. sid_data = devm_kzalloc(&pdev->dev, sizeof(struct sunxi_sid_data),
  96. GFP_KERNEL);
  97. if (!sid_data)
  98. return -ENOMEM;
  99. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  100. sid_data->reg_base = devm_ioremap_resource(&pdev->dev, res);
  101. if (IS_ERR(sid_data->reg_base))
  102. return PTR_ERR(sid_data->reg_base);
  103. of_dev_id = of_match_device(sunxi_sid_of_match, &pdev->dev);
  104. if (!of_dev_id)
  105. return -ENODEV;
  106. sid_data->keysize = (int)of_dev_id->data;
  107. platform_set_drvdata(pdev, sid_data);
  108. sid_bin_attr.size = sid_data->keysize;
  109. if (device_create_bin_file(&pdev->dev, &sid_bin_attr))
  110. return -ENODEV;
  111. entropy = kzalloc(sizeof(u8) * sid_data->keysize, GFP_KERNEL);
  112. for (i = 0; i < sid_data->keysize; i++)
  113. entropy[i] = sunxi_sid_read_byte(sid_data, i);
  114. add_device_randomness(entropy, sid_data->keysize);
  115. kfree(entropy);
  116. dev_dbg(&pdev->dev, "loaded\n");
  117. return 0;
  118. }
  119. static struct platform_driver sunxi_sid_driver = {
  120. .probe = sunxi_sid_probe,
  121. .remove = sunxi_sid_remove,
  122. .driver = {
  123. .name = DRV_NAME,
  124. .owner = THIS_MODULE,
  125. .of_match_table = sunxi_sid_of_match,
  126. },
  127. };
  128. module_platform_driver(sunxi_sid_driver);
  129. MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
  130. MODULE_DESCRIPTION("Allwinner sunxi security id driver");
  131. MODULE_LICENSE("GPL");