Debian Codegraph @

src/mainboard/dell/snb_ivb_workstations/sch5545_ec.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <cbfs.h>
4 #include <cf9_reset.h>
5 #include <option.h>
6 #include <cpu/x86/msr.h>
7 #include <console/console.h>
8 #include <cpu/intel/model_206ax/model_206ax.h>
9 #include <gpio.h>
10 #include <superio/smsc/sch5545/sch5545.h>
11 #include <superio/smsc/sch5545/sch5545_emi.h>
12
13 #include <baseboard/sch5545_ec.h>
14 #include <variant/sch5545_ec_tables.h>
15
16 #define GPIO_CHASSIS_ID0 1
17 #define GPIO_CHASSIS_ID1 17
18 #define GPIO_CHASSIS_ID2 37
19 #define GPIO_FRONT_PANEL_CHASSIS_DET_L 70
20
21 static const struct ec_val_reg ec_hwm_init_seq[] = {
22 { 0xa0, 0x02fc },
23 { 0x32, 0x02fd },
24 { 0x77, 0x0005 },
25 { 0x0f, 0x0018 },
26 { 0x2f, 0x0019 },
27 { 0x2f, 0x001a },
28 { 0x33, 0x008a },
29 { 0x33, 0x008b },
30 { 0x33, 0x008c },
31 { 0x10, 0x00ba },
32 { 0xff, 0x00d1 },
33 { 0xff, 0x00d6 },
34 { 0xff, 0x00db },
35 { 0x00, 0x0048 },
36 { 0x00, 0x0049 },
37 { 0x00, 0x007a },
38 { 0x00, 0x007b },
39 { 0x00, 0x007c },
40 { 0x00, 0x0080 },
41 { 0x00, 0x0081 },
42 { 0x00, 0x0082 },
43 { 0xbb, 0x0083 },
44 { 0xb0, 0x0084 },
45 { 0x88, 0x01a1 },
46 { 0x80, 0x01a4 },
47 { 0x00, 0x0088 },
48 { 0x00, 0x0089 },
49 { 0x02, 0x00a0 },
50 { 0x02, 0x00a1 },
51 { 0x02, 0x00a2 },
52 { 0x04, 0x00a4 },
53 { 0x04, 0x00a5 },
54 { 0x04, 0x00a6 },
55 { 0x00, 0x00ab },
56 { 0x3f, 0x00ad },
57 { 0x07, 0x00b7 },
58 { 0x50, 0x0062 },
59 { 0x46, 0x0063 },
60 { 0x50, 0x0064 },
61 { 0x46, 0x0065 },
62 { 0x50, 0x0066 },
63 { 0x46, 0x0067 },
64 { 0x98, 0x0057 },
65 { 0x98, 0x0059 },
66 { 0x7c, 0x0061 },
67 { 0x00, 0x01bc },
68 { 0x00, 0x01bd },
69 { 0x00, 0x01bb },
70 { 0xdd, 0x0085 },
71 { 0xdd, 0x0086 },
72 { 0x07, 0x0087 },
73 { 0x5e, 0x0090 },
74 { 0x5e, 0x0091 },
75 { 0x5d, 0x0095 },
76 { 0x00, 0x0096 },
77 { 0x00, 0x0097 },
78 { 0x00, 0x009b },
79 { 0x86, 0x00ae },
80 { 0x86, 0x00af },
81 { 0x67, 0x00b3 },
82 { 0xff, 0x00c4 },
83 { 0xff, 0x00c5 },
84 { 0xff, 0x00c9 },
85 { 0x01, 0x0040 },
86 { 0x00, 0x02fc },
87 { 0x9a, 0x02b3 },
88 { 0x05, 0x02b4 },
89 { 0x01, 0x02cc },
90 { 0x4c, 0x02d0 },
91 { 0x01, 0x02d2 },
92 { 0x01, 0x006f },
93 { 0x02, 0x0070 },
94 { 0x03, 0x0071 },
95 };
96
97
98 static uint8_t send_mbox_msg_with_int(uint8_t mbox_message)
99 {
100 uint8_t int_sts, int_cond;
101
102 sch5545_emi_h2ec_mbox_write(mbox_message);
103
104 do {
105 int_sts = sch5545_emi_get_int_src_low();
106 int_cond = int_sts & 0x71;
107 } while (int_cond == 0);
108
109 sch5545_emi_set_int_src_low(int_cond);
110
111 if ((int_sts & 1) == 0)
112 return 0;
113
114 if (sch5545_emi_ec2h_mbox_read() == mbox_message)
115 return 1;
116
117 return 0;
118 }
119
120 static uint8_t send_mbox_msg_simple(uint8_t mbox_message)
121 {
122 uint8_t int_sts;
123
124 sch5545_emi_h2ec_mbox_write(mbox_message);
125
126 do {
127 int_sts = sch5545_emi_get_int_src_low();
128 if ((int_sts & 70) != 0)
129 return 0;
130 } while ((int_sts & 1) == 0);
131
132 if (sch5545_emi_ec2h_mbox_read() == mbox_message)
133 return 1;
134
135 return 0;
136 }
137
138 static void ec_check_mbox_and_int_status(uint8_t int_src, uint8_t mbox_msg)
139 {
140 uint8_t val;
141
142 val = sch5545_emi_ec2h_mbox_read();
143 if (val != mbox_msg)
144 printk(BIOS_SPEW, "EC2H mailbox should be %02x, is %02x\n", mbox_msg, val);
145
146 val = sch5545_emi_get_int_src_low();
147 if (val != int_src)
148 printk(BIOS_SPEW, "EC INT SRC should be %02x, is %02x\n", int_src, val);
149
150 sch5545_emi_set_int_src_low(val);
151 }
152
153 static uint8_t ec_read_write_reg(uint8_t ldn, uint16_t reg, uint8_t *value, uint8_t rw_bit)
154 {
155 uint8_t int_mask_bckup, ret = 0;
156 rw_bit &= 1;
157
158 int_mask_bckup = sch5545_emi_get_int_mask_low();
159 sch5545_emi_set_int_mask_low(0);
160
161 sch5545_emi_ec_write16(0x8000, (ldn << 1) | 0x100 | rw_bit);
162 if (rw_bit)
163 sch5545_emi_ec_write32(0x8004, (reg << 16) | *value);
164 else
165 sch5545_emi_ec_write32(0x8004, reg << 16);
166
167 ret = send_mbox_msg_with_int(1);
168 if (ret && !rw_bit)
169 *value = sch5545_emi_ec_read8(0x8004);
170 else if (ret != 1 && rw_bit)
171 printk(BIOS_WARNING, "EC mailbox returned unexpected value "
172 "when writing %02x to %04x\n", *value, reg);
173 else if (ret != 1 && !rw_bit)
174 printk(BIOS_WARNING, "EC mailbox returned unexpected value "
175 "when reading %04x\n", reg);
176
177 sch5545_emi_set_int_mask_low(int_mask_bckup);
178
179 return ret;
180 }
181
182 uint16_t sch5545_get_ec_fw_version(void)
183 {
184 uint8_t val = 0;
185 uint16_t ec_fw_version;
186
187 /* Read the FW version currently loaded used by EC */
188 ec_read_write_reg(EC_HWM_LDN, 0x2ad, &val, READ_OP);
189 ec_fw_version = (val << 8);
190 ec_read_write_reg(EC_HWM_LDN, 0x2ae, &val, READ_OP);
191 ec_fw_version |= val;
192 ec_read_write_reg(EC_HWM_LDN, 0x2ac, &val, READ_OP);
193 ec_read_write_reg(EC_HWM_LDN, 0x2fd, &val, READ_OP);
194 ec_read_write_reg(EC_HWM_LDN, 0x2b0, &val, READ_OP);
195
196 return ec_fw_version;
197 }
198
199 void sch5545_update_ec_firmware(uint16_t ec_version)
200 {
201 uint8_t status;
202 uint16_t ec_fw_version;
203 uint32_t *ec_fw_file;
204 size_t ec_fw_file_size;
205
206 ec_fw_file = cbfs_map("sch5545_ecfw.bin", &ec_fw_file_size);
207
208 if (!ec_fw_file || ec_fw_file_size != 0x1750) {
209 printk(BIOS_ERR, "EC firmware file not found in CBFS!\n");
210 printk(BIOS_ERR, "The fans will keep running at maximum speed.\n");
211 return;
212 }
213
214 ec_fw_version = ec_fw_file[3] & 0xffff;
215
216 /*
217 * After power failure EC loses its configuration. The currently used firmware version
218 * by EC will be reported as 0x0000. In such case EC firmware needs to be uploaded.
219 */
220 if (ec_version != ec_fw_version) {
221 printk(BIOS_INFO, "SCH5545 EC is not functional, probably due to power "
222 "failure\n");
223 printk(BIOS_INFO, "Uploading EC firmware (version %04x) to SCH5545\n",
224 ec_fw_version);
225
226 if (!send_mbox_msg_simple(0x03)) {
227 printk(BIOS_WARNING, "EC didn't accept FW upload start signal\n");
228 printk(BIOS_WARNING, "EC firmware update failed!\n");
229 return;
230 }
231
232 sch5545_emi_ec_write32_bulk(0x8100, ec_fw_file, ec_fw_file_size);
233
234 status = send_mbox_msg_simple(0x04);
235 status += send_mbox_msg_simple(0x06);
236
237 if (status != 2)
238 printk(BIOS_WARNING, "EC firmware update failed!\n");
239
240 if (ec_fw_version != sch5545_get_ec_fw_version()) {
241 printk(BIOS_ERR, "EC firmware update failed!\n");
242 printk(BIOS_ERR, "The fans will keep running at maximum speed\n");
243 } else {
244 printk(BIOS_INFO, "EC firmware update success\n");
245 /*
246 * The vendor BIOS does a full reset after EC firmware update. Most
247 * likely because the fans are adapting very slowly after automatic fan
248 * control is enabled. This makes huge noise. To avoid it, also do the
249 * full reset. On next boot, it will not be necessary.
250 */
251 full_reset();
252 }
253 } else {
254 printk(BIOS_INFO, "SCH5545 EC firmware up to date (version %04x)\n",
255 ec_version);
256 }
257 }
258
259 void sch5545_ec_hwm_early_init(void)
260 {
261 uint8_t val = 0;
262 int i;
263
264 printk(BIOS_DEBUG, "%s\n", __func__);
265
266 ec_check_mbox_and_int_status(0x20, 0x01);
267
268 ec_read_write_reg(2, 0xcb, &val, READ_OP);
269 ec_read_write_reg(2, 0xb8, &val, READ_OP);
270
271 for (i = 0; i < ARRAY_SIZE(ec_hwm_init_seq); i++) {
272 val = ec_hwm_init_seq[i].val;
273 ec_read_write_reg(EC_HWM_LDN, ec_hwm_init_seq[i].reg, &val,
274 WRITE_OP);
275 }
276
277 ec_check_mbox_and_int_status(0x01, 0x01);
278 }
279
280 static uint8_t get_sku_tdp_config(void)
281 {
282 msr_t msr;
283 uint32_t power_unit, tdp;
284 /* Get units */
285 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
286 power_unit = msr.lo & 0xf;
287
288 /* Get power defaults for this SKU */
289 msr = rdmsr(MSR_PKG_POWER_SKU);
290 tdp = msr.lo & 0x7fff;
291
292 /* These numbers will determine which settings to use to init EC */
293 if ((tdp >> power_unit) < 66)
294 return 16;
295 else
296 return 32;
297 }
298
299 static uint8_t get_chassis_type(void)
300 {
301 uint8_t chassis_id;
302
303 const gpio_t chassis_id_pins[] = {GPIO_CHASSIS_ID0, GPIO_CHASSIS_ID1,
304 GPIO_CHASSIS_ID2, GPIO_FRONT_PANEL_CHASSIS_DET_L};
305 chassis_id = gpio_base2_value(chassis_id_pins, ARRAY_SIZE(chassis_id_pins));
306
307 /* This mapping will determine which EC init sequence to use */
308 switch (chassis_id) {
309 case 0x0:
310 case 0x4:
311 return 5; /* MT */
312 case 0x8:
313 return 4; /* DT */
314 case 0x3:
315 case 0xb:
316 return 3; /* USFF */
317 case 0x1:
318 case 0x9:
319 case 0x5:
320 case 0xd:
321 return 6; /* SFF */
322 default:
323 printk(BIOS_DEBUG, "Unknown chassis ID %x\n", chassis_id);
324 break;
325 }
326
327 return 0xff;
328 }
329
330 static void ec_hwm_init_late(const ec_chassis_tdp_t *ec_hwm_sequence, size_t size)
331 {
332 unsigned int i;
333 uint8_t val;
334 uint8_t tdp_config = get_sku_tdp_config();
335
336 for (i = 0; i < size; i++) {
337 if (ec_hwm_sequence[i].tdp == tdp_config ||
338 ec_hwm_sequence[i].tdp == TDP_COMMON) {
339 val = ec_hwm_sequence[i].val;
340 ec_read_write_reg(EC_HWM_LDN, ec_hwm_sequence[i].reg, &val, WRITE_OP);
341 }
342 }
343 }
344
345 static void prepare_for_hwm_ec_sequence(uint8_t write_only, uint8_t *value)
346 {
347 uint16_t reg;
348 uint8_t val;
349
350 if (write_only == 1) {
351 val = *value;
352 reg = 0x02fc;
353 } else {
354 if (value != NULL)
355 ec_read_write_reg(EC_HWM_LDN, 0x02fc, value, READ_OP);
356 val = 0xa0;
357 ec_read_write_reg(EC_HWM_LDN, 0x2fc, &val, WRITE_OP);
358 val = 0x32;
359 reg = 0x02fd;
360 }
361
362 ec_read_write_reg(1, reg, &val, WRITE_OP);
363 }
364
365 void sch5545_ec_hwm_init(void *unused)
366 {
367 uint8_t val = 0, val_2fc, chassis_type;
368
369 printk(BIOS_DEBUG, "%s\n", __func__);
370 sch5545_emi_init(0x2e);
371
372 chassis_type = get_chassis_type();
373
374 ec_read_write_reg(EC_HWM_LDN, 0x0048, &val, READ_OP);
375 ec_read_write_reg(EC_HWM_LDN, 0x0048, &val, WRITE_OP);
376 ec_read_write_reg(EC_HWM_LDN, 0x0042, &val, READ_OP);
377 ec_read_write_reg(EC_HWM_LDN, 0x0048, &val, READ_OP);
378 val |= 0x02;
379 ec_read_write_reg(EC_HWM_LDN, 0x0048, &val, WRITE_OP);
380
381 ec_read_write_reg(EC_HWM_LDN, 0x0048, &val, READ_OP);
382 ec_read_write_reg(EC_HWM_LDN, 0x0048, &val, WRITE_OP);
383 ec_read_write_reg(EC_HWM_LDN, 0x0042, &val, READ_OP);
384 ec_read_write_reg(EC_HWM_LDN, 0x0048, &val, READ_OP);
385 val |= 0x04;
386 ec_read_write_reg(EC_HWM_LDN, 0x0048, &val, WRITE_OP);
387
388 ec_read_write_reg(EC_HWM_LDN, 0x0081, &val, READ_OP);
389 ec_read_write_reg(EC_HWM_LDN, 0x0027, &val, READ_OP);
390
391 ec_check_mbox_and_int_status(0x00, 0x01);
392
393 prepare_for_hwm_ec_sequence(0, &val_2fc);
394
395 if (chassis_type != 0xff) {
396 printk(BIOS_DEBUG, "Performing HWM init for chassis %d\n", chassis_type);
397 switch (chassis_type) {
398 case 3:
399 ec_hwm_init_late(ec_hwm_chassis3, ARRAY_SIZE(ec_hwm_chassis3));
400 break;
401 case 4:
402 ec_hwm_init_late(ec_hwm_chassis4, ARRAY_SIZE(ec_hwm_chassis4));
403 break;
404 case 5:
405 ec_hwm_init_late(ec_hwm_chassis5, ARRAY_SIZE(ec_hwm_chassis5));
406 break;
407 case 6:
408 ec_hwm_init_late(ec_hwm_chassis6, ARRAY_SIZE(ec_hwm_chassis6));
409 break;
410 }
411 }
412
413 if (CONFIG_MAX_CPUS > 2) {
414 val = 0x30;
415 ec_read_write_reg(EC_HWM_LDN, 0x009e, &val, WRITE_OP);
416 ec_read_write_reg(EC_HWM_LDN, 0x00ea, &val, READ_OP);
417 ec_read_write_reg(EC_HWM_LDN, 0x00eb, &val, WRITE_OP);
418 }
419
420 ec_read_write_reg(EC_HWM_LDN, 0x02fc, &val_2fc, WRITE_OP);
421
422 unsigned int fan_speed_full = get_uint_option("fan_full_speed", 0);
423 if (fan_speed_full) {
424 printk(BIOS_INFO, "Will set up fans to run at full speed\n");
425 ec_read_write_reg(EC_HWM_LDN, 0x0080, &val, READ_OP);
426 val |= 0x60;
427 ec_read_write_reg(EC_HWM_LDN, 0x0080, &val, WRITE_OP);
428 ec_read_write_reg(EC_HWM_LDN, 0x0081, &val, READ_OP);
429 val |= 0x60;
430 ec_read_write_reg(EC_HWM_LDN, 0x0081, &val, WRITE_OP);
431 } else {
432 printk(BIOS_INFO, "Will set up fans for automatic control\n");
433 }
434
435 ec_read_write_reg(EC_HWM_LDN, 0x00b8, &val, READ_OP);
436
437
438 if (chassis_type == 4 || chassis_type == 5) {
439 ec_read_write_reg(EC_HWM_LDN, 0x0027, &val, READ_OP);
440 if (val == 0) {
441 printk(BIOS_INFO, "Applying HWM fix-up for MT/DT chassis\n");
442 ec_read_write_reg(EC_HWM_LDN, 0x00a0, &val, READ_OP);
443 val &= 0xfb;
444 ec_read_write_reg(EC_HWM_LDN, 0x00a0, &val, WRITE_OP);
445 ec_read_write_reg(EC_HWM_LDN, 0x00a1, &val, READ_OP);
446 val &= 0xfb;
447 ec_read_write_reg(EC_HWM_LDN, 0x00a1, &val, WRITE_OP);
448 ec_read_write_reg(EC_HWM_LDN, 0x00a2, &val, READ_OP);
449 val &= 0xfb;
450 ec_read_write_reg(EC_HWM_LDN, 0x00a2, &val, WRITE_OP);
451 val = 0x99;
452 ec_read_write_reg(EC_HWM_LDN, 0x008a, &val, WRITE_OP);
453 val = 0x47;
454 ec_read_write_reg(EC_HWM_LDN, 0x008b, &val, WRITE_OP);
455 val = 0x91;
456 ec_read_write_reg(EC_HWM_LDN, 0x008c, &val, WRITE_OP);
457 }
458 }
459
460 ec_read_write_reg(EC_HWM_LDN, 0x0049, &val, READ_OP);
461 val &= 0xf7;
462 ec_read_write_reg(EC_HWM_LDN, 0x0049, &val, WRITE_OP);
463
464 val = 0x6a;
465 if (chassis_type != 3)
466 ec_read_write_reg(EC_HWM_LDN, 0x0059, &val, WRITE_OP);
467 else
468 ec_read_write_reg(EC_HWM_LDN, 0x0057, &val, WRITE_OP);
469
470 ec_read_write_reg(EC_HWM_LDN, 0x0041, &val, READ_OP);
471 val |= 0x40;
472 ec_read_write_reg(EC_HWM_LDN, 0x0041, &val, WRITE_OP);
473
474 if (chassis_type == 3) {
475 ec_read_write_reg(EC_HWM_LDN, 0x0049, &val, READ_OP);
476 val |= 0x04;
477 } else {
478 ec_read_write_reg(EC_HWM_LDN, 0x0049, &val, READ_OP);
479 val |= 0x08;
480 }
481 ec_read_write_reg(EC_HWM_LDN, 0x0049, &val, WRITE_OP);
482
483 val = 0x0e;
484 ec_read_write_reg(EC_HWM_LDN, 0x007b, &val, WRITE_OP);
485 ec_read_write_reg(EC_HWM_LDN, 0x007c, &val, WRITE_OP);
486 val = 0x01;
487 ec_read_write_reg(EC_HWM_LDN, 0x007a, &val, WRITE_OP);
488 }