sunxi_sid.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/kobject.h>
  26. #include <linux/module.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/random.h>
  30. #include <linux/slab.h>
  31. #include <linux/stat.h>
  32. #include <linux/sysfs.h>
  33. #include <linux/types.h>
  34. #define DRV_NAME "sunxi-sid"
  35. struct sunxi_sid_data {
  36. void __iomem *reg_base;
  37. unsigned int keysize;
  38. };
  39. /* We read the entire key, due to a 32 bit read alignment requirement. Since we
  40. * want to return the requested byte, this results in somewhat slower code and
  41. * uses 4 times more reads as needed but keeps code simpler. Since the SID is
  42. * only very rarely probed, this is not really an issue.
  43. */
  44. static u8 sunxi_sid_read_byte(const struct sunxi_sid_data *sid_data,
  45. const unsigned int offset)
  46. {
  47. u32 sid_key;
  48. if (offset >= sid_data->keysize)
  49. return 0;
  50. sid_key = ioread32be(sid_data->reg_base + round_down(offset, 4));
  51. sid_key >>= (offset % 4) * 8;
  52. return sid_key; /* Only return the last byte */
  53. }
  54. static ssize_t sid_read(struct file *fd, struct kobject *kobj,
  55. struct bin_attribute *attr, char *buf,
  56. loff_t pos, size_t size)
  57. {
  58. struct platform_device *pdev;
  59. struct sunxi_sid_data *sid_data;
  60. int i;
  61. pdev = to_platform_device(kobj_to_dev(kobj));
  62. sid_data = platform_get_drvdata(pdev);
  63. if (pos < 0 || pos >= sid_data->keysize)
  64. return 0;
  65. if (size > sid_data->keysize - pos)
  66. size = sid_data->keysize - pos;
  67. for (i = 0; i < size; i++)
  68. buf[i] = sunxi_sid_read_byte(sid_data, pos + i);
  69. return i;
  70. }
  71. static struct bin_attribute sid_bin_attr = {
  72. .attr = { .name = "eeprom", .mode = S_IRUGO, },
  73. .read = sid_read,
  74. };
  75. static int sunxi_sid_remove(struct platform_device *pdev)
  76. {
  77. device_remove_bin_file(&pdev->dev, &sid_bin_attr);
  78. dev_dbg(&pdev->dev, "driver unloaded\n");
  79. return 0;
  80. }
  81. static const struct of_device_id sunxi_sid_of_match[] = {
  82. { .compatible = "allwinner,sun4i-a10-sid", .data = (void *)16},
  83. { .compatible = "allwinner,sun7i-a20-sid", .data = (void *)512},
  84. {/* sentinel */},
  85. };
  86. MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
  87. static int sunxi_sid_probe(struct platform_device *pdev)
  88. {
  89. struct sunxi_sid_data *sid_data;
  90. struct resource *res;
  91. const struct of_device_id *of_dev_id;
  92. u8 *entropy;
  93. unsigned int i;
  94. sid_data = devm_kzalloc(&pdev->dev, sizeof(struct sunxi_sid_data),
  95. GFP_KERNEL);
  96. if (!sid_data)
  97. return -ENOMEM;
  98. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  99. sid_data->reg_base = devm_ioremap_resource(&pdev->dev, res);
  100. if (IS_ERR(sid_data->reg_base))
  101. return PTR_ERR(sid_data->reg_base);
  102. of_dev_id = of_match_device(sunxi_sid_of_match, &pdev->dev);
  103. if (!of_dev_id)
  104. return -ENODEV;
  105. sid_data->keysize = (int)of_dev_id->data;
  106. platform_set_drvdata(pdev, sid_data);
  107. sid_bin_attr.size = sid_data->keysize;
  108. if (device_create_bin_file(&pdev->dev, &sid_bin_attr))
  109. return -ENODEV;
  110. entropy = kzalloc(sizeof(u8) * sid_data->keysize, GFP_KERNEL);
  111. for (i = 0; i < sid_data->keysize; i++)
  112. entropy[i] = sunxi_sid_read_byte(sid_data, i);
  113. add_device_randomness(entropy, sid_data->keysize);
  114. kfree(entropy);
  115. dev_dbg(&pdev->dev, "loaded\n");
  116. return 0;
  117. }
  118. static struct platform_driver sunxi_sid_driver = {
  119. .probe = sunxi_sid_probe,
  120. .remove = sunxi_sid_remove,
  121. .driver = {
  122. .name = DRV_NAME,
  123. .of_match_table = sunxi_sid_of_match,
  124. },
  125. };
  126. module_platform_driver(sunxi_sid_driver);
  127. MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
  128. MODULE_DESCRIPTION("Allwinner sunxi security id driver");
  129. MODULE_LICENSE("GPL");