| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <assert.h> |
| 9 |
#include <delay.h> |
| 10 |
#include <soc/efuse.h> |
| 11 |
#include <soc/thermal.h> |
| 12 |
|
| 13 |
#define LVTS_DEVICE_WRITE_CONFIG 0xC103 |
| 14 |
|
| 15 |
static uint32_t golden_temp; |
| 16 |
|
| 17 |
static u32 ts_edata[L_TS_LVTS_NUM]; |
| 18 |
static u32 op_cali[LVTS_CONTROLLER_NUM]; |
| 19 |
static u32 count_rc_now[L_TS_LVTS_NUM]; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static const struct lvts_thermal_controller lvts_tscpu_g_tc[LVTS_CONTROLLER_NUM] = { |
| 34 |
[LVTS_MCU_CONTROLLER0] = { |
| 35 |
.ts = {L_TS_LVTS1_0, L_TS_LVTS1_1, L_TS_LVTS1_2, L_TS_LVTS1_3}, |
| 36 |
.sensor_on_off = {SEN_ON, SEN_ON, SEN_ON, SEN_ON}, |
| 37 |
.ts_number = 4, |
| 38 |
.reboot_temperature = 119000, |
| 39 |
.dominator_ts_idx = 0, |
| 40 |
.reboot_msr_sram_idx = 0, |
| 41 |
.has_reboot_msr_sram = false, |
| 42 |
.has_reboot_temp_sram = false, |
| 43 |
.speed = { |
| 44 |
.group_interval_delay = 0x0F6, |
| 45 |
.period_unit = 0x001, |
| 46 |
.filter_interval_delay = 0x001, |
| 47 |
.sensor_interval_delay = 0x001 |
| 48 |
}, |
| 49 |
.regs = mtk_lvts_mcu_controller0, |
| 50 |
}, |
| 51 |
[LVTS_MCU_CONTROLLER1] = { |
| 52 |
.ts = {L_TS_LVTS2_0, L_TS_LVTS2_1, L_TS_LVTS2_2, L_TS_LVTS2_3}, |
| 53 |
.sensor_on_off = {SEN_ON, SEN_ON, SEN_ON, SEN_ON}, |
| 54 |
.ts_number = 4, |
| 55 |
.reboot_temperature = 119000, |
| 56 |
.dominator_ts_idx = 0, |
| 57 |
.reboot_msr_sram_idx = 0, |
| 58 |
.has_reboot_msr_sram = false, |
| 59 |
.has_reboot_temp_sram = false, |
| 60 |
.speed = { |
| 61 |
.group_interval_delay = 0x0F6, |
| 62 |
.period_unit = 0x001, |
| 63 |
.filter_interval_delay = 0x001, |
| 64 |
.sensor_interval_delay = 0x001 |
| 65 |
}, |
| 66 |
.regs = mtk_lvts_mcu_controller1, |
| 67 |
}, |
| 68 |
[LVTS_MCU_CONTROLLER2] = { |
| 69 |
.ts = {L_TS_LVTS3_0, L_TS_LVTS3_1, L_TS_LVTS3_2, L_TS_LVTS3_3}, |
| 70 |
.sensor_on_off = {SEN_ON, SEN_ON, SEN_ON, SEN_ON}, |
| 71 |
.ts_number = 4, |
| 72 |
.reboot_temperature = 119000, |
| 73 |
.dominator_ts_idx = 0, |
| 74 |
.reboot_msr_sram_idx = 0, |
| 75 |
.has_reboot_msr_sram = false, |
| 76 |
.has_reboot_temp_sram = false, |
| 77 |
.speed = { |
| 78 |
.group_interval_delay = 0x1C1, |
| 79 |
.period_unit = 0x001, |
| 80 |
.filter_interval_delay = 0x001, |
| 81 |
.sensor_interval_delay = 0x001 |
| 82 |
}, |
| 83 |
.regs = mtk_lvts_mcu_controller2, |
| 84 |
}, |
| 85 |
[LVTS_AP_CONTROLLER0] = { |
| 86 |
.ts = {L_TS_LVTS4_0, L_TS_LVTS4_1, L_TS_LVTS4_2, L_TS_LVTS4_3}, |
| 87 |
.sensor_on_off = {SEN_ON, SEN_ON, SEN_ON, SEN_ON}, |
| 88 |
.ts_number = 4, |
| 89 |
.reboot_temperature = 119000, |
| 90 |
.dominator_ts_idx = 0, |
| 91 |
.reboot_msr_sram_idx = 0, |
| 92 |
.has_reboot_msr_sram = false, |
| 93 |
.has_reboot_temp_sram = false, |
| 94 |
.speed = { |
| 95 |
.group_interval_delay = 0x0D2, |
| 96 |
.period_unit = 0x001, |
| 97 |
.filter_interval_delay = 0x001, |
| 98 |
.sensor_interval_delay = 0x001 |
| 99 |
}, |
| 100 |
.regs = mtk_lvts_ap_controller0, |
| 101 |
}, |
| 102 |
[LVTS_MFG_CONTROLLER0] = { |
| 103 |
.ts = {L_TS_LVTS5_0, L_TS_LVTS5_1}, |
| 104 |
.sensor_on_off = {SEN_ON, SEN_ON}, |
| 105 |
.ts_number = 2, |
| 106 |
.reboot_temperature = 119000, |
| 107 |
.dominator_ts_idx = 0, |
| 108 |
.reboot_msr_sram_idx = 0, |
| 109 |
.has_reboot_msr_sram = false, |
| 110 |
.has_reboot_temp_sram = false, |
| 111 |
.speed = { |
| 112 |
.group_interval_delay = 0x08A, |
| 113 |
.period_unit = 0x001, |
| 114 |
.filter_interval_delay = 0x001, |
| 115 |
.sensor_interval_delay = 0x001 |
| 116 |
}, |
| 117 |
.regs = mtk_lvts_mfg_controller0, |
| 118 |
}, |
| 119 |
}; |
| 120 |
|
| 121 |
const struct lvts_thermal_controller *lvts_get_controller(int tc_num) |
| 122 |
{ |
| 123 |
assert(tc_num < LVTS_CONTROLLER_NUM); |
| 124 |
|
| 125 |
return &lvts_tscpu_g_tc[tc_num]; |
| 126 |
} |
| 127 |
|
| 128 |
void lvts_clock_gate_disable(void) |
| 129 |
{ |
| 130 |
write32(&mtk_infracfg_ao->infracfg_ao_module_cg_0_clr, THERM_CG); |
| 131 |
} |
| 132 |
|
| 133 |
void lvts_tscpu_reset_thermal(void) |
| 134 |
{ |
| 135 |
|
| 136 |
write32(&mtk_infracfg->ap_rst_set, BIT(23)); |
| 137 |
write32(&mtk_infracfg->ap_rst_clr, BIT(23)); |
| 138 |
|
| 139 |
|
| 140 |
write32(&mtk_infracfg->mcu_rst_set, BIT(12)); |
| 141 |
write32(&mtk_infracfg->mcu_rst_clr, BIT(12)); |
| 142 |
} |
| 143 |
|
| 144 |
void lvts_thermal_cal_prepare(void) |
| 145 |
{ |
| 146 |
u32 temp[LVTS_ADDRESS_INDEX_NUM]; |
| 147 |
int i; |
| 148 |
bool efuse_calibrated = false; |
| 149 |
enum lvts_sensor sensor_groups[] = {L_TS_LVTS1_0, L_TS_LVTS2_0, |
| 150 |
L_TS_LVTS3_0, L_TS_LVTS4_0, |
| 151 |
L_TS_LVTS5_0}; |
| 152 |
const size_t sensor_group_size = 4; |
| 153 |
int idx; |
| 154 |
|
| 155 |
for (i = 0; i < ARRAY_SIZE(temp); i++) { |
| 156 |
temp[i] = read32(&mtk_efuse->lvts_reg[i]); |
| 157 |
printk(BIOS_INFO, "[lvts_cali] %d: 0x%x\n", i, temp[i]); |
| 158 |
|
| 159 |
if (temp[i] != 0) |
| 160 |
efuse_calibrated = true; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
golden_temp = (temp[0] & GENMASK(7, 0)); |
| 165 |
idx = 0; |
| 166 |
|
| 167 |
for (i = 0; i < ARRAY_SIZE(sensor_groups); i++) { |
| 168 |
idx++; |
| 169 |
for (int j = 0; j < sensor_group_size; j++) { |
| 170 |
enum lvts_sensor sensor = sensor_groups[i] + j; |
| 171 |
if (sensor >= L_TS_LVTS_NUM) |
| 172 |
break; |
| 173 |
ts_edata[sensor] = temp[idx++] & GENMASK(23, 0); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
op_cali[LVTS_MCU_CONTROLLER0] = ((temp[0] & GENMASK(31, 8)) >> 8); |
| 179 |
|
| 180 |
op_cali[LVTS_MCU_CONTROLLER1] = (temp[5] & GENMASK(23, 0)); |
| 181 |
|
| 182 |
op_cali[LVTS_MCU_CONTROLLER2] = (temp[10] & GENMASK(23, 0)); |
| 183 |
|
| 184 |
|
| 185 |
op_cali[LVTS_AP_CONTROLLER0] = (temp[15] & GENMASK(23, 0)); |
| 186 |
|
| 187 |
|
| 188 |
op_cali[LVTS_MFG_CONTROLLER0] = (temp[20] & GENMASK(23, 0)); |
| 189 |
|
| 190 |
if (!efuse_calibrated) { |
| 191 |
|
| 192 |
printk(BIOS_ERR, |
| 193 |
"[lvts_cal] The efuses of this sample are all zero, " |
| 194 |
"meaning it is not calibrated, fake!!!\n"); |
| 195 |
golden_temp = DEFAULT_EFUSE_GOLDEN_TEMP; |
| 196 |
for (i = 0; i < L_TS_LVTS_NUM; i++) |
| 197 |
ts_edata[i] = DEFAULT_EFUSE_COUNT; |
| 198 |
|
| 199 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) |
| 200 |
op_cali[i] = DEFAULT_EFUSE_COUNT_RC; |
| 201 |
} |
| 202 |
|
| 203 |
printk(BIOS_INFO, "[lvts_cal] golden_temp = %d mdegC\n", golden_temp); |
| 204 |
|
| 205 |
printk(BIOS_INFO, "[lvts_cal] num:ts_edata:op_cali "); |
| 206 |
for (i = 0; i < ARRAY_SIZE(ts_edata); i++) { |
| 207 |
if (i < ARRAY_SIZE(op_cali)) |
| 208 |
printk(BIOS_INFO, "%d:%d:%d ", i, ts_edata[i], op_cali[i]); |
| 209 |
else |
| 210 |
printk(BIOS_INFO, "%d:%d ", i, ts_edata[i]); |
| 211 |
} |
| 212 |
printk(BIOS_INFO, "\n"); |
| 213 |
|
| 214 |
write32(&lvts_tscpu_g_tc[0].regs->lvtsspare[1], golden_temp); |
| 215 |
} |
| 216 |
|
| 217 |
void lvts_device_identification(void) |
| 218 |
{ |
| 219 |
u32 dev_id, data; |
| 220 |
int i; |
| 221 |
|
| 222 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 223 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 224 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 225 |
|
| 226 |
write32(&tc->regs->lvtsclken_0, 0x00000001); |
| 227 |
|
| 228 |
|
| 229 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0xFF, 0xFF, i); |
| 230 |
|
| 231 |
|
| 232 |
lvts_write_device(0xC502, 0xFC, 0x55, i); |
| 233 |
|
| 234 |
dev_id = 0x81 + i; |
| 235 |
|
| 236 |
if (!retry(LVTS_READ_ID_RETRY_CNT, |
| 237 |
(data = read32(&tc->regs->lvts_id_0) & GENMASK(7, 0)) == dev_id, |
| 238 |
udelay(LVTS_READ_ID_DELAY_US))) |
| 239 |
printk(BIOS_ERR, |
| 240 |
"LVTS_TC_%d read timeout, addr:0x%lx, Device ID should be 0x%x," |
| 241 |
" but 0x%x\n", |
| 242 |
i, (uintptr_t)(&tc->regs->lvts_id_0), dev_id, data); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
void lvts_device_enable_init_all_devices(void) |
| 247 |
{ |
| 248 |
int i; |
| 249 |
|
| 250 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 251 |
|
| 252 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 253 |
|
| 254 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x03, 0x00, i); |
| 255 |
|
| 256 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x07, 0xA6, i); |
| 257 |
|
| 258 |
|
| 259 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x05, 0x00, i); |
| 260 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x04, 0x20, i); |
| 261 |
|
| 262 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x0A, 0x8C, i); |
| 263 |
|
| 264 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x0C, 0xFC, i); |
| 265 |
|
| 266 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x09, 0x8D, i); |
| 267 |
|
| 268 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x08, 0xF1, i); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
static u32 lvts_read_device(u32 config, u32 dev_reg_idx, int tc_num) |
| 273 |
{ |
| 274 |
u32 data; |
| 275 |
u32 *lvts_config_reg = &lvts_tscpu_g_tc[tc_num].regs->lvts_config_0; |
| 276 |
|
| 277 |
config |= (dev_reg_idx & 0xFF) << 8; |
| 278 |
|
| 279 |
write32(lvts_config_reg, config); |
| 280 |
|
| 281 |
|
| 282 |
udelay(5); |
| 283 |
|
| 284 |
if (!retry(CHECK_DEVICE_ACCESS_RETRY_CNT, |
| 285 |
!(read32(lvts_config_reg) & DEVICE_ACCESS_START_BIT), |
| 286 |
udelay(2))) |
| 287 |
printk(BIOS_ERR, |
| 288 |
"read err: DEVICE_ACCESS_START_BIT is not ready, " |
| 289 |
"ASIF transaction not finished, reg0x%x\n", |
| 290 |
dev_reg_idx); |
| 291 |
|
| 292 |
data = read32(&lvts_tscpu_g_tc[tc_num].regs->lvtsrdata0_0); |
| 293 |
|
| 294 |
return data; |
| 295 |
} |
| 296 |
|
| 297 |
static void lvts_device_check_counting_status(const struct lvts_thermal_controller *tc) |
| 298 |
{ |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
if (!retry(CHECK_DEVICE_ACCESS_RETRY_CNT, |
| 305 |
!(read32(&tc->regs->lvts_config_0) & DEVICE_SENSING_STATUS), |
| 306 |
udelay(2))) |
| 307 |
printk(BIOS_ERR, "Error: DEVICE_SENSING_STATUS is not ready\n"); |
| 308 |
} |
| 309 |
|
| 310 |
static void lvts_device_read_count_rc_n(void) |
| 311 |
{ |
| 312 |
|
| 313 |
int i, j, s_index; |
| 314 |
u32 data; |
| 315 |
|
| 316 |
|
| 317 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) |
| 318 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x0E, 0x00, i); |
| 319 |
|
| 320 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 321 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 322 |
|
| 323 |
|
| 324 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x0E, 0x00, i); |
| 325 |
|
| 326 |
for (j = 0; j < tc->ts_number; j++) { |
| 327 |
if (tc->sensor_on_off[j] == SEN_OFF) |
| 328 |
continue; |
| 329 |
|
| 330 |
s_index = tc->ts[j]; |
| 331 |
|
| 332 |
|
| 333 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x0D, j, i); |
| 334 |
|
| 335 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x06, 0x78, i); |
| 336 |
|
| 337 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x08, 0xF5, i); |
| 338 |
|
| 339 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x08, 0xFD, i); |
| 340 |
|
| 341 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x08, 0xF5, i); |
| 342 |
|
| 343 |
udelay(10); |
| 344 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x03, 0x02, i); |
| 345 |
|
| 346 |
lvts_device_check_counting_status(tc); |
| 347 |
|
| 348 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x08, 0xF1, i); |
| 349 |
|
| 350 |
|
| 351 |
if (j == 0) |
| 352 |
udelay(42); |
| 353 |
|
| 354 |
|
| 355 |
data = lvts_read_device(0xC1020000, 0x00, i); |
| 356 |
|
| 357 |
udelay(5); |
| 358 |
|
| 359 |
|
| 360 |
count_rc_now[s_index] = data & GENMASK(23, 0); |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x0D, (0x10 | j), i); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
printk(BIOS_INFO, "[COUNT_RC_NOW] "); |
| 372 |
for (i = 0; i < L_TS_LVTS_NUM; i++) |
| 373 |
printk(BIOS_INFO, "%d:%d ", i, count_rc_now[i]); |
| 374 |
|
| 375 |
printk(BIOS_INFO, "\n"); |
| 376 |
} |
| 377 |
|
| 378 |
void lvts_efuse_setting(void) |
| 379 |
{ |
| 380 |
int i, j, s_index; |
| 381 |
uint32_t efuse_data; |
| 382 |
|
| 383 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 384 |
|
| 385 |
lvts_device_read_count_rc_n(); |
| 386 |
|
| 387 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 388 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 389 |
|
| 390 |
assert(tc->ts_number <= ARRAY_SIZE(tc->regs->lvtsedata)); |
| 391 |
for (j = 0; j < tc->ts_number; j++) { |
| 392 |
if (tc->sensor_on_off[j] == SEN_OFF) |
| 393 |
continue; |
| 394 |
|
| 395 |
s_index = tc->ts[j]; |
| 396 |
|
| 397 |
efuse_data = ((u64)count_rc_now[s_index] * ts_edata[s_index]) >> 14; |
| 398 |
|
| 399 |
write32(&tc->regs->lvtsedata[j], efuse_data); |
| 400 |
printk(BIOS_INFO, |
| 401 |
"efuse LVTSEDATA[%d]_%d %#x\n", j, i, |
| 402 |
read32(&tc->regs->lvtsedata[j])); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
void lvts_tscpu_thermal_read_tc_temp(const struct lvts_thermal_controller *tc, int order) |
| 408 |
{ |
| 409 |
enum lvts_sensor ts_name = tc->ts[order]; |
| 410 |
int temperature; |
| 411 |
assert(order < ARRAY_SIZE(tc->regs->lvtsmsr)); |
| 412 |
temperature = lvts_read_tc_raw_and_temp(&tc->regs->lvtsmsr[order], |
| 413 |
ts_name, |
| 414 |
golden_temp); |
| 415 |
|
| 416 |
printk(BIOS_INFO, |
| 417 |
"%s order %d ts_name %d temp %d mdegC\n", |
| 418 |
__func__, order, ts_name, temperature); |
| 419 |
} |
| 420 |
|
| 421 |
void lvts_set_device_single_mode(int tc_num) |
| 422 |
{ |
| 423 |
lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x06, 0xF8, tc_num); |
| 424 |
} |
| 425 |
|
| 426 |
uint16_t lvts_temp_to_raw(int temp_mc, enum lvts_sensor ts_name) |
| 427 |
{ |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
uint32_t msr_raw; |
| 434 |
int64_t temp1; |
| 435 |
|
| 436 |
temp1 = (int64_t)golden_temp * 500 - LVTS_COEFF_A - temp_mc; |
| 437 |
assert(temp1 >= 0); |
| 438 |
msr_raw = ((temp1 << 14) / (-1 * LVTS_COEFF_A)) & 0xFFFF; |
| 439 |
|
| 440 |
printk(BIOS_INFO, "%s msr_raw = %u, temp_mc = %d\n", __func__, msr_raw, temp_mc); |
| 441 |
return msr_raw; |
| 442 |
} |
| 443 |
|
| 444 |
void thermal_init(void) |
| 445 |
{ |
| 446 |
lvts_thermal_init(); |
| 447 |
thermal_sram_init(); |
| 448 |
} |