| 1 |
|
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <delay.h> |
| 6 |
#include <device/mmio.h> |
| 7 |
#include <soc/thermal.h> |
| 8 |
|
| 9 |
#define LVTS_DEVICE_ACCESS_DELAY_US 3 |
| 10 |
#define THERMAL_LVTS_MSR_OFT 64552 |
| 11 |
#define LVTS_READ_ID_DELAY_US 3 |
| 12 |
#define LVTS_READ_ID_RETRY_CNT 5 |
| 13 |
#define LVTS_SINGLE_SENSE_MODE_EN BIT(9) |
| 14 |
#define CHECK_SENSING_POINTS_IDLE_RETRY_CNT 100 |
| 15 |
#define LVTS_DEVICE_WRITE_CONFIG 0x8103 |
| 16 |
#define LVTS_DEVICE_READ_CONFIG 0x8502 |
| 17 |
#define LVTS_SENSOR_POINT_SELECTION_SETTING 0x13121110 |
| 18 |
#define LVTS_CALCULATION_SCALING_RULE 0x00000300 |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static const struct lvts_thermal_controller lvts_tscpu_g_tc[LVTS_CONTROLLER_NUM] = { |
| 39 |
[LVTS_AP_CONTROLLER0] = { |
| 40 |
.ts = {L_TS_LVTS11_0, L_TS_LVTS11_1, L_TS_LVTS11_2, L_TS_LVTS11_3}, |
| 41 |
.sensor_on_off = {SEN_ON, SEN_ON, SEN_ON, SEN_ON}, |
| 42 |
.ts_number = 4, |
| 43 |
.reboot_temperature = 118800, |
| 44 |
.dominator_ts_idx = 0, |
| 45 |
.reboot_msr_sram_idx = 0, |
| 46 |
.has_reboot_msr_sram = true, |
| 47 |
.has_reboot_temp_sram = true, |
| 48 |
.speed = { |
| 49 |
.group_interval_delay = 0x7fff, |
| 50 |
.period_unit = 0x001, |
| 51 |
.filter_interval_delay = 0x001, |
| 52 |
.sensor_interval_delay = 0x001, |
| 53 |
}, |
| 54 |
.regs = mtk_lvts_ap_controller0, |
| 55 |
}, |
| 56 |
[LVTS_AP_CONTROLLER1] = { |
| 57 |
.ts = {L_TS_LVTS12_0, L_TS_LVTS12_1, L_TS_LVTS12_2, L_TS_LVTS12_3}, |
| 58 |
.sensor_on_off = {SEN_ON, SEN_ON, SEN_ON, SEN_ON}, |
| 59 |
.ts_number = 4, |
| 60 |
.reboot_temperature = 118800, |
| 61 |
.dominator_ts_idx = 0, |
| 62 |
.reboot_msr_sram_idx = 1, |
| 63 |
.has_reboot_msr_sram = true, |
| 64 |
.has_reboot_temp_sram = false, |
| 65 |
.speed = { |
| 66 |
.group_interval_delay = 0x7fff, |
| 67 |
.period_unit = 0x001, |
| 68 |
.filter_interval_delay = 0x001, |
| 69 |
.sensor_interval_delay = 0x001, |
| 70 |
}, |
| 71 |
.regs = mtk_lvts_ap_controller1, |
| 72 |
}, |
| 73 |
}; |
| 74 |
|
| 75 |
static uint32_t golden_temp; |
| 76 |
static uint32_t ts_edata[L_TS_LVTS_NUM]; |
| 77 |
static uint8_t op_cali[LVTS_CONTROLLER_NUM]; |
| 78 |
|
| 79 |
const struct lvts_thermal_controller *lvts_get_controller(int tc_num) |
| 80 |
{ |
| 81 |
assert(tc_num < LVTS_CONTROLLER_NUM); |
| 82 |
|
| 83 |
return &lvts_tscpu_g_tc[tc_num]; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
uint16_t lvts_temp_to_raw(int temp_mc, enum lvts_sensor ts_name) |
| 91 |
{ |
| 92 |
uint32_t msr_raw = 0; |
| 93 |
int64_t coff_a = 0; |
| 94 |
int64_t temp1; |
| 95 |
|
| 96 |
coff_a = LVTS_COEFF_A; |
| 97 |
|
| 98 |
temp1 = (int64_t)temp_mc - (golden_temp * 500) + coff_a; |
| 99 |
assert(temp1 > 0); |
| 100 |
msr_raw = ((coff_a << 14) / temp1) & 0xFFFF; |
| 101 |
|
| 102 |
printk(BIOS_DEBUG, "%s: msr_raw=%u, temp_mc=%d\n", __func__, msr_raw, temp_mc); |
| 103 |
|
| 104 |
return msr_raw; |
| 105 |
} |
| 106 |
|
| 107 |
void lvts_efuse_setting(void) |
| 108 |
{ |
| 109 |
int i, j, s_index; |
| 110 |
uint32_t efuse_data; |
| 111 |
uint32_t val_0, val_1; |
| 112 |
|
| 113 |
printk(BIOS_INFO, "%s\n", __func__); |
| 114 |
|
| 115 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 116 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 117 |
val_0 = 0; |
| 118 |
val_1 = 0; |
| 119 |
for (j = 0; j < tc->ts_number; j++) { |
| 120 |
if (tc->sensor_on_off[j] == SEN_OFF) |
| 121 |
continue; |
| 122 |
|
| 123 |
s_index = tc->ts[j]; |
| 124 |
|
| 125 |
efuse_data = ts_edata[s_index] + THERMAL_LVTS_MSR_OFT; |
| 126 |
|
| 127 |
switch (j) { |
| 128 |
case 0: |
| 129 |
write32(&tc->regs->lvtsedata[0], efuse_data); |
| 130 |
printk(BIOS_INFO, "efuse LVTSEDATA00_%d %#x\n", i, |
| 131 |
read32(&tc->regs->lvtsedata[0])); |
| 132 |
val_0 |= ((LVTS_COF_T_SLP_GLD + 500) / 1000); |
| 133 |
break; |
| 134 |
|
| 135 |
case 1: |
| 136 |
write32(&tc->regs->lvtsedata[1], efuse_data); |
| 137 |
printk(BIOS_INFO, "efuse LVTSEDATA01_%d %#x\n", i, |
| 138 |
read32(&tc->regs->lvtsedata[1])); |
| 139 |
val_0 |= (((LVTS_COF_T_SLP_GLD + 500) / 1000) << 10); |
| 140 |
break; |
| 141 |
case 2: |
| 142 |
write32(&tc->regs->lvtsedata[2], efuse_data); |
| 143 |
printk(BIOS_INFO, "efuse LVTSEDATA02_%d %#x\n", i, |
| 144 |
read32(&tc->regs->lvtsedata[2])); |
| 145 |
val_1 |= ((LVTS_COF_T_SLP_GLD + 500) / 1000); |
| 146 |
break; |
| 147 |
case 3: |
| 148 |
write32(&tc->regs->lvtsedata[3], efuse_data); |
| 149 |
printk(BIOS_INFO, "efuse LVTSEDATA03_%d %#x\n", i, |
| 150 |
read32(&tc->regs->lvtsedata[3])); |
| 151 |
val_1 |= (((LVTS_COF_T_SLP_GLD + 500) / 1000) << 10); |
| 152 |
break; |
| 153 |
default: |
| 154 |
printk(BIOS_ERR, "%s, illegal ts order : %d!!\n", __func__, j); |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
val_0 |= (golden_temp << 20); |
| 159 |
printk(BIOS_INFO, "%s, spare setting: %#x, %#x\n", __func__, val_0, val_1); |
| 160 |
write32(&tc->regs->lvtsmsroft_0, THERMAL_LVTS_MSR_OFT); |
| 161 |
write32(&tc->regs->lvtsspare[1], val_0); |
| 162 |
write32(&tc->regs->lvtsspare[2], val_1); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void lvts_device_identification(void) |
| 167 |
{ |
| 168 |
uint32_t dev_id, data; |
| 169 |
int i; |
| 170 |
|
| 171 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 172 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 173 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 174 |
|
| 175 |
write32(&tc->regs->lvtsclken_0, 0x00000001); |
| 176 |
|
| 177 |
|
| 178 |
write32(&tc->regs->lvtsgslope_0, LDO_ON_SETTING); |
| 179 |
udelay(10); |
| 180 |
|
| 181 |
|
| 182 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0xFF, 0xFF, i); |
| 183 |
|
| 184 |
|
| 185 |
lvts_write_device(LVTS_DEVICE_READ_CONFIG, 0xFC, 0x55, i); |
| 186 |
|
| 187 |
dev_id = 0x8B + i; |
| 188 |
|
| 189 |
if (!retry(LVTS_READ_ID_RETRY_CNT, |
| 190 |
(data = read32(&tc->regs->lvts_id_0) & GENMASK(7, 0)) == dev_id, |
| 191 |
udelay(LVTS_READ_ID_DELAY_US))) |
| 192 |
printk(BIOS_ERR, |
| 193 |
"LVTS_TC_%d read timeout, addr:0x%lx, Device ID should be 0x%x, but 0x%x\n", |
| 194 |
i, (uintptr_t)(&tc->regs->lvts_id_0), dev_id, data); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void lvts_device_enable_init_all_devices(void) |
| 199 |
{ |
| 200 |
int i; |
| 201 |
uint8_t cali_0, cali_1; |
| 202 |
|
| 203 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 204 |
|
| 205 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x03, 0x00, i); |
| 206 |
|
| 207 |
|
| 208 |
if ((op_cali[i] & (1 << 5)) != 0) { |
| 209 |
cali_0 = (op_cali[i] & 0xf) << 4; |
| 210 |
cali_1 = ((op_cali[i] >> 4) & 1) << 4; |
| 211 |
} else { |
| 212 |
cali_0 = (op_cali[i] & 0xf); |
| 213 |
cali_1 = ((op_cali[i] >> 4) & 1) << 3; |
| 214 |
} |
| 215 |
cali_1 |= 0xA0; |
| 216 |
|
| 217 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x20, cali_0, i); |
| 218 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x21, cali_1, i); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
void lvts_thermal_cal_prepare(void) |
| 223 |
{ |
| 224 |
uint32_t temp[7]; |
| 225 |
int i, j; |
| 226 |
bool efuse_calibrated = false; |
| 227 |
|
| 228 |
temp[0] = read32p(LVTS_ADDRESS_INDEX_0); |
| 229 |
temp[1] = read32p(LVTS_ADDRESS_INDEX_12); |
| 230 |
temp[2] = read32p(LVTS_ADDRESS_INDEX_13); |
| 231 |
temp[3] = read32p(LVTS_ADDRESS_INDEX_14); |
| 232 |
temp[4] = read32p(LVTS_ADDRESS_INDEX_15); |
| 233 |
temp[5] = read32p(LVTS_ADDRESS_INDEX_27); |
| 234 |
temp[6] = read32p(LVTS_ADDRESS_INDEX_28); |
| 235 |
|
| 236 |
printk(BIOS_INFO, |
| 237 |
"[lvts_cali] %d: %#x, %d: %#x, %d: %#x, %d: %#x, %d: %#x, %d: %#x, %d: %#x\n", |
| 238 |
0, temp[0], 1, temp[1], 2, temp[2], 3, temp[3], 4, temp[4], 5, temp[5], 6, temp[6]); |
| 239 |
|
| 240 |
golden_temp = temp[0] & GENMASK(7, 0); |
| 241 |
|
| 242 |
ts_edata[L_TS_LVTS11_0] = (temp[1] & GENMASK(31, 16)) >> 16; |
| 243 |
ts_edata[L_TS_LVTS11_1] = temp[1] & GENMASK(15, 0); |
| 244 |
ts_edata[L_TS_LVTS11_2] = (temp[2] & GENMASK(31, 16)) >> 16; |
| 245 |
ts_edata[L_TS_LVTS11_3] = temp[2] & GENMASK(15, 0); |
| 246 |
ts_edata[L_TS_LVTS12_0] = (temp[3] & GENMASK(31, 16)) >> 16; |
| 247 |
ts_edata[L_TS_LVTS12_1] = temp[3] & GENMASK(15, 0); |
| 248 |
ts_edata[L_TS_LVTS12_2] = (temp[4] & GENMASK(31, 16)) >> 16; |
| 249 |
ts_edata[L_TS_LVTS12_3] = temp[4] & GENMASK(15, 0); |
| 250 |
op_cali[LVTS_AP_CONTROLLER0] = (temp[5] & GENMASK(29, 24)) >> 24; |
| 251 |
op_cali[LVTS_AP_CONTROLLER1] = (temp[6] & GENMASK(29, 24)) >> 24; |
| 252 |
|
| 253 |
for (i = 0; i < ARRAY_SIZE(temp); i++) { |
| 254 |
if (temp[i] != 0) { |
| 255 |
efuse_calibrated = true; |
| 256 |
break; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if (!efuse_calibrated) { |
| 261 |
|
| 262 |
printk(BIOS_ERR, "[lvts_cal] This sample is not calibrated; Use fake efuse\n"); |
| 263 |
golden_temp = DEFAULT_EFUSE_GOLDEN_TEMP; |
| 264 |
for (i = 0; i < L_TS_LVTS_NUM; i++) |
| 265 |
ts_edata[i] = DEFAULT_EFUSE_COUNT; |
| 266 |
|
| 267 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) |
| 268 |
op_cali[i] = 0; |
| 269 |
} |
| 270 |
printk(BIOS_INFO, "[lvts_cal] golden_temp = %d\n", golden_temp); |
| 271 |
|
| 272 |
printk(BIOS_INFO, "[lvts_cal] num:ts_edata "); |
| 273 |
for (i = 0; i < L_TS_LVTS_NUM; i++) |
| 274 |
printk(BIOS_INFO, "%d:%d ", i, ts_edata[i]); |
| 275 |
printk(BIOS_INFO, "\n"); |
| 276 |
|
| 277 |
printk(BIOS_INFO, "num:op_cali "); |
| 278 |
for (j = 0; j < LVTS_CONTROLLER_NUM; j++) |
| 279 |
printk(BIOS_INFO, "%d:0x%x ", j, op_cali[j]); |
| 280 |
|
| 281 |
printk(BIOS_INFO, "\n"); |
| 282 |
|
| 283 |
write32(&lvts_tscpu_g_tc[LVTS_AP_CONTROLLER0].regs->lvtsspare[1], golden_temp); |
| 284 |
} |
| 285 |
|
| 286 |
void lvts_tscpu_thermal_read_tc_temp(const struct lvts_thermal_controller *tc, int order) |
| 287 |
{ |
| 288 |
uint32_t rg_temp; |
| 289 |
enum lvts_sensor ts_name = tc->ts[order]; |
| 290 |
int temperature; |
| 291 |
|
| 292 |
ASSERT(order < ARRAY_SIZE(tc->regs->lvtsatp)); |
| 293 |
|
| 294 |
temperature = lvts_read_tc_raw_and_temp(&tc->regs->lvtsatp[order], |
| 295 |
ts_name, |
| 296 |
golden_temp); |
| 297 |
rg_temp = read32(&tc->regs->lvtstemp[order]) & 0x7FFFFF; |
| 298 |
|
| 299 |
printk(BIOS_INFO, "%s order %d ts_name %d temp %d rg_temp %d(%d)\n", __func__, order, |
| 300 |
ts_name, temperature, (rg_temp * 1000 / 1024), rg_temp); |
| 301 |
} |
| 302 |
|
| 303 |
void lvts_tscpu_reset_thermal(void) |
| 304 |
{ |
| 305 |
|
| 306 |
write32p(AP_RST_SET, BIT(11)); |
| 307 |
|
| 308 |
|
| 309 |
write32p(AP_RST_CLR, BIT(11)); |
| 310 |
} |
| 311 |
|
| 312 |
static void reset_cpu_lvts(void) |
| 313 |
{ |
| 314 |
write32p(CPU_LVTS_RESET_ADDR, 1); |
| 315 |
udelay(10); |
| 316 |
write32p(CPU_LVTS_RESET_ADDR, 0); |
| 317 |
|
| 318 |
printk(BIOS_INFO, "%s done\n", __func__); |
| 319 |
} |
| 320 |
|
| 321 |
void thermal_init(void) |
| 322 |
{ |
| 323 |
reset_cpu_lvts(); |
| 324 |
lvts_thermal_init(); |
| 325 |
thermal_sram_init(); |
| 326 |
} |