blob: 2cf4f90fa2c9481e8b43b0637158b3b2e901a5d5 [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,
43};
44
Nico Huber6d6a2ac2013-07-12 14:35:00 +020045static 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 Hubera53266b2013-05-02 15:26:08 +020052static void it8516e_set_fan_mode(const u8 idx, const u8 mode)
53{
54 if (send_ec_command(IT8516E_CMD_SET_FAN_MODE))
55 return;
56 if (send_ec_data(idx))
57 return;
58 send_ec_data(mode);
59}
60
61static void it8516e_set_fan_pwm(const u8 idx, const u8 pwm)
62{
63 if (send_ec_command(IT8516E_CMD_SET_FAN_PWM))
64 return;
65 if (send_ec_data(idx))
66 return;
67 send_ec_data(pwm);
68}
69
70static void it8516e_set_fan_speed(const u8 idx, const u16 speed)
71{
72 if (send_ec_command(IT8516E_CMD_SET_FAN_SPEED))
73 return;
74 if (send_ec_data(idx))
75 return;
76 if (send_ec_data(speed & 0xff))
77 return;
78 send_ec_data(speed >> 8);
79}
80
81static void it8516e_set_fan_temperature(const u8 idx, const u8 temp)
82{
83 if (send_ec_command(IT8516E_CMD_SET_FAN_TEMP))
84 return;
85 if (send_ec_data(idx))
86 return;
87 if (send_ec_data((temp << 6) & 0xff))
88 return;
89 send_ec_data(((temp << 6) >> 8) & 0xff);
90}
91
92static void it8516e_set_fan_from_options(const config_t *const config,
93 const u8 fan_idx)
94{
95 static char fanX_mode[] = "fanX_mode";
96 static char fanX_target[] = "fanX_target";
97
98 u8 fan_mode = config->default_fan_mode[fan_idx];
99 u16 fan_target = config->default_fan_target[fan_idx];
100
101 fanX_mode[3] = '1' + fan_idx;
102 get_option(&fan_mode, fanX_mode);
103 if (!fan_mode)
104 fan_mode = IT8516E_MODE_AUTO;
105 it8516e_set_fan_mode(fan_idx, fan_mode);
106
107 fanX_target[3] = '1' + fan_idx;
108 get_option(&fan_target, fanX_target);
109 switch (fan_mode) {
110 case IT8516E_MODE_AUTO:
111 printk(BIOS_DEBUG,
112 "Setting it8516e fan%d "
113 "control to auto.\n",
114 fan_idx + 1);
115 break;
116 case IT8516E_MODE_PWM:
117 printk(BIOS_DEBUG,
118 "Setting it8516e fan%d "
119 "control to %d%% PWM.\n",
120 fan_idx + 1, fan_target);
Nico Huber942b6c22013-07-12 14:40:23 +0200121 it8516e_set_fan_pwm(fan_idx, (fan_target * 255) / 100);
Nico Hubera53266b2013-05-02 15:26:08 +0200122 break;
123 case IT8516E_MODE_SPEED:
124 printk(BIOS_DEBUG,
125 "Setting it8516e fan%d "
126 "control to %d RPMs.\n",
127 fan_idx + 1, fan_target);
128 it8516e_set_fan_speed(fan_idx, fan_target);
129 break;
130 case IT8516E_MODE_THERMAL:
131 printk(BIOS_DEBUG,
132 "Setting it8516e fan%d "
133 "control to %d°C.\n",
134 fan_idx + 1, fan_target);
135 it8516e_set_fan_temperature(
136 fan_idx, fan_target);
137 break;
138 }
139}
140
141static void it8516e_pm2_init(const device_t dev)
142{
143 const config_t *const config = dev->chip_info;
144
145 /* TODO: Set frequency / divider? */
146
147 ec_set_ports(find_resource(dev, PNP_IDX_IO1)->base,
148 find_resource(dev, PNP_IDX_IO0)->base);
149
Nico Huber6d6a2ac2013-07-12 14:35:00 +0200150 u8 systemp_type = config->default_systemp;
151 get_option(&systemp_type, "systemp_type");
152 if (systemp_type >= IT8516E_SYSTEMP_LASTPLUSONE)
153 systemp_type = IT8516E_SYSTEMP_NONE;
154 it8516e_set_systemp_type(systemp_type);
155
Nico Hubera53266b2013-05-02 15:26:08 +0200156 it8516e_set_fan_from_options(config, 0);
157 it8516e_set_fan_from_options(config, 1);
158}
159
160static struct device_operations it8516e_pm2_ops = {
Nico Huber9cb09412013-06-15 15:30:19 +0200161 .read_resources = pnp_read_resources,
162 .set_resources = pnp_set_resources,
163 .enable_resources = pnp_enable_resources,
164 .enable = pnp_enable,
165 .init = it8516e_pm2_init
Nico Hubera53266b2013-05-02 15:26:08 +0200166};
167
168static struct pnp_info it8516e_dev_infos[] = {
169 { NULL, IT8516E_LDN_UART1, PNP_IO0 | PNP_IRQ0, { 0x07f8, }, },
170 { NULL, IT8516E_LDN_UART2, PNP_IO0 | PNP_IRQ0, { 0x07f8, }, },
171 { NULL, IT8516E_LDN_SWUC, PNP_IO0 | PNP_IRQ0, { 0xff7e0, }, },
172 { NULL, IT8516E_LDN_MOUSE, PNP_IRQ0, },
173 { NULL, IT8516E_LDN_KBD, PNP_IO0 | PNP_IO1 | PNP_IRQ0, { 0x07ff, }, { 0x07ff, }, },
174 { NULL, IT8516E_LDN_SMFI, PNP_IO0 | PNP_IRQ0, { 0xfff0, }, },
175 { NULL, IT8516E_LDN_BRAM, PNP_IO0 | PNP_IO1, { 0xfffe, }, { 0xfffe, }, },
176 { NULL, IT8516E_LDN_PM1, PNP_IO0 | PNP_IO1 | PNP_IRQ0, { 0x07ff, }, { 0x07ff, }, },
177 { &it8516e_pm2_ops, IT8516E_LDN_PM2, PNP_IO0 | PNP_IO1 | PNP_IRQ0, { 0x07ff, }, { 0x07ff, }, },
178 { NULL, IT8516E_LDN_PM3, PNP_IO0 | PNP_IO1 | PNP_IRQ0, { 0x07ff, }, { 0x07ff, }, },
179};
180
181static void it8516e_enable(const device_t dev)
182{
183 pnp_enable_devices(dev, &pnp_ops,
184 ARRAY_SIZE(it8516e_dev_infos), it8516e_dev_infos);
185}
186
187const struct chip_operations ec_kontron_it8516e_ops = {
188 CHIP_NAME("Kontron (Fintec/ITE) IT8516E EC")
189 .enable_dev = it8516e_enable
190};