summaryrefslogtreecommitdiff
path: root/src/superio/fintek/f81803a/fan_control.c
blob: 17ae9c6a20d6fb052f55d3c294505128e78f0d39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2019 Richard Spiegel <richard.spiegel@silverbackltd.com>
 * Copyright (C) 2019 Silverback ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <console/console.h>
#include "../common/fan_control.h"
#include "f81803a_hwm.h"

static const char msg_err_invalid[] = "Error: invalid";
static const char msg_err_wrong_order[] = "Error: wrong order,";
static const char msg_err_fan[] = "fan";
static const char msg_err_temp_source[] = "temperature source";
static const char msg_err_type[] = "type";
static const char msg_err_mode[] = "mode";
static const char msg_err_rate[] = "change rate";
static const char msg_err_frequency[] = "frequency";
static const char msg_err_temp_sensor[] = "temperature sensor";
static const char msg_err_bondary[] = "boundary";
static const char msg_err_section[] = "section";
static const char no_msg[] = "";

struct cross_ref {
	int selection;
	const char *message;
};
static struct cross_ref msg_table[] = {
	{HWM_STATUS_INVALID_FAN,		msg_err_fan},
	{HWM_STATUS_INVALID_TEMP_SOURCE,	msg_err_temp_source},
	{HWM_STATUS_INVALID_TYPE,		msg_err_type},
	{HWM_STATUS_INVALID_MODE,		msg_err_mode},
	{HWM_STATUS_INVALID_RATE,		msg_err_rate},
	{HWM_STATUS_INVALID_FREQUENCY,		msg_err_frequency},
	{HWM_STATUS_INVALID_TEMP_SENSOR,	msg_err_temp_sensor},
	{0, NULL},
};

static const char *get_msg(int err)
{
	int i = 0;
	while (msg_table[i].selection) {
		if (msg_table[i].selection == err)
			return msg_table[i].message;
		i++;
	}
	return no_msg;
}

static int message_invalid_1(int err, u8 fan)
{
	if (err == HWM_STATUS_INVALID_FAN)
		printk(BIOS_ERR, "%s %s %d!\n", msg_err_invalid, get_msg(err), fan);
	else
		printk(BIOS_ERR, "%s Fan %d %s!\n", msg_err_invalid, fan, get_msg(err));
	return err;
}

static int message_invalid_2(int err, u8 fan)
{
	switch (err) {
	case HWM_STATUS_INVALID_BOUNDARY_VALUE:
		printk(BIOS_ERR, "%s fan %d %s value!\n", msg_err_invalid, fan,
					msg_err_bondary);
		break;
	case HWM_STATUS_INVALID_SECTION_VALUE:
		printk(BIOS_ERR, "%s fan %d %s value!\n", msg_err_invalid, fan,
					msg_err_section);
		break;
	case HWM_STATUS_BOUNDARY_WRONG_ORDER:
		printk(BIOS_ERR, "%s fan %d %s!\n", msg_err_wrong_order, fan, msg_err_bondary);
		break;
	case HWM_STATUS_SECTIONS_WRONG_ORDER:
		printk(BIOS_ERR, "%s fan %d %s!\n", msg_err_wrong_order, fan, msg_err_section);
		break;
	default:
		break;
	}
	return err;
}

static void write_hwm_reg(u16 address, u8 index, u8 value)
{
	u16 index_add, data_add;
	index_add = address | 0x0001;	/* force odd address */
	data_add = index_add + 1;
	outb(index, index_add);
	outb(value, data_add);
}

static u8 read_hwm_reg(u16 address, u8 index)
{
	u16 index_add, data_add;
	index_add = address | 0x0001;	/* force odd address */
	data_add = index_add + 1;
	outb(index, index_add);
	return inb(data_add);
}

