Debian Codegraph @

src/superio/ite/common/env_ctrl.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <console/console.h>
4 #include <delay.h>
5 #include <stddef.h>
6 #include <superio/hwm5_conf.h>
7 #include <option.h>
8
9 #include "env_ctrl.h"
10 #include "env_ctrl_chip.h"
11
12 static void extemp_force_idle_status(const u16 base)
13 {
14 u8 reg;
15 int retries = 10;
16
17 /* Wait up to 10ms for non-busy state. */
18 while (retries > 0) {
19 reg = pnp_read_hwm5_index(base, ITE_EC_EXTEMP_STATUS);
20
21 if ((reg & ITE_EC_EXTEMP_STATUS_HOST_BUSY) == 0x0)
22 break;
23
24 retries--;
25
26 mdelay(1);
27 }
28
29 if (retries == 0 && (reg & ITE_EC_EXTEMP_STATUS_HOST_BUSY) == 0x1) {
30 /*
31 * SIO is busy due to unfinished peci transaction.
32 * Re-configure Register 0x8E to terminate processes.
33 */
34 pnp_write_hwm5_index(base, ITE_EC_EXTEMP_CONTROL,
35 ITE_EC_EXTEMP_CTRL_AUTO_4HZ | ITE_EC_EXTEMP_CTRL_AUTO_START);
36 }
37 }
38
39 /*
40 * Setup PECI interface
41 */
42 static void enable_peci(const u16 base)
43 {
44 /* Enable PECI interface */
45 pnp_write_hwm5_index(base, ITE_EC_INTERFACE_SELECT,
46 ITE_EC_INTERFACE_SEL_PECI | ITE_EC_INTERFACE_SPEED_TOLERANCE);
47
48 /* Setup External Temperature using PECI GetTemp */
49 pnp_write_hwm5_index(base, ITE_EC_EXTEMP_ADDRESS, PECI_CLIENT_ADDRESS);
50 pnp_write_hwm5_index(base, ITE_EC_EXTEMP_COMMAND, PECI_GETTEMP_COMMAND);
51 pnp_write_hwm5_index(base, ITE_EC_EXTEMP_WRITE_LENGTH, PECI_GETTEMP_WRITE_LENGTH);
52 pnp_write_hwm5_index(base, ITE_EC_EXTEMP_READ_LENGTH, PECI_GETTEMP_READ_LENGTH);
53 pnp_write_hwm5_index(base, ITE_EC_EXTEMP_CONTROL,
54 ITE_EC_EXTEMP_CTRL_AUTO_4HZ | ITE_EC_EXTEMP_CTRL_AUTO_START);
55 }
56
57 /*
58 * Set up External Temperature to read via PECI or thermal diode/resistor
59 * into TMPINx register
60 */
61 static void enable_tmpin(const u16 base, const u8 tmpin,
62 const struct ite_ec_thermal_config *const conf)
63 {
64 u8 reg;
65 u8 reg_extra;
66
67 reg = pnp_read_hwm5_index(base, ITE_EC_ADC_TEMP_CHANNEL_ENABLE);
68 reg_extra = pnp_read_hwm5_index(base, ITE_EC_ADC_TEMP_EXTRA_CHANNEL_ENABLE);
69
70 switch (conf->mode) {
71 case THERMAL_MODE_DISABLED:
72 return;
73 case THERMAL_PECI:
74 /* Some chips can set any TMPIN as the target for PECI readings
75 while others can only read to TMPIN3. In the latter case a
76 different register is used for enabling it. */
77 if (CONFIG(SUPERIO_ITE_ENV_CTRL_EXT_ANY_TMPIN)) {
78 /* IT8721F is an exception, it cannot use TMPIN2 for PECI. */
79 if (CONFIG(SUPERIO_ITE_IT8721F) && tmpin == 2) {
80 printk(BIOS_WARNING,
81 "PECI to TMPIN2 not supported on IT8721F\n");
82 return;
83 }
84 u8 reg_new = (reg & ~ITE_EC_ADC_TEMP_EXT_REPORTS_TO_MASK)
85 | ITE_EC_ADC_TEMP_EXT_REPORTS_TO(tmpin);
86 /* Registers stick on reboot and resume,
87 don't warn for correct reg values */
88 if (reg & ITE_EC_ADC_TEMP_EXT_REPORTS_TO_MASK && reg != reg_new) {
89 printk(BIOS_WARNING,
90 "PECI specified for another TMPIN, overwriting\n");
91 }
92 reg = reg_new;
93 } else if (tmpin == 3) {
94 reg_extra |= ITE_EC_ADC_TEMP_EXTRA_TMPIN3_EXT;
95 pnp_write_hwm5_index(base, ITE_EC_ADC_TEMP_EXTRA_CHANNEL_ENABLE,
96 reg_extra);
97 } else {
98 printk(BIOS_WARNING, "PECI to TMPIN%d not supported on this Super I/O",
99 tmpin);
100 return;
101 }
102 enable_peci(base);
103
104 break;
105 case THERMAL_DIODE:
106 reg |= ITE_EC_ADC_TEMP_DIODE_MODE(tmpin);
107 break;
108 case THERMAL_RESISTOR:
109 reg |= ITE_EC_ADC_TEMP_RESISTOR_MODE(tmpin);
110 break;
111 default:
112 printk(BIOS_WARNING, "Unsupported thermal mode 0x%x on TMPIN%d\n",
113 conf->mode, tmpin);
114 return;
115 }
116
117 pnp_write_hwm5_index(base, ITE_EC_ADC_TEMP_CHANNEL_ENABLE, reg);
118
119 /* Set temperature offsets */
120 if (conf->mode != THERMAL_RESISTOR) {
121 reg = pnp_read_hwm5_index(base, ITE_EC_BEEP_ENABLE);
122 reg |= ITE_EC_TEMP_ADJUST_WRITE_ENABLE;
123 pnp_write_hwm5_index(base, ITE_EC_BEEP_ENABLE, reg);
124 pnp_write_hwm5_index(base, ITE_EC_TEMP_ADJUST[tmpin-1], conf->offset);
125 }
126
127 /* Set temperature limits */
128 u8 max = conf->max;
129 pnp_write_hwm5_index(base, ITE_EC_HIGH_TEMP_LIMIT(tmpin), max ? max : 127);
130 pnp_write_hwm5_index(base, ITE_EC_LOW_TEMP_LIMIT(tmpin), conf->min);
131
132 /* Enable the startup of monitoring operation */
133 reg = pnp_read_hwm5_index(base, ITE_EC_CONFIGURATION);
134 reg |= ITE_EC_CONFIGURATION_START;
135 pnp_write_hwm5_index(base, ITE_EC_CONFIGURATION, reg);
136 }
137
138 static void fan_smartconfig(const u16 base, const u8 fan,
139 const enum ite_ec_fan_mode mode,
140 const struct ite_ec_fan_smartconfig *const conf)
141 {
142 u8 pwm_ctrl;
143 u8 pwm_start = 0;
144 u8 pwm_auto = 0;
145 u8 delta_temp;
146
147 if (mode == FAN_SMART_SOFTWARE) {
148 pwm_ctrl = ITE_EC_FAN_CTL_PWM_MODE_SOFTWARE;
149
150 /* 50% duty cycle by default */
151 const u8 duty = conf->pwm_start ? conf->pwm_start : 50;
152 if (CONFIG(SUPERIO_ITE_ENV_CTRL_8BIT_PWM))
153 pwm_start = ITE_EC_FAN_CTL_PWM_DUTY(duty);
154 else
155 pwm_ctrl |= ITE_EC_FAN_CTL_PWM_DUTY(duty);
156 } else {
157 pwm_ctrl = ITE_EC_FAN_CTL_PWM_MODE_AUTOMATIC;
158 pwm_ctrl |= ITE_EC_FAN_CTL_TEMPIN(conf->tmpin);
159
160 if (conf->clsd_loop) {
161 pwm_ctrl |= ITE_EC_FAN_PWM_CLSD_LOOP;
162 pwm_start = ITE_EC_FAN_CTL_PWM_START_RPM(conf->rpm_start);
163 pwm_auto = ITE_EC_FAN_CTL_PWM_SLOPE_LOWER(conf->slope);
164 } else {
165 pwm_start = ITE_EC_FAN_CTL_PWM_START_DUTY(conf->pwm_start);
166
167 if (CONFIG(SUPERIO_ITE_ENV_CTRL_7BIT_SLOPE_REG)) {
168 pwm_auto = conf->slope & 0x7f;
169 } else {
170 pwm_start |= ITE_EC_FAN_CTL_PWM_SLOPE_BIT6(conf->slope);
171 pwm_auto = ITE_EC_FAN_CTL_PWM_SLOPE_LOWER(conf->slope);
172 }
173 }
174
175 if (conf->smoothing)
176 pwm_auto |= ITE_EC_FAN_CTL_AUTO_SMOOTHING_EN;
177
178 pnp_write_hwm5_index(base, ITE_EC_FAN_CTL_TEMP_LIMIT_OFF(fan), conf->tmp_off);
179 pnp_write_hwm5_index(base, ITE_EC_FAN_CTL_TEMP_LIMIT_START(fan),
180 conf->tmp_start);
181 /* Full speed above 127°C by default */
182 pnp_write_hwm5_index(base, ITE_EC_FAN_CTL_TEMP_LIMIT_FULL(fan),
183 conf->tmp_full ? conf->tmp_full : 127);
184
185 delta_temp = ITE_EC_FAN_CTL_DELTA_TEMP_INTRVL(conf->tmp_delta);
186 if (!CONFIG(SUPERIO_ITE_ENV_CTRL_NO_FULLSPEED_SETTING))
187 delta_temp |= ITE_EC_FAN_CTL_FULL_AT_THRML_LMT(conf->full_lmt);
188 pnp_write_hwm5_index(base, ITE_EC_FAN_CTL_DELTA_TEMP(fan),
189 delta_temp);
190 }
191
192 pnp_write_hwm5_index(base, ITE_EC_FAN_CTL_PWM_CONTROL(fan), pwm_ctrl);
193 pnp_write_hwm5_index(base, ITE_EC_FAN_CTL_PWM_START(fan), pwm_start);
194 pnp_write_hwm5_index(base, ITE_EC_FAN_CTL_PWM_AUTO(fan), pwm_auto);
195 }
196
197 static void enable_fan(const u16 base, const u8 fan,
198 const struct ite_ec_fan_config *const conf)
199 {
200 u8 reg;
201
202 if (conf->mode == FAN_IGNORE ||
203 (CONFIG(SUPERIO_ITE_ENV_CTRL_NO_ONOFF) &&
204 conf->mode <= FAN_MODE_OFF))
205 return;
206
207 /* FAN_CTL2 might have its own frequency setting */
208 if (CONFIG(SUPERIO_ITE_ENV_CTRL_PWM_FREQ2) && fan == 2) {
209 reg = pnp_read_hwm5_index(base, ITE_EC_ADC_TEMP_EXTRA_CHANNEL_ENABLE);
210 reg &= ~ITE_EC_FAN_PWM_CLOCK_MASK;
211 reg |= ITE_EC_FAN_PWM_DEFAULT_CLOCK;
212 pnp_write_hwm5_index(base, ITE_EC_ADC_TEMP_EXTRA_CHANNEL_ENABLE, reg);
213 }
214
215 if (conf->mode >= FAN_SMART_SOFTWARE) {
216 fan_smartconfig(base, fan, conf->mode, &conf->smart);
217 } else {
218 reg = pnp_read_hwm5_index(base, ITE_EC_FAN_CTL_MODE);
219 if (conf->mode == FAN_MODE_ON)
220 reg |= ITE_EC_FAN_CTL_ON(fan);
221 else
222 reg &= ~ITE_EC_FAN_CTL_ON(fan);
223 pnp_write_hwm5_index(base, ITE_EC_FAN_CTL_MODE, reg);
224 }
225
226 if (CONFIG(SUPERIO_ITE_ENV_CTRL_FAN16_CONFIG) && conf->mode >= FAN_MODE_ON) {
227 reg = pnp_read_hwm5_index(base, ITE_EC_FAN_TAC_COUNTER_ENABLE);
228 reg |= ITE_EC_FAN_TAC_16BIT_ENABLE(fan);
229 pnp_write_hwm5_index(base, ITE_EC_FAN_TAC_COUNTER_ENABLE, reg);
230 }
231
232 if (CONFIG(SUPERIO_ITE_ENV_CTRL_5FANS) && fan > 3) {
233 reg = pnp_read_hwm5_index(base, ITE_EC_FAN_SEC_CTL);
234 if (conf->mode >= FAN_MODE_ON)
235 reg |= ITE_EC_FAN_SEC_CTL_TAC_EN(fan);
236 else
237 reg &= ~ITE_EC_FAN_SEC_CTL_TAC_EN(fan);
238 pnp_write_hwm5_index(base, ITE_EC_FAN_SEC_CTL, reg);
239 } else {
240 reg = pnp_read_hwm5_index(base, ITE_EC_FAN_MAIN_CTL);
241 if (conf->mode >= FAN_MODE_ON)
242 reg |= ITE_EC_FAN_MAIN_CTL_TAC_EN(fan);
243 else
244 reg &= ~ITE_EC_FAN_MAIN_CTL_TAC_EN(fan);
245
246 /* Some ITEs have SmartGuardian always enabled */
247 if (!CONFIG(SUPERIO_ITE_ENV_CTRL_NO_ONOFF)) {
248 if (conf->mode >= FAN_SMART_SOFTWARE)
249 reg |= ITE_EC_FAN_MAIN_CTL_SMART(fan);
250 else
251 reg &= ~ITE_EC_FAN_MAIN_CTL_SMART(fan);
252 }
253 pnp_write_hwm5_index(base, ITE_EC_FAN_MAIN_CTL, reg);
254 }
255 }
256
257 static void enable_fan_vector(const u16 base, const u8 fan_vector,
258 const struct ite_ec_fan_vector_config *const conf)
259 {
260 u8 reg;
261
262 u8 start = conf->tmp_start;
263 if (!start) {
264 /* When tmp_start is not configured we would set the
265 * register to it's default of 0xFF here, which would
266 * effectively disable the vector functionality of the
267 * SuperIO altogether since that temperature will never
268 * be reached. We can therefore return here and don't
269 * need to set any other registers.
270 */
271 return;
272 }
273 pnp_write_hwm5_index(base, ITE_EC_FAN_VEC_CTL_LIMIT_START(fan_vector), start);
274
275 const s8 slope = conf->slope;
276 const bool slope_neg = slope < 0;
277 if (slope <= -128)
278 reg = 127;
279 else if (slope_neg)
280 reg = -slope;
281 else
282 reg = slope;
283 reg |= ITE_EC_FAN_VEC_CTL_SLOPE_TMPIN0(conf->tmpin);
284 pnp_write_hwm5_index(base, ITE_EC_FAN_VEC_CTL_SLOPE(fan_vector), reg);
285
286 reg = ITE_EC_FAN_VEC_CTL_DELTA_TEMP_INTRVL(conf->tmp_delta);
287 reg |= ITE_EC_FAN_VEC_CTL_DELTA_FANOUT(conf->fanout);
288 reg |= ITE_EC_FAN_VEC_CTL_DELTA_TMPIN1(conf->tmpin);
289 pnp_write_hwm5_index(base, ITE_EC_FAN_VEC_CTL_DELTA(fan_vector), reg);
290
291 if (CONFIG(SUPERIO_ITE_ENV_CTRL_FAN_VECTOR_RANGED)) {
292 reg = conf->tmp_range & 0x7f;
293 reg |= ITE_EC_FAN_VEC_CTL_RANGE_SLOPESIGN(slope_neg);
294 pnp_write_hwm5_index(base, ITE_EC_FAN_VEC_CTL_RANGE(fan_vector), reg);
295 } else if (slope_neg) {
296 printk(BIOS_WARNING, "Unsupported negative slope on fan vector control\n");
297 }
298 }
299
300 static void enable_beeps(const u16 base, const struct ite_ec_config *const conf)
301 {
302 u8 reg = 0;
303 u8 freq = ITE_EC_BEEP_TONE_DIVISOR(10) | ITE_EC_BEEP_FREQ_DIVISOR(10);
304
305 if (conf->tmpin_beep) {
306 reg |= ITE_EC_BEEP_ON_TMP_LIMIT;
307 pnp_write_hwm5_index(base, ITE_EC_BEEP_FREQ_DIV_OF_TMPIN, freq);
308 }
309 if (conf->fan_beep) {
310 reg |= ITE_EC_BEEP_ON_FAN_LIMIT;
311 pnp_write_hwm5_index(base, ITE_EC_BEEP_FREQ_DIV_OF_FAN, freq);
312 }
313 if (conf->vin_beep) {
314 reg |= ITE_EC_BEEP_ON_VIN_LIMIT;
315 pnp_write_hwm5_index(base, ITE_EC_BEEP_FREQ_DIV_OF_VIN, freq);
316 }
317
318 if (reg) {
319 reg |= pnp_read_hwm5_index(base, ITE_EC_BEEP_ENABLE);
320 pnp_write_hwm5_index(base, ITE_EC_BEEP_ENABLE, reg);
321 }
322 }
323
324 void ite_ec_init(const u16 base, const struct ite_ec_config *const conf)
325 {
326 size_t i;
327
328 /* Configure 23.43kHz PWM active high output */
329 u8 fan_ctl = pnp_read_hwm5_index(base, ITE_EC_FAN_CTL_MODE);
330 fan_ctl &= ~ITE_EC_FAN_PWM_CLOCK_MASK;
331 fan_ctl |= ITE_EC_FAN_PWM_DEFAULT_CLOCK;
332 fan_ctl |= ITE_EC_FAN_CTL_POLARITY_HIGH;
333 pnp_write_hwm5_index(base, ITE_EC_FAN_CTL_MODE, fan_ctl);
334
335 /* Enable HWM if configured */
336 for (i = 0; i < ITE_EC_TMPIN_CNT; ++i)
337 enable_tmpin(base, i + 1, &conf->tmpin[i]);
338
339 /* Enable External Sensor SMBus Host if configured */
340 if (conf->smbus_en) {
341 pnp_write_hwm5_index(base, ITE_EC_INTERFACE_SELECT,
342 pnp_read_hwm5_index(base, ITE_EC_INTERFACE_SELECT) |
343 ITE_EC_INTERFACE_SMB_ENABLE);
344 }
345
346 /* Set SST/PECI Host Controller Clock to either 24 MHz or internal 32 MHz */
347 if (conf->smbus_24mhz) {
348 pnp_write_hwm5_index(base, ITE_EC_INTERFACE_SELECT,
349 pnp_read_hwm5_index(base, ITE_EC_INTERFACE_SELECT) |
350 ITE_EC_INTERFACE_CLOCK_24MHZ);
351 }
352
353 /* Enable reading of voltage pins */
354 pnp_write_hwm5_index(base, ITE_EC_ADC_VOLTAGE_CHANNEL_ENABLE, conf->vin_mask);
355
356 /* Enable FANx if configured */
357 for (i = 0; i < ITE_EC_FAN_CNT; ++i)
358 enable_fan(base, i + 1, &conf->fan[i]);
359
360 if (CONFIG(SUPERIO_ITE_ENV_CTRL_FAN_VECTOR)) {
361 /* Enable Special FAN Vector X if configured */
362 for (i = 0; i < ITE_EC_FAN_VECTOR_CNT; ++i)
363 enable_fan_vector(base, i, &conf->fan_vector[i]);
364 }
365
366 /* Enable beeps if configured */
367 enable_beeps(base, conf);
368
369 /*
370 * System may get wrong temperature data when SIO is in
371 * busy state. Therefore, check the status and terminate
372 * processes if needed.
373 */
374 for (i = 0; i < ITE_EC_TMPIN_CNT; ++i)
375 if (conf->tmpin[i].mode == THERMAL_PECI)
376 extemp_force_idle_status(base);
377 }
378
379 void ite_ec_set_power_state(struct device *dev)
380 {
381 uint8_t power_status;
382 uint8_t reg_pcr1, reg_pcr2;
383
384 /* Set power state after power fail */
385 power_status = get_uint_option("power_on_after_fail",
386 CONFIG_MAINBOARD_POWER_FAILURE_STATE);
387 pnp_enter_conf_mode(dev);
388 pnp_set_logical_device(dev);
389 reg_pcr1 = pnp_read_config(dev, ITE_EC_REG_PCR1);
390 reg_pcr2 = pnp_read_config(dev, ITE_EC_REG_PCR2);
391 if (power_status == MAINBOARD_POWER_ON) {
392 reg_pcr2 |= (1 << 5);
393 } else if (power_status == MAINBOARD_POWER_KEEP) {
394 reg_pcr2 &= ~(1 << 5);
395 reg_pcr1 |= (1 << 5);
396 } else {
397 reg_pcr2 &= ~(1 << 5);
398 reg_pcr1 &= ~(1 << 5);
399 }
400 pnp_write_config(dev, ITE_EC_REG_PCR1, reg_pcr1);
401 pnp_write_config(dev, ITE_EC_REG_PCR2, reg_pcr2);
402 pnp_exit_conf_mode(dev);
403 printk(BIOS_INFO, "set power %u after power fail\n", power_status);
404 }