Debian Codegraph @

src/soc/mediatek/mt8189/thermal.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
2
3 /*
4 * This file is created based on MT8189 Functional Specification
5 * Chapter number: 9.13
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 * module LVTS Plan
23 *=====================================================
24 * MCU_BIG core0/1 LVTS1-0, LVTS1-1, LVTS1-2, LVTS1-3
25 * MCU_BIG core2/3 LVTS2-0, LVTS2-1, LVTS2-2, LVTS2-3
26 * MCU_LITTLE LVTS3-0, LVTS3-1, LVTS3-2, LVTS3-3
27 * SOC-TOP,APU LVTS4-0, LVTS4-1, LVTS4-2, LVTS4-3
28 * GPU LVTS5-0, LVTS5-1
29 *
30 * ptp_therm_ctrl_AP Base address: (+0x1031_5000), 0x1031_5100, 0x1031_5200
31 * ptp_therm_ctrl_MCU Base address: (+0x1031_6000), 0x1031_6100, 0x1031_6200, 0x1031_6300
32 */
33 static const struct lvts_thermal_controller lvts_tscpu_g_tc[LVTS_CONTROLLER_NUM] = {
34 [LVTS_MCU_CONTROLLER0] = { /* MCU_BIG core0/1 */
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] = { /* MCU_BIG core2/3 */
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] = { /* MCU_LITTLE */
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] = { /* SOC-TOP, APU */
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] = { /* MFG */
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 /* Reset AP thermal control */
136 write32(&mtk_infracfg->ap_rst_set, BIT(23));
137 write32(&mtk_infracfg->ap_rst_clr, BIT(23));
138
139 /* Reset MCU thermal control */
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 /* 0x11F1_01A4,LVTS1 */
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 /* 0x11F1_01A4,LVTS1 */
178 op_cali[LVTS_MCU_CONTROLLER0] = ((temp[0] & GENMASK(31, 8)) >> 8);
179 /* 0x11F1_01B8,LVTS2 */
180 op_cali[LVTS_MCU_CONTROLLER1] = (temp[5] & GENMASK(23, 0));
181 /* 0x11F1_01CC,LVTS3 */
182 op_cali[LVTS_MCU_CONTROLLER2] = (temp[10] & GENMASK(23, 0));
183
184 /* 0x11F1_01E0,LVTS4 */
185 op_cali[LVTS_AP_CONTROLLER0] = (temp[15] & GENMASK(23, 0));
186
187 /* 0x11F1_01F4,LVTS5 */
188 op_cali[LVTS_MFG_CONTROLLER0] = (temp[20] & GENMASK(23, 0));
189
190 if (!efuse_calibrated) {
191 /* It means all efuse data are equal to 0 */
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 /* Enable LVTS_CTRL Clock */
226 write32(&tc->regs->lvtsclken_0, 0x00000001);
227
228 /* Reset All Devices */
229 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0xFF, 0xFF, i);
230
231 /* Read back Dev_ID with Update */
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 /* Stop Counting (RG_TSFM_ST=0) */
254 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x03, 0x00, i);
255 /* RG_TSFM_LPDLY[1:0]=2' 10 */
256 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x07, 0xA6, i);
257 /* Set LVTS device counting window 20us */
258 /* Counting Window */
259 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x05, 0x00, i);
260 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x04, 0x20, i);
261 /* TSV2F_CHOP_CKSEL & TSV2F_EN */
262 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x0A, 0x8C, i);
263 /* TSBG_DEM_CKSEL * TSBG_CHOP_EN */
264 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x0C, 0xFC, i);
265 /* Set TS_RSV */
266 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x09, 0x8D, i);
267 /* Set TS_CHOP control */
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 /* Wait 2us + 3us buffer */
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 * Check this when LVTS device is counting for a temperature or
301 * a RC (Resistor-Capacitor Calibration) now
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 /* Perform RC (Resistor-Capacitor Calibration) and read current count (count_rc_n) */
313 int i, j, s_index;
314 u32 data;
315
316 /* Set LVTS MANUAL-RCK operation */
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 /* Set LVTS Manual-RCK operation */
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 /* Select sensor-N with RCK */
333 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x0D, j, i);
334 /* Set Device Low-Power Single mode */
335 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x06, 0x78, i);
336 /* Set TS_EN and DEV_EN */
337 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x08, 0xF5, i);
338 /* Toggle VCO_RST */
339 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x08, 0xFD, i);
340 /* Set TS_EN and DEV_EN */
341 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x08, 0xF5, i);
342 /* wait 10us */
343 udelay(10);
344 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x03, 0x02, i);
345 /* polling counting status */
346 lvts_device_check_counting_status(tc);
347 /* disable TS_EN */
348 lvts_write_device(LVTS_DEVICE_WRITE_CONFIG, 0x08, 0xF1, i);
349 /* wait 40us at sensor0 */
350 /* 2us is margin for different IC */
351 if (j == 0)
352 udelay(42);
353
354 /* Get RCK count data (sensor-N) */
355 data = lvts_read_device(0xC1020000, 0x00, i);
356 /* wait 2us + 3us buffer */
357 udelay(5);
358
359 /* Get RCK value from LSB[23:0] */
360 count_rc_now[s_index] = data & GENMASK(23, 0);
361
362 /*
363 * Recover Setting for Normal Access on
364 * temperature fetch
365 */
366 /* Select Sensor-N without RCK */
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 * MSR_RAW = ((temp_mc - GOLDEN_TEMP/2 - b) * 16384) / a
430 * a = -250.46
431 * b = 250.46
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 }