static void hwm_reg_modify(u16 address, u8 index, u8 shift, u8 mask,
								u8 value)
{
	u8 use_mask = mask << shift;
	u8 use_value = (value & mask) << shift;
	u8 temp = read_hwm_reg(address, index);

	temp &= ~use_mask;
	temp |= use_value;
	write_hwm_reg(address, index, temp);
}

/*
 * Registers 0x94,0x95, 0x96 and 0x9b have 2 versions (banks) selected through
 * bit 7 of register 0x9f.
 */
static inline void select_hwm_bank(u16 address, u8 value)
{
	hwm_reg_modify(address, FAN_FAULT_TIME_REG, FAN_FUNC_PROG_SEL_SHIFT,
				FAN_BIT_MASK, value);
}

/*
 * Boundaries and sections must be presented in the same order as in the HWM
 * registers, that is, from highest value to lowest. This procedure checks for
 * the correct order.
 */
static int check_value_seq(u8 *values, u8 count)
{
	u8 last_value = CPU_DAMAGE_TEMP;
	u8 current_value, i;
	for (i = 0; i < count; i++) {
		current_value = values[i];
		if (current_value  > CPU_DAMAGE_TEMP)
			return STATUS_INVALID_VALUE;
		if (current_value >= last_value)
			return STATUS_INVALID_ORDER;
		last_value = current_value;
	}
	return HWM_STATUS_SUCCESS;
}

int set_sensor_type(u16 base_address, external_sensor sensor,
						temp_sensor_type type)
{
	u8 sensor_status = read_hwm_reg(base_address, TP_DIODE_STATUS);

	printk(BIOS_DEBUG, "%s\n", __func__);
	switch (sensor) {
	case EXTERNAL_SENSOR1:
		if (sensor_status & TP_EXTERNAL_SENSOR1_OPEN) {
			printk(BIOS_WARNING, "Sensor 1 disconected!\n");
			return HWM_STATUS_WARNING_SENSOR_DISCONECTED;
		}
		hwm_reg_modify(base_address, TP_SENSOR_TYPE,
			TP_SENSOR1_TYPE_SHIFT, TP_SENSOR_TYPE_MASK, type);
		break;
	case EXTERNAL_SENSOR2:
		if (sensor_status & TP_EXTERNAL_SENSOR2_OPEN) {
			printk(BIOS_WARNING, "Sensor 2 disconected!\n");
			return HWM_STATUS_WARNING_SENSOR_DISCONECTED;
		}
		hwm_reg_modify(base_address, TP_SENSOR_TYPE,
			TP_SENSOR2_TYPE_SHIFT, TP_SENSOR_TYPE_MASK, type);
		break;
	case IGNORE_SENSOR:
		break;
	default:
		return message_invalid_1(HWM_STATUS_INVALID_TEMP_SENSOR, 0);
	}
	return HWM_STATUS_SUCCESS;
}

int set_fan_temperature_source(u16 base_address, u8 fan,
						fan_temp_source source)
{
	u8 index, high_value, low_value;

	printk(BIOS_DEBUG, "%s\n", __func__);
	if ((fan < FIRST_FAN) || (fan > LAST_FAN))
		return message_invalid_1(HWM_STATUS_INVALID_FAN, fan);
	index = FAN_ADJUST(fan, FAN_TMP_MAPPING);
	high_value = (source >> 2) & FAN_BIT_MASK;
	low_value = source & FAN_TEMP_SEL_LOW_MASK;
	hwm_reg_modify(base_address, index, FAN_TEMP_SEL_HIGH_SHIFT,
				FAN_BIT_MASK, high_value);
	hwm_reg_modify(base_address, index, FAN_TEMP_SEL_LOW_SHIFT,
				FAN_TEMP_SEL_LOW_MASK, low_value);
	/*
	 * Fan 1 has a weight mechanism for adjusting for next fan speed. Basically the idea is
	 * to react more aggressively (normally CPU fan) based on how high another temperature
	 * (system, thermistor near the CPU, anything) is. This would be highly platform
	 * dependent, and by setting the weight temperature same as the control temperature.
	 * This code cancels the weight mechanism and make it work with any board. If a board
	 * wants to use the weight mechanism, OEM should implement it after calling the main
	 * HWM programming.
	 */
	if (fan == FIRST_FAN) {
		select_hwm_bank(base_address, 1);
		hwm_reg_modify(base_address, FAN_MODE_REG,
				FAN1_ADJ_SEL_SHIFT, FAN1_ADJ_SEL_MASK, source);
		select_hwm_bank(base_address, 0);
	}
	return HWM_STATUS_SUCCESS;
}

