Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/mach-tegra/fuse.c |
| 3 | * |
| 4 | * Copyright (C) 2010 Google, Inc. |
Danny Huang | 7495b2e | 2013-03-18 19:17:34 +0800 | [diff] [blame] | 5 | * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved. |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 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 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/io.h> |
Linus Torvalds | 3480059 | 2012-03-27 16:41:24 -0700 | [diff] [blame] | 23 | #include <linux/export.h> |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 24 | #include <linux/random.h> |
Alexandre Courbot | cdcb5a0 | 2013-11-21 11:40:13 +0900 | [diff] [blame] | 25 | #include <linux/clk.h> |
Prashant Gaikwad | c7736ed | 2013-01-11 13:16:19 +0530 | [diff] [blame] | 26 | #include <linux/tegra-soc.h> |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 27 | |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 28 | #include "fuse.h" |
Stephen Warren | 2be39c0 | 2012-10-04 14:24:09 -0600 | [diff] [blame] | 29 | #include "iomap.h" |
Olof Johansson | d262f49 | 2011-10-13 00:14:08 -0700 | [diff] [blame] | 30 | #include "apbio.h" |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 31 | |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 32 | /* Tegra20 only */ |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 33 | #define FUSE_UID_LOW 0x108 |
| 34 | #define FUSE_UID_HIGH 0x10c |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 35 | |
| 36 | /* Tegra30 and later */ |
| 37 | #define FUSE_VENDOR_CODE 0x200 |
| 38 | #define FUSE_FAB_CODE 0x204 |
| 39 | #define FUSE_LOT_CODE_0 0x208 |
| 40 | #define FUSE_LOT_CODE_1 0x20c |
| 41 | #define FUSE_WAFER_ID 0x210 |
| 42 | #define FUSE_X_COORDINATE 0x214 |
| 43 | #define FUSE_Y_COORDINATE 0x218 |
| 44 | |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 45 | #define FUSE_SKU_INFO 0x110 |
Danny Huang | 1f851a2 | 2012-11-15 15:42:32 +0800 | [diff] [blame] | 46 | |
| 47 | #define TEGRA20_FUSE_SPARE_BIT 0x200 |
Danny Huang | f8ddda7 | 2012-11-15 15:42:34 +0800 | [diff] [blame] | 48 | #define TEGRA30_FUSE_SPARE_BIT 0x244 |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 49 | |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 50 | int tegra_sku_id; |
| 51 | int tegra_cpu_process_id; |
| 52 | int tegra_core_process_id; |
Peter De Schrijver | 4c4ad66 | 2012-02-10 01:47:42 +0200 | [diff] [blame] | 53 | int tegra_chip_id; |
Danny Huang | f8ddda7 | 2012-11-15 15:42:34 +0800 | [diff] [blame] | 54 | int tegra_cpu_speedo_id; /* only exist in Tegra30 and later */ |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 55 | int tegra_soc_speedo_id; |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 56 | enum tegra_revision tegra_revision; |
| 57 | |
Alexandre Courbot | cdcb5a0 | 2013-11-21 11:40:13 +0900 | [diff] [blame] | 58 | static struct clk *fuse_clk; |
Danny Huang | 1f851a2 | 2012-11-15 15:42:32 +0800 | [diff] [blame] | 59 | static int tegra_fuse_spare_bit; |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 60 | static void (*tegra_init_speedo_data)(void); |
Danny Huang | 1f851a2 | 2012-11-15 15:42:32 +0800 | [diff] [blame] | 61 | |
Olof Johansson | dee4718 | 2011-10-17 16:39:24 -0700 | [diff] [blame] | 62 | /* The BCT to use at boot is specified by board straps that can be read |
| 63 | * through a APB misc register and decoded. 2 bits, i.e. 4 possible BCTs. |
| 64 | */ |
| 65 | int tegra_bct_strapping; |
| 66 | |
| 67 | #define STRAP_OPT 0x008 |
| 68 | #define GMI_AD0 (1 << 4) |
| 69 | #define GMI_AD1 (1 << 5) |
| 70 | #define RAM_ID_MASK (GMI_AD0 | GMI_AD1) |
| 71 | #define RAM_CODE_SHIFT 4 |
| 72 | |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 73 | static const char *tegra_revision_name[TEGRA_REVISION_MAX] = { |
| 74 | [TEGRA_REVISION_UNKNOWN] = "unknown", |
| 75 | [TEGRA_REVISION_A01] = "A01", |
| 76 | [TEGRA_REVISION_A02] = "A02", |
| 77 | [TEGRA_REVISION_A03] = "A03", |
| 78 | [TEGRA_REVISION_A03p] = "A03 prime", |
| 79 | [TEGRA_REVISION_A04] = "A04", |
| 80 | }; |
| 81 | |
Alexandre Courbot | cdcb5a0 | 2013-11-21 11:40:13 +0900 | [diff] [blame] | 82 | static void tegra_fuse_enable_clk(void) |
| 83 | { |
| 84 | if (IS_ERR(fuse_clk)) |
| 85 | fuse_clk = clk_get_sys(NULL, "fuse"); |
| 86 | if (IS_ERR(fuse_clk)) |
| 87 | return; |
| 88 | clk_prepare_enable(fuse_clk); |
| 89 | } |
| 90 | |
| 91 | static void tegra_fuse_disable_clk(void) |
| 92 | { |
| 93 | if (IS_ERR(fuse_clk)) |
| 94 | return; |
| 95 | clk_disable_unprepare(fuse_clk); |
| 96 | } |
| 97 | |
Danny Huang | 1f851a2 | 2012-11-15 15:42:32 +0800 | [diff] [blame] | 98 | u32 tegra_fuse_readl(unsigned long offset) |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 99 | { |
Olof Johansson | d262f49 | 2011-10-13 00:14:08 -0700 | [diff] [blame] | 100 | return tegra_apb_readl(TEGRA_FUSE_BASE + offset); |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Danny Huang | 1f851a2 | 2012-11-15 15:42:32 +0800 | [diff] [blame] | 103 | bool tegra_spare_fuse(int bit) |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 104 | { |
Alexandre Courbot | cdcb5a0 | 2013-11-21 11:40:13 +0900 | [diff] [blame] | 105 | bool ret; |
| 106 | |
| 107 | tegra_fuse_enable_clk(); |
| 108 | |
| 109 | ret = tegra_fuse_readl(tegra_fuse_spare_bit + bit * 4); |
| 110 | |
| 111 | tegra_fuse_disable_clk(); |
| 112 | |
| 113 | return ret; |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Peter De Schrijver | 35b1498 | 2012-02-10 01:47:41 +0200 | [diff] [blame] | 116 | static enum tegra_revision tegra_get_revision(u32 id) |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 117 | { |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 118 | u32 minor_rev = (id >> 16) & 0xf; |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 119 | |
| 120 | switch (minor_rev) { |
| 121 | case 1: |
| 122 | return TEGRA_REVISION_A01; |
| 123 | case 2: |
| 124 | return TEGRA_REVISION_A02; |
| 125 | case 3: |
Peter De Schrijver | 35b1498 | 2012-02-10 01:47:41 +0200 | [diff] [blame] | 126 | if (tegra_chip_id == TEGRA20 && |
Danny Huang | 1f851a2 | 2012-11-15 15:42:32 +0800 | [diff] [blame] | 127 | (tegra_spare_fuse(18) || tegra_spare_fuse(19))) |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 128 | return TEGRA_REVISION_A03p; |
| 129 | else |
| 130 | return TEGRA_REVISION_A03; |
| 131 | case 4: |
| 132 | return TEGRA_REVISION_A04; |
| 133 | default: |
| 134 | return TEGRA_REVISION_UNKNOWN; |
| 135 | } |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 138 | static void tegra_get_process_id(void) |
| 139 | { |
| 140 | u32 reg; |
| 141 | |
Alexandre Courbot | cdcb5a0 | 2013-11-21 11:40:13 +0900 | [diff] [blame] | 142 | tegra_fuse_enable_clk(); |
| 143 | |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 144 | reg = tegra_fuse_readl(tegra_fuse_spare_bit); |
| 145 | tegra_cpu_process_id = (reg >> 6) & 3; |
| 146 | reg = tegra_fuse_readl(tegra_fuse_spare_bit); |
| 147 | tegra_core_process_id = (reg >> 12) & 3; |
Alexandre Courbot | cdcb5a0 | 2013-11-21 11:40:13 +0900 | [diff] [blame] | 148 | |
| 149 | tegra_fuse_disable_clk(); |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 150 | } |
| 151 | |
Prashant Gaikwad | c7736ed | 2013-01-11 13:16:19 +0530 | [diff] [blame] | 152 | u32 tegra_read_chipid(void) |
| 153 | { |
| 154 | return readl_relaxed(IO_ADDRESS(TEGRA_APB_MISC_BASE) + 0x804); |
| 155 | } |
| 156 | |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 157 | static void __init tegra20_fuse_init_randomness(void) |
| 158 | { |
| 159 | u32 randomness[2]; |
| 160 | |
| 161 | randomness[0] = tegra_fuse_readl(FUSE_UID_LOW); |
| 162 | randomness[1] = tegra_fuse_readl(FUSE_UID_HIGH); |
| 163 | |
| 164 | add_device_randomness(randomness, sizeof(randomness)); |
| 165 | } |
| 166 | |
| 167 | /* Applies to Tegra30 or later */ |
| 168 | static void __init tegra30_fuse_init_randomness(void) |
| 169 | { |
| 170 | u32 randomness[7]; |
| 171 | |
| 172 | randomness[0] = tegra_fuse_readl(FUSE_VENDOR_CODE); |
| 173 | randomness[1] = tegra_fuse_readl(FUSE_FAB_CODE); |
| 174 | randomness[2] = tegra_fuse_readl(FUSE_LOT_CODE_0); |
| 175 | randomness[3] = tegra_fuse_readl(FUSE_LOT_CODE_1); |
| 176 | randomness[4] = tegra_fuse_readl(FUSE_WAFER_ID); |
| 177 | randomness[5] = tegra_fuse_readl(FUSE_X_COORDINATE); |
| 178 | randomness[6] = tegra_fuse_readl(FUSE_Y_COORDINATE); |
| 179 | |
| 180 | add_device_randomness(randomness, sizeof(randomness)); |
| 181 | } |
| 182 | |
Stephen Warren | 5875df1 | 2013-09-13 12:18:44 -0600 | [diff] [blame] | 183 | void __init tegra_init_fuse(void) |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 184 | { |
Peter De Schrijver | 35b1498 | 2012-02-10 01:47:41 +0200 | [diff] [blame] | 185 | u32 id; |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 186 | u32 randomness[5]; |
Peter De Schrijver | 35b1498 | 2012-02-10 01:47:41 +0200 | [diff] [blame] | 187 | |
Laxman Dewangan | f8e798a | 2012-08-10 18:33:02 +0530 | [diff] [blame] | 188 | u32 reg = readl(IO_ADDRESS(TEGRA_CLK_RESET_BASE + 0x48)); |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 189 | reg |= 1 << 28; |
Laxman Dewangan | f8e798a | 2012-08-10 18:33:02 +0530 | [diff] [blame] | 190 | writel(reg, IO_ADDRESS(TEGRA_CLK_RESET_BASE + 0x48)); |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 191 | |
Alexandre Courbot | cdcb5a0 | 2013-11-21 11:40:13 +0900 | [diff] [blame] | 192 | /* |
| 193 | * Enable FUSE clock. This needs to be hardcoded because the clock |
| 194 | * subsystem is not active during early boot. |
| 195 | */ |
| 196 | reg = readl(IO_ADDRESS(TEGRA_CLK_RESET_BASE + 0x14)); |
| 197 | reg |= 1 << 7; |
| 198 | writel(reg, IO_ADDRESS(TEGRA_CLK_RESET_BASE + 0x14)); |
| 199 | fuse_clk = ERR_PTR(-EINVAL); |
| 200 | |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 201 | reg = tegra_fuse_readl(FUSE_SKU_INFO); |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 202 | randomness[0] = reg; |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 203 | tegra_sku_id = reg & 0xFF; |
| 204 | |
Olof Johansson | dee4718 | 2011-10-17 16:39:24 -0700 | [diff] [blame] | 205 | reg = tegra_apb_readl(TEGRA_APB_MISC_BASE + STRAP_OPT); |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 206 | randomness[1] = reg; |
Olof Johansson | dee4718 | 2011-10-17 16:39:24 -0700 | [diff] [blame] | 207 | tegra_bct_strapping = (reg & RAM_ID_MASK) >> RAM_CODE_SHIFT; |
| 208 | |
Prashant Gaikwad | c7736ed | 2013-01-11 13:16:19 +0530 | [diff] [blame] | 209 | id = tegra_read_chipid(); |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 210 | randomness[2] = id; |
Peter De Schrijver | 35b1498 | 2012-02-10 01:47:41 +0200 | [diff] [blame] | 211 | tegra_chip_id = (id >> 8) & 0xff; |
| 212 | |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 213 | switch (tegra_chip_id) { |
| 214 | case TEGRA20: |
Danny Huang | f8ddda7 | 2012-11-15 15:42:34 +0800 | [diff] [blame] | 215 | tegra_fuse_spare_bit = TEGRA20_FUSE_SPARE_BIT; |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 216 | tegra_init_speedo_data = &tegra20_init_speedo_data; |
| 217 | break; |
Danny Huang | f8ddda7 | 2012-11-15 15:42:34 +0800 | [diff] [blame] | 218 | case TEGRA30: |
| 219 | tegra_fuse_spare_bit = TEGRA30_FUSE_SPARE_BIT; |
| 220 | tegra_init_speedo_data = &tegra30_init_speedo_data; |
| 221 | break; |
Danny Huang | 7495b2e | 2013-03-18 19:17:34 +0800 | [diff] [blame] | 222 | case TEGRA114: |
| 223 | tegra_init_speedo_data = &tegra114_init_speedo_data; |
| 224 | break; |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 225 | default: |
Danny Huang | f8ddda7 | 2012-11-15 15:42:34 +0800 | [diff] [blame] | 226 | pr_warn("Tegra: unknown chip id %d\n", tegra_chip_id); |
| 227 | tegra_fuse_spare_bit = TEGRA20_FUSE_SPARE_BIT; |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 228 | tegra_init_speedo_data = &tegra_get_process_id; |
| 229 | } |
| 230 | |
Peter De Schrijver | 35b1498 | 2012-02-10 01:47:41 +0200 | [diff] [blame] | 231 | tegra_revision = tegra_get_revision(id); |
Danny Huang | 25cd5a3 | 2012-11-15 15:42:33 +0800 | [diff] [blame] | 232 | tegra_init_speedo_data(); |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 233 | randomness[3] = (tegra_cpu_process_id << 16) | tegra_core_process_id; |
| 234 | randomness[4] = (tegra_cpu_speedo_id << 16) | tegra_soc_speedo_id; |
| 235 | |
| 236 | add_device_randomness(randomness, sizeof(randomness)); |
| 237 | switch (tegra_chip_id) { |
| 238 | case TEGRA20: |
| 239 | tegra20_fuse_init_randomness(); |
Stephen Warren | b988ba1 | 2013-12-06 14:31:02 -0700 | [diff] [blame] | 240 | break; |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 241 | case TEGRA30: |
| 242 | case TEGRA114: |
| 243 | default: |
| 244 | tegra30_fuse_init_randomness(); |
Stephen Warren | b988ba1 | 2013-12-06 14:31:02 -0700 | [diff] [blame] | 245 | break; |
Stephen Warren | 3bd1ae5 | 2013-09-12 16:51:19 -0600 | [diff] [blame] | 246 | } |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 247 | |
| 248 | pr_info("Tegra Revision: %s SKU: %d CPU Process: %d Core Process: %d\n", |
Peter De Schrijver | 35b1498 | 2012-02-10 01:47:41 +0200 | [diff] [blame] | 249 | tegra_revision_name[tegra_revision], |
Olof Johansson | 9a1086d | 2011-10-13 00:31:20 -0700 | [diff] [blame] | 250 | tegra_sku_id, tegra_cpu_process_id, |
| 251 | tegra_core_process_id); |
Colin Cross | 73625e3 | 2010-06-23 15:49:17 -0700 | [diff] [blame] | 252 | } |