blob: da37d66bfadfe936c768f3da610789f06c7e9852 [file] [log] [blame]
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +01001/*
2 * (C) Copyright 2008 Dmitry Rakhchev, EmCraft Systems, rda@emcraft.com
3 *
4 * Developed for DENX Software Engineering GmbH
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +01007 */
8
9#include <post.h>
10#include <common.h>
11
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010012/*
13 * SYSMON test
14 *
15 * This test performs the system hardware monitoring.
16 * The test passes when all the following voltages and temperatures
17 * are within allowed ranges:
18 *
Sascha Laue6aee00f2008-04-01 10:10:18 +020019 * Temperature -40 .. +90 C
20 * +5V +4.50 .. +5.50 V
21 * +5V standby +3.50 .. +5.50 V
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010022 *
23 * LCD backlight is not enabled if temperature values are not within
24 * allowed ranges (-30 .. + 80). The brightness of backlite can be
25 * controlled by setting "brightness" enviroment variable. Default value is 50%
26 *
27 * See the list of all parameters in the sysmon_table below
28 */
29
30#include <post.h>
31#include <watchdog.h>
32#include <i2c.h>
33
Yuri Tikhonov0f855a12008-03-18 13:27:57 +010034#if defined(CONFIG_VIDEO)
35#include <mb862xx.h>
36#endif
37
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038#if CONFIG_POST & CONFIG_SYS_POST_SYSMON
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010039
40DECLARE_GLOBAL_DATA_PTR;
41
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010042/* from dspic.c */
Sascha Lauef14ae412010-08-19 09:38:56 +020043extern int dspic_read(ushort reg, ushort *data);
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010044
Sascha Laue6aee00f2008-04-01 10:10:18 +020045#define REG_TEMPERATURE 0x12BC
46#define REG_VOLTAGE_5V 0x12CA
47#define REG_VOLTAGE_5V_STANDBY 0x12C6
48
49#define TEMPERATURE_MIN (-40) /* degr. C */
50#define TEMPERATURE_MAX (+90) /* degr. C */
51#define TEMPERATURE_DISPLAY_MIN (-35) /* degr. C */
Wolfgang Denk6adf61d2008-05-04 12:10:33 +020052#define TEMPERATURE_DISPLAY_MAX (+85) /* degr. C */
Sascha Laue6aee00f2008-04-01 10:10:18 +020053
54#define VOLTAGE_5V_MIN (+4500) /* mV */
55#define VOLTAGE_5V_MAX (+5500) /* mV */
56
57#define VOLTAGE_5V_STANDBY_MIN (+3500) /* mV */
58#define VOLTAGE_5V_STANDBY_MAX (+5500) /* mV */
59
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010060typedef struct sysmon_s sysmon_t;
61typedef struct sysmon_table_s sysmon_table_t;
62
Sascha Lauef14ae412010-08-19 09:38:56 +020063static void sysmon_dspic_init(sysmon_t *this);
64static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val);
65static int sysmon_dspic_read_sgn(sysmon_t *this, uint addr, int *val);
66static void sysmon_backlight_disable(sysmon_table_t *this);
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010067
Sascha Lauef14ae412010-08-19 09:38:56 +020068struct sysmon_s {
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010069 uchar chip;
70 void (*init)(sysmon_t *);
Sascha Lauef14ae412010-08-19 09:38:56 +020071 int (*read)(sysmon_t *, uint, int *);
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010072};
73
Sascha Lauef14ae412010-08-19 09:38:56 +020074static sysmon_t sysmon_dspic = {
75 CONFIG_SYS_I2C_DSPIC_IO_ADDR,
76 sysmon_dspic_init,
77 sysmon_dspic_read
78};
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010079
Sascha Lauef14ae412010-08-19 09:38:56 +020080static sysmon_t sysmon_dspic_sgn = {
81 CONFIG_SYS_I2C_DSPIC_IO_ADDR,
82 sysmon_dspic_init,
83 sysmon_dspic_read_sgn
84};
85
86static sysmon_t *sysmon_list[] = {
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010087 &sysmon_dspic,
88 NULL
89};
90
Sascha Lauef14ae412010-08-19 09:38:56 +020091struct sysmon_table_s {
92 char *name;
93 char *unit_name;
94 sysmon_t *sysmon;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010095 void (*exec_before)(sysmon_table_t *);
96 void (*exec_after)(sysmon_table_t *);
97
98 int unit_precision;
99 int unit_div;
100 int unit_min;
101 int unit_max;
102 uint val_mask;
103 uint val_min;
104 uint val_max;
105 int val_valid;
106 uint val_min_alt;
107 uint val_max_alt;
108 int val_valid_alt;
109 uint addr;
110};
111
Sascha Lauef14ae412010-08-19 09:38:56 +0200112static sysmon_table_t sysmon_table[] = {
Sascha Laue6aee00f2008-04-01 10:10:18 +0200113 {
Sascha Lauef14ae412010-08-19 09:38:56 +0200114 "Temperature", " C", &sysmon_dspic, NULL, sysmon_backlight_disable,
115 1, 1, -32768, 32767, 0xFFFF,
116 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
117 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
118 REG_TEMPERATURE,
Sascha Laue6aee00f2008-04-01 10:10:18 +0200119 },
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100120
Sascha Laue6aee00f2008-04-01 10:10:18 +0200121 {
Sascha Lauef14ae412010-08-19 09:38:56 +0200122 "+ 5 V", "V", &sysmon_dspic, NULL, NULL,
123 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
124 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
125 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
126 REG_VOLTAGE_5V,
Sascha Laue6aee00f2008-04-01 10:10:18 +0200127 },
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100128
Sascha Laue6aee00f2008-04-01 10:10:18 +0200129 {
Sascha Lauef14ae412010-08-19 09:38:56 +0200130 "+ 5 V standby", "V", &sysmon_dspic, NULL, NULL,
131 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
132 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
133 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
134 REG_VOLTAGE_5V_STANDBY,
135 },
136
137 {
138 "Temperature", "°C", &sysmon_dspic_sgn, NULL, sysmon_backlight_disable,
139 1, 1, -32768, 32767, 0xFFFF,
140 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
141 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
142 REG_TEMPERATURE,
Sascha Laue6aee00f2008-04-01 10:10:18 +0200143 },
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100144};
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100145
Sascha Lauef14ae412010-08-19 09:38:56 +0200146int sysmon_init_f(void)
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100147{
Sascha Lauef14ae412010-08-19 09:38:56 +0200148 sysmon_t **l;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100149
150 for (l = sysmon_list; *l; l++)
151 (*l)->init(*l);
152
153 return 0;
154}
155
Sascha Lauef14ae412010-08-19 09:38:56 +0200156void sysmon_reloc(void)
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100157{
Peter Tyser80f73b92009-09-21 11:20:34 -0500158 /* Do nothing for now, sysmon_reloc() is required by the sysmon post */
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100159}
160
Sascha Lauef14ae412010-08-19 09:38:56 +0200161static char *sysmon_unit_value(sysmon_table_t *s, uint val)
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100162{
163 static char buf[32];
164 char *p, sign;
165 int decimal, frac;
166 int unit_val;
167
Wolfgang Denk7ed40112008-04-26 00:34:42 +0200168 unit_val = s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100169
170 if (val == -1)
171 return "I/O ERROR";
172
173 if (unit_val < 0) {
174 sign = '-';
175 unit_val = -unit_val;
Sascha Lauef14ae412010-08-19 09:38:56 +0200176 } else {
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100177 sign = '+';
Sascha Lauef14ae412010-08-19 09:38:56 +0200178 }
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100179
180 p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
181
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100182 frac = unit_val % s->unit_div;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100183 frac /= (s->unit_div / s->unit_precision);
184
185 decimal = s->unit_precision;
186
187 if (decimal != 1)
188 *p++ = '.';
189 for (decimal /= 10; decimal != 0; decimal /= 10)
190 *p++ = '0' + (frac / decimal) % 10;
191 strcpy(p, s->unit_name);
192
193 return buf;
194}
195
Sascha Lauef14ae412010-08-19 09:38:56 +0200196static void sysmon_dspic_init(sysmon_t *this)
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100197{
198}
199
Sascha Lauef14ae412010-08-19 09:38:56 +0200200static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val)
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100201{
Sascha Lauef14ae412010-08-19 09:38:56 +0200202 ushort data;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100203
Sascha Lauef14ae412010-08-19 09:38:56 +0200204 if (dspic_read(addr, &data) == 0){
205 /* To fit into the table range we should add 0x8000 */
206 *val = data + 0x8000;
207 return 0;
208 }
209
210 return -1;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100211}
212
Sascha Lauef14ae412010-08-19 09:38:56 +0200213static int sysmon_dspic_read_sgn(sysmon_t *this, uint addr, int *val)
214{
215 ushort data;
216
217 if (dspic_read(addr, &data) == 0){
218 /* To fit into the table range we should add 0x8000 */
219 *val = (signed short)data + 0x8000;
220 return 0;
221 }
222
223 return -1;
224}
225
226static void sysmon_backlight_disable(sysmon_table_t *this)
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100227{
Yuri Tikhonov0f855a12008-03-18 13:27:57 +0100228#if defined(CONFIG_VIDEO)
229 board_backlight_switch(this->val_valid_alt);
230#endif
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100231}
232
Sascha Lauef14ae412010-08-19 09:38:56 +0200233int sysmon_post_test(int flags)
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100234{
235 int res = 0;
236 sysmon_table_t * t;
237 int val;
238
Sascha Lauef14ae412010-08-19 09:38:56 +0200239 for (t = sysmon_table; t < sysmon_table + ARRAY_SIZE(sysmon_table); t++) {
240 t->val_valid = 1;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100241 if (t->exec_before)
242 t->exec_before(t);
243
Sascha Lauef14ae412010-08-19 09:38:56 +0200244 if (t->sysmon->read(t->sysmon, t->addr, &val) != 0) {
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100245 t->val_valid = 0;
246 t->val_valid_alt = 0;
Sascha Lauef14ae412010-08-19 09:38:56 +0200247 post_log(": read failed\n");
248 res = 1;
249 break;
250 }
251
252 if (t->val_valid != 0) {
253 t->val_valid = val >= t->val_min && val <= t->val_max;
254 t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100255 }
256
257 if (t->exec_after)
258 t->exec_after(t);
259
Sascha Lauef14ae412010-08-19 09:38:56 +0200260 if ((!t->val_valid) || (flags)) {
261 post_log("\n\t%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
262 post_log("allowed range");
263 post_log(" %-8s ..", sysmon_unit_value(t, t->val_min));
264 post_log(" %-8s", sysmon_unit_value(t, t->val_max));
265 post_log(" %s", t->val_valid ? "OK" : "FAIL");
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100266 }
267
Sascha Lauef14ae412010-08-19 09:38:56 +0200268 if (!t->val_valid) {
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100269 res = 1;
Sascha Lauef14ae412010-08-19 09:38:56 +0200270 break;
271 }
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100272 }
Sascha Lauef14ae412010-08-19 09:38:56 +0200273 post_log("\n");
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100274
275 return res;
276}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200277#endif /* CONFIG_POST & CONFIG_SYS_POST_SYSMON */