int set_fan_type_mode(u16 base_address, u8 fan, fan_type type, fan_mode mode)
{
	u8 shift;

	printk(BIOS_DEBUG, "%s\n", __func__);
	if ((fan < FIRST_FAN) || (fan > LAST_FAN))
		return message_invalid_1(HWM_STATUS_INVALID_FAN, fan);
	select_hwm_bank(base_address, 0);
	if (type < FAN_TYPE_RESERVED) {
		shift = FAN_TYPE_SHIFT(fan);
		hwm_reg_modify(base_address, FAN_TYPE_REG, shift,
						FAN_TYPE_MASK, type);
	}
	if (mode < FAN_MODE_DEFAULT) {
		shift = FAN_MODE_SHIFT(fan);
		hwm_reg_modify(base_address, FAN_MODE_REG, shift,
						FAN_MODE_MASK, mode);
	}
	return HWM_STATUS_SUCCESS;
}

int set_pwm_frequency(u16 base_address, u8 fan, fan_pwm_freq frequency)
{
	u8 shift, index, byte;

	printk(BIOS_DEBUG, "%s\n", __func__);
	if ((fan < FIRST_FAN) || (fan > LAST_FAN))
		return message_invalid_1(HWM_STATUS_INVALID_FAN, fan);
	byte = read_hwm_reg(base_address, FAN_TYPE_REG);
	shift = FAN_TYPE_SHIFT(fan);
	if (((byte >> shift) & FAN_TYPE_PWM_CHECK) == FAN_TYPE_PWM_CHECK) {
		printk(BIOS_WARNING, "Fan %d not programmed as PWM!\n", fan);
		return HWM_STATUS_WARNING_FAN_NOT_PWM;
	}
	select_hwm_bank(base_address, 1);
	shift = FAN_FREQ_SEL_ADD_SHIFT(fan);
	byte = (frequency >> 1) & FAN_BIT_MASK;
	hwm_reg_modify(base_address, FAN_MODE_REG, shift, FAN_BIT_MASK,
									byte);
	select_hwm_bank(base_address, 0);
	index = FAN_ADJUST(fan, FAN_TMP_MAPPING);
	byte = frequency & FAN_BIT_MASK;
	hwm_reg_modify(base_address, index, FAN_PWM_FREQ_SEL_SHIFT,
							FAN_BIT_MASK, byte);
	return HWM_STATUS_SUCCESS;
}

