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