blob: ca695b33785461999d09e96fef4f97c0c81bcfd4 [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.
Nico Hubera53266b2013-05-02 15:26:08 +020014 */
15
Nico Hubera53266b2013-05-02 15:26:08 +020016#include <console/console.h>
17#include <device/device.h>
18#include <device/pnp.h>
19#include <ec/acpi/ec.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020020#include <option.h>
Nico Hubera53266b2013-05-02 15:26:08 +020021
22#include "ec.h"
23#include "chip.h"
24
25typedef struct ec_kontron_it8516e_config config_t;
26
27enum { /* EC commands */
Nico Huber6d6a2ac2013-07-12 14:35:00 +020028 IT8516E_CMD_SET_SYSTEMP_TYPE = 0x06,
29 IT8516E_CMD_GET_SYSTEMP_TYPE = 0x07,
Nico Hubera53266b2013-05-02 15:26:08 +020030 IT8516E_CMD_GET_FAN_MODE = 0x10,
31 IT8516E_CMD_SET_FAN_MODE = 0x11,
32 IT8516E_CMD_GET_FAN_PWM = 0x12,
33 IT8516E_CMD_SET_FAN_PWM = 0x13,
34 IT8516E_CMD_GET_FAN_SPEED = 0x14,
35 IT8516E_CMD_SET_FAN_SPEED = 0x15,
36 IT8516E_CMD_GET_FAN_TEMP = 0x16,
37 IT8516E_CMD_SET_FAN_TEMP = 0x17,
Nico Huber9ce71b32013-07-12 14:43:11 +020038 IT8516E_CMD_SET_FAN_LIMITS = 0x1a,
Nico Hubera53266b2013-05-02 15:26:08 +020039};
40
Nico Huber1f9f6782013-07-18 11:50:59 +020041/**
42 * Sets the type of the external temperature sensor used
43 *
44 * @param type Type of sensor to set
45 */
Nico Huber6d6a2ac2013-07-12 14:35:00 +020046static 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 Huber1f9f6782013-07-18 11:50:59 +020053/**
54 * Sets the operating mode of a fan
55 *
56 * @param idx Selects the fan; 0: CPU, 1: System
57 * @param mode Mode to set
58 */
Nico Hubera53266b2013-05-02 15:26:08 +020059static void it8516e_set_fan_mode(const u8 idx, const u8 mode)
60{
61 if (send_ec_command(IT8516E_CMD_SET_FAN_MODE))
62 return;
63 if (send_ec_data(idx))
64 return;
65 send_ec_data(mode);
66}
67
Nico Huber1f9f6782013-07-18 11:50:59 +020068/**
69 * Sets the PWM rate of a fan in IT8516E_MODE_PWM
70 *
71 * @param idx Selects the fan; 0: CPU, 1: System
72 * @param pwm PWM rate measured in 255ths
73 */
Nico Hubera53266b2013-05-02 15:26:08 +020074static void it8516e_set_fan_pwm(const u8 idx, const u8 pwm)
75{
76 if (send_ec_command(IT8516E_CMD_SET_FAN_PWM))
77 return;
78 if (send_ec_data(idx))
79 return;
80 send_ec_data(pwm);
81}
82
Nico Huber1f9f6782013-07-18 11:50:59 +020083/**
84 * Sets the target speed in RPM for a fan in IT8516E_MODE_SPEED
85 *
86 * @param idx Selects the fan; 0: CPU, 1: System
87 * @param speed Speed in RPM
88 */
Nico Hubera53266b2013-05-02 15:26:08 +020089static void it8516e_set_fan_speed(const u8 idx, const u16 speed)
90{
91 if (send_ec_command(IT8516E_CMD_SET_FAN_SPEED))
92 return;
93 if (send_ec_data(idx))
94 return;
95 if (send_ec_data(speed & 0xff))
96 return;
97 send_ec_data(speed >> 8);
98}
99
Nico Huber1f9f6782013-07-18 11:50:59 +0200100/**
101 * Sets the target temperature for a fan in IT8516E_MODE_THERMAL
102 *
103 * @param idx Selects the fan; 0: CPU, 1: System
104 * @param temp Temperature in 64ths degree C
105 */
Nico Huber260c33b2013-07-18 11:27:30 +0200106static void it8516e_set_fan_temperature(const u8 idx, const u16 temp)
Nico Hubera53266b2013-05-02 15:26:08 +0200107{
108 if (send_ec_command(IT8516E_CMD_SET_FAN_TEMP))
109 return;
110 if (send_ec_data(idx))
111 return;
Nico Huber260c33b2013-07-18 11:27:30 +0200112 if (send_ec_data(temp & 0xff))
Nico Hubera53266b2013-05-02 15:26:08 +0200113 return;
Nico Huber260c33b2013-07-18 11:27:30 +0200114 send_ec_data(temp >> 8);
Nico Hubera53266b2013-05-02 15:26:08 +0200115}
116
Nico Huber1f9f6782013-07-18 11:50:59 +0200117/**
118 * Sets the minimum and maximum PWM rate of a fan in IT8516E_MODE_THERMAL
119 *
120 * @param idx Selects the fan; 0: CPU, 1: System
121 * @param min Minimum PWM rate in %
122 * @param max Maximum PWM rate in %
123 */
Nico Huber9ce71b32013-07-12 14:43:11 +0200124static void it8516e_set_fan_limits(const u8 idx, const u8 min, const u8 max)
125{
126 if (send_ec_command(IT8516E_CMD_SET_FAN_LIMITS))
127 return;
128 if (send_ec_data(idx))
129 return;
130 if (send_ec_data(min))
131 return;
132 send_ec_data(max);
133}
134
Nico Hubera53266b2013-05-02 15:26:08 +0200135static void it8516e_set_fan_from_options(const config_t *const config,
136 const u8 fan_idx)
137{
138 static char fanX_mode[] = "fanX_mode";
139 static char fanX_target[] = "fanX_target";
Nico Huber9ce71b32013-07-12 14:43:11 +0200140 static char fanX_min[] = "fanX_min";
141 static char fanX_max[] = "fanX_max";
Nico Hubera53266b2013-05-02 15:26:08 +0200142
Nico Huber9ce71b32013-07-12 14:43:11 +0200143 u8 fan_mode = config->default_fan_mode[fan_idx];
144 u16 fan_target = config->default_fan_target[fan_idx];
145 u8 fan_min = config->default_fan_min[fan_idx];
146 u8 fan_max = config->default_fan_max[fan_idx];
Nico Hubera53266b2013-05-02 15:26:08 +0200147
148 fanX_mode[3] = '1' + fan_idx;
149 get_option(&fan_mode, fanX_mode);
150 if (!fan_mode)
151 fan_mode = IT8516E_MODE_AUTO;
152 it8516e_set_fan_mode(fan_idx, fan_mode);
153
154 fanX_target[3] = '1' + fan_idx;
155 get_option(&fan_target, fanX_target);
156 switch (fan_mode) {
157 case IT8516E_MODE_AUTO:
158 printk(BIOS_DEBUG,
159 "Setting it8516e fan%d "
160 "control to auto.\n",
161 fan_idx + 1);
162 break;
163 case IT8516E_MODE_PWM:
164 printk(BIOS_DEBUG,
165 "Setting it8516e fan%d "
166 "control to %d%% PWM.\n",
167 fan_idx + 1, fan_target);
Nico Huberca4f0732013-07-18 12:27:00 +0200168 if (fan_target > 100) /* Constrain to 100% */
169 fan_target = 100;
Nico Huber942b6c22013-07-12 14:40:23 +0200170 it8516e_set_fan_pwm(fan_idx, (fan_target * 255) / 100);
Nico Hubera53266b2013-05-02 15:26:08 +0200171 break;
172 case IT8516E_MODE_SPEED:
173 printk(BIOS_DEBUG,
174 "Setting it8516e fan%d "
175 "control to %d RPMs.\n",
176 fan_idx + 1, fan_target);
177 it8516e_set_fan_speed(fan_idx, fan_target);
178 break;
179 case IT8516E_MODE_THERMAL:
180 printk(BIOS_DEBUG,
Nico Huber9ce71b32013-07-12 14:43:11 +0200181 "Setting it8516e fan%d control to %d C.\n",
Nico Hubera53266b2013-05-02 15:26:08 +0200182 fan_idx + 1, fan_target);
Nico Huberca4f0732013-07-18 12:27:00 +0200183 if (fan_target > 1024) /* Constrain to 1K */
184 fan_target = 1024;
Nico Huber260c33b2013-07-18 11:27:30 +0200185 it8516e_set_fan_temperature(fan_idx, fan_target * 64);
Nico Huber9ce71b32013-07-12 14:43:11 +0200186
187 fanX_min[3] = '1' + fan_idx;
188 fanX_max[3] = '1' + fan_idx;
189 get_option(&fan_min, fanX_min);
190 get_option(&fan_max, fanX_max);
191
192 if (!fan_max || fan_max > 100) /* Constrain fan_max to 100% */
193 fan_max = 100;
194 if (fan_min >= 100) /* Constrain fan_min to 99% */
195 fan_min = 99;
196 if (fan_max <= fan_min) /* If fan_min is the higher of the two,
197 it's safer for the hardware to keep
198 its value. Therefore, update fan_max. */
199 fan_max = fan_min + 1;
200
201 printk(BIOS_DEBUG,
202 "Setting it8516e fan%d limits to %d%% - %d%% PWM.\n",
203 fan_idx + 1, fan_min, fan_max);
204 it8516e_set_fan_limits(fan_idx, fan_min, fan_max);
Nico Hubera53266b2013-05-02 15:26:08 +0200205 break;
206 }
207}
208
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100209static void it8516e_pm2_init(struct device *dev)
Nico Hubera53266b2013-05-02 15:26:08 +0200210{
211 const config_t *const config = dev->chip_info;
212
213 /* TODO: Set frequency / divider? */
214
215 ec_set_ports(find_resource(dev, PNP_IDX_IO1)->base,
216 find_resource(dev, PNP_IDX_IO0)->base);
217
Nico Huber6d6a2ac2013-07-12 14:35:00 +0200218 u8 systemp_type = config->default_systemp;
219 get_option(&systemp_type, "systemp_type");
220 if (systemp_type >= IT8516E_SYSTEMP_LASTPLUSONE)
221 systemp_type = IT8516E_SYSTEMP_NONE;
222 it8516e_set_systemp_type(systemp_type);
223
Nico Hubera53266b2013-05-02 15:26:08 +0200224 it8516e_set_fan_from_options(config, 0);
225 it8516e_set_fan_from_options(config, 1);
226}
227
228static struct device_operations it8516e_pm2_ops = {
Nico Huber9cb09412013-06-15 15:30:19 +0200229 .read_resources = pnp_read_resources,
230 .set_resources = pnp_set_resources,
231 .enable_resources = pnp_enable_resources,
232 .enable = pnp_enable,
233 .init = it8516e_pm2_init
Nico Hubera53266b2013-05-02 15:26:08 +0200234};
235
236static struct pnp_info it8516e_dev_infos[] = {
Samuel Holland7daac912017-06-06 22:55:01 -0500237 { NULL, IT8516E_LDN_UART1, PNP_IO0 | PNP_IRQ0, 0x07f8, },
238 { NULL, IT8516E_LDN_UART2, PNP_IO0 | PNP_IRQ0, 0x07f8, },
239 { NULL, IT8516E_LDN_SWUC, PNP_IO0 | PNP_IRQ0, 0xffe0, },
Nico Hubera53266b2013-05-02 15:26:08 +0200240 { NULL, IT8516E_LDN_MOUSE, PNP_IRQ0, },
Samuel Holland7daac912017-06-06 22:55:01 -0500241 { NULL, IT8516E_LDN_KBD, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
242 { NULL, IT8516E_LDN_SMFI, PNP_IO0 | PNP_IRQ0, 0xfff0, },
243 { NULL, IT8516E_LDN_BRAM, PNP_IO0 | PNP_IO1, 0xfffe, 0xfffe, },
244 { NULL, IT8516E_LDN_PM1, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
245 { &it8516e_pm2_ops, IT8516E_LDN_PM2, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
246 { NULL, IT8516E_LDN_PM3, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
Nico Hubera53266b2013-05-02 15:26:08 +0200247};
248
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100249static void it8516e_enable(struct device *dev)
Nico Hubera53266b2013-05-02 15:26:08 +0200250{
251 pnp_enable_devices(dev, &pnp_ops,
252 ARRAY_SIZE(it8516e_dev_infos), it8516e_dev_infos);
253}
254
255const struct chip_operations ec_kontron_it8516e_ops = {
256 CHIP_NAME("Kontron (Fintec/ITE) IT8516E EC")
257 .enable_dev = it8516e_enable
258};