fuse-tegra30.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <linux/device.h>
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_address.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/random.h>
  26. #include <soc/tegra/fuse.h>
  27. #include "fuse.h"
  28. #define FUSE_BEGIN 0x100
  29. /* Tegra30 and later */
  30. #define FUSE_VENDOR_CODE 0x100
  31. #define FUSE_FAB_CODE 0x104
  32. #define FUSE_LOT_CODE_0 0x108
  33. #define FUSE_LOT_CODE_1 0x10c
  34. #define FUSE_WAFER_ID 0x110
  35. #define FUSE_X_COORDINATE 0x114
  36. #define FUSE_Y_COORDINATE 0x118
  37. #define FUSE_HAS_REVISION_INFO BIT(0)
  38. enum speedo_idx {
  39. SPEEDO_TEGRA30 = 0,
  40. SPEEDO_TEGRA114,
  41. SPEEDO_TEGRA124,
  42. };
  43. struct tegra_fuse_info {
  44. int size;
  45. int spare_bit;
  46. enum speedo_idx speedo_idx;
  47. };
  48. static void __iomem *fuse_base;
  49. static struct clk *fuse_clk;
  50. static struct tegra_fuse_info *fuse_info;
  51. u32 tegra30_fuse_readl(const unsigned int offset)
  52. {
  53. u32 val;
  54. /*
  55. * early in the boot, the fuse clock will be enabled by
  56. * tegra_init_fuse()
  57. */
  58. if (fuse_clk)
  59. clk_prepare_enable(fuse_clk);
  60. val = readl_relaxed(fuse_base + FUSE_BEGIN + offset);
  61. if (fuse_clk)
  62. clk_disable_unprepare(fuse_clk);
  63. return val;
  64. }
  65. static struct tegra_fuse_info tegra30_info = {
  66. .size = 0x2a4,
  67. .spare_bit = 0x144,
  68. .speedo_idx = SPEEDO_TEGRA30,
  69. };
  70. static struct tegra_fuse_info tegra114_info = {
  71. .size = 0x2a0,
  72. .speedo_idx = SPEEDO_TEGRA114,
  73. };
  74. static struct tegra_fuse_info tegra124_info = {
  75. .size = 0x300,
  76. .speedo_idx = SPEEDO_TEGRA124,
  77. };
  78. static const struct of_device_id tegra30_fuse_of_match[] = {
  79. { .compatible = "nvidia,tegra30-efuse", .data = &tegra30_info },
  80. { .compatible = "nvidia,tegra114-efuse", .data = &tegra114_info },
  81. { .compatible = "nvidia,tegra124-efuse", .data = &tegra124_info },
  82. {},
  83. };
  84. static int tegra30_fuse_probe(struct platform_device *pdev)
  85. {
  86. const struct of_device_id *of_dev_id;
  87. of_dev_id = of_match_device(tegra30_fuse_of_match, &pdev->dev);
  88. if (!of_dev_id)
  89. return -ENODEV;
  90. fuse_clk = devm_clk_get(&pdev->dev, NULL);
  91. if (IS_ERR(fuse_clk)) {
  92. dev_err(&pdev->dev, "missing clock");
  93. return PTR_ERR(fuse_clk);
  94. }
  95. platform_set_drvdata(pdev, NULL);
  96. if (tegra_fuse_create_sysfs(&pdev->dev, fuse_info->size,
  97. tegra30_fuse_readl))
  98. return -ENODEV;
  99. dev_dbg(&pdev->dev, "loaded\n");
  100. return 0;
  101. }
  102. static struct platform_driver tegra30_fuse_driver = {
  103. .probe = tegra30_fuse_probe,
  104. .driver = {
  105. .name = "tegra_fuse",
  106. .owner = THIS_MODULE,
  107. .of_match_table = tegra30_fuse_of_match,
  108. }
  109. };
  110. static int __init tegra30_fuse_init(void)
  111. {
  112. return platform_driver_register(&tegra30_fuse_driver);
  113. }
  114. postcore_initcall(tegra30_fuse_init);
  115. /* Early boot code. This code is called before the devices are created */
  116. typedef void (*speedo_f)(struct tegra_sku_info *sku_info);
  117. static speedo_f __initdata speedo_tbl[] = {
  118. [SPEEDO_TEGRA30] = tegra30_init_speedo_data,
  119. [SPEEDO_TEGRA114] = tegra114_init_speedo_data,
  120. [SPEEDO_TEGRA124] = tegra124_init_speedo_data,
  121. };
  122. static void __init tegra30_fuse_add_randomness(void)
  123. {
  124. u32 randomness[12];
  125. randomness[0] = tegra_sku_info.sku_id;
  126. randomness[1] = tegra_read_straps();
  127. randomness[2] = tegra_read_chipid();
  128. randomness[3] = tegra_sku_info.cpu_process_id << 16;
  129. randomness[3] |= tegra_sku_info.core_process_id;
  130. randomness[4] = tegra_sku_info.cpu_speedo_id << 16;
  131. randomness[4] |= tegra_sku_info.soc_speedo_id;
  132. randomness[5] = tegra30_fuse_readl(FUSE_VENDOR_CODE);
  133. randomness[6] = tegra30_fuse_readl(FUSE_FAB_CODE);
  134. randomness[7] = tegra30_fuse_readl(FUSE_LOT_CODE_0);
  135. randomness[8] = tegra30_fuse_readl(FUSE_LOT_CODE_1);
  136. randomness[9] = tegra30_fuse_readl(FUSE_WAFER_ID);
  137. randomness[10] = tegra30_fuse_readl(FUSE_X_COORDINATE);
  138. randomness[11] = tegra30_fuse_readl(FUSE_Y_COORDINATE);
  139. add_device_randomness(randomness, sizeof(randomness));
  140. }
  141. static void __init legacy_fuse_init(void)
  142. {
  143. switch (tegra_get_chip_id()) {
  144. case TEGRA30:
  145. fuse_info = &tegra30_info;
  146. break;
  147. case TEGRA114:
  148. fuse_info = &tegra114_info;
  149. break;
  150. case TEGRA124:
  151. fuse_info = &tegra124_info;
  152. break;
  153. default:
  154. return;
  155. }
  156. fuse_base = ioremap(TEGRA_FUSE_BASE, TEGRA_FUSE_SIZE);
  157. }
  158. bool __init tegra30_spare_fuse(int spare_bit)
  159. {
  160. u32 offset = fuse_info->spare_bit + spare_bit * 4;
  161. return tegra30_fuse_readl(offset) & 1;
  162. }
  163. void __init tegra30_init_fuse_early(void)
  164. {
  165. struct device_node *np;
  166. const struct of_device_id *of_match;
  167. np = of_find_matching_node_and_match(NULL, tegra30_fuse_of_match,
  168. &of_match);
  169. if (np) {
  170. fuse_base = of_iomap(np, 0);
  171. fuse_info = (struct tegra_fuse_info *)of_match->data;
  172. } else
  173. legacy_fuse_init();
  174. if (!fuse_base) {
  175. pr_warn("fuse DT node missing and unknown chip id: 0x%02x\n",
  176. tegra_get_chip_id());
  177. return;
  178. }
  179. tegra_init_revision();
  180. speedo_tbl[fuse_info->speedo_idx](&tegra_sku_info);
  181. tegra30_fuse_add_randomness();
  182. }