fuse.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * arch/arm/mach-tegra/fuse.c
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  6. *
  7. * Author:
  8. * Colin Cross <ccross@android.com>
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/io.h>
  22. #include <linux/export.h>
  23. #include <linux/random.h>
  24. #include <linux/tegra-soc.h>
  25. #include "fuse.h"
  26. #include "iomap.h"
  27. #include "apbio.h"
  28. /* Tegra20 only */
  29. #define FUSE_UID_LOW 0x108
  30. #define FUSE_UID_HIGH 0x10c
  31. /* Tegra30 and later */
  32. #define FUSE_VENDOR_CODE 0x200
  33. #define FUSE_FAB_CODE 0x204
  34. #define FUSE_LOT_CODE_0 0x208
  35. #define FUSE_LOT_CODE_1 0x20c
  36. #define FUSE_WAFER_ID 0x210
  37. #define FUSE_X_COORDINATE 0x214
  38. #define FUSE_Y_COORDINATE 0x218
  39. #define FUSE_SKU_INFO 0x110
  40. #define TEGRA20_FUSE_SPARE_BIT 0x200
  41. #define TEGRA30_FUSE_SPARE_BIT 0x244
  42. int tegra_sku_id;
  43. int tegra_cpu_process_id;
  44. int tegra_core_process_id;
  45. int tegra_chip_id;
  46. int tegra_cpu_speedo_id; /* only exist in Tegra30 and later */
  47. int tegra_soc_speedo_id;
  48. enum tegra_revision tegra_revision;
  49. static int tegra_fuse_spare_bit;
  50. static void (*tegra_init_speedo_data)(void);
  51. /* The BCT to use at boot is specified by board straps that can be read
  52. * through a APB misc register and decoded. 2 bits, i.e. 4 possible BCTs.
  53. */
  54. int tegra_bct_strapping;
  55. #define STRAP_OPT 0x008
  56. #define GMI_AD0 (1 << 4)
  57. #define GMI_AD1 (1 << 5)
  58. #define RAM_ID_MASK (GMI_AD0 | GMI_AD1)
  59. #define RAM_CODE_SHIFT 4
  60. static const char *tegra_revision_name[TEGRA_REVISION_MAX] = {
  61. [TEGRA_REVISION_UNKNOWN] = "unknown",
  62. [TEGRA_REVISION_A01] = "A01",
  63. [TEGRA_REVISION_A02] = "A02",
  64. [TEGRA_REVISION_A03] = "A03",
  65. [TEGRA_REVISION_A03p] = "A03 prime",
  66. [TEGRA_REVISION_A04] = "A04",
  67. };
  68. u32 tegra_fuse_readl(unsigned long offset)
  69. {
  70. return tegra_apb_readl(TEGRA_FUSE_BASE + offset);
  71. }
  72. bool tegra_spare_fuse(int bit)
  73. {
  74. return tegra_fuse_readl(tegra_fuse_spare_bit + bit * 4);
  75. }
  76. static enum tegra_revision tegra_get_revision(u32 id)
  77. {
  78. u32 minor_rev = (id >> 16) & 0xf;
  79. switch (minor_rev) {
  80. case 1:
  81. return TEGRA_REVISION_A01;
  82. case 2:
  83. return TEGRA_REVISION_A02;
  84. case 3:
  85. if (tegra_chip_id == TEGRA20 &&
  86. (tegra_spare_fuse(18) || tegra_spare_fuse(19)))
  87. return TEGRA_REVISION_A03p;
  88. else
  89. return TEGRA_REVISION_A03;
  90. case 4:
  91. return TEGRA_REVISION_A04;
  92. default:
  93. return TEGRA_REVISION_UNKNOWN;
  94. }
  95. }
  96. static void tegra_get_process_id(void)
  97. {
  98. u32 reg;
  99. reg = tegra_fuse_readl(tegra_fuse_spare_bit);
  100. tegra_cpu_process_id = (reg >> 6) & 3;
  101. reg = tegra_fuse_readl(tegra_fuse_spare_bit);
  102. tegra_core_process_id = (reg >> 12) & 3;
  103. }
  104. u32 tegra_read_chipid(void)
  105. {
  106. return readl_relaxed(IO_ADDRESS(TEGRA_APB_MISC_BASE) + 0x804);
  107. }
  108. static void __init tegra20_fuse_init_randomness(void)
  109. {
  110. u32 randomness[2];
  111. randomness[0] = tegra_fuse_readl(FUSE_UID_LOW);
  112. randomness[1] = tegra_fuse_readl(FUSE_UID_HIGH);
  113. add_device_randomness(randomness, sizeof(randomness));
  114. }
  115. /* Applies to Tegra30 or later */
  116. static void __init tegra30_fuse_init_randomness(void)
  117. {
  118. u32 randomness[7];
  119. randomness[0] = tegra_fuse_readl(FUSE_VENDOR_CODE);
  120. randomness[1] = tegra_fuse_readl(FUSE_FAB_CODE);
  121. randomness[2] = tegra_fuse_readl(FUSE_LOT_CODE_0);
  122. randomness[3] = tegra_fuse_readl(FUSE_LOT_CODE_1);
  123. randomness[4] = tegra_fuse_readl(FUSE_WAFER_ID);
  124. randomness[5] = tegra_fuse_readl(FUSE_X_COORDINATE);
  125. randomness[6] = tegra_fuse_readl(FUSE_Y_COORDINATE);
  126. add_device_randomness(randomness, sizeof(randomness));
  127. }
  128. void __init tegra_init_fuse(void)
  129. {
  130. u32 id;
  131. u32 randomness[5];
  132. u32 reg = readl(IO_ADDRESS(TEGRA_CLK_RESET_BASE + 0x48));
  133. reg |= 1 << 28;
  134. writel(reg, IO_ADDRESS(TEGRA_CLK_RESET_BASE + 0x48));
  135. reg = tegra_fuse_readl(FUSE_SKU_INFO);
  136. randomness[0] = reg;
  137. tegra_sku_id = reg & 0xFF;
  138. reg = tegra_apb_readl(TEGRA_APB_MISC_BASE + STRAP_OPT);
  139. randomness[1] = reg;
  140. tegra_bct_strapping = (reg & RAM_ID_MASK) >> RAM_CODE_SHIFT;
  141. id = tegra_read_chipid();
  142. randomness[2] = id;
  143. tegra_chip_id = (id >> 8) & 0xff;
  144. switch (tegra_chip_id) {
  145. case TEGRA20:
  146. tegra_fuse_spare_bit = TEGRA20_FUSE_SPARE_BIT;
  147. tegra_init_speedo_data = &tegra20_init_speedo_data;
  148. break;
  149. case TEGRA30:
  150. tegra_fuse_spare_bit = TEGRA30_FUSE_SPARE_BIT;
  151. tegra_init_speedo_data = &tegra30_init_speedo_data;
  152. break;
  153. case TEGRA114:
  154. tegra_init_speedo_data = &tegra114_init_speedo_data;
  155. break;
  156. default:
  157. pr_warn("Tegra: unknown chip id %d\n", tegra_chip_id);
  158. tegra_fuse_spare_bit = TEGRA20_FUSE_SPARE_BIT;
  159. tegra_init_speedo_data = &tegra_get_process_id;
  160. }
  161. tegra_revision = tegra_get_revision(id);
  162. tegra_init_speedo_data();
  163. randomness[3] = (tegra_cpu_process_id << 16) | tegra_core_process_id;
  164. randomness[4] = (tegra_cpu_speedo_id << 16) | tegra_soc_speedo_id;
  165. add_device_randomness(randomness, sizeof(randomness));
  166. switch (tegra_chip_id) {
  167. case TEGRA20:
  168. tegra20_fuse_init_randomness();
  169. case TEGRA30:
  170. case TEGRA114:
  171. default:
  172. tegra30_fuse_init_randomness();
  173. }
  174. pr_info("Tegra Revision: %s SKU: %d CPU Process: %d Core Process: %d\n",
  175. tegra_revision_name[tegra_revision],
  176. tegra_sku_id, tegra_cpu_process_id,
  177. tegra_core_process_id);
  178. }
  179. unsigned long long tegra_chip_uid(void)
  180. {
  181. unsigned long long lo, hi;
  182. lo = tegra_fuse_readl(FUSE_UID_LOW);
  183. hi = tegra_fuse_readl(FUSE_UID_HIGH);
  184. return (hi << 32ull) | lo;
  185. }
  186. EXPORT_SYMBOL(tegra_chip_uid);