blob: a07afdc79c1c8e0a08a4d12f8bbee4363d6d05ac [file] [log] [blame]
Felix Held3f3eca92020-01-23 17:12:32 +01001/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* This file is part of the coreboot project. */
Patrick Rudolph45766002018-03-27 15:58:38 +02003
4#include <arch/io.h>
Patrick Rudolph9bd60152018-05-04 09:01:38 +02005#include <console/console.h>
Patrick Rudolph45766002018-03-27 15:58:38 +02006#include <device/device.h>
7#include <device/pnp.h>
Patrick Rudolph9bd60152018-05-04 09:01:38 +02008#include <option.h>
Patrick Rudolph45766002018-03-27 15:58:38 +02009#include <pc80/keyboard.h>
Patrick Rudolph45766002018-03-27 15:58:38 +020010#include <superio/conf_mode.h>
Patrick Rudolph9ae150a2018-07-17 11:41:10 +020011#include <arch/acpi.h>
12#include <arch/acpigen.h>
Patrick Rudolphe1498ce2020-02-12 15:23:05 +010013#include <superio/common/ssdt.h>
14#include <stdlib.h>
Patrick Rudolph45766002018-03-27 15:58:38 +020015
16#include "npcd378.h"
17
Patrick Rudolph9bd60152018-05-04 09:01:38 +020018uint8_t npcd378_hwm_read(const uint16_t iobase, const uint16_t reg)
19{
20 outb((reg >> 8) & 0xf, iobase + 0xff);
21 uint8_t reg8 = inb(iobase + (reg & 0xff));
22 if (reg8 == 0xff)
23 reg8 = inb(iobase + (reg & 0xff));
24
25 outb(0, iobase + 0xff);
26 return reg8;
27}
28
29void npcd378_hwm_write(const uint16_t iobase, const uint16_t reg,
30 const uint8_t val)
31{
32 outb((reg >> 8) & 0xf, iobase + 0xff);
33 outb(val, iobase + (reg & 0xff));
34
35 outb(0, iobase + 0xff);
36}
37
38void npcd378_hwm_write_start(const uint16_t iobase)
39{
40 u8 reg8 = npcd378_hwm_read(iobase, NPCD837_HWM_WRITE_LOCK_CTRL);
41 reg8 &= ~NPCD837_HWM_WRITE_LOCK_BIT;
42 npcd378_hwm_write(iobase, NPCD837_HWM_WRITE_LOCK_CTRL, reg8);
43}
44
45void npcd378_hwm_write_finished(const uint16_t iobase)
46{
47 u8 reg8 = npcd378_hwm_read(iobase, NPCD837_HWM_WRITE_LOCK_CTRL);
48 reg8 |= NPCD837_HWM_WRITE_LOCK_BIT;
49 npcd378_hwm_write(iobase, NPCD837_HWM_WRITE_LOCK_CTRL, reg8);
50}
51
Patrick Rudolph45766002018-03-27 15:58:38 +020052static void npcd378_init(struct device *dev)
53{
Patrick Rudolph9bd60152018-05-04 09:01:38 +020054 struct resource *res;
55 uint8_t pwm, fan_lvl;
56
Patrick Rudolph45766002018-03-27 15:58:38 +020057 if (!dev->enabled)
58 return;
59
60 switch (dev->path.pnp.device) {
Patrick Rudolph9bd60152018-05-04 09:01:38 +020061 /* TODO: Might potentially need code for FDC etc. */
Patrick Rudolph45766002018-03-27 15:58:38 +020062 case NPCD378_KBC:
63 pc_keyboard_init(PROBE_AUX_DEVICE);
64 break;
Patrick Rudolph9bd60152018-05-04 09:01:38 +020065 case NPCD378_HWM:
66 res = find_resource(dev, PNP_IDX_IO0);
67 if (!res || !res->base) {
68 printk(BIOS_ERR, "NPCD378: LDN%u IOBASE not set.\n",
69 NPCD378_HWM);
70 break;
71 }
72
73 npcd378_hwm_write_start(res->base);
74
75 if (!get_option(&fan_lvl, "psu_fan_lvl") || fan_lvl > 7)
76 fan_lvl = 3;
77
78 pwm = NPCD378_HWM_PSU_FAN_MIN +
79 (NPCD378_HWM_PSU_FAN_MAX - NPCD378_HWM_PSU_FAN_MIN) *
80 fan_lvl / 7;
81
82 /* Set PSU fan PWM lvl */
83 npcd378_hwm_write(res->base, NPCD378_HWM_PSU_FAN_PWM_CTRL, pwm);
84 printk(BIOS_INFO, "NPCD378: PSU fan PWM 0x%02x\n", pwm);
85
86 npcd378_hwm_write_finished(res->base);
87 break;
Patrick Rudolph45766002018-03-27 15:58:38 +020088 }
89}
90
Julius Wernercd49cce2019-03-05 16:53:33 -080091#if CONFIG(HAVE_ACPI_TABLES)
Patrick Rudolphe1498ce2020-02-12 15:23:05 +010092/* Provide ACPI HIDs for generic Super I/O SSDT */
93static const char *npcd378_acpi_hid(const struct device *dev)
Patrick Rudolph9ae150a2018-07-17 11:41:10 +020094{
Patrick Rudolphe1498ce2020-02-12 15:23:05 +010095 /* Sanity checks */
96 if (dev->path.type != DEVICE_PATH_PNP)
97 return NULL;
98 if (dev->path.pnp.port == 0)
99 return NULL;
100 if ((dev->path.pnp.device & 0xff) > NPCD378_GPIOA)
101 return NULL;
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200102
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100103 switch (dev->path.pnp.device & 0xff) {
104 case NPCD378_FDC:
105 return ACPI_HID_FDC;
106 case NPCD378_PP:
107 return ACPI_HID_LPT;
108 case NPCD378_SP1: /* fallthrough */
109 case NPCD378_SP2:
110 return ACPI_HID_COM;
111 case NPCD378_AUX:
112 return ACPI_HID_MOUSE;
113 case NPCD378_KBC:
114 return ACPI_HID_KEYBOARD;
115 default:
116 return ACPI_HID_PNP;
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200117 }
118}
119
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100120static void npcd378_ssdt_aux(struct device *dev)
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200121{
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100122 /* Scope */
123 acpigen_write_scope(acpi_device_path(dev));
124
125 acpigen_write_method("_PSW", 1);
126 acpigen_write_store();
127 acpigen_emit_byte(ARG0_OP);
128 acpigen_emit_namestring("^^MSFG");
129 acpigen_pop_len(); /* Pop Method */
130
131 acpigen_write_PRW(8, 3);
132
133 acpigen_pop_len(); /* Pop Scope */
134}
135
136static void npcd378_ssdt_kbc(struct device *dev)
137{
138 /* Scope */
139 acpigen_write_scope(acpi_device_path(dev));
140
141 acpigen_write_method("_PSW", 1);
142 acpigen_write_store();
143 acpigen_emit_byte(ARG0_OP);
144 acpigen_emit_namestring("^^KBFG");
145 acpigen_pop_len(); /* Pop Method */
146
147 acpigen_write_PRW(8, 3);
148
149 acpigen_pop_len(); /* Pop Scope */
150}
151
152static void npcd378_ssdt_pwr(struct device *dev)
153{
154 const char *name = acpi_device_path(dev);
155 const char *scope = acpi_device_scope(dev);
156 char *tmp_name;
157
158 /* Scope */
159 acpigen_write_scope(name);
160
161 acpigen_emit_ext_op(OPREGION_OP);
162 acpigen_emit_namestring("SWCR");
163 acpigen_emit_byte(SYSTEMIO);
164 acpigen_emit_namestring("IO0B");
165 acpigen_emit_namestring("IO0S");
166
167 struct fieldlist l1[] = {
168 FIELDLIST_OFFSET(0),
169 FIELDLIST_NAMESTR("LEDC", 8),
170 FIELDLIST_NAMESTR("SWCC", 8),
171 };
172
173 acpigen_write_field("SWCR", l1, ARRAY_SIZE(l1), FIELD_BYTEACC |
174 FIELD_NOLOCK | FIELD_PRESERVE);
175
176 acpigen_emit_ext_op(OPREGION_OP);
177 acpigen_emit_namestring("RNTR");
178 acpigen_emit_byte(SYSTEMIO);
179 acpigen_emit_namestring("IO1B");
180 acpigen_emit_namestring("IO1S");
181
182 struct fieldlist l2[] = {
183 FIELDLIST_OFFSET(0),
184 FIELDLIST_NAMESTR("GPES", 8),
185 FIELDLIST_NAMESTR("GPEE", 8),
186 FIELDLIST_OFFSET(8),
187 FIELDLIST_NAMESTR("GPS0", 8),
188 FIELDLIST_NAMESTR("GPS1", 8),
189 FIELDLIST_NAMESTR("GPS2", 8),
190 FIELDLIST_NAMESTR("GPS3", 8),
191 FIELDLIST_NAMESTR("GPE0", 8),
192 FIELDLIST_NAMESTR("GPE1", 8),
193 FIELDLIST_NAMESTR("GPE2", 8),
194 FIELDLIST_NAMESTR("GPE3", 8),
195 };
196
197 acpigen_write_field("RNTR", l2, ARRAY_SIZE(l2), FIELD_BYTEACC |
198 FIELD_NOLOCK | FIELD_PRESERVE);
199
200 /* Method (SIOW, 1, NotSerialized) */
201 acpigen_write_method("SIOW", 1);
202 acpigen_write_store();
203 acpigen_emit_namestring("^GPS2");
204 acpigen_emit_namestring("^^PMFG");
205
206 acpigen_write_store();
207 acpigen_emit_byte(ZERO_OP);
208 acpigen_emit_namestring("^GPEE");
209
210 acpigen_write_store();
211 acpigen_emit_byte(ZERO_OP);
212 acpigen_emit_namestring("^GPE0");
213
214 acpigen_write_store();
215 acpigen_emit_byte(ZERO_OP);
216 acpigen_emit_namestring("^GPE1");
217
218 acpigen_emit_byte(AND_OP);
219 acpigen_emit_namestring("^LEDC");
220 acpigen_write_integer(0xE0);
221 acpigen_emit_byte(LOCAL0_OP);
222
223 acpigen_emit_byte(OR_OP);
224 acpigen_emit_byte(LOCAL0_OP);
225 acpigen_write_integer(0x1E);
226 acpigen_emit_namestring("^LEDC");
227
228 acpigen_emit_byte(AND_OP);
229 acpigen_emit_namestring("^SWCC");
230 acpigen_write_integer(0xBF);
231 acpigen_emit_namestring("^SWCC");
232
233 acpigen_pop_len(); /* SIOW method */
234
235 /* Method (SIOS, 1, NotSerialized) */
236 acpigen_write_method("SIOS", 1);
237
238 acpigen_write_if();
239 acpigen_emit_byte(LNOT_OP);
240 acpigen_emit_byte(LEQUAL_OP);
241 acpigen_emit_byte(ARG0_OP);
242 acpigen_write_integer(5);
243
244 acpigen_write_if();
245 acpigen_emit_byte(LEQUAL_OP);
246 acpigen_emit_namestring("^^KBFG");
247 acpigen_emit_byte(ONE_OP);
248
249 acpigen_emit_byte(OR_OP);
250 acpigen_emit_namestring("^GPE2");
251 acpigen_write_integer(0xE8);
252 acpigen_emit_namestring("^GPE2");
253
254 acpigen_pop_len(); /* Pop If */
255 acpigen_write_else();
256
257 acpigen_emit_byte(AND_OP);
258 acpigen_emit_namestring("^GPE2");
259 acpigen_write_integer(0x17);
260 acpigen_emit_namestring("^GPE2");
261
262 acpigen_pop_len(); /* Pop Else */
263
264 acpigen_write_if();
265 acpigen_emit_byte(LEQUAL_OP);
266 acpigen_emit_namestring("^^MSFG");
267 acpigen_emit_byte(ONE_OP);
268
269 acpigen_emit_byte(OR_OP);
270 acpigen_emit_namestring("^GPE2");
271 acpigen_write_integer(0x10);
272 acpigen_emit_namestring("^GPE2");
273
274 acpigen_pop_len(); /* Pop If */
275 acpigen_write_else();
276
277 acpigen_emit_byte(AND_OP);
278 acpigen_emit_namestring("^GPE2");
279 acpigen_write_integer(0xEF);
280 acpigen_emit_namestring("^GPE2");
281
282 acpigen_pop_len(); /* Pop Else */
283
284 /* Enable wake on GPE */
285 acpigen_write_store();
286 acpigen_emit_byte(ONE_OP);
287 acpigen_emit_namestring("^GPEE");
288
289 acpigen_write_if();
290 acpigen_emit_byte(LEQUAL_OP);
291 acpigen_emit_byte(ARG0_OP);
292 acpigen_write_integer(3);
293
294 acpigen_emit_byte(AND_OP);
295 acpigen_emit_namestring("^LEDC");
296 acpigen_write_integer(0xE0);
297 acpigen_emit_byte(LOCAL0_OP);
298
299 acpigen_emit_byte(OR_OP);
300 acpigen_emit_byte(LOCAL0_OP);
301 acpigen_write_integer(0x1C);
302 acpigen_emit_namestring("^LEDC");
303
304 acpigen_emit_byte(AND_OP);
305 acpigen_emit_namestring("^SWCC");
306 acpigen_write_integer(0xBF);
307 acpigen_emit_byte(LOCAL0_OP);
308
309 acpigen_emit_byte(OR_OP);
310 acpigen_emit_byte(LOCAL0_OP);
311 acpigen_write_integer(0x40);
312 acpigen_emit_namestring("^SWCC");
313
314 acpigen_pop_len(); /* Pop If */
315
316 acpigen_pop_len(); /* Pop If */
317
318 acpigen_write_store();
319 acpigen_write_integer(0x10);
320 acpigen_emit_namestring("^GPE0");
321
322 acpigen_write_store();
323 acpigen_write_integer(0x20);
324 acpigen_emit_namestring("^GPE1");
325
326 acpigen_pop_len(); /* Pop SIOS method */
327
328 acpigen_pop_len(); /* Pop Scope */
329
330 /* Inject into parent: */
331 acpigen_write_scope(acpi_device_scope(dev));
332
333 acpigen_write_name_integer("MSFG", 1);
334 acpigen_write_name_integer("KBFG", 1);
335 acpigen_write_name_integer("PMFG", 0);
336
337 /* DSDT must call SIOW on _WAK */
338 /* Method (SIOW, 1, NotSerialized) */
339 acpigen_write_method("SIOW", 1);
340 acpigen_emit_byte(RETURN_OP);
341 tmp_name = strconcat(name, ".SIOW");
342 acpigen_emit_namestring(tmp_name);
343 free(tmp_name);
344
345 acpigen_emit_byte(ARG0_OP);
346 acpigen_pop_len();
347
348 /* DSDT must call SIOS on _PTS */
349 /* Method (SIOS, 1, NotSerialized) */
350 acpigen_write_method("SIOS", 1);
351 acpigen_emit_byte(RETURN_OP);
352 tmp_name = strconcat(name, ".SIOS");
353 acpigen_emit_namestring(tmp_name);
354 free(tmp_name);
355 acpigen_emit_byte(ARG0_OP);
356 acpigen_pop_len(); /* Pop Method */
357
358 acpigen_pop_len(); /* Scope */
359
360 acpigen_write_scope("\\_GPE");
361
362 /* Method (SIOH, 0, NotSerialized) */
363 acpigen_write_method("_L08", 0);
364 acpigen_emit_byte(AND_OP);
365 tmp_name = strconcat(scope, ".PMFG");
366 acpigen_emit_namestring(tmp_name);
367 free(tmp_name);
368 acpigen_write_integer(0xE8);
369 acpigen_emit_byte(LOCAL0_OP);
370
371 acpigen_write_if();
372 acpigen_emit_byte(LGREATER_OP);
373 acpigen_emit_byte(LOCAL0_OP);
374 acpigen_emit_byte(ZERO_OP);
375
376 acpigen_emit_byte(NOTIFY_OP);
377 tmp_name = strconcat(scope, ".L060");
378 acpigen_emit_namestring(tmp_name);
379 free(tmp_name);
380 acpigen_write_integer(2);
381
382 acpigen_pop_len(); /* Pop If */
383
384 acpigen_emit_byte(AND_OP);
385 tmp_name = strconcat(scope, ".PMFG");
386 acpigen_emit_namestring(tmp_name);
387 free(tmp_name);
388 acpigen_write_integer(0x10);
389 acpigen_emit_byte(LOCAL0_OP);
390
391 acpigen_write_if();
392 acpigen_emit_byte(LGREATER_OP);
393 acpigen_emit_byte(LOCAL0_OP);
394 acpigen_emit_byte(ZERO_OP);
395
396 acpigen_emit_byte(NOTIFY_OP);
397 tmp_name = strconcat(scope, ".L050");
398 acpigen_emit_namestring(tmp_name);
399 free(tmp_name);
400 acpigen_write_integer(2);
401 acpigen_pop_len(); /* Pop If */
402
403 acpigen_pop_len(); /* Pop Method */
404
405 acpigen_pop_len(); /* Scope */
406}
407
408static void npcd378_fill_ssdt_generator(struct device *dev)
409{
410 superio_common_fill_ssdt_generator(dev);
411
412 switch (dev->path.pnp.device) {
413 case NPCD378_PWR:
414 npcd378_ssdt_pwr(dev);
415 break;
416 case NPCD378_AUX:
417 npcd378_ssdt_aux(dev);
418 break;
419 case NPCD378_KBC:
420 npcd378_ssdt_kbc(dev);
421 break;
422 }
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200423}
424#endif
425
Patrick Rudolph45766002018-03-27 15:58:38 +0200426static struct device_operations ops = {
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100427 .read_resources = pnp_read_resources,
428 .set_resources = pnp_set_resources,
429 .enable_resources = pnp_enable_resources,
430 .enable = pnp_alt_enable,
431 .init = npcd378_init,
432 .ops_pnp_mode = &pnp_conf_mode_8787_aa,
Julius Wernercd49cce2019-03-05 16:53:33 -0800433#if CONFIG(HAVE_ACPI_TABLES)
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100434 .acpi_fill_ssdt_generator = npcd378_fill_ssdt_generator,
435 .acpi_name = superio_common_ldn_acpi_name,
436 .acpi_hid = npcd378_acpi_hid,
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200437#endif
Patrick Rudolph45766002018-03-27 15:58:38 +0200438};
439
440static struct pnp_info pnp_dev_info[] = {
Felix Held9911d642018-07-06 20:55:53 +0200441 { NULL, NPCD378_FDC, PNP_IO0|PNP_IRQ0|PNP_DRQ0, 0x0ff8, },
442 { NULL, NPCD378_PP, PNP_IO0|PNP_IRQ0|PNP_DRQ0, 0x0ff8, },
443 { NULL, NPCD378_SP1, PNP_IO0|PNP_IRQ0, 0x0ff8, },
444 { NULL, NPCD378_SP2, PNP_IO0|PNP_IRQ0, 0x0ff8, },
445 { NULL, NPCD378_PWR, PNP_IO0|PNP_IO1|PNP_IRQ0|PNP_MSC0|
Patrick Rudolph45766002018-03-27 15:58:38 +0200446 PNP_MSC1|PNP_MSC2|PNP_MSC3|PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|
447 PNP_MSC8|PNP_MSC9|PNP_MSCA|PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE,
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200448 0x0ff8, 0x0ff0},
Felix Held9911d642018-07-06 20:55:53 +0200449 { NULL, NPCD378_AUX, PNP_IRQ0, 0x0fff, 0x0fff, },
450 { NULL, NPCD378_KBC, PNP_IO0|PNP_IO1|PNP_IRQ0,
Patrick Rudolph45766002018-03-27 15:58:38 +0200451 0x0fff, 0x0fff, },
Felix Held9911d642018-07-06 20:55:53 +0200452 { NULL, NPCD378_WDT1, PNP_IO0|PNP_MSC8|PNP_MSC9|
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200453 PNP_MSCA|PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE, 0x0fe0},
Felix Held9911d642018-07-06 20:55:53 +0200454 { NULL, NPCD378_HWM, PNP_IO0|PNP_MSC0|PNP_MSC1|PNP_MSC2|PNP_MSC3|
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200455 PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|PNP_IRQ0, 0x0f00},
Felix Held9911d642018-07-06 20:55:53 +0200456 { NULL, NPCD378_GPIO_PP_OD, PNP_MSC0|PNP_MSC1|PNP_MSC2|PNP_MSC3|
Patrick Rudolph45766002018-03-27 15:58:38 +0200457 PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|PNP_MSC8|PNP_MSC9|PNP_MSCA|
458 PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE},
Felix Held9911d642018-07-06 20:55:53 +0200459 { NULL, NPCD378_I2C, PNP_IO0|PNP_IO1|PNP_IRQ0|PNP_MSC0|
Patrick Rudolph45766002018-03-27 15:58:38 +0200460 PNP_MSC1|PNP_MSC2|PNP_MSC3|PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|
461 PNP_MSC8|PNP_MSC9|PNP_MSCA|PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE,
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200462 0x0ff0, 0x0ff0},
463 { NULL, NPCD378_SUSPEND, PNP_IO0, 0x0fe0 },
Felix Held9911d642018-07-06 20:55:53 +0200464 { NULL, NPCD378_GPIOA, PNP_IO0|PNP_MSC0|PNP_MSC1|PNP_MSC2|PNP_MSC3|
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200465 PNP_MSC4, 0x0fe0},
Patrick Rudolph45766002018-03-27 15:58:38 +0200466};
467
468static void enable_dev(struct device *dev)
469{
470 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
471}
472
473struct chip_operations superio_nuvoton_npcd378_ops = {
474 CHIP_NAME("NUVOTON NPCD378 Super I/O")
475 .enable_dev = enable_dev,
476};