| 1 |
|
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#include <delay.h> |
| 5 |
#include <device/mmio.h> |
| 6 |
#include <soc/thermal.h> |
| 7 |
#include <timer.h> |
| 8 |
|
| 9 |
#define LVTS_SINGLE_SENSE_MODE_EN BIT(9) |
| 10 |
#define LVTS_SENSOR_POINT_SELECTION_SETTING 0x13121110 |
| 11 |
#define LVTS_CALCULATION_SCALING_RULE 0x300 |
| 12 |
|
| 13 |
__weak void lvts_set_device_single_mode(int tc_num) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
__weak void lvts_clock_gate_disable(void) |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
static int lvts_raw_to_temp(uint16_t msr_raw, enum lvts_sensor ts_name, uint32_t golden_temp) |
| 22 |
{ |
| 23 |
int temp_mc; |
| 24 |
int64_t temp1, coeff_a; |
| 25 |
|
| 26 |
coeff_a = LVTS_COEFF_A; |
| 27 |
|
| 28 |
temp1 = (coeff_a * msr_raw) / (1 << 14); |
| 29 |
temp_mc = temp1 + golden_temp * 500 - coeff_a; |
| 30 |
|
| 31 |
return temp_mc; |
| 32 |
} |
| 33 |
|
| 34 |
int lvts_read_tc_raw_and_temp(void *msr_reg, enum lvts_sensor ts_name, uint32_t golden_temp) |
| 35 |
{ |
| 36 |
int temp; |
| 37 |
uint32_t msr_data; |
| 38 |
uint16_t msr_raw; |
| 39 |
|
| 40 |
msr_data = read32(msr_reg); |
| 41 |
msr_raw = msr_data & 0xFFFF; |
| 42 |
|
| 43 |
if (msr_raw > 0) { |
| 44 |
temp = lvts_raw_to_temp(msr_raw, ts_name, golden_temp); |
| 45 |
} else { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
temp = 26111; |
| 57 |
} |
| 58 |
|
| 59 |
printk(BIOS_INFO, "[LVTS_MSR] ts%d msr_all=%x, msr_temp=%d, temp=%d\n", ts_name, |
| 60 |
msr_data, msr_raw, temp); |
| 61 |
|
| 62 |
return temp; |
| 63 |
} |
| 64 |
|
| 65 |
static void lvts_tscpu_thermal_initial_all_tc(void) |
| 66 |
{ |
| 67 |
u32 i = 0; |
| 68 |
|
| 69 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 70 |
|
| 71 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 72 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 73 |
|
| 74 |
write32(&tc->regs->lvtstssel_0, LVTS_SENSOR_POINT_SELECTION_SETTING); |
| 75 |
|
| 76 |
write32(&tc->regs->lvtscalscale_0, LVTS_CALCULATION_SCALING_RULE); |
| 77 |
|
| 78 |
lvts_set_device_single_mode(i); |
| 79 |
|
| 80 |
lvts_configure_polling_speed_and_filter(tc); |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
void lvts_configure_polling_speed_and_filter(const struct lvts_thermal_controller *tc) |
| 86 |
{ |
| 87 |
u32 lvtsMonCtl1, lvtsMonCtl2; |
| 88 |
|
| 89 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 90 |
|
| 91 |
lvtsMonCtl1 = ((tc->speed.group_interval_delay << 20) & |
| 92 |
LVTS_GROUP_INTERVAL_DELAY_MASK) | (tc->speed.period_unit & GENMASK(9, 0)); |
| 93 |
|
| 94 |
lvtsMonCtl2 = ((tc->speed.filter_interval_delay << 16) & GENMASK(25, 16)) | |
| 95 |
(tc->speed.sensor_interval_delay & GENMASK(9, 0)); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
write32(&tc->regs->lvtsmonctl1_0, lvtsMonCtl1); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
write32(&tc->regs->lvtsmonctl2_0, lvtsMonCtl2); |
| 112 |
|
| 113 |
|
| 114 |
write32(&tc->regs->lvtsmsrctl0_0, 0); |
| 115 |
|
| 116 |
udelay(1); |
| 117 |
printk(BIOS_INFO, |
| 118 |
"%s,LVTSMONCTL1_0= %#x,LVTSMONCTL2_0= %#x,LVTSMSRCTL0_0= %#x\n", |
| 119 |
__func__, |
| 120 |
read32(&tc->regs->lvtsmonctl1_0), |
| 121 |
read32(&tc->regs->lvtsmonctl2_0), |
| 122 |
read32(&tc->regs->lvtsmsrctl0_0)); |
| 123 |
} |
| 124 |
|
| 125 |
static int lvts_check_all_sensing_points_idle(void) |
| 126 |
{ |
| 127 |
uint32_t mask, temp; |
| 128 |
int i; |
| 129 |
|
| 130 |
mask = BIT(10) | BIT(7) | BIT(0); |
| 131 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 132 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 133 |
temp = read32(&tc->regs->lvtsmsrctl1_0); |
| 134 |
if ((temp & mask) != 0) |
| 135 |
return -1; |
| 136 |
} |
| 137 |
|
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
static void lvts_wait_all_sensing_points_idle(void) |
| 142 |
{ |
| 143 |
if (!retry(CHECK_DEVICE_ACCESS_RETRY_CNT, |
| 144 |
lvts_check_all_sensing_points_idle() == 0, |
| 145 |
udelay(2))) |
| 146 |
printk(BIOS_ERR, "%s timeout\n", __func__); |
| 147 |
} |
| 148 |
|
| 149 |
static bool lvts_lk_init_check(void) |
| 150 |
{ |
| 151 |
int i; |
| 152 |
|
| 153 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 154 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 155 |
|
| 156 |
if ((read32(&tc->regs->lvtsspare[0]) & GENMASK(11, 0)) != LVTS_MAGIC) |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
int lvts_write_device(uint16_t config, uint8_t dev_reg_idx, uint8_t data, int tc_num) |
| 164 |
{ |
| 165 |
const struct lvts_thermal_controller *tc = lvts_get_controller(tc_num); |
| 166 |
uint32_t config_data = (config << 16) | (dev_reg_idx << 8) | data; |
| 167 |
|
| 168 |
write32(&tc->regs->lvts_config_0, config_data); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
udelay(LVTS_DEVICE_ACCESS_DELAY_US); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if (!retry(CHECK_DEVICE_ACCESS_RETRY_CNT, |
| 181 |
!(read32(&tc->regs->lvts_config_0) & DEVICE_ACCESS_START_BIT), udelay(2))) { |
| 182 |
printk(BIOS_ERR, "DEVICE_ACCESS_START_BIT didn't ready, reg0x%x\n", |
| 183 |
dev_reg_idx); |
| 184 |
} |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
static void lvts_enable_sensing_points(const struct lvts_thermal_controller *tc) |
| 189 |
{ |
| 190 |
int i; |
| 191 |
|
| 192 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 193 |
uint32_t value = LVTS_SINGLE_SENSE_MODE_EN; |
| 194 |
|
| 195 |
for (i = 0; i < tc->ts_number; i++) { |
| 196 |
if (tc->sensor_on_off[i] == SEN_ON) |
| 197 |
value |= BIT(i); |
| 198 |
} |
| 199 |
|
| 200 |
write32(&tc->regs->lvtsmonctl0_0, value); |
| 201 |
|
| 202 |
printk(BIOS_INFO, "%s,value in LVTSMONCTL0_0 = %#x\n", __func__, value); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
static void lvts_disable_all_sensing_points(void) |
| 209 |
{ |
| 210 |
int i; |
| 211 |
|
| 212 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 213 |
|
| 214 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 215 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 216 |
write32(&tc->regs->lvtsmonctl0_0, LVTS_SINGLE_SENSE_MODE_EN); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
static void lvts_enable_all_sensing_points(void) |
| 221 |
{ |
| 222 |
int i = 0; |
| 223 |
|
| 224 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 225 |
|
| 226 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 227 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 228 |
lvts_enable_sensing_points(tc); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
static void lvts_set_init_flag(void) |
| 233 |
{ |
| 234 |
u32 i; |
| 235 |
|
| 236 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 241 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 242 |
printk(BIOS_INFO, "%s %d:%zu, tc_base_addr:%p\n", __func__, i, |
| 243 |
tc->ts_number, tc->regs); |
| 244 |
|
| 245 |
write32(&tc->regs->lvtsspare[0], LVTS_MAGIC); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
static void lvts_set_tc_trigger_hw_protect(const struct lvts_thermal_controller *tc) |
| 250 |
{ |
| 251 |
int d_index, i; |
| 252 |
uint32_t raw_high; |
| 253 |
uint16_t raw; |
| 254 |
enum lvts_sensor ts_name; |
| 255 |
|
| 256 |
if (tc->dominator_ts_idx < tc->ts_number) { |
| 257 |
d_index = tc->dominator_ts_idx; |
| 258 |
} else { |
| 259 |
printk(BIOS_ERR, "LVTS, dominator_ts_idx %d >= ts_number %zu; use idx 0\n", |
| 260 |
tc->dominator_ts_idx, tc->ts_number); |
| 261 |
d_index = 0; |
| 262 |
} |
| 263 |
|
| 264 |
ts_name = tc->ts[d_index]; |
| 265 |
|
| 266 |
printk(BIOS_INFO, "%s, the dominator ts_name is %d\n", __func__, ts_name); |
| 267 |
|
| 268 |
|
| 269 |
raw_high = 0; |
| 270 |
for (i = 0; i < tc->ts_number; i++) { |
| 271 |
ts_name = tc->ts[i]; |
| 272 |
raw = lvts_temp_to_raw(tc->reboot_temperature, ts_name); |
| 273 |
raw_high = MAX(raw_high, raw); |
| 274 |
} |
| 275 |
if (tc->has_reboot_msr_sram) |
| 276 |
thermal_write_reboot_msr_sram(tc->reboot_msr_sram_idx, raw_high); |
| 277 |
if (tc->has_reboot_temp_sram) |
| 278 |
thermal_write_reboot_temp_sram(tc->reboot_temperature); |
| 279 |
|
| 280 |
setbits32(&tc->regs->lvtsprotctl_0, 0x3FFF); |
| 281 |
|
| 282 |
write32(&tc->regs->lvtsmonint_0, 0); |
| 283 |
|
| 284 |
clrsetbits32(&tc->regs->lvtsprotctl_0, 0xF << 16, BIT(16)); |
| 285 |
|
| 286 |
write32(&tc->regs->lvtsprottc_0, raw_high); |
| 287 |
|
| 288 |
|
| 289 |
write32(&tc->regs->lvtsmonint_0, STAGE3_INT_EN); |
| 290 |
|
| 291 |
clrbits32(&tc->regs->lvtsprotctl_0, 0xFFFF); |
| 292 |
} |
| 293 |
|
| 294 |
void lvts_config_all_tc_hw_protect(void) |
| 295 |
{ |
| 296 |
int i; |
| 297 |
|
| 298 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 299 |
|
| 300 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 301 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 302 |
lvts_set_tc_trigger_hw_protect(tc); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
static void read_all_tc_lvts_temperature(void) |
| 307 |
{ |
| 308 |
int i, j; |
| 309 |
|
| 310 |
for (i = 0; i < LVTS_CONTROLLER_NUM; i++) { |
| 311 |
const struct lvts_thermal_controller *tc = lvts_get_controller(i); |
| 312 |
for (j = 0; j < tc->ts_number; j++) |
| 313 |
lvts_tscpu_thermal_read_tc_temp(tc, j); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
void lvts_thermal_init(void) |
| 318 |
{ |
| 319 |
printk(BIOS_INFO, "===== %s begin ======\n", __func__); |
| 320 |
|
| 321 |
if (lvts_lk_init_check()) |
| 322 |
return; |
| 323 |
|
| 324 |
lvts_clock_gate_disable(); |
| 325 |
|
| 326 |
lvts_tscpu_reset_thermal(); |
| 327 |
|
| 328 |
lvts_thermal_cal_prepare(); |
| 329 |
lvts_device_identification(); |
| 330 |
lvts_device_enable_init_all_devices(); |
| 331 |
lvts_efuse_setting(); |
| 332 |
|
| 333 |
lvts_disable_all_sensing_points(); |
| 334 |
lvts_wait_all_sensing_points_idle(); |
| 335 |
lvts_tscpu_thermal_initial_all_tc(); |
| 336 |
lvts_config_all_tc_hw_protect(); |
| 337 |
lvts_enable_all_sensing_points(); |
| 338 |
|
| 339 |
lvts_set_init_flag(); |
| 340 |
|
| 341 |
printk(BIOS_INFO, "%s: thermal initialized\n", __func__); |
| 342 |
|
| 343 |
read_all_tc_lvts_temperature(); |
| 344 |
} |