blob: d3e776a1c212e7d0df9a441d956391bfbfa1df13 [file] [log] [blame]
Sven Schnelleca682972012-07-03 21:33:47 +02001/*
2 * This file is part of the coreboot project.
3 *
Timothy Pearsond3e31be2015-02-21 01:45:35 -06004 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Sven Schnelleca682972012-07-03 21:33:47 +02005 * Copyright (C) 2012 Advanced Micro Devices, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Sven Schnelleca682972012-07-03 21:33:47 +020015 */
16
17#include <stdint.h>
18#include <arch/cpu.h>
19#include <console/console.h>
20#include <device/device.h>
21#include "w83793.h"
22#include <device/smbus.h>
23#include "chip.h"
24
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110025static int w83793_fan_limit(struct device *dev, int fan, uint16_t limit)
Sven Schnelleca682972012-07-03 21:33:47 +020026{
27 return smbus_write_byte(dev, 0x90 + fan * 2, limit >> 8) ||
28 smbus_write_byte(dev, 0x91 + fan * 2, limit & 0xff);
29}
30
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110031static int w83793_bank(struct device *dev, int bank)
Sven Schnelleca682972012-07-03 21:33:47 +020032{
33 return smbus_write_byte(dev, 0, bank);
34}
35
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110036static int w83793_td_level(struct device *dev, int fan, const char *level)
Sven Schnelleca682972012-07-03 21:33:47 +020037{
38 fan *= 0x10;
39
40 smbus_write_byte(dev, 0x30 + fan, level[0]);
41 smbus_write_byte(dev, 0x31 + fan, level[1]);
42 smbus_write_byte(dev, 0x32 + fan, level[2]);
43 smbus_write_byte(dev, 0x33 + fan, level[3]);
44 smbus_write_byte(dev, 0x34 + fan, level[4]);
45 smbus_write_byte(dev, 0x35 + fan, level[5]);
46 smbus_write_byte(dev, 0x36 + fan, level[6]);
47 return 0;
48}
49
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110050static int w83793_tr_level(struct device *dev, int fan, const char *level)
Sven Schnelleca682972012-07-03 21:33:47 +020051{
52 fan *= 0x10;
53
54 smbus_write_byte(dev, 0x70 + fan, level[0]);
55 smbus_write_byte(dev, 0x71 + fan, level[1]);
56 smbus_write_byte(dev, 0x72 + fan, level[2]);
57 smbus_write_byte(dev, 0x73 + fan, level[3]);
58 smbus_write_byte(dev, 0x74 + fan, level[4]);
59 smbus_write_byte(dev, 0x75 + fan, level[5]);
60 smbus_write_byte(dev, 0x76 + fan, level[6]);
61 return 0;
62}
63
64
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110065static int w83793_td_fan_level(struct device *dev, int fan, const char *level)
Sven Schnelleca682972012-07-03 21:33:47 +020066{
67 fan *= 0x10;
68
69 smbus_write_byte(dev, 0x38 + fan, level[0]);
70 smbus_write_byte(dev, 0x39 + fan, level[1]);
71 smbus_write_byte(dev, 0x3a + fan, level[2]);
72 smbus_write_byte(dev, 0x3b + fan, level[3]);
73 smbus_write_byte(dev, 0x3c + fan, level[4]);
74 smbus_write_byte(dev, 0x3d + fan, level[5]);
75 smbus_write_byte(dev, 0x3e + fan, level[6]);
76 return 0;
77}
78
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110079static int w83793_tr_fan_level(struct device *dev, int fan, const char *level)
Sven Schnelleca682972012-07-03 21:33:47 +020080{
81 fan *= 0x10;
82
83 smbus_write_byte(dev, 0x78 + fan, level[0]);
84 smbus_write_byte(dev, 0x79 + fan, level[1]);
85 smbus_write_byte(dev, 0x7a + fan, level[2]);
86 smbus_write_byte(dev, 0x7b + fan, level[3]);
87 smbus_write_byte(dev, 0x7c + fan, level[4]);
88 smbus_write_byte(dev, 0x7d + fan, level[5]);
89 smbus_write_byte(dev, 0x7e + fan, level[6]);
90 return 0;
91}
92
Timothy Pearsond3e31be2015-02-21 01:45:35 -060093static uint8_t millivolts_to_limit_value_type1(int millivolts)
94{
95 /* Datasheet v1.4 page 64 (VCoreA, VCoreB, Vtt Limit) */
96 return ((millivolts * 125) / 1000);
97}
98
99static uint8_t millivolts_to_limit_value_type2(int millivolts)
100{
101 /* Datasheet v1.4 page 64 (VSEN1, VEN2, VSEN3) */
102 return ((millivolts * 625) / 10000);
103}
104
105static uint8_t millivolts_to_limit_value_type3(int millivolts)
106{
107 /* Datasheet v1.4 page 64 (5VDD, 5VSB) */
108 return ((((millivolts * 10) + 1500) * 417) / 10000);
109}
110
111static uint8_t fan_pct_to_cfg_val(uint8_t percent)
112{
113 uint8_t cfg = (((unsigned int)percent * 10000) / 15873);
114 if (cfg > 0x3f)
115 cfg = 0x3f;
116 return cfg;
117}
Sven Schnelleca682972012-07-03 21:33:47 +0200118
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100119static void w83793_init(struct device *dev)
Sven Schnelleca682972012-07-03 21:33:47 +0200120{
121 struct drivers_i2c_w83793_config *config = dev->chip_info;
122 uint16_t id;
123 int i;
124
125 if (w83793_bank(dev, 0))
126 printk(BIOS_ERR, "%s: failed\n", __func__);
127
128 if (!config)
129 return;
130
131 id = smbus_read_byte(dev, 0x0d);
132 w83793_bank(dev, 0x80);
133 id |= smbus_read_byte(dev, 0x0d) << 8;
134 printk(BIOS_ERR, "ID: %04x\n", id);
135
136 /* reset configuration */
137 smbus_write_byte(dev, 0x40, 0x80);
138 smbus_write_byte(dev, 0x51, 0x06);
139
140 /* Multi function control */
141 smbus_write_byte(dev, 0x58, config->mfc);
142
143 /* FANIN_Ctrl */
144 smbus_write_byte(dev, 0x5c, config->fanin);
145
Timothy Pearsond3e31be2015-02-21 01:45:35 -0600146 /* FANIN_Sel */
147 smbus_write_byte(dev, 0x5d, config->fanin_sel);
148
149 /* Temperature source */
150 smbus_write_byte(dev, 0x5e, config->td_mode_select);
Sven Schnelleca682972012-07-03 21:33:47 +0200151 /* TR monitor enable */
152 smbus_write_byte(dev, 0x5f, config->tr_enable);
153 /* PECI Agent configuration */
154 smbus_write_byte(dev, 0xd0, config->peci_agent_conf);
155 /* TCase */
156 smbus_write_byte(dev, 0xd1, config->tcase0);
157 smbus_write_byte(dev, 0xd2, config->tcase1);
158 smbus_write_byte(dev, 0xd3, config->tcase2);
159 smbus_write_byte(dev, 0xd4, config->tcase3);
160 /* PECI Reportstyle */
161 smbus_write_byte(dev, 0xd5, 0x00);
162
Timothy Pearsond3e31be2015-02-21 01:45:35 -0600163 /* Voltage high/low limits */
164 smbus_write_byte(dev, 0x60, millivolts_to_limit_value_type1(config->vcorea_high_limit_mv));
165 smbus_write_byte(dev, 0x61, millivolts_to_limit_value_type1(config->vcorea_low_limit_mv));
166 smbus_write_byte(dev, 0x62, millivolts_to_limit_value_type1(config->vcoreb_high_limit_mv));
167 smbus_write_byte(dev, 0x63, millivolts_to_limit_value_type1(config->vcoreb_low_limit_mv));
168 smbus_write_byte(dev, 0x64, millivolts_to_limit_value_type1(config->vtt_high_limit_mv));
169 smbus_write_byte(dev, 0x65, millivolts_to_limit_value_type1(config->vtt_low_limit_mv));
170 smbus_write_byte(dev, 0x6a, millivolts_to_limit_value_type2(config->vsen1_high_limit_mv));
171 smbus_write_byte(dev, 0x6b, millivolts_to_limit_value_type2(config->vsen1_low_limit_mv));
172 smbus_write_byte(dev, 0x6c, millivolts_to_limit_value_type2(config->vsen2_high_limit_mv));
173 smbus_write_byte(dev, 0x6d, millivolts_to_limit_value_type2(config->vsen2_low_limit_mv));
174 smbus_write_byte(dev, 0x6e, millivolts_to_limit_value_type2(config->vsen3_high_limit_mv));
175 smbus_write_byte(dev, 0x6f, millivolts_to_limit_value_type2(config->vsen3_low_limit_mv));
176 smbus_write_byte(dev, 0x70, millivolts_to_limit_value_type1(config->vsen4_high_limit_mv));
177 smbus_write_byte(dev, 0x71, millivolts_to_limit_value_type1(config->vsen4_low_limit_mv));
178 smbus_write_byte(dev, 0x72, millivolts_to_limit_value_type3(config->vdd_high_limit_mv));
179 smbus_write_byte(dev, 0x73, millivolts_to_limit_value_type3(config->vdd_low_limit_mv));
180 smbus_write_byte(dev, 0x74, millivolts_to_limit_value_type3(config->vsb_high_limit_mv));
181 smbus_write_byte(dev, 0x75, millivolts_to_limit_value_type3(config->vsb_low_limit_mv));
182 smbus_write_byte(dev, 0x76, millivolts_to_limit_value_type2(config->vbat_high_limit_mv));
183 smbus_write_byte(dev, 0x77, millivolts_to_limit_value_type2(config->vbat_low_limit_mv));
184
185 /* Temperature high/low limits */
186 smbus_write_byte(dev, 0x78, config->td1_critical_temperature);
187 smbus_write_byte(dev, 0x79, config->td1_critical_hysteresis);
188 smbus_write_byte(dev, 0x7a, config->td1_warning_temperature);
189 smbus_write_byte(dev, 0x7b, config->td1_warning_hysteresis);
190 smbus_write_byte(dev, 0x7c, config->td2_critical_temperature);
191 smbus_write_byte(dev, 0x7d, config->td2_critical_hysteresis);
192 smbus_write_byte(dev, 0x7e, config->td2_warning_temperature);
193 smbus_write_byte(dev, 0x7f, config->td2_warning_hysteresis);
194 smbus_write_byte(dev, 0x80, config->td3_critical_temperature);
195 smbus_write_byte(dev, 0x81, config->td3_critical_hysteresis);
196 smbus_write_byte(dev, 0x82, config->td3_warning_temperature);
197 smbus_write_byte(dev, 0x83, config->td3_warning_hysteresis);
198 smbus_write_byte(dev, 0x84, config->td4_critical_temperature);
199 smbus_write_byte(dev, 0x85, config->td4_critical_hysteresis);
200 smbus_write_byte(dev, 0x86, config->td4_warning_temperature);
201 smbus_write_byte(dev, 0x87, config->td4_warning_hysteresis);
202 smbus_write_byte(dev, 0x88, config->tr1_critical_temperature);
203 smbus_write_byte(dev, 0x89, config->tr1_critical_hysteresis);
204 smbus_write_byte(dev, 0x8a, config->tr1_warning_temperature);
205 smbus_write_byte(dev, 0x8b, config->tr1_warning_hysteresis);
206 smbus_write_byte(dev, 0x8c, config->tr2_critical_temperature);
207 smbus_write_byte(dev, 0x8d, config->tr2_critical_hysteresis);
208 smbus_write_byte(dev, 0x8e, config->tr2_warning_temperature);
209 smbus_write_byte(dev, 0x8f, config->tr2_warning_hysteresis);
210
211 /* Set minimum FAN speeds before alarms will be set */
212 for (i = 0; i < config->first_valid_fan_number; i++)
213 w83793_fan_limit(dev, i, 0x0);
214 for (i = config->first_valid_fan_number; i < 12; i++)
Sven Schnelleca682972012-07-03 21:33:47 +0200215 w83793_fan_limit(dev, i, 0x768);
216
217 /* Fan output style control */
Timothy Pearsond3e31be2015-02-21 01:45:35 -0600218 smbus_write_byte(dev, 0xb0, config->fanctrl1);
219 smbus_write_byte(dev, 0xb1, config->fanctrl2);
Sven Schnelleca682972012-07-03 21:33:47 +0200220
221 /* FAN Uptime */
222 smbus_write_byte(dev, 0xc3, 0x02);
223
224 /* FAN downtime */
225 smbus_write_byte(dev, 0xc4, 0x03);
226
227 /* ALL FAN critical temperature */
228 smbus_write_byte(dev, 0xc5, config->critical_temperature);
229
230 /* Temperature offset */
231 smbus_write_byte(dev, 0xa8, 0xf9);
232 smbus_write_byte(dev, 0xa9, 0xf9);
233 smbus_write_byte(dev, 0xaa, 0xf9);
234 smbus_write_byte(dev, 0xab, 0xf9);
235
Timothy Pearsond3e31be2015-02-21 01:45:35 -0600236 /* Default FAN speed */
237 smbus_write_byte(dev, 0xb2, fan_pct_to_cfg_val(config->default_speed));
238
239 /* Manual FAN speeds */
240 smbus_write_byte(dev, 0xb3, fan_pct_to_cfg_val(config->fan1_duty));
241 smbus_write_byte(dev, 0xb4, fan_pct_to_cfg_val(config->fan2_duty));
242 smbus_write_byte(dev, 0xb5, fan_pct_to_cfg_val(config->fan3_duty));
243 smbus_write_byte(dev, 0xb6, fan_pct_to_cfg_val(config->fan4_duty));
244 smbus_write_byte(dev, 0xb7, fan_pct_to_cfg_val(config->fan5_duty));
245 smbus_write_byte(dev, 0xb8, fan_pct_to_cfg_val(config->fan6_duty));
246 smbus_write_byte(dev, 0xb9, fan_pct_to_cfg_val(config->fan7_duty));
247 smbus_write_byte(dev, 0xba, fan_pct_to_cfg_val(config->fan8_duty));
248
Sven Schnelleca682972012-07-03 21:33:47 +0200249 /* BANK 2 */
250 smbus_write_byte(dev, 0x00, 0x02);
251
252 /* TD FAN select */
253 smbus_write_byte(dev, 0x01, config->td1_fan_select);
254 smbus_write_byte(dev, 0x02, config->td2_fan_select);
255 smbus_write_byte(dev, 0x03, config->td3_fan_select);
256 smbus_write_byte(dev, 0x04, config->td4_fan_select);
257
258 smbus_write_byte(dev, 0x05, config->tr1_fan_select);
259 smbus_write_byte(dev, 0x06, config->tr2_fan_select);
260
261 /* FAN control mode */
262 smbus_write_byte(dev, 0x07, 0x00);
263
264 /* hysteresis tolerance */
265 smbus_write_byte(dev, 0x08, 0xaa);
266 smbus_write_byte(dev, 0x09, 0xaa);
267
268 /* FanNonStop */
Timothy Pearsond3e31be2015-02-21 01:45:35 -0600269 smbus_write_byte(dev, 0x18, fan_pct_to_cfg_val(config->fan1_nonstop));
270 smbus_write_byte(dev, 0x19, fan_pct_to_cfg_val(config->fan2_nonstop));
271 smbus_write_byte(dev, 0x1a, fan_pct_to_cfg_val(config->fan3_nonstop));
272 smbus_write_byte(dev, 0x1b, fan_pct_to_cfg_val(config->fan4_nonstop));
273 smbus_write_byte(dev, 0x1c, fan_pct_to_cfg_val(config->fan5_nonstop));
274 smbus_write_byte(dev, 0x1d, fan_pct_to_cfg_val(config->fan6_nonstop));
275 smbus_write_byte(dev, 0x1e, fan_pct_to_cfg_val(config->fan7_nonstop));
276 smbus_write_byte(dev, 0x1f, fan_pct_to_cfg_val(config->fan8_nonstop));
Sven Schnelleca682972012-07-03 21:33:47 +0200277
278 /* FanStart */
279 smbus_write_byte(dev, 0x20, 0x08);
280 smbus_write_byte(dev, 0x21, 0x08);
281 smbus_write_byte(dev, 0x22, 0x08);
282 smbus_write_byte(dev, 0x23, 0x08);
283 smbus_write_byte(dev, 0x24, 0x08);
284 smbus_write_byte(dev, 0x25, 0x08);
285 smbus_write_byte(dev, 0x26, 0x08);
286 smbus_write_byte(dev, 0x27, 0x08);
287
288 for (i = 0; i < 4; i++)
289 w83793_td_level(dev, i, (const char[]){ 0x32, 0x32, 0x32, 0x32, 0x37, 0x41, 0x4b });
290
291 for (i = 0; i < 2; i++)
292 w83793_tr_level(dev, i, (const char[]){ 0x1e, 0x23, 0x28, 0x2d, 0x32, 0x37, 0x3c });
293
294 for (i = 0; i < 4; i++)
295 w83793_td_fan_level(dev, i, (const char[]){ 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x26 });
296
297 for (i = 0; i < 2; i++)
298 w83793_tr_fan_level(dev, i, (const char[]){ 0x08, 0x0c, 0x10, 0x18, 0x20, 0x30, 0x38 });
299
300
301 smbus_write_byte(dev, 0x00, 0x00);
302
Sven Schnelleca682972012-07-03 21:33:47 +0200303 /* start monitoring operation */
304 smbus_write_byte(dev, 0x40, 0x09);
305
306}
307
Sven Schnelleca682972012-07-03 21:33:47 +0200308static struct device_operations w83793_operations = {
Edward O'Callaghan524625d2014-10-31 07:55:45 +1100309 .read_resources = DEVICE_NOOP,
310 .set_resources = DEVICE_NOOP,
311 .enable_resources = DEVICE_NOOP,
Sven Schnelleca682972012-07-03 21:33:47 +0200312 .init = w83793_init,
313};
314
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100315static void enable_dev(struct device *dev)
Sven Schnelleca682972012-07-03 21:33:47 +0200316{
317 dev->ops = &w83793_operations;
318}
319
320struct chip_operations drivers_i2c_w83793_ops = {
321 CHIP_NAME("Nuvoton W83793 Hardware Monitor")
322 .enable_dev = enable_dev,
323};