int set_sections(u16 base_address, u8 fan, u8 *boundaries, u8 *sections)
{
	int status, temp;
	u8 i, index, value;

	printk(BIOS_DEBUG, "%s\n", __func__);
	if ((fan < FIRST_FAN) || (fan > LAST_FAN))
		return message_invalid_1(HWM_STATUS_INVALID_FAN, fan);
	status = check_value_seq(boundaries,
				FINTEK_BOUNDARIES_SIZE);
	if (status != HWM_STATUS_SUCCESS) {
		if (status == STATUS_INVALID_VALUE)
			return message_invalid_2(HWM_STATUS_INVALID_BOUNDARY_VALUE, fan);
		return message_invalid_2(HWM_STATUS_BOUNDARY_WRONG_ORDER, fan);
	}
	status = check_value_seq(sections,
				FINTEK_SECTIONS_SIZE);
	if (status != HWM_STATUS_SUCCESS) {
		if (status == STATUS_INVALID_VALUE)
			return message_invalid_2(HWM_STATUS_INVALID_SECTION_VALUE, fan);
		return message_invalid_2(HWM_STATUS_SECTIONS_WRONG_ORDER, fan);
	}
	index = FAN_ADJUST(fan, FAN_BOUND_TEMP);
	for (i = 0; i < FINTEK_BOUNDARIES_SIZE; i++) {
		value = boundaries[i];
		write_hwm_reg(base_address, index, value);
		index++;
	}
	index = FAN_ADJUST(fan, FAN_SECTION_SPEED);
	for (i = 0; i < FINTEK_SECTIONS_SIZE; i++) {
		value = sections[i];
		if (value > 100)
			return message_invalid_2(HWM_STATUS_INVALID_SECTION_VALUE, fan);
		temp = (255 * value) / 100;
		value = (u8) (temp & 0x00ff);
		write_hwm_reg(base_address, index, value);
		index++;
	}
	return HWM_STATUS_SUCCESS;
}

int set_fan_speed_change_rate(u16 base_address, u8 fan, fan_rate_up rate_up,
						fan_rate_down rate_down)
{
	u8 shift, index;

	printk(BIOS_DEBUG, "%s\n", __func__);
	if ((fan < FIRST_FAN) || (fan > LAST_FAN))
		return message_invalid_1(HWM_STATUS_INVALID_FAN, fan);

	index = FAN_ADJUST(fan, FAN_TMP_MAPPING);
	shift = FAN_RATE_SHIFT(fan);

	if (rate_up == FAN_UP_RATE_JUMP) {
		hwm_reg_modify(base_address, index, FAN_JUMP_UP_SHIFT,
							FAN_BIT_MASK, 1);
	} else {
		hwm_reg_modify(base_address, index, FAN_JUMP_UP_SHIFT,
							FAN_BIT_MASK, 0);
		if (rate_up < FAN_UP_RATE_DEFAULT) {
			hwm_reg_modify(base_address,	FAN_UP_RATE_REG,
					shift, FAN_RATE_MASK, rate_up);
		}
	}

	if (rate_down == FAN_DOWN_RATE_JUMP) {
		hwm_reg_modify(base_address, index, FAN_JUMP_DOWN_SHIFT,
							FAN_BIT_MASK, 1);
	} else {
		hwm_reg_modify(base_address, index, FAN_JUMP_UP_SHIFT,
							FAN_BIT_MASK, 0);
		select_hwm_bank(base_address, 0);
		if (rate_down < FAN_DOWN_RATE_DEFAULT) {
			hwm_reg_modify(base_address,	FAN_DOWN_RATE_REG,
					shift, FAN_RATE_MASK, rate_down);
			hwm_reg_modify(base_address, FAN_DOWN_RATE_REG,
					FAN_DOWN_RATE_DIFF_FROM_UP_SHIFT,
					FAN_BIT_MASK, 0);
		}
		if (rate_down == FAN_DOWN_RATE_SAME_AS_UP) {
			hwm_reg_modify(base_address, FAN_DOWN_RATE_REG,
					FAN_DOWN_RATE_DIFF_FROM_UP_SHIFT,
					FAN_BIT_MASK, 1);
		}
	}
	return HWM_STATUS_SUCCESS;
}

int set_fan_follow(u16 base_address, u8 fan, fan_follow follow)
{
	u8 index;

	printk(BIOS_DEBUG, "%s\n", __func__);
	if ((fan < FIRST_FAN) || (fan > LAST_FAN))
		return message_invalid_1(HWM_STATUS_INVALID_FAN, fan);
	index = FAN_ADJUST(fan, FAN_TMP_MAPPING);
	hwm_reg_modify(base_address, index, FAN_INTERPOLATION_SHIFT,
				FAN_BIT_MASK, follow);
	return HWM_STATUS_SUCCESS;
}