Angel Pons | 1ddb894 | 2020-04-04 18:51:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 2 | |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 3 | /* EXYNOS - Thermal Management Unit */ |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 4 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 5 | #include <device/mmio.h> |
Julius Werner | 1ed0c8c | 2014-10-20 13:16:29 -0700 | [diff] [blame] | 6 | #include <console/console.h> |
| 7 | #include <soc/power.h> |
| 8 | #include <soc/tmu.h> |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 9 | |
| 10 | #define TRIMINFO_RELOAD 1 |
| 11 | #define CORE_EN 1 |
| 12 | #define THERM_TRIP_EN (1 << 12) |
| 13 | |
| 14 | #define INTEN_RISE0 1 |
| 15 | #define INTEN_RISE1 (1 << 4) |
| 16 | #define INTEN_RISE2 (1 << 8) |
| 17 | #define INTEN_FALL0 (1 << 16) |
| 18 | #define INTEN_FALL1 (1 << 20) |
| 19 | #define INTEN_FALL2 (1 << 24) |
| 20 | |
| 21 | #define TRIM_INFO_MASK 0xff |
| 22 | |
| 23 | #define INTCLEAR_RISE0 1 |
| 24 | #define INTCLEAR_RISE1 (1 << 4) |
| 25 | #define INTCLEAR_RISE2 (1 << 8) |
| 26 | #define INTCLEAR_FALL0 (1 << 16) |
| 27 | #define INTCLEAR_FALL1 (1 << 20) |
| 28 | #define INTCLEAR_FALL2 (1 << 24) |
| 29 | #define INTCLEARALL (INTCLEAR_RISE0 | INTCLEAR_RISE1 | \ |
| 30 | INTCLEAR_RISE2 | INTCLEAR_FALL0 | \ |
| 31 | INTCLEAR_FALL1 | INTCLEAR_FALL2) |
| 32 | |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 33 | struct tmu_info exynos5250_tmu_info = { |
| 34 | .tmu_base = 0x10060000, |
| 35 | .tmu_mux = 6, |
| 36 | .data = { |
| 37 | .ts = { |
| 38 | .min_val = 25, |
| 39 | .max_val = 125, |
| 40 | .start_warning = 95, |
| 41 | .start_tripping = 105, |
| 42 | .hardware_tripping = 110, |
| 43 | }, |
| 44 | .efuse_min_value = 40, |
| 45 | .efuse_value = 55, |
| 46 | .efuse_max_value = 100, |
| 47 | .slope = 0x10008802, |
| 48 | }, |
| 49 | .dc_value = 25, |
| 50 | }; |
| 51 | |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 52 | /* |
| 53 | * After reading temperature code from register, compensating |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 54 | * its value and calculating celsius temperature, |
| 55 | * get current temperature. |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 56 | * |
| 57 | * @return current temperature of the chip as sensed by TMU |
| 58 | */ |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 59 | static int get_cur_temp(struct tmu_info *info) |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 60 | { |
| 61 | int cur_temp; |
| 62 | struct tmu_reg *reg = (struct tmu_reg *)info->tmu_base; |
| 63 | |
| 64 | /* Temperature code range between min 25 and max 125 */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 65 | cur_temp = read32(®->current_temp) & 0xff; |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 66 | |
| 67 | /* Calibrate current temperature */ |
| 68 | if (cur_temp) |
| 69 | cur_temp = cur_temp - info->te1 + info->dc_value; |
| 70 | |
| 71 | return cur_temp; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Monitors status of the TMU device and exynos temperature |
| 76 | * |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 77 | * @info TMU info |
| 78 | * @temp pointer to the current temperature value |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 79 | * @return enum tmu_status_t value, code indicating event to execute |
| 80 | */ |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 81 | enum tmu_status_t tmu_monitor(struct tmu_info *info, int *temp) |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 82 | { |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 83 | if (info->tmu_state == TMU_STATUS_INIT) |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 84 | return -1; |
| 85 | |
| 86 | int cur_temp; |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 87 | struct tmu_data *data = &info->data; |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 88 | |
| 89 | /* Read current temperature of the SOC */ |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 90 | cur_temp = get_cur_temp(info); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 91 | *temp = cur_temp; |
| 92 | |
| 93 | /* Temperature code lies between min 25 and max 125 */ |
| 94 | if (cur_temp >= data->ts.start_tripping && |
| 95 | cur_temp <= data->ts.max_val) |
| 96 | return TMU_STATUS_TRIPPED; |
| 97 | else if (cur_temp >= data->ts.start_warning) |
| 98 | return TMU_STATUS_WARNING; |
| 99 | else if (cur_temp < data->ts.start_warning && |
| 100 | cur_temp >= data->ts.min_val) |
| 101 | return TMU_STATUS_NORMAL; |
| 102 | /* Temperature code does not lie between min 25 and max 125 */ |
| 103 | else { |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 104 | info->tmu_state = TMU_STATUS_INIT; |
| 105 | printk(BIOS_DEBUG, "EXYNOS_TMU: Thermal reading failed\n"); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 112 | * Calibrate and calculate threshold values and |
| 113 | * enable interrupt levels |
| 114 | * |
| 115 | * @param info pointer to the tmu_info struct |
| 116 | */ |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 117 | static void tmu_setup_parameters(struct tmu_info *info) |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 118 | { |
| 119 | unsigned int te_temp, con; |
| 120 | unsigned int warning_code, trip_code, hwtrip_code; |
| 121 | unsigned int cooling_temp; |
| 122 | unsigned int rising_value; |
| 123 | struct tmu_data *data = &info->data; |
| 124 | struct tmu_reg *reg = (struct tmu_reg *)info->tmu_base; |
| 125 | |
| 126 | /* Must reload for using efuse value at EXYNOS */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 127 | write32(®->triminfo_control, TRIMINFO_RELOAD); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 128 | |
| 129 | /* Get the compensation parameter */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 130 | te_temp = read32(®->triminfo); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 131 | info->te1 = te_temp & TRIM_INFO_MASK; |
| 132 | info->te2 = ((te_temp >> 8) & TRIM_INFO_MASK); |
| 133 | |
| 134 | if ((data->efuse_min_value > info->te1) || |
| 135 | (info->te1 > data->efuse_max_value) |
| 136 | || (info->te2 != 0)) |
| 137 | info->te1 = data->efuse_value; |
| 138 | |
| 139 | /* Get RISING & FALLING Threshold value */ |
| 140 | warning_code = data->ts.start_warning |
| 141 | + info->te1 - info->dc_value; |
| 142 | trip_code = data->ts.start_tripping |
| 143 | + info->te1 - info->dc_value; |
| 144 | hwtrip_code = data->ts.hardware_tripping |
| 145 | + info->te1 - info->dc_value; |
| 146 | |
| 147 | cooling_temp = 0; |
| 148 | |
| 149 | rising_value = ((warning_code << 8) | |
| 150 | (trip_code << 16) | |
| 151 | (hwtrip_code << 24)); |
| 152 | |
| 153 | /* Set interrupt level */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 154 | write32(®->threshold_temp_rise, rising_value); |
| 155 | write32(®->threshold_temp_fall, cooling_temp); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * Need to init all register settings after getting parameter info |
| 159 | * [28:23] vref [11:8] slope - Tuning parameter |
| 160 | * |
| 161 | * WARNING: this slope value writes into many bits in the tmu_control |
| 162 | * register, with the default FDT value of 268470274 (0x10008802) |
| 163 | * we are using this essentially sets the default register setting |
| 164 | * from the TRM for tmu_control. |
| 165 | * TODO(bhthompson): rewrite this code such that we are not performing |
| 166 | * a hard wipe of tmu_control and re verify functionality. |
| 167 | */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 168 | write32(®->tmu_control, data->slope); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 169 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 170 | write32(®->intclear, INTCLEARALL); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 171 | /* TMU core enable */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 172 | con = read32(®->tmu_control); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 173 | con |= (info->tmu_mux << 20) | THERM_TRIP_EN | CORE_EN; |
| 174 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 175 | write32(®->tmu_control, con); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 176 | |
| 177 | /* Enable HW thermal trip */ |
| 178 | power_enable_hw_thermal_trip(); |
| 179 | |
| 180 | /* LEV1 LEV2 interrupt enable */ |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 181 | write32(®->inten, INTEN_RISE1 | INTEN_RISE2); |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Initialize TMU device |
| 186 | * |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 187 | * @return int value, 0 for success |
| 188 | */ |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 189 | int tmu_init(struct tmu_info *info) |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 190 | { |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 191 | info->tmu_state = TMU_STATUS_INIT; |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 192 | |
David Hendricks | cd14ed7 | 2013-04-18 14:21:15 -0700 | [diff] [blame] | 193 | tmu_setup_parameters(info); |
| 194 | info->tmu_state = TMU_STATUS_NORMAL; |
David Hendricks | 90a7009 | 2013-04-18 14:01:45 -0700 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |