aboutsummaryrefslogtreecommitdiff
path: root/src/mainboard/dell/optiplex_9020/mainboard.c
blob: 6630a12e2abc7c8092281b6559561fe27b5f0038 (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* SPDX-License-Identifier: GPL-2.0-only */

#include <bootstate.h>
#include <cpu/x86/msr.h>
#include <device/device.h>
#include <drivers/intel/gma/int15.h>
#include <option.h>
#include <southbridge/intel/common/gpio.h>
#include "sch5555_ec.h"

static void mainboard_enable(struct device *dev)
{
	install_intel_vga_int15_handler(GMA_INT15_ACTIVE_LFP_NONE,
					GMA_INT15_PANEL_FIT_DEFAULT,
					GMA_INT15_BOOT_DISPLAY_DEFAULT, 0);
}

struct chip_operations mainboard_ops = {
	.enable_dev = mainboard_enable,
};

#define HWM_TAB_ADD_TEMP_TARGET	1
#define HWM_TAB_PKG_POWER_ANY	0xffff
#define CHASSIS_TYPE_UNKNOWN	0xff

struct hwm_tab_entry {
	uint16_t addr;
	uint8_t val;
	uint8_t flags;
	uint16_t pkg_power;
};

struct hwm_tab_entry HWM_TAB3[] = {
	{ 0x005, 0x33, 0, 0xffff },
	{ 0x018, 0x2f, 0, 0xffff },
	{ 0x019, 0x2f, 0, 0xffff },
	{ 0x01a, 0x2f, 0, 0xffff },
	{ 0x080, 0x00, 0, 0xffff },
	{ 0x081, 0x00, 0, 0xffff },
	{ 0x083, 0xbb, 0, 0xffff },
	{ 0x085, 0x8a, 0, 0x0010 },
	{ 0x086, 0x4c, 0, 0x0010 },
	{ 0x08a, 0x66, 0, 0x0010 },
	{ 0x08b, 0x5b, 0, 0x0010 },
	{ 0x090, 0x65, 0, 0xffff },
	{ 0x091, 0x70, 0, 0xffff },
	{ 0x092, 0x86, 0, 0xffff },
	{ 0x096, 0xa4, 0, 0xffff },
	{ 0x097, 0xa4, 0, 0xffff },
	{ 0x098, 0xa4, 0, 0xffff },
	{ 0x09b, 0xa4, 0, 0xffff },
	{ 0x0a0, 0x0e, 0, 0xffff },
	{ 0x0a1, 0x0e, 0, 0xffff },
	{ 0x0ae, 0x7c, 0, 0xffff },
	{ 0x0af, 0x86, 0, 0xffff },
	{ 0x0b0, 0x9a, 0, 0xffff },
	{ 0x0b3, 0x9a, 0, 0xffff },
	{ 0x0b6, 0x08, 0, 0xffff },
	{ 0x0b7, 0x08, 0, 0xffff },
	{ 0x0ea, 0x64, 0, 0x0020 },
	{ 0x0ea, 0x5c, 0, 0x0010 },
	{ 0x0ef, 0xff, 0, 0xffff },
	{ 0x0f8, 0x15, 0, 0xffff },
	{ 0x0f9, 0x00, 0, 0xffff },
	{ 0x0f0, 0x30, 0, 0xffff },
	{ 0x0fd, 0x01, 0, 0xffff },
	{ 0x1a1, 0x00, 0, 0xffff },
	{ 0x1a2, 0x00, 0, 0xffff },
	{ 0x1b1, 0x08, 0, 0xffff },
	{ 0x1be, 0x99, 0, 0xffff },
	{ 0x280, 0xa0, 0, 0x0010 },
	{ 0x281, 0x0f, 0, 0x0010 },
	{ 0x282, 0x03, 0, 0xffff },
	{ 0x283, 0x0a, 0, 0xffff },
	{ 0x284, 0x80, 0, 0xffff },
	{ 0x285, 0x03, 0, 0xffff },
	{ 0x288, 0x68, 0, 0x0010 },
	{ 0x289, 0x10, 0, 0x0010 },
	{ 0x28a, 0x03, 0, 0xffff },
	{ 0x28b, 0x0a, 0, 0xffff },
	{ 0x28c, 0x80, 0, 0xffff },
	{ 0x28d, 0x03, 0, 0xffff },
};

struct hwm_tab_entry HWM_TAB4[] = {
	{ 0x005, 0x33, 0, 0xffff },
	{ 0x018, 0x2f, 0, 0xffff },
	{ 0x019, 0x2f, 0, 0xffff },
	{ 0x01a, 0x2f, 0, 0xffff },
	{ 0x080, 0x00, 0, 0xffff },
	{ 0x081, 0x00, 0, 0xffff },
	{ 0x083, 0xbb, 0, 0xffff },
	{ 0x085, 0x99, 0, 0x0020 },
	{ 0x085, 0xad, 0, 0x0010 },
	{ 0x086, 0x1c, 0, 0xffff },
	{ 0x08a, 0x39, 0, 0x0020 },
	{ 0x08a, 0x41, 0, 0x0010 },
	{ 0x08b, 0x76, 0, 0x0020 },
	{ 0x08b, 0x8b, 0, 0x0010 },
	{ 0x090, 0x5e, 0, 0xffff },
	{ 0x091, 0x5e, 0, 0xffff },
	{ 0x092, 0x86, 0, 0xffff },
	{ 0x096, 0xa4, 0, 0xffff },
	{ 0x097, 0xa4, 0, 0xffff },
	{ 0x098, 0xa4, 0, 0xffff },
	{ 0x09b, 0xa4, 0, 0xffff },
	{ 0x0a0, 0x0a, 0, 0xffff },
	{ 0x0a1, 0x0a, 0, 0xffff },
	{ 0x0ae, 0x7c, 0, 0xffff },
	{ 0x0af, 0x7c, 0, 0xffff },
	{ 0x0b0, 0x9a, 0, 0xffff },
	{ 0x0b3, 0x7c, 0, 0xffff },
	{ 0x0b6, 0x08, 0, 0xffff },
	{ 0x0b7, 0x08, 0, 0xffff },
	{ 0x0ea, 0x64, 0, 0x0020 },
	{ 0x0ea, 0x5c, 0, 0x0010 },
	{ 0x0ef, 0xff, 0, 0xffff },
	{ 0x0f8, 0x15, 0, 0xffff },
	{ 0x0f9, 0x00, 0, 0xffff },
	{ 0x0f0, 0x30, 0, 0xffff },
	{ 0x0fd, 0x01, 0, 0xffff },
	{ 0x1a1, 0x00, 0, 0xffff },
	{ 0x1a2, 0x00, 0, 0xffff },
	{ 0x1b1, 0x08, 0, 0xffff },
	{ 0x1be, 0x90, 0, 0xffff },
	{ 0x280, 0x94, 0, 0x0020 },
	{ 0x281, 0x11, 0, 0x0020 },
	{ 0x280, 0x94, 0, 0x0010 },
	{ 0x281, 0x11, 0, 0x0010 },
	{ 0x282, 0x03, 0, 0xffff },
	{ 0x283, 0x0a, 0, 0xffff },
	{ 0x284, 0x80, 0, 0xffff },
	{ 0x285, 0x03, 0, 0xffff },
	{ 0x288, 0x28, 0, 0x0020 },
	{ 0x289, 0x0a, 0, 0x0020 },
	{ 0x288, 0x28, 0, 0x0010 },
	{ 0x289, 0x0a, 0, 0x0010 },
	{ 0x28a, 0x03, 0, 0xffff },
	{ 0x28b, 0x0a, 0, 0xffff },
	{ 0x28c, 0x80, 0, 0xffff },
	{ 0x28d, 0x03, 0, 0xffff },
};

struct hwm_tab_entry HWM_TAB5[] = {
	{ 0x005, 0x33, 0, 0xffff },
	{ 0x018, 0x2f, 0, 0xffff },
	{ 0x019, 0x2f, 0, 0xffff },
	{ 0x01a, 0x2f, 0, 0xffff },
	{ 0x080, 0x00, 0, 0xffff },
	{ 0x081, 0x00, 0, 0xffff },
	{ 0x083, 0xbb, 0, 0xffff },
	{ 0x085, 0x66, 0, 0x0020 },
	{ 0x085, 0x5d, 0, 0x0010 },
	{ 0x086, 0x1c, 0, 0xffff },
	{ 0x08a, 0x39, 0, 0x0020 },
	{ 0x08a, 0x41, 0, 0x0010 },
	{ 0x08b, 0x76, 0, 0x0020 },
	{ 0x08b, 0x80, 0, 0x0010 },
	{ 0x090, 0x5d, 0, 0x0020 },
	{ 0x090, 0x5e, 0, 0x0010 },
	{ 0x091, 0x5e, 0, 0xffff },
	{ 0x092, 0x86, 0, 0xffff },
	{ 0x096, 0xa4, 0, 0xffff },
	{ 0x097, 0xa4, 0, 0xffff },
	{ 0x098, 0xa3, 0, 0x0020 },
	{ 0x098, 0xa4, 0, 0x0010 },
	{ 0x09b, 0xa4, 0, 0xffff },
	{ 0x0a0, 0x08, 0, 0xffff },
	{ 0x0a1, 0x0a, 0, 0xffff },
	{ 0x0ae, 0x7c, 0, 0xffff },
	{ 0x0af, 0x7c, 0, 0xffff },
	{ 0x0b0, 0x9a, 0, 0xffff },
	{ 0x0b3, 0x7c, 0, 0xffff },
	{ 0x0b6, 0x08, 0, 0xffff },
	{ 0x0b7, 0x08, 0, 0xffff },
	{ 0x0ea, 0x64, 0, 0x0020 },
	{ 0x0ea, 0x5c, 0, 0x0010 },
	{ 0x0ef, 0xff, 0, 0xffff },
	{ 0x0f8, 0x15, 0, 0xffff },
	{ 0x0f9, 0x00, 0, 0xffff },
	{ 0x0f0, 0x30, 0, 0xffff },
	{ 0x0fd, 0x01, 0, 0xffff },
	{ 0x1a1, 0x00, 0, 0xffff },
	{ 0x1a2, 0x00, 0, 0xffff },
	{ 0x1b1, 0x08, 0, 0xffff },
	{ 0x1be, 0x98, 0, 0x0020 },
	{ 0x1be, 0x90, 0, 0x0010 },
	{ 0x280, 0x94, 0, 0x0020 },
	{ 0x281, 0x11, 0, 0x0020 },
	{ 0x280, 0x94, 0, 0x0010 },
	{ 0x281, 0x11, 0, 0x0010 },
	{ 0x282, 0x03, 0, 0xffff },
	{ 0x283, 0x0a, 0, 0xffff },
	{ 0x284, 0x80, 0, 0xffff },
	{ 0x285, 0x03, 0, 0xffff },
	{ 0x288, 0x28, 0, 0x0020 },
	{ 0x289, 0x0a, 0, 0x0020 },
	{ 0x288, 0x28, 0, 0x0010 },
	{ 0x289, 0x0a, 0, 0x0010 },
	{ 0x28a, 0x03, 0, 0xffff },
	{ 0x28b, 0x0a, 0, 0xffff },
	{ 0x28c, 0x80, 0, 0xffff },
	{ 0x28d, 0x03, 0, 0xffff },
};

struct hwm_tab_entry HWM_TAB6[] = {
	{ 0x005, 0x33, 0, 0xffff },
	{ 0x018, 0x2f, 0, 0xffff },
	{ 0x019, 0x2f, 0, 0xffff },
	{ 0x01a, 0x2f, 0, 0xffff },
	{ 0x080, 0x00, 0, 0xffff },
	{ 0x081, 0x00, 0, 0xffff },
	{ 0x083, 0xbb, 0, 0xffff },
	{ 0x085, 0x98, 0, 0xffff },
	{ 0x086, 0x3c, 0, 0xffff },
	{ 0x08a, 0x39, 0, 0x0020 },
	{ 0x08a, 0x3d, 0, 0x0010 },
	{ 0x08b, 0x44, 0, 0x0020 },
	{ 0x08b, 0x51, 0, 0x0010 },
	{ 0x090, 0x61, 0, 0xffff },
	{ 0x091, 0x6d, 0, 0xffff },
	{ 0x092, 0x86, 0, 0xffff },
	{ 0x096, 0xa4, 0, 0xffff },
	{ 0x097, 0xa4, 0, 0xffff },
	{ 0x098, 0x9f, 0, 0x0020 },
	{ 0x098, 0xa4, 0, 0x0010 },
	{ 0x09b, 0xa4, 0, 0xffff },
	{ 0x0a0, 0x0e, 0, 0xffff },
	{ 0x0a1, 0x0e, 0, 0xffff },
	{ 0x0ae, 0x7c, 0, 0xffff },
	{ 0x0af, 0x7c, 0, 0xffff },
	{ 0x0b0, 0x9b, 0, 0x0020 },
	{ 0x0b0, 0x98, 0, 0x0010 },
	{ 0x0b3, 0x9a, 0, 0xffff },
	{ 0x0b6, 0x08, 0, 0xffff },
	{ 0x0b7, 0x08, 0, 0xffff },
	{ 0x0ea, 0x64, 0, 0x0020 },
	{ 0x0ea, 0x5c, 0, 0x0010 },
	{ 0x0ef, 0xff, 0, 0xffff },
	{ 0x0f8, 0x15, 0, 0xffff },
	{ 0x0f9, 0x00, 0, 0xffff },
	{ 0x0f0, 0x30, 0, 0xffff },
	{ 0x0fd, 0x01, 0, 0xffff },
	{ 0x1a1, 0x00, 0, 0xffff },
	{ 0x1a2, 0x00, 0, 0xffff },
	{ 0x1b1, 0x08, 0, 0xffff },
	{ 0x1be, 0x9a, 0, 0x0020 },
	{ 0x1be, 0x96, 0, 0x0010 },
	{ 0x280, 0x94, 0, 0x0020 },
	{ 0x281, 0x11, 0, 0x0020 },
	{ 0x280, 0x94, 0, 0x0010 },
	{ 0x281, 0x11, 0, 0x0010 },
	{ 0x282, 0x03, 0, 0xffff },
	{ 0x283, 0x0a, 0, 0xffff },
	{ 0x284, 0x80, 0, 0xffff },
	{ 0x285, 0x03, 0, 0xffff },
	{ 0x288, 0x94, 0, 0x0020 },
	{ 0x289, 0x11, 0, 0x0020 },
	{ 0x288, 0x94, 0, 0x0010 },
	{ 0x289, 0x11, 0, 0x0010 },
	{ 0x28a, 0x03, 0, 0xffff },
	{ 0x28b, 0x0a, 0, 0xffff },
	{ 0x28c, 0x80, 0, 0xffff },
	{ 0x28d, 0x03, 0, 0xffff },
};

static uint8_t get_chassis_type(void)
{
	uint8_t gpio_chassis_type;

	// Read chassis type from GPIO
	gpio_chassis_type = get_gpio(70) << 3 | get_gpio(38) << 2 |
			    get_gpio(17) << 1 | get_gpio(1);

	printk(BIOS_DEBUG, "GPIO chassis type = %#x\n", gpio_chassis_type);

	// Turn it into internal chassis index
	switch (gpio_chassis_type) {
	case 0x08:
	case 0x0a:
		return 4;
	case 0x0b:
		return 3;
	case 0x0c:
		return 5;
	case 0x0d: // SFF
	case 0x0e:
	case 0x0f:
		return 6;
	default:
		return CHASSIS_TYPE_UNKNOWN;
	}

}

static uint8_t get_temp_target(void)
{
	uint8_t val = rdmsr(0x1a2).lo >> 8 & 0xff;
	if (!val)
		val = 20;
	return 0x95 - val;
}

static uint16_t get_pkg_power(void)
{
	uint8_t rapl_power_unit = rdmsr(0x606).lo & 0xf;
	if (rapl_power_unit)
		rapl_power_unit = 2 << (rapl_power_unit - 1);
	uint16_t pkg_power_info = rdmsr(0x614).lo & 0x7fff;
	if (pkg_power_info / rapl_power_unit > 0x41)
		return 32;
	else
		return 16;
}

static void apply_hwm_tab(struct hwm_tab_entry *arr, size_t size)
{
	uint8_t temp_target = get_temp_target();
	uint16_t pkg_power = get_pkg_power();

	printk(BIOS_DEBUG, "Temp target = %#x\n", temp_target);
	printk(BIOS_DEBUG, "Package power = %#x\n", pkg_power);

	for (size_t i = 0; i < size; ++i) {
		// Skip entry if it doesn't apply for this package power
		if (arr[i].pkg_power != pkg_power &&
		    arr[i].pkg_power != HWM_TAB_PKG_POWER_ANY)
			continue;

		uint8_t val = arr[i].val;

		// Add temp target to value if requested (current tables never do)
		if (arr[i].flags & HWM_TAB_ADD_TEMP_TARGET)
			val += temp_target;

		// Perform write
		sch5555_mbox_write(1, arr[i].addr, val);

	}
}

static void sch5555_ec_hwm_init(void *arg)
{
	uint8_t chassis_type, saved_2fc;

	printk(BIOS_DEBUG, "OptiPlex 9020 late HWM init\n");

	saved_2fc = sch5555_mbox_read(1, 0x2fc);
	sch5555_mbox_write(1, 0x2fc, 0xa0);
	sch5555_mbox_write(1, 0x2fd, 0x32);

	chassis_type = get_chassis_type();

	if (chassis_type != CHASSIS_TYPE_UNKNOWN) {
		printk(BIOS_DEBUG, "Chassis type = %#x\n", chassis_type);
	} else {
		printk(BIOS_DEBUG, "WARNING: Unknown chassis type\n");
	}

	// Apply HWM table based on chassis type
	switch (chassis_type) {
	case 3:
		apply_hwm_tab(HWM_TAB3, ARRAY_SIZE(HWM_TAB3));
		break;
	case 4:
		apply_hwm_tab(HWM_TAB4, ARRAY_SIZE(HWM_TAB4));
		break;
	case 5:
		apply_hwm_tab(HWM_TAB5, ARRAY_SIZE(HWM_TAB5));
		break;
	case 6:
		apply_hwm_tab(HWM_TAB6, ARRAY_SIZE(HWM_TAB6));
		break;
	}

	// NOTE: vendor firmware applies these when "max core address" > 2
	// i think this is always the case
	sch5555_mbox_write(1, 0x9e, 0x30);
	sch5555_mbox_write(1, 0xeb, sch5555_mbox_read(1, 0xea));

	sch5555_mbox_write(1, 0x2fc, saved_2fc);

	// Apply full speed fan config if requested or if the chassis type is unknown
	if (chassis_type == CHASSIS_TYPE_UNKNOWN || get_uint_option("fan_full_speed", 0)) {
		printk(BIOS_DEBUG, "Setting full fan speed\n");
		sch5555_mbox_write(1, 0x80, 0x60 | sch5555_mbox_read(1, 0x80));
		sch5555_mbox_write(1, 0x81, 0x60 | sch5555_mbox_read(1, 0x81));
	}

	sch5555_mbox_read(1, 0xb8);

	if ((chassis_type == 4 || chassis_type == 5) && sch5555_mbox_read(1, 0x26) == 0) {
		sch5555_mbox_write(1, 0xa0, sch5555_mbox_read(1, 0xa0) & 0xfb);
		sch5555_mbox_write(1, 0xa1, sch5555_mbox_read(1, 0xa1) & 0xfb);
		sch5555_mbox_write(1, 0xa2, sch5555_mbox_read(1, 0xa2) & 0xfb);
		sch5555_mbox_write(1, 0x8a, 0x99);
		sch5555_mbox_write(1, 0x8b, 0x47);
		sch5555_mbox_write(1, 0x8c, 0x91);
	}
}

BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, sch5555_ec_hwm_init, NULL);