Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2011 Advanced Micro Devices, Inc. |
| 5 | * Copyright (C) 2014 Felix Held <felix-coreboot@felixheld.de> |
| 6 | * Copyright (C) 2014 Edward O'Callaghan <eocallaghan@alterapraxis.com> |
| 7 | * Copyright (C) 2018 Patrick Rudolph <siro@das-labor.org> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <arch/io.h> |
Patrick Rudolph | 9bd6015 | 2018-05-04 09:01:38 +0200 | [diff] [blame] | 21 | #include <console/console.h> |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 22 | #include <device/device.h> |
| 23 | #include <device/pnp.h> |
Patrick Rudolph | 9bd6015 | 2018-05-04 09:01:38 +0200 | [diff] [blame] | 24 | #include <option.h> |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 25 | #include <pc80/keyboard.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <superio/conf_mode.h> |
Patrick Rudolph | 9ae150a | 2018-07-17 11:41:10 +0200 | [diff] [blame] | 28 | #include <arch/acpi.h> |
| 29 | #include <arch/acpigen.h> |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 30 | |
| 31 | #include "npcd378.h" |
| 32 | |
Patrick Rudolph | 9bd6015 | 2018-05-04 09:01:38 +0200 | [diff] [blame] | 33 | uint8_t npcd378_hwm_read(const uint16_t iobase, const uint16_t reg) |
| 34 | { |
| 35 | outb((reg >> 8) & 0xf, iobase + 0xff); |
| 36 | uint8_t reg8 = inb(iobase + (reg & 0xff)); |
| 37 | if (reg8 == 0xff) |
| 38 | reg8 = inb(iobase + (reg & 0xff)); |
| 39 | |
| 40 | outb(0, iobase + 0xff); |
| 41 | return reg8; |
| 42 | } |
| 43 | |
| 44 | void npcd378_hwm_write(const uint16_t iobase, const uint16_t reg, |
| 45 | const uint8_t val) |
| 46 | { |
| 47 | outb((reg >> 8) & 0xf, iobase + 0xff); |
| 48 | outb(val, iobase + (reg & 0xff)); |
| 49 | |
| 50 | outb(0, iobase + 0xff); |
| 51 | } |
| 52 | |
| 53 | void npcd378_hwm_write_start(const uint16_t iobase) |
| 54 | { |
| 55 | u8 reg8 = npcd378_hwm_read(iobase, NPCD837_HWM_WRITE_LOCK_CTRL); |
| 56 | reg8 &= ~NPCD837_HWM_WRITE_LOCK_BIT; |
| 57 | npcd378_hwm_write(iobase, NPCD837_HWM_WRITE_LOCK_CTRL, reg8); |
| 58 | } |
| 59 | |
| 60 | void npcd378_hwm_write_finished(const uint16_t iobase) |
| 61 | { |
| 62 | u8 reg8 = npcd378_hwm_read(iobase, NPCD837_HWM_WRITE_LOCK_CTRL); |
| 63 | reg8 |= NPCD837_HWM_WRITE_LOCK_BIT; |
| 64 | npcd378_hwm_write(iobase, NPCD837_HWM_WRITE_LOCK_CTRL, reg8); |
| 65 | } |
| 66 | |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 67 | static void npcd378_init(struct device *dev) |
| 68 | { |
Patrick Rudolph | 9bd6015 | 2018-05-04 09:01:38 +0200 | [diff] [blame] | 69 | struct resource *res; |
| 70 | uint8_t pwm, fan_lvl; |
| 71 | |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 72 | if (!dev->enabled) |
| 73 | return; |
| 74 | |
| 75 | switch (dev->path.pnp.device) { |
Patrick Rudolph | 9bd6015 | 2018-05-04 09:01:38 +0200 | [diff] [blame] | 76 | /* TODO: Might potentially need code for FDC etc. */ |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 77 | case NPCD378_KBC: |
| 78 | pc_keyboard_init(PROBE_AUX_DEVICE); |
| 79 | break; |
Patrick Rudolph | 9bd6015 | 2018-05-04 09:01:38 +0200 | [diff] [blame] | 80 | case NPCD378_HWM: |
| 81 | res = find_resource(dev, PNP_IDX_IO0); |
| 82 | if (!res || !res->base) { |
| 83 | printk(BIOS_ERR, "NPCD378: LDN%u IOBASE not set.\n", |
| 84 | NPCD378_HWM); |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | npcd378_hwm_write_start(res->base); |
| 89 | |
| 90 | if (!get_option(&fan_lvl, "psu_fan_lvl") || fan_lvl > 7) |
| 91 | fan_lvl = 3; |
| 92 | |
| 93 | pwm = NPCD378_HWM_PSU_FAN_MIN + |
| 94 | (NPCD378_HWM_PSU_FAN_MAX - NPCD378_HWM_PSU_FAN_MIN) * |
| 95 | fan_lvl / 7; |
| 96 | |
| 97 | /* Set PSU fan PWM lvl */ |
| 98 | npcd378_hwm_write(res->base, NPCD378_HWM_PSU_FAN_PWM_CTRL, pwm); |
| 99 | printk(BIOS_INFO, "NPCD378: PSU fan PWM 0x%02x\n", pwm); |
| 100 | |
| 101 | npcd378_hwm_write_finished(res->base); |
| 102 | break; |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame^] | 106 | #if CONFIG(HAVE_ACPI_TABLES) |
Patrick Rudolph | 9ae150a | 2018-07-17 11:41:10 +0200 | [diff] [blame] | 107 | static void npcd378_ssdt(struct device *dev) |
| 108 | { |
| 109 | struct resource *res; |
| 110 | |
| 111 | const char *scope = acpi_device_path(dev); |
| 112 | if (!scope) { |
| 113 | printk(BIOS_ERR, "%s: Missing ACPI scope\n", dev_path(dev)); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | switch (dev->path.pnp.device) { |
| 118 | case NPCD378_PWR: { |
| 119 | res = find_resource(dev, PNP_IDX_IO0); |
| 120 | if (!res || !res->base) { |
| 121 | printk(BIOS_ERR, "NPCD378: LDN%u IOBASE not set.\n", |
| 122 | NPCD378_PWR); |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | acpigen_write_scope(scope); |
| 127 | acpigen_write_name_integer("SWB", res->base); |
| 128 | acpigen_write_name_integer("SWL", res->size); |
| 129 | acpigen_pop_len(); /* pop scope */ |
| 130 | |
| 131 | res = find_resource(dev, PNP_IDX_IO1); |
| 132 | if (!res || !res->base) { |
| 133 | printk(BIOS_ERR, "NPCD378: LDN%u IOBASE2 not set.\n", |
| 134 | NPCD378_PWR); |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | acpigen_write_scope(scope); |
| 139 | acpigen_write_name_integer("RNB", res->base); |
| 140 | acpigen_write_name_integer("RNL", res->size); |
| 141 | acpigen_pop_len(); /* pop scope */ |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static const char *npcd378_acpi_name(const struct device *dev) |
| 148 | { |
| 149 | return "SIO0"; |
| 150 | } |
| 151 | #endif |
| 152 | |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 153 | static struct device_operations ops = { |
| 154 | .read_resources = pnp_read_resources, |
| 155 | .set_resources = pnp_set_resources, |
| 156 | .enable_resources = pnp_enable_resources, |
| 157 | .enable = pnp_alt_enable, |
| 158 | .init = npcd378_init, |
| 159 | .ops_pnp_mode = &pnp_conf_mode_8787_aa, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame^] | 160 | #if CONFIG(HAVE_ACPI_TABLES) |
Patrick Rudolph | 9ae150a | 2018-07-17 11:41:10 +0200 | [diff] [blame] | 161 | .acpi_fill_ssdt_generator = npcd378_ssdt, |
| 162 | .acpi_name = npcd378_acpi_name, |
| 163 | #endif |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | static struct pnp_info pnp_dev_info[] = { |
Felix Held | 9911d64 | 2018-07-06 20:55:53 +0200 | [diff] [blame] | 167 | { NULL, NPCD378_FDC, PNP_IO0|PNP_IRQ0|PNP_DRQ0, 0x0ff8, }, |
| 168 | { NULL, NPCD378_PP, PNP_IO0|PNP_IRQ0|PNP_DRQ0, 0x0ff8, }, |
| 169 | { NULL, NPCD378_SP1, PNP_IO0|PNP_IRQ0, 0x0ff8, }, |
| 170 | { NULL, NPCD378_SP2, PNP_IO0|PNP_IRQ0, 0x0ff8, }, |
| 171 | { NULL, NPCD378_PWR, PNP_IO0|PNP_IO1|PNP_IRQ0|PNP_MSC0| |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 172 | PNP_MSC1|PNP_MSC2|PNP_MSC3|PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7| |
| 173 | PNP_MSC8|PNP_MSC9|PNP_MSCA|PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE, |
Patrick Rudolph | 61c3b59 | 2018-07-17 11:36:15 +0200 | [diff] [blame] | 174 | 0x0ff8, 0x0ff0}, |
Felix Held | 9911d64 | 2018-07-06 20:55:53 +0200 | [diff] [blame] | 175 | { NULL, NPCD378_AUX, PNP_IRQ0, 0x0fff, 0x0fff, }, |
| 176 | { NULL, NPCD378_KBC, PNP_IO0|PNP_IO1|PNP_IRQ0, |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 177 | 0x0fff, 0x0fff, }, |
Felix Held | 9911d64 | 2018-07-06 20:55:53 +0200 | [diff] [blame] | 178 | { NULL, NPCD378_WDT1, PNP_IO0|PNP_MSC8|PNP_MSC9| |
Patrick Rudolph | 61c3b59 | 2018-07-17 11:36:15 +0200 | [diff] [blame] | 179 | PNP_MSCA|PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE, 0x0fe0}, |
Felix Held | 9911d64 | 2018-07-06 20:55:53 +0200 | [diff] [blame] | 180 | { NULL, NPCD378_HWM, PNP_IO0|PNP_MSC0|PNP_MSC1|PNP_MSC2|PNP_MSC3| |
Patrick Rudolph | 61c3b59 | 2018-07-17 11:36:15 +0200 | [diff] [blame] | 181 | PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|PNP_IRQ0, 0x0f00}, |
Felix Held | 9911d64 | 2018-07-06 20:55:53 +0200 | [diff] [blame] | 182 | { NULL, NPCD378_GPIO_PP_OD, PNP_MSC0|PNP_MSC1|PNP_MSC2|PNP_MSC3| |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 183 | PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7|PNP_MSC8|PNP_MSC9|PNP_MSCA| |
| 184 | PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE}, |
Felix Held | 9911d64 | 2018-07-06 20:55:53 +0200 | [diff] [blame] | 185 | { NULL, NPCD378_I2C, PNP_IO0|PNP_IO1|PNP_IRQ0|PNP_MSC0| |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 186 | PNP_MSC1|PNP_MSC2|PNP_MSC3|PNP_MSC4|PNP_MSC5|PNP_MSC6|PNP_MSC7| |
| 187 | PNP_MSC8|PNP_MSC9|PNP_MSCA|PNP_MSCB|PNP_MSCC|PNP_MSCD|PNP_MSCE, |
Patrick Rudolph | 61c3b59 | 2018-07-17 11:36:15 +0200 | [diff] [blame] | 188 | 0x0ff0, 0x0ff0}, |
| 189 | { NULL, NPCD378_SUSPEND, PNP_IO0, 0x0fe0 }, |
Felix Held | 9911d64 | 2018-07-06 20:55:53 +0200 | [diff] [blame] | 190 | { NULL, NPCD378_GPIOA, PNP_IO0|PNP_MSC0|PNP_MSC1|PNP_MSC2|PNP_MSC3| |
Patrick Rudolph | 61c3b59 | 2018-07-17 11:36:15 +0200 | [diff] [blame] | 191 | PNP_MSC4, 0x0fe0}, |
Patrick Rudolph | 4576600 | 2018-03-27 15:58:38 +0200 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | static void enable_dev(struct device *dev) |
| 195 | { |
| 196 | pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
| 197 | } |
| 198 | |
| 199 | struct chip_operations superio_nuvoton_npcd378_ops = { |
| 200 | CHIP_NAME("NUVOTON NPCD378 Super I/O") |
| 201 | .enable_dev = enable_dev, |
| 202 | }; |