blob: aef5bd018a36f3e5ea69eb56991960fa3b223028 [file] [log] [blame]
Yuri Tikhonovc147d482008-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 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <post.h>
26#include <common.h>
27
Yuri Tikhonovc147d482008-02-04 14:10:42 +010028/*
29 * SYSMON test
30 *
31 * This test performs the system hardware monitoring.
32 * The test passes when all the following voltages and temperatures
33 * are within allowed ranges:
34 *
Sascha Laue86c0af72008-04-01 10:10:18 +020035 * Temperature -40 .. +90 C
36 * +5V +4.50 .. +5.50 V
37 * +5V standby +3.50 .. +5.50 V
Yuri Tikhonovc147d482008-02-04 14:10:42 +010038 *
39 * LCD backlight is not enabled if temperature values are not within
40 * allowed ranges (-30 .. + 80). The brightness of backlite can be
41 * controlled by setting "brightness" enviroment variable. Default value is 50%
42 *
43 * See the list of all parameters in the sysmon_table below
44 */
45
46#include <post.h>
47#include <watchdog.h>
48#include <i2c.h>
49
Yuri Tikhonov7151a342008-03-18 13:27:57 +010050#if defined(CONFIG_VIDEO)
51#include <mb862xx.h>
52#endif
53
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020054#if CONFIG_POST & CONFIG_SYS_POST_SYSMON
Yuri Tikhonovc147d482008-02-04 14:10:42 +010055
56DECLARE_GLOBAL_DATA_PTR;
57
Yuri Tikhonovc147d482008-02-04 14:10:42 +010058/* from dspic.c */
59extern int dspic_read(ushort reg);
Yuri Tikhonovc147d482008-02-04 14:10:42 +010060
61#define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off)
62
Sascha Laue86c0af72008-04-01 10:10:18 +020063#define REG_TEMPERATURE 0x12BC
64#define REG_VOLTAGE_5V 0x12CA
65#define REG_VOLTAGE_5V_STANDBY 0x12C6
66
67#define TEMPERATURE_MIN (-40) /* degr. C */
68#define TEMPERATURE_MAX (+90) /* degr. C */
69#define TEMPERATURE_DISPLAY_MIN (-35) /* degr. C */
Wolfgang Denk44b8ad42008-05-04 12:10:33 +020070#define TEMPERATURE_DISPLAY_MAX (+85) /* degr. C */
Sascha Laue86c0af72008-04-01 10:10:18 +020071
72#define VOLTAGE_5V_MIN (+4500) /* mV */
73#define VOLTAGE_5V_MAX (+5500) /* mV */
74
75#define VOLTAGE_5V_STANDBY_MIN (+3500) /* mV */
76#define VOLTAGE_5V_STANDBY_MAX (+5500) /* mV */
77
Yuri Tikhonovc147d482008-02-04 14:10:42 +010078typedef struct sysmon_s sysmon_t;
79typedef struct sysmon_table_s sysmon_table_t;
80
81static void sysmon_dspic_init (sysmon_t * this);
82static int sysmon_dspic_read (sysmon_t * this, uint addr);
83static void sysmon_backlight_disable (sysmon_table_t * this);
Yuri Tikhonovc147d482008-02-04 14:10:42 +010084
85struct sysmon_s
86{
87 uchar chip;
88 void (*init)(sysmon_t *);
89 int (*read)(sysmon_t *, uint);
90};
91
92static sysmon_t sysmon_dspic =
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020093 {CONFIG_SYS_I2C_DSPIC_IO_ADDR, sysmon_dspic_init, sysmon_dspic_read};
Yuri Tikhonovc147d482008-02-04 14:10:42 +010094
95static sysmon_t * sysmon_list[] =
96{
97 &sysmon_dspic,
98 NULL
99};
100
101struct sysmon_table_s
102{
103 char * name;
104 char * unit_name;
105 sysmon_t * sysmon;
106 void (*exec_before)(sysmon_table_t *);
107 void (*exec_after)(sysmon_table_t *);
108
109 int unit_precision;
110 int unit_div;
111 int unit_min;
112 int unit_max;
113 uint val_mask;
114 uint val_min;
115 uint val_max;
116 int val_valid;
117 uint val_min_alt;
118 uint val_max_alt;
119 int val_valid_alt;
120 uint addr;
121};
122
123static sysmon_table_t sysmon_table[] =
124{
Sascha Laue86c0af72008-04-01 10:10:18 +0200125 {
126 "Temperature", " C", &sysmon_dspic, NULL, sysmon_backlight_disable,
127 1, 1, -32768, 32767, 0xFFFF,
128 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
129 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
Wolfgang Denka0b23e22008-04-29 21:33:08 +0200130 REG_TEMPERATURE,
Sascha Laue86c0af72008-04-01 10:10:18 +0200131 },
Yuri Tikhonovc147d482008-02-04 14:10:42 +0100132
Sascha Laue86c0af72008-04-01 10:10:18 +0200133 {
134 "+ 5 V", "V", &sysmon_dspic, NULL, NULL,
Sascha Laue6313f132008-04-30 15:16:35 +0200135 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
Sascha Laue7ca04222008-04-30 15:23:38 +0200136 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
137 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
Wolfgang Denka0b23e22008-04-29 21:33:08 +0200138 REG_VOLTAGE_5V,
Sascha Laue86c0af72008-04-01 10:10:18 +0200139 },
Yuri Tikhonovc147d482008-02-04 14:10:42 +0100140
Sascha Laue86c0af72008-04-01 10:10:18 +0200141 {
142 "+ 5 V standby", "V", &sysmon_dspic, NULL, NULL,
Sascha Laue6313f132008-04-30 15:16:35 +0200143 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
Sascha Laue7ca04222008-04-30 15:23:38 +0200144 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
145 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
Wolfgang Denka0b23e22008-04-29 21:33:08 +0200146 REG_VOLTAGE_5V_STANDBY,
Sascha Laue86c0af72008-04-01 10:10:18 +0200147 },
Yuri Tikhonovc147d482008-02-04 14:10:42 +0100148};
149static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]);
150
151int sysmon_init_f (void)
152{
153 sysmon_t ** l;
154
155 for (l = sysmon_list; *l; l++)
156 (*l)->init(*l);
157
158 return 0;
159}
160
161void sysmon_reloc (void)
162{
163 sysmon_t ** l;
164 sysmon_table_t * t;
165
166 for (l = sysmon_list; *l; l++) {
167 RELOC(*l);
168 RELOC((*l)->init);
169 RELOC((*l)->read);
170 }
171
172 for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
173 RELOC(t->exec_before);
174 RELOC(t->exec_after);
175 RELOC(t->sysmon);
176 }
177}
178
179static char *sysmon_unit_value (sysmon_table_t *s, uint val)
180{
181 static char buf[32];
182 char *p, sign;
183 int decimal, frac;
184 int unit_val;
185
Wolfgang Denk7f39a012008-04-26 00:34:42 +0200186 unit_val = s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
Yuri Tikhonovc147d482008-02-04 14:10:42 +0100187
188 if (val == -1)
189 return "I/O ERROR";
190
191 if (unit_val < 0) {
192 sign = '-';
193 unit_val = -unit_val;
194 } else
195 sign = '+';
196
197 p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
198
199
200 frac = unit_val % s->unit_div;
201
202 frac /= (s->unit_div / s->unit_precision);
203
204 decimal = s->unit_precision;
205
206 if (decimal != 1)
207 *p++ = '.';
208 for (decimal /= 10; decimal != 0; decimal /= 10)
209 *p++ = '0' + (frac / decimal) % 10;
210 strcpy(p, s->unit_name);
211
212 return buf;
213}
214
215static void sysmon_dspic_init (sysmon_t * this)
216{
217}
218
219static int sysmon_dspic_read (sysmon_t * this, uint addr)
220{
221 int res = dspic_read(addr);
222
223 /* To fit into the table range we should add 0x8000 */
224 return (res == -1) ? -1 : (res + 0x8000);
225}
226
227static void sysmon_backlight_disable (sysmon_table_t * this)
228{
Yuri Tikhonov7151a342008-03-18 13:27:57 +0100229#if defined(CONFIG_VIDEO)
230 board_backlight_switch(this->val_valid_alt);
231#endif
Yuri Tikhonovc147d482008-02-04 14:10:42 +0100232}
233
234int sysmon_post_test (int flags)
235{
236 int res = 0;
237 sysmon_table_t * t;
238 int val;
239
240 for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
241 if (t->exec_before)
242 t->exec_before(t);
243
244 val = t->sysmon->read(t->sysmon, t->addr);
245 if (val != -1) {
246 t->val_valid = val >= t->val_min && val <= t->val_max;
247 t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
248 } else {
249 t->val_valid = 0;
250 t->val_valid_alt = 0;
251 }
252
253 if (t->exec_after)
254 t->exec_after(t);
255
256 if ((!t->val_valid) || (flags & POST_MANUAL)) {
257 printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
258 printf("allowed range");
259 printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
260 printf(" %-8s", sysmon_unit_value(t, t->val_max));
261 printf(" %s\n", t->val_valid ? "OK" : "FAIL");
262 }
263
264 if (!t->val_valid)
265 res = 1;
266 }
267
268 return res;
269}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200270#endif /* CONFIG_POST & CONFIG_SYS_POST_SYSMON */