Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 15 | #include <console/console.h> |
| 16 | #include <device/device.h> |
| 17 | #include <device/pnp.h> |
| 18 | #include <ec/acpi/ec.h> |
Kyösti Mälkki | cbf9571 | 2020-01-05 08:05:45 +0200 | [diff] [blame] | 19 | #include <option.h> |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 20 | |
| 21 | #include "ec.h" |
| 22 | #include "chip.h" |
| 23 | |
| 24 | typedef struct ec_kontron_it8516e_config config_t; |
| 25 | |
| 26 | enum { /* EC commands */ |
Nico Huber | 6d6a2ac | 2013-07-12 14:35:00 +0200 | [diff] [blame] | 27 | IT8516E_CMD_SET_SYSTEMP_TYPE = 0x06, |
| 28 | IT8516E_CMD_GET_SYSTEMP_TYPE = 0x07, |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 29 | IT8516E_CMD_GET_FAN_MODE = 0x10, |
| 30 | IT8516E_CMD_SET_FAN_MODE = 0x11, |
| 31 | IT8516E_CMD_GET_FAN_PWM = 0x12, |
| 32 | IT8516E_CMD_SET_FAN_PWM = 0x13, |
| 33 | IT8516E_CMD_GET_FAN_SPEED = 0x14, |
| 34 | IT8516E_CMD_SET_FAN_SPEED = 0x15, |
| 35 | IT8516E_CMD_GET_FAN_TEMP = 0x16, |
| 36 | IT8516E_CMD_SET_FAN_TEMP = 0x17, |
Nico Huber | 9ce71b3 | 2013-07-12 14:43:11 +0200 | [diff] [blame] | 37 | IT8516E_CMD_SET_FAN_LIMITS = 0x1a, |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
Nico Huber | 1f9f678 | 2013-07-18 11:50:59 +0200 | [diff] [blame] | 40 | /** |
| 41 | * Sets the type of the external temperature sensor used |
| 42 | * |
| 43 | * @param type Type of sensor to set |
| 44 | */ |
Nico Huber | 6d6a2ac | 2013-07-12 14:35:00 +0200 | [diff] [blame] | 45 | static void it8516e_set_systemp_type(const u8 type) |
| 46 | { |
| 47 | if (send_ec_command(IT8516E_CMD_SET_SYSTEMP_TYPE)) |
| 48 | return; |
| 49 | send_ec_data(type); |
| 50 | } |
| 51 | |
Nico Huber | 1f9f678 | 2013-07-18 11:50:59 +0200 | [diff] [blame] | 52 | /** |
| 53 | * Sets the operating mode of a fan |
| 54 | * |
| 55 | * @param idx Selects the fan; 0: CPU, 1: System |
| 56 | * @param mode Mode to set |
| 57 | */ |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 58 | static void it8516e_set_fan_mode(const u8 idx, const u8 mode) |
| 59 | { |
| 60 | if (send_ec_command(IT8516E_CMD_SET_FAN_MODE)) |
| 61 | return; |
| 62 | if (send_ec_data(idx)) |
| 63 | return; |
| 64 | send_ec_data(mode); |
| 65 | } |
| 66 | |
Nico Huber | 1f9f678 | 2013-07-18 11:50:59 +0200 | [diff] [blame] | 67 | /** |
| 68 | * Sets the PWM rate of a fan in IT8516E_MODE_PWM |
| 69 | * |
| 70 | * @param idx Selects the fan; 0: CPU, 1: System |
| 71 | * @param pwm PWM rate measured in 255ths |
| 72 | */ |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 73 | static void it8516e_set_fan_pwm(const u8 idx, const u8 pwm) |
| 74 | { |
| 75 | if (send_ec_command(IT8516E_CMD_SET_FAN_PWM)) |
| 76 | return; |
| 77 | if (send_ec_data(idx)) |
| 78 | return; |
| 79 | send_ec_data(pwm); |
| 80 | } |
| 81 | |
Nico Huber | 1f9f678 | 2013-07-18 11:50:59 +0200 | [diff] [blame] | 82 | /** |
| 83 | * Sets the target speed in RPM for a fan in IT8516E_MODE_SPEED |
| 84 | * |
| 85 | * @param idx Selects the fan; 0: CPU, 1: System |
| 86 | * @param speed Speed in RPM |
| 87 | */ |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 88 | static void it8516e_set_fan_speed(const u8 idx, const u16 speed) |
| 89 | { |
| 90 | if (send_ec_command(IT8516E_CMD_SET_FAN_SPEED)) |
| 91 | return; |
| 92 | if (send_ec_data(idx)) |
| 93 | return; |
| 94 | if (send_ec_data(speed & 0xff)) |
| 95 | return; |
| 96 | send_ec_data(speed >> 8); |
| 97 | } |
| 98 | |
Nico Huber | 1f9f678 | 2013-07-18 11:50:59 +0200 | [diff] [blame] | 99 | /** |
| 100 | * Sets the target temperature for a fan in IT8516E_MODE_THERMAL |
| 101 | * |
| 102 | * @param idx Selects the fan; 0: CPU, 1: System |
| 103 | * @param temp Temperature in 64ths degree C |
| 104 | */ |
Nico Huber | 260c33b | 2013-07-18 11:27:30 +0200 | [diff] [blame] | 105 | static void it8516e_set_fan_temperature(const u8 idx, const u16 temp) |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 106 | { |
| 107 | if (send_ec_command(IT8516E_CMD_SET_FAN_TEMP)) |
| 108 | return; |
| 109 | if (send_ec_data(idx)) |
| 110 | return; |
Nico Huber | 260c33b | 2013-07-18 11:27:30 +0200 | [diff] [blame] | 111 | if (send_ec_data(temp & 0xff)) |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 112 | return; |
Nico Huber | 260c33b | 2013-07-18 11:27:30 +0200 | [diff] [blame] | 113 | send_ec_data(temp >> 8); |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Nico Huber | 1f9f678 | 2013-07-18 11:50:59 +0200 | [diff] [blame] | 116 | /** |
| 117 | * Sets the minimum and maximum PWM rate of a fan in IT8516E_MODE_THERMAL |
| 118 | * |
| 119 | * @param idx Selects the fan; 0: CPU, 1: System |
| 120 | * @param min Minimum PWM rate in % |
| 121 | * @param max Maximum PWM rate in % |
| 122 | */ |
Nico Huber | 9ce71b3 | 2013-07-12 14:43:11 +0200 | [diff] [blame] | 123 | static void it8516e_set_fan_limits(const u8 idx, const u8 min, const u8 max) |
| 124 | { |
| 125 | if (send_ec_command(IT8516E_CMD_SET_FAN_LIMITS)) |
| 126 | return; |
| 127 | if (send_ec_data(idx)) |
| 128 | return; |
| 129 | if (send_ec_data(min)) |
| 130 | return; |
| 131 | send_ec_data(max); |
| 132 | } |
| 133 | |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 134 | static void it8516e_set_fan_from_options(const config_t *const config, |
| 135 | const u8 fan_idx) |
| 136 | { |
| 137 | static char fanX_mode[] = "fanX_mode"; |
| 138 | static char fanX_target[] = "fanX_target"; |
Nico Huber | 9ce71b3 | 2013-07-12 14:43:11 +0200 | [diff] [blame] | 139 | static char fanX_min[] = "fanX_min"; |
| 140 | static char fanX_max[] = "fanX_max"; |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 141 | |
Nico Huber | 9ce71b3 | 2013-07-12 14:43:11 +0200 | [diff] [blame] | 142 | u8 fan_mode = config->default_fan_mode[fan_idx]; |
| 143 | u16 fan_target = config->default_fan_target[fan_idx]; |
| 144 | u8 fan_min = config->default_fan_min[fan_idx]; |
| 145 | u8 fan_max = config->default_fan_max[fan_idx]; |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 146 | |
| 147 | fanX_mode[3] = '1' + fan_idx; |
| 148 | get_option(&fan_mode, fanX_mode); |
| 149 | if (!fan_mode) |
| 150 | fan_mode = IT8516E_MODE_AUTO; |
| 151 | it8516e_set_fan_mode(fan_idx, fan_mode); |
| 152 | |
| 153 | fanX_target[3] = '1' + fan_idx; |
| 154 | get_option(&fan_target, fanX_target); |
| 155 | switch (fan_mode) { |
| 156 | case IT8516E_MODE_AUTO: |
| 157 | printk(BIOS_DEBUG, |
| 158 | "Setting it8516e fan%d " |
| 159 | "control to auto.\n", |
| 160 | fan_idx + 1); |
| 161 | break; |
| 162 | case IT8516E_MODE_PWM: |
| 163 | printk(BIOS_DEBUG, |
| 164 | "Setting it8516e fan%d " |
| 165 | "control to %d%% PWM.\n", |
| 166 | fan_idx + 1, fan_target); |
Nico Huber | ca4f073 | 2013-07-18 12:27:00 +0200 | [diff] [blame] | 167 | if (fan_target > 100) /* Constrain to 100% */ |
| 168 | fan_target = 100; |
Nico Huber | 942b6c2 | 2013-07-12 14:40:23 +0200 | [diff] [blame] | 169 | it8516e_set_fan_pwm(fan_idx, (fan_target * 255) / 100); |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 170 | break; |
| 171 | case IT8516E_MODE_SPEED: |
| 172 | printk(BIOS_DEBUG, |
| 173 | "Setting it8516e fan%d " |
| 174 | "control to %d RPMs.\n", |
| 175 | fan_idx + 1, fan_target); |
| 176 | it8516e_set_fan_speed(fan_idx, fan_target); |
| 177 | break; |
| 178 | case IT8516E_MODE_THERMAL: |
| 179 | printk(BIOS_DEBUG, |
Nico Huber | 9ce71b3 | 2013-07-12 14:43:11 +0200 | [diff] [blame] | 180 | "Setting it8516e fan%d control to %d C.\n", |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 181 | fan_idx + 1, fan_target); |
Nico Huber | ca4f073 | 2013-07-18 12:27:00 +0200 | [diff] [blame] | 182 | if (fan_target > 1024) /* Constrain to 1K */ |
| 183 | fan_target = 1024; |
Nico Huber | 260c33b | 2013-07-18 11:27:30 +0200 | [diff] [blame] | 184 | it8516e_set_fan_temperature(fan_idx, fan_target * 64); |
Nico Huber | 9ce71b3 | 2013-07-12 14:43:11 +0200 | [diff] [blame] | 185 | |
| 186 | fanX_min[3] = '1' + fan_idx; |
| 187 | fanX_max[3] = '1' + fan_idx; |
| 188 | get_option(&fan_min, fanX_min); |
| 189 | get_option(&fan_max, fanX_max); |
| 190 | |
| 191 | if (!fan_max || fan_max > 100) /* Constrain fan_max to 100% */ |
| 192 | fan_max = 100; |
| 193 | if (fan_min >= 100) /* Constrain fan_min to 99% */ |
| 194 | fan_min = 99; |
| 195 | if (fan_max <= fan_min) /* If fan_min is the higher of the two, |
| 196 | it's safer for the hardware to keep |
| 197 | its value. Therefore, update fan_max. */ |
| 198 | fan_max = fan_min + 1; |
| 199 | |
| 200 | printk(BIOS_DEBUG, |
| 201 | "Setting it8516e fan%d limits to %d%% - %d%% PWM.\n", |
| 202 | fan_idx + 1, fan_min, fan_max); |
| 203 | it8516e_set_fan_limits(fan_idx, fan_min, fan_max); |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 208 | static void it8516e_pm2_init(struct device *dev) |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 209 | { |
| 210 | const config_t *const config = dev->chip_info; |
| 211 | |
| 212 | /* TODO: Set frequency / divider? */ |
| 213 | |
| 214 | ec_set_ports(find_resource(dev, PNP_IDX_IO1)->base, |
| 215 | find_resource(dev, PNP_IDX_IO0)->base); |
| 216 | |
Nico Huber | 6d6a2ac | 2013-07-12 14:35:00 +0200 | [diff] [blame] | 217 | u8 systemp_type = config->default_systemp; |
| 218 | get_option(&systemp_type, "systemp_type"); |
| 219 | if (systemp_type >= IT8516E_SYSTEMP_LASTPLUSONE) |
| 220 | systemp_type = IT8516E_SYSTEMP_NONE; |
| 221 | it8516e_set_systemp_type(systemp_type); |
| 222 | |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 223 | it8516e_set_fan_from_options(config, 0); |
| 224 | it8516e_set_fan_from_options(config, 1); |
| 225 | } |
| 226 | |
| 227 | static struct device_operations it8516e_pm2_ops = { |
Nico Huber | 9cb0941 | 2013-06-15 15:30:19 +0200 | [diff] [blame] | 228 | .read_resources = pnp_read_resources, |
| 229 | .set_resources = pnp_set_resources, |
| 230 | .enable_resources = pnp_enable_resources, |
| 231 | .enable = pnp_enable, |
| 232 | .init = it8516e_pm2_init |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | static struct pnp_info it8516e_dev_infos[] = { |
Samuel Holland | 7daac91 | 2017-06-06 22:55:01 -0500 | [diff] [blame] | 236 | { NULL, IT8516E_LDN_UART1, PNP_IO0 | PNP_IRQ0, 0x07f8, }, |
| 237 | { NULL, IT8516E_LDN_UART2, PNP_IO0 | PNP_IRQ0, 0x07f8, }, |
| 238 | { NULL, IT8516E_LDN_SWUC, PNP_IO0 | PNP_IRQ0, 0xffe0, }, |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 239 | { NULL, IT8516E_LDN_MOUSE, PNP_IRQ0, }, |
Samuel Holland | 7daac91 | 2017-06-06 22:55:01 -0500 | [diff] [blame] | 240 | { NULL, IT8516E_LDN_KBD, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, }, |
| 241 | { NULL, IT8516E_LDN_SMFI, PNP_IO0 | PNP_IRQ0, 0xfff0, }, |
| 242 | { NULL, IT8516E_LDN_BRAM, PNP_IO0 | PNP_IO1, 0xfffe, 0xfffe, }, |
| 243 | { NULL, IT8516E_LDN_PM1, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, }, |
| 244 | { &it8516e_pm2_ops, IT8516E_LDN_PM2, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, }, |
| 245 | { NULL, IT8516E_LDN_PM3, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, }, |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 246 | }; |
| 247 | |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 248 | static void it8516e_enable(struct device *dev) |
Nico Huber | a53266b | 2013-05-02 15:26:08 +0200 | [diff] [blame] | 249 | { |
| 250 | pnp_enable_devices(dev, &pnp_ops, |
| 251 | ARRAY_SIZE(it8516e_dev_infos), it8516e_dev_infos); |
| 252 | } |
| 253 | |
| 254 | const struct chip_operations ec_kontron_it8516e_ops = { |
| 255 | CHIP_NAME("Kontron (Fintec/ITE) IT8516E EC") |
| 256 | .enable_dev = it8516e_enable |
| 257 | }; |