blob: 6feee5e9da52cd234e5bbc5b21e81e7f2d0cc13b [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>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070011#include <acpi/acpi.h>
12#include <acpi/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
Elyes HAOUAS7774de52020-03-30 16:46:18 +020029void npcd378_hwm_write(const uint16_t iobase, const uint16_t reg, const uint8_t val)
Patrick Rudolph9bd60152018-05-04 09:01:38 +020030{
31 outb((reg >> 8) & 0xf, iobase + 0xff);
32 outb(val, iobase + (reg & 0xff));
33
34 outb(0, iobase + 0xff);
35}
36
37void npcd378_hwm_write_start(const uint16_t iobase)
38{
39 u8 reg8 = npcd378_hwm_read(iobase, NPCD837_HWM_WRITE_LOCK_CTRL);
40 reg8 &= ~NPCD837_HWM_WRITE_LOCK_BIT;
41 npcd378_hwm_write(iobase, NPCD837_HWM_WRITE_LOCK_CTRL, reg8);
42}
43
44void npcd378_hwm_write_finished(const uint16_t iobase)
45{
46 u8 reg8 = npcd378_hwm_read(iobase, NPCD837_HWM_WRITE_LOCK_CTRL);
47 reg8 |= NPCD837_HWM_WRITE_LOCK_BIT;
48 npcd378_hwm_write(iobase, NPCD837_HWM_WRITE_LOCK_CTRL, reg8);
49}
50
Patrick Rudolph45766002018-03-27 15:58:38 +020051static void npcd378_init(struct device *dev)
52{
Patrick Rudolph9bd60152018-05-04 09:01:38 +020053 struct resource *res;
54 uint8_t pwm, fan_lvl;
55
Patrick Rudolph45766002018-03-27 15:58:38 +020056 if (!dev->enabled)
57 return;
58
59 switch (dev->path.pnp.device) {
Patrick Rudolph9bd60152018-05-04 09:01:38 +020060 /* TODO: Might potentially need code for FDC etc. */
Patrick Rudolph45766002018-03-27 15:58:38 +020061 case NPCD378_KBC:
62 pc_keyboard_init(PROBE_AUX_DEVICE);
63 break;
Patrick Rudolph9bd60152018-05-04 09:01:38 +020064 case NPCD378_HWM:
65 res = find_resource(dev, PNP_IDX_IO0);
66 if (!res || !res->base) {
Elyes HAOUAS7774de52020-03-30 16:46:18 +020067 printk(BIOS_ERR, "NPCD378: LDN%u IOBASE not set.\n", NPCD378_HWM);
Patrick Rudolph9bd60152018-05-04 09:01:38 +020068 break;
69 }
70
71 npcd378_hwm_write_start(res->base);
72
73 if (!get_option(&fan_lvl, "psu_fan_lvl") || fan_lvl > 7)
74 fan_lvl = 3;
75
76 pwm = NPCD378_HWM_PSU_FAN_MIN +
77 (NPCD378_HWM_PSU_FAN_MAX - NPCD378_HWM_PSU_FAN_MIN) *
78 fan_lvl / 7;
79
80 /* Set PSU fan PWM lvl */
81 npcd378_hwm_write(res->base, NPCD378_HWM_PSU_FAN_PWM_CTRL, pwm);
82 printk(BIOS_INFO, "NPCD378: PSU fan PWM 0x%02x\n", pwm);
83
84 npcd378_hwm_write_finished(res->base);
85 break;
Patrick Rudolph45766002018-03-27 15:58:38 +020086 }
87}
88
Julius Wernercd49cce2019-03-05 16:53:33 -080089#if CONFIG(HAVE_ACPI_TABLES)
Patrick Rudolphe1498ce2020-02-12 15:23:05 +010090/* Provide ACPI HIDs for generic Super I/O SSDT */
91static const char *npcd378_acpi_hid(const struct device *dev)
Patrick Rudolph9ae150a2018-07-17 11:41:10 +020092{
Patrick Rudolphe1498ce2020-02-12 15:23:05 +010093 /* Sanity checks */
94 if (dev->path.type != DEVICE_PATH_PNP)
95 return NULL;
96 if (dev->path.pnp.port == 0)
97 return NULL;
98 if ((dev->path.pnp.device & 0xff) > NPCD378_GPIOA)
99 return NULL;
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200100
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100101 switch (dev->path.pnp.device & 0xff) {
102 case NPCD378_FDC:
103 return ACPI_HID_FDC;
104 case NPCD378_PP:
105 return ACPI_HID_LPT;
106 case NPCD378_SP1: /* fallthrough */
107 case NPCD378_SP2:
108 return ACPI_HID_COM;
109 case NPCD378_AUX:
110 return ACPI_HID_MOUSE;
111 case NPCD378_KBC:
112 return ACPI_HID_KEYBOARD;
113 default:
114 return ACPI_HID_PNP;
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200115 }
116}
117
Furquan Shaikh7536a392020-04-24 21:59:21 -0700118static void npcd378_ssdt_aux(const struct device *dev)
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200119{
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100120 /* Scope */
121 acpigen_write_scope(acpi_device_path(dev));
122
123 acpigen_write_method("_PSW", 1);
124 acpigen_write_store();
125 acpigen_emit_byte(ARG0_OP);
126 acpigen_emit_namestring("^^MSFG");
127 acpigen_pop_len(); /* Pop Method */
128
129 acpigen_write_PRW(8, 3);
130
131 acpigen_pop_len(); /* Pop Scope */
132}
133
Furquan Shaikh7536a392020-04-24 21:59:21 -0700134static void npcd378_ssdt_kbc(const struct device *dev)
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100135{
136 /* Scope */
137 acpigen_write_scope(acpi_device_path(dev));
138
139 acpigen_write_method("_PSW", 1);
140 acpigen_write_store();
141 acpigen_emit_byte(ARG0_OP);
142 acpigen_emit_namestring("^^KBFG");
143 acpigen_pop_len(); /* Pop Method */
144
145 acpigen_write_PRW(8, 3);
146
147 acpigen_pop_len(); /* Pop Scope */
148}
149
Furquan Shaikh7536a392020-04-24 21:59:21 -0700150static void npcd378_ssdt_pwr(const struct device *dev)
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100151{
152 const char *name = acpi_device_path(dev);
153 const char *scope = acpi_device_scope(dev);
154 char *tmp_name;
155
156 /* Scope */
157 acpigen_write_scope(name);
158
159 acpigen_emit_ext_op(OPREGION_OP);
160 acpigen_emit_namestring("SWCR");
161 acpigen_emit_byte(SYSTEMIO);
162 acpigen_emit_namestring("IO0B");
163 acpigen_emit_namestring("IO0S");
164
165 struct fieldlist l1[] = {
166 FIELDLIST_OFFSET(0),
167 FIELDLIST_NAMESTR("LEDC", 8),
168 FIELDLIST_NAMESTR("SWCC", 8),
169 };
170
171 acpigen_write_field("SWCR", l1, ARRAY_SIZE(l1), FIELD_BYTEACC |
172 FIELD_NOLOCK | FIELD_PRESERVE);
173
174 acpigen_emit_ext_op(OPREGION_OP);
175 acpigen_emit_namestring("RNTR");
176 acpigen_emit_byte(SYSTEMIO);
177 acpigen_emit_namestring("IO1B");
178 acpigen_emit_namestring("IO1S");
179
180 struct fieldlist l2[] = {
181 FIELDLIST_OFFSET(0),
182 FIELDLIST_NAMESTR("GPES", 8),
183 FIELDLIST_NAMESTR("GPEE", 8),
184 FIELDLIST_OFFSET(8),
185 FIELDLIST_NAMESTR("GPS0", 8),
186 FIELDLIST_NAMESTR("GPS1", 8),
187 FIELDLIST_NAMESTR("GPS2", 8),
188 FIELDLIST_NAMESTR("GPS3", 8),
189 FIELDLIST_NAMESTR("GPE0", 8),
190 FIELDLIST_NAMESTR("GPE1", 8),
191 FIELDLIST_NAMESTR("GPE2", 8),
192 FIELDLIST_NAMESTR("GPE3", 8),
193 };
194
195 acpigen_write_field("RNTR", l2, ARRAY_SIZE(l2), FIELD_BYTEACC |
196 FIELD_NOLOCK | FIELD_PRESERVE);
197
198 /* Method (SIOW, 1, NotSerialized) */
199 acpigen_write_method("SIOW", 1);
200 acpigen_write_store();
201 acpigen_emit_namestring("^GPS2");
202 acpigen_emit_namestring("^^PMFG");
203
204 acpigen_write_store();
205 acpigen_emit_byte(ZERO_OP);
206 acpigen_emit_namestring("^GPEE");
207
208 acpigen_write_store();
209 acpigen_emit_byte(ZERO_OP);
210 acpigen_emit_namestring("^GPE0");
211
212 acpigen_write_store();
213 acpigen_emit_byte(ZERO_OP);
214 acpigen_emit_namestring("^GPE1");
215
216 acpigen_emit_byte(AND_OP);
217 acpigen_emit_namestring("^LEDC");
218 acpigen_write_integer(0xE0);
219 acpigen_emit_byte(LOCAL0_OP);
220
221 acpigen_emit_byte(OR_OP);
222 acpigen_emit_byte(LOCAL0_OP);
223 acpigen_write_integer(0x1E);
224 acpigen_emit_namestring("^LEDC");
225
226 acpigen_emit_byte(AND_OP);
227 acpigen_emit_namestring("^SWCC");
228 acpigen_write_integer(0xBF);
229 acpigen_emit_namestring("^SWCC");
230
231 acpigen_pop_len(); /* SIOW method */
232
233 /* Method (SIOS, 1, NotSerialized) */
234 acpigen_write_method("SIOS", 1);
235
236 acpigen_write_if();
237 acpigen_emit_byte(LNOT_OP);
238 acpigen_emit_byte(LEQUAL_OP);
239 acpigen_emit_byte(ARG0_OP);
240 acpigen_write_integer(5);
241
242 acpigen_write_if();
243 acpigen_emit_byte(LEQUAL_OP);
244 acpigen_emit_namestring("^^KBFG");
245 acpigen_emit_byte(ONE_OP);
246
247 acpigen_emit_byte(OR_OP);
248 acpigen_emit_namestring("^GPE2");
249 acpigen_write_integer(0xE8);
250 acpigen_emit_namestring("^GPE2");
251
252 acpigen_pop_len(); /* Pop If */
253 acpigen_write_else();
254
255 acpigen_emit_byte(AND_OP);
256 acpigen_emit_namestring("^GPE2");
257 acpigen_write_integer(0x17);
258 acpigen_emit_namestring("^GPE2");
259
260 acpigen_pop_len(); /* Pop Else */
261
262 acpigen_write_if();
263 acpigen_emit_byte(LEQUAL_OP);
264 acpigen_emit_namestring("^^MSFG");
265 acpigen_emit_byte(ONE_OP);
266
267 acpigen_emit_byte(OR_OP);
268 acpigen_emit_namestring("^GPE2");
269 acpigen_write_integer(0x10);
270 acpigen_emit_namestring("^GPE2");
271
272 acpigen_pop_len(); /* Pop If */
273 acpigen_write_else();
274
275 acpigen_emit_byte(AND_OP);
276 acpigen_emit_namestring("^GPE2");
277 acpigen_write_integer(0xEF);
278 acpigen_emit_namestring("^GPE2");
279
280 acpigen_pop_len(); /* Pop Else */
281
282 /* Enable wake on GPE */
283 acpigen_write_store();
284 acpigen_emit_byte(ONE_OP);
285 acpigen_emit_namestring("^GPEE");
286
287 acpigen_write_if();
288 acpigen_emit_byte(LEQUAL_OP);
289 acpigen_emit_byte(ARG0_OP);
290 acpigen_write_integer(3);
291
292 acpigen_emit_byte(AND_OP);
293 acpigen_emit_namestring("^LEDC");
294 acpigen_write_integer(0xE0);
295 acpigen_emit_byte(LOCAL0_OP);
296
297 acpigen_emit_byte(OR_OP);
298 acpigen_emit_byte(LOCAL0_OP);
299 acpigen_write_integer(0x1C);
300 acpigen_emit_namestring("^LEDC");
301
302 acpigen_emit_byte(AND_OP);
303 acpigen_emit_namestring("^SWCC");
304 acpigen_write_integer(0xBF);
305 acpigen_emit_byte(LOCAL0_OP);
306
307 acpigen_emit_byte(OR_OP);
308 acpigen_emit_byte(LOCAL0_OP);
309 acpigen_write_integer(0x40);
310 acpigen_emit_namestring("^SWCC");
311
312 acpigen_pop_len(); /* Pop If */
313
314 acpigen_pop_len(); /* Pop If */
315
316 acpigen_write_store();
317 acpigen_write_integer(0x10);
318 acpigen_emit_namestring("^GPE0");
319
320 acpigen_write_store();
321 acpigen_write_integer(0x20);
322 acpigen_emit_namestring("^GPE1");
323
324 acpigen_pop_len(); /* Pop SIOS method */
325
326 acpigen_pop_len(); /* Pop Scope */
327
328 /* Inject into parent: */
329 acpigen_write_scope(acpi_device_scope(dev));
330
331 acpigen_write_name_integer("MSFG", 1);
332 acpigen_write_name_integer("KBFG", 1);
333 acpigen_write_name_integer("PMFG", 0);
334
335 /* DSDT must call SIOW on _WAK */
336 /* Method (SIOW, 1, NotSerialized) */
337 acpigen_write_method("SIOW", 1);
338 acpigen_emit_byte(RETURN_OP);
339 tmp_name = strconcat(name, ".SIOW");
340 acpigen_emit_namestring(tmp_name);
341 free(tmp_name);
342
343 acpigen_emit_byte(ARG0_OP);
344 acpigen_pop_len();
345
346 /* DSDT must call SIOS on _PTS */
347 /* Method (SIOS, 1, NotSerialized) */
348 acpigen_write_method("SIOS", 1);
349 acpigen_emit_byte(RETURN_OP);
350 tmp_name = strconcat(name, ".SIOS");
351 acpigen_emit_namestring(tmp_name);
352 free(tmp_name);
353 acpigen_emit_byte(ARG0_OP);
354 acpigen_pop_len(); /* Pop Method */
355
356 acpigen_pop_len(); /* Scope */
357
358 acpigen_write_scope("\\_GPE");
359
360 /* Method (SIOH, 0, NotSerialized) */
361 acpigen_write_method("_L08", 0);
362 acpigen_emit_byte(AND_OP);
363 tmp_name = strconcat(scope, ".PMFG");
364 acpigen_emit_namestring(tmp_name);
365 free(tmp_name);
366 acpigen_write_integer(0xE8);
367 acpigen_emit_byte(LOCAL0_OP);
368
369 acpigen_write_if();
370 acpigen_emit_byte(LGREATER_OP);
371 acpigen_emit_byte(LOCAL0_OP);
372 acpigen_emit_byte(ZERO_OP);
373
374 acpigen_emit_byte(NOTIFY_OP);
375 tmp_name = strconcat(scope, ".L060");
376 acpigen_emit_namestring(tmp_name);
377 free(tmp_name);
378 acpigen_write_integer(2);
379
380 acpigen_pop_len(); /* Pop If */
381
382 acpigen_emit_byte(AND_OP);
383 tmp_name = strconcat(scope, ".PMFG");
384 acpigen_emit_namestring(tmp_name);
385 free(tmp_name);
386 acpigen_write_integer(0x10);
387 acpigen_emit_byte(LOCAL0_OP);
388
389 acpigen_write_if();
390 acpigen_emit_byte(LGREATER_OP);
391 acpigen_emit_byte(LOCAL0_OP);
392 acpigen_emit_byte(ZERO_OP);
393
394 acpigen_emit_byte(NOTIFY_OP);
395 tmp_name = strconcat(scope, ".L050");
396 acpigen_emit_namestring(tmp_name);
397 free(tmp_name);
398 acpigen_write_integer(2);
399 acpigen_pop_len(); /* Pop If */
400
401 acpigen_pop_len(); /* Pop Method */
402
403 acpigen_pop_len(); /* Scope */
404}
405
Furquan Shaikh7536a392020-04-24 21:59:21 -0700406static void npcd378_fill_ssdt_generator(const struct device *dev)
Patrick Rudolphe1498ce2020-02-12 15:23:05 +0100407{
408 superio_common_fill_ssdt_generator(dev);
409
410 switch (dev->path.pnp.device) {
411 case NPCD378_PWR:
412 npcd378_ssdt_pwr(dev);
413 break;
414 case NPCD378_AUX:
415 npcd378_ssdt_aux(dev);
416 break;
417 case NPCD378_KBC:
418 npcd378_ssdt_kbc(dev);
419 break;
420 }
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200421}
422#endif
423
Patrick Rudolph45766002018-03-27 15:58:38 +0200424static struct device_operations ops = {
Nico Huber68680dd2020-03-31 17:34:52 +0200425 .read_resources = pnp_read_resources,
426 .set_resources = pnp_set_resources,
427 .enable_resources = pnp_enable_resources,
428 .enable = pnp_alt_enable,
429 .init = npcd378_init,
430 .ops_pnp_mode = &pnp_conf_mode_8787_aa,
Julius Wernercd49cce2019-03-05 16:53:33 -0800431#if CONFIG(HAVE_ACPI_TABLES)
Nico Huber68680dd2020-03-31 17:34:52 +0200432 .acpi_fill_ssdt = npcd378_fill_ssdt_generator,
433 .acpi_name = superio_common_ldn_acpi_name,
434 .acpi_hid = npcd378_acpi_hid,
Patrick Rudolph9ae150a2018-07-17 11:41:10 +0200435#endif
Patrick Rudolph45766002018-03-27 15:58:38 +0200436};
437
438static struct pnp_info pnp_dev_info[] = {
Felix Held9911d642018-07-06 20:55:53 +0200439 { NULL, NPCD378_FDC, PNP_IO0|PNP_IRQ0|PNP_DRQ0, 0x0ff8, },
440 { NULL, NPCD378_PP, PNP_IO0|PNP_IRQ0|PNP_DRQ0, 0x0ff8, },
441 { NULL, NPCD378_SP1, PNP_IO0|PNP_IRQ0, 0x0ff8, },
442 { NULL, NPCD378_SP2, PNP_IO0|PNP_IRQ0, 0x0ff8, },
443 { NULL, NPCD378_PWR, PNP_IO0|PNP_IO1|PNP_IRQ0|PNP_MSC0|
Patrick Rudolph45766002018-03-27 15:58:38 +0200444 PNP_MSC1|PNP_MSC2|PNP_MSC3|PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|
445 PNP_MSC8|PNP_MSC9|PNP_MSCA|PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE,
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200446 0x0ff8, 0x0ff0},
Felix Held9911d642018-07-06 20:55:53 +0200447 { NULL, NPCD378_AUX, PNP_IRQ0, 0x0fff, 0x0fff, },
448 { NULL, NPCD378_KBC, PNP_IO0|PNP_IO1|PNP_IRQ0,
Patrick Rudolph45766002018-03-27 15:58:38 +0200449 0x0fff, 0x0fff, },
Felix Held9911d642018-07-06 20:55:53 +0200450 { NULL, NPCD378_WDT1, PNP_IO0|PNP_MSC8|PNP_MSC9|
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200451 PNP_MSCA|PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE, 0x0fe0},
Felix Held9911d642018-07-06 20:55:53 +0200452 { NULL, NPCD378_HWM, PNP_IO0|PNP_MSC0|PNP_MSC1|PNP_MSC2|PNP_MSC3|
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200453 PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|PNP_IRQ0, 0x0f00},
Felix Held9911d642018-07-06 20:55:53 +0200454 { NULL, NPCD378_GPIO_PP_OD, PNP_MSC0|PNP_MSC1|PNP_MSC2|PNP_MSC3|
Patrick Rudolph45766002018-03-27 15:58:38 +0200455 PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|PNP_MSC8|PNP_MSC9|PNP_MSCA|
456 PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE},
Felix Held9911d642018-07-06 20:55:53 +0200457 { NULL, NPCD378_I2C, PNP_IO0|PNP_IO1|PNP_IRQ0|PNP_MSC0|
Patrick Rudolph45766002018-03-27 15:58:38 +0200458 PNP_MSC1|PNP_MSC2|PNP_MSC3|PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|
459 PNP_MSC8|PNP_MSC9|PNP_MSCA|PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE,
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200460 0x0ff0, 0x0ff0},
461 { NULL, NPCD378_SUSPEND, PNP_IO0, 0x0fe0 },
Felix Held9911d642018-07-06 20:55:53 +0200462 { NULL, NPCD378_GPIOA, PNP_IO0|PNP_MSC0|PNP_MSC1|PNP_MSC2|PNP_MSC3|
Patrick Rudolph61c3b592018-07-17 11:36:15 +0200463 PNP_MSC4, 0x0fe0},
Patrick Rudolph45766002018-03-27 15:58:38 +0200464};
465
466static void enable_dev(struct device *dev)
467{
468 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
469}
470
471struct chip_operations superio_nuvoton_npcd378_ops = {
472 CHIP_NAME("NUVOTON NPCD378 Super I/O")
473 .enable_dev = enable_dev,
474};