blob: 721412199d47400aec61e8272f06138cc6ce4097 [file] [log] [blame]
Nico Hubera53266b2013-05-02 15:26:08 +02001/*
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
30typedef struct ec_kontron_it8516e_config config_t;
31
32enum { /* EC commands */
Nico Huber6d6a2ac2013-07-12 14:35:00 +020033 IT8516E_CMD_SET_SYSTEMP_TYPE = 0x06,
34 IT8516E_CMD_GET_SYSTEMP_TYPE = 0x07,
Nico Hubera53266b2013-05-02 15:26:08 +020035 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 Huber9ce71b32013-07-12 14:43:11 +020043 IT8516E_CMD_SET_FAN_LIMITS = 0x1a,
Nico Hubera53266b2013-05-02 15:26:08 +020044};
45
Nico Huber1f9f6782013-07-18 11:50:59 +020046/**
47 * Sets the type of the external temperature sensor used
48 *
49 * @param type Type of sensor to set
50 */
Nico Huber6d6a2ac2013-07-12 14:35:00 +020051static void it8516e_set_systemp_type(const u8 type)
52{
53 if (send_ec_command(IT8516E_CMD_SET_SYSTEMP_TYPE))
54 return;
55 send_ec_data(type);
56}
57
Nico Huber1f9f6782013-07-18 11:50:59 +020058/**
59 * Sets the operating mode of a fan
60 *
61 * @param idx Selects the fan; 0: CPU, 1: System
62 * @param mode Mode to set
63 */
Nico Hubera53266b2013-05-02 15:26:08 +020064static void it8516e_set_fan_mode(const u8 idx, const u8 mode)
65{
66 if (send_ec_command(IT8516E_CMD_SET_FAN_MODE))
67 return;
68 if (send_ec_data(idx))
69 return;
70 send_ec_data(mode);
71}
72
Nico Huber1f9f6782013-07-18 11:50:59 +020073/**
74 * Sets the PWM rate of a fan in IT8516E_MODE_PWM
75 *
76 * @param idx Selects the fan; 0: CPU, 1: System
77 * @param pwm PWM rate measured in 255ths
78 */
Nico Hubera53266b2013-05-02 15:26:08 +020079static void it8516e_set_fan_pwm(const u8 idx, const u8 pwm)
80{
81 if (send_ec_command(IT8516E_CMD_SET_FAN_PWM))
82 return;
83 if (send_ec_data(idx))
84 return;
85 send_ec_data(pwm);
86}
87
Nico Huber1f9f6782013-07-18 11:50:59 +020088/**
89 * Sets the target speed in RPM for a fan in IT8516E_MODE_SPEED
90 *
91 * @param idx Selects the fan; 0: CPU, 1: System
92 * @param speed Speed in RPM
93 */
Nico Hubera53266b2013-05-02 15:26:08 +020094static void it8516e_set_fan_speed(const u8 idx, const u16 speed)
95{
96 if (send_ec_command(IT8516E_CMD_SET_FAN_SPEED))
97 return;
98 if (send_ec_data(idx))
99 return;
100 if (send_ec_data(speed & 0xff))
101 return;
102 send_ec_data(speed >> 8);
103}
104
Nico Huber1f9f6782013-07-18 11:50:59 +0200105/**
106 * Sets the target temperature for a fan in IT8516E_MODE_THERMAL
107 *
108 * @param idx Selects the fan; 0: CPU, 1: System
109 * @param temp Temperature in 64ths degree C
110 */
Nico Huber260c33b2013-07-18 11:27:30 +0200111static void it8516e_set_fan_temperature(const u8 idx, const u16 temp)
Nico Hubera53266b2013-05-02 15:26:08 +0200112{
113 if (send_ec_command(IT8516E_CMD_SET_FAN_TEMP))
114 return;
115 if (send_ec_data(idx))
116 return;
Nico Huber260c33b2013-07-18 11:27:30 +0200117 if (send_ec_data(temp & 0xff))
Nico Hubera53266b2013-05-02 15:26:08 +0200118 return;
Nico Huber260c33b2013-07-18 11:27:30 +0200119 send_ec_data(temp >> 8);
Nico Hubera53266b2013-05-02 15:26:08 +0200120}
121
Nico Huber1f9f6782013-07-18 11:50:59 +0200122/**
123 * Sets the minimum and maximum PWM rate of a fan in IT8516E_MODE_THERMAL
124 *
125 * @param idx Selects the fan; 0: CPU, 1: System
126 * @param min Minimum PWM rate in %
127 * @param max Maximum PWM rate in %
128 */
Nico Huber9ce71b32013-07-12 14:43:11 +0200129static void it8516e_set_fan_limits(const u8 idx, const u8 min, const u8 max)
130{
131 if (send_ec_command(IT8516E_CMD_SET_FAN_LIMITS))
132 return;
133 if (send_ec_data(idx))
134 return;
135 if (send_ec_data(min))
136 return;
137 send_ec_data(max);
138}
139
Nico Hubera53266b2013-05-02 15:26:08 +0200140static void it8516e_set_fan_from_options(const config_t *const config,
141 const u8 fan_idx)
142{
143 static char fanX_mode[] = "fanX_mode";
144 static char fanX_target[] = "fanX_target";
Nico Huber9ce71b32013-07-12 14:43:11 +0200145 static char fanX_min[] = "fanX_min";
146 static char fanX_max[] = "fanX_max";
Nico Hubera53266b2013-05-02 15:26:08 +0200147
Nico Huber9ce71b32013-07-12 14:43:11 +0200148 u8 fan_mode = config->default_fan_mode[fan_idx];
149 u16 fan_target = config->default_fan_target[fan_idx];
150 u8 fan_min = config->default_fan_min[fan_idx];
151 u8 fan_max = config->default_fan_max[fan_idx];
Nico Hubera53266b2013-05-02 15:26:08 +0200152
153 fanX_mode[3] = '1' + fan_idx;
154 get_option(&fan_mode, fanX_mode);
155 if (!fan_mode)
156 fan_mode = IT8516E_MODE_AUTO;
157 it8516e_set_fan_mode(fan_idx, fan_mode);
158
159 fanX_target[3] = '1' + fan_idx;
160 get_option(&fan_target, fanX_target);
161 switch (fan_mode) {
162 case IT8516E_MODE_AUTO:
163 printk(BIOS_DEBUG,
164 "Setting it8516e fan%d "
165 "control to auto.\n",
166 fan_idx + 1);
167 break;
168 case IT8516E_MODE_PWM:
169 printk(BIOS_DEBUG,
170 "Setting it8516e fan%d "
171 "control to %d%% PWM.\n",
172 fan_idx + 1, fan_target);
Nico Huber942b6c22013-07-12 14:40:23 +0200173 it8516e_set_fan_pwm(fan_idx, (fan_target * 255) / 100);
Nico Hubera53266b2013-05-02 15:26:08 +0200174 break;
175 case IT8516E_MODE_SPEED:
176 printk(BIOS_DEBUG,
177 "Setting it8516e fan%d "
178 "control to %d RPMs.\n",
179 fan_idx + 1, fan_target);
180 it8516e_set_fan_speed(fan_idx, fan_target);
181 break;
182 case IT8516E_MODE_THERMAL:
183 printk(BIOS_DEBUG,
Nico Huber9ce71b32013-07-12 14:43:11 +0200184 "Setting it8516e fan%d control to %d C.\n",
Nico Hubera53266b2013-05-02 15:26:08 +0200185 fan_idx + 1, fan_target);
Nico Huber260c33b2013-07-18 11:27:30 +0200186 it8516e_set_fan_temperature(fan_idx, fan_target * 64);
Nico Huber9ce71b32013-07-12 14:43:11 +0200187
188 fanX_min[3] = '1' + fan_idx;
189 fanX_max[3] = '1' + fan_idx;
190 get_option(&fan_min, fanX_min);
191 get_option(&fan_max, fanX_max);
192
193 if (!fan_max || fan_max > 100) /* Constrain fan_max to 100% */
194 fan_max = 100;
195 if (fan_min >= 100) /* Constrain fan_min to 99% */
196 fan_min = 99;
197 if (fan_max <= fan_min) /* If fan_min is the higher of the two,
198 it's safer for the hardware to keep
199 its value. Therefore, update fan_max. */
200 fan_max = fan_min + 1;
201
202 printk(BIOS_DEBUG,
203 "Setting it8516e fan%d limits to %d%% - %d%% PWM.\n",
204 fan_idx + 1, fan_min, fan_max);
205 it8516e_set_fan_limits(fan_idx, fan_min, fan_max);
Nico Hubera53266b2013-05-02 15:26:08 +0200206 break;
207 }
208}
209
210static void it8516e_pm2_init(const device_t dev)
211{
212 const config_t *const config = dev->chip_info;
213
214 /* TODO: Set frequency / divider? */
215
216 ec_set_ports(find_resource(dev, PNP_IDX_IO1)->base,
217 find_resource(dev, PNP_IDX_IO0)->base);
218
Nico Huber6d6a2ac2013-07-12 14:35:00 +0200219 u8 systemp_type = config->default_systemp;
220 get_option(&systemp_type, "systemp_type");
221 if (systemp_type >= IT8516E_SYSTEMP_LASTPLUSONE)
222 systemp_type = IT8516E_SYSTEMP_NONE;
223 it8516e_set_systemp_type(systemp_type);
224
Nico Hubera53266b2013-05-02 15:26:08 +0200225 it8516e_set_fan_from_options(config, 0);
226 it8516e_set_fan_from_options(config, 1);
227}
228
229static struct device_operations it8516e_pm2_ops = {
Nico Huber9cb09412013-06-15 15:30:19 +0200230 .read_resources = pnp_read_resources,
231 .set_resources = pnp_set_resources,
232 .enable_resources = pnp_enable_resources,
233 .enable = pnp_enable,
234 .init = it8516e_pm2_init
Nico Hubera53266b2013-05-02 15:26:08 +0200235};
236
237static struct pnp_info it8516e_dev_infos[] = {
238 { NULL, IT8516E_LDN_UART1, PNP_IO0 | PNP_IRQ0, { 0x07f8, }, },
239 { NULL, IT8516E_LDN_UART2, PNP_IO0 | PNP_IRQ0, { 0x07f8, }, },
240 { NULL, IT8516E_LDN_SWUC, PNP_IO0 | PNP_IRQ0, { 0xff7e0, }, },
241 { NULL, IT8516E_LDN_MOUSE, PNP_IRQ0, },
242 { NULL, IT8516E_LDN_KBD, PNP_IO0 | PNP_IO1 | PNP_IRQ0, { 0x07ff, }, { 0x07ff, }, },
243 { NULL, IT8516E_LDN_SMFI, PNP_IO0 | PNP_IRQ0, { 0xfff0, }, },
244 { NULL, IT8516E_LDN_BRAM, PNP_IO0 | PNP_IO1, { 0xfffe, }, { 0xfffe, }, },
245 { NULL, IT8516E_LDN_PM1, PNP_IO0 | PNP_IO1 | PNP_IRQ0, { 0x07ff, }, { 0x07ff, }, },
246 { &it8516e_pm2_ops, IT8516E_LDN_PM2, PNP_IO0 | PNP_IO1 | PNP_IRQ0, { 0x07ff, }, { 0x07ff, }, },
247 { NULL, IT8516E_LDN_PM3, PNP_IO0 | PNP_IO1 | PNP_IRQ0, { 0x07ff, }, { 0x07ff, }, },
248};
249
250static void it8516e_enable(const device_t dev)
251{
252 pnp_enable_devices(dev, &pnp_ops,
253 ARRAY_SIZE(it8516e_dev_infos), it8516e_dev_infos);
254}
255
256const struct chip_operations ec_kontron_it8516e_ops = {
257 CHIP_NAME("Kontron (Fintec/ITE) IT8516E EC")
258 .enable_dev = it8516e_enable
259};