blob: f7e51a3cfe6aa18ef2dcd8f3876308d37d14031c [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 *
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
28#ifdef CONFIG_POST
29
30/*
31 * SYSMON test
32 *
33 * This test performs the system hardware monitoring.
34 * The test passes when all the following voltages and temperatures
35 * are within allowed ranges:
36 *
37 * Temperature -40 .. +85 C
38 * +5V +4.75 .. +5.25 V
39 * +5V standby +4.75 .. +5.25 V
40 *
41 * LCD backlight is not enabled if temperature values are not within
42 * allowed ranges (-30 .. + 80). The brightness of backlite can be
43 * controlled by setting "brightness" enviroment variable. Default value is 50%
44 *
45 * See the list of all parameters in the sysmon_table below
46 */
47
48#include <post.h>
49#include <watchdog.h>
50#include <i2c.h>
51
Yuri Tikhonov0f855a12008-03-18 13:27:57 +010052#if defined(CONFIG_VIDEO)
53#include <mb862xx.h>
54#endif
55
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010056#if CONFIG_POST & CFG_POST_SYSMON
57
58DECLARE_GLOBAL_DATA_PTR;
59
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010060/* from dspic.c */
61extern int dspic_read(ushort reg);
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010062
63#define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off)
64
65typedef struct sysmon_s sysmon_t;
66typedef struct sysmon_table_s sysmon_table_t;
67
68static void sysmon_dspic_init (sysmon_t * this);
69static int sysmon_dspic_read (sysmon_t * this, uint addr);
70static void sysmon_backlight_disable (sysmon_table_t * this);
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +010071
72struct sysmon_s
73{
74 uchar chip;
75 void (*init)(sysmon_t *);
76 int (*read)(sysmon_t *, uint);
77};
78
79static sysmon_t sysmon_dspic =
80 {CFG_I2C_DSPIC_IO_ADDR, sysmon_dspic_init, sysmon_dspic_read};
81
82static sysmon_t * sysmon_list[] =
83{
84 &sysmon_dspic,
85 NULL
86};
87
88struct sysmon_table_s
89{
90 char * name;
91 char * unit_name;
92 sysmon_t * sysmon;
93 void (*exec_before)(sysmon_table_t *);
94 void (*exec_after)(sysmon_table_t *);
95
96 int unit_precision;
97 int unit_div;
98 int unit_min;
99 int unit_max;
100 uint val_mask;
101 uint val_min;
102 uint val_max;
103 int val_valid;
104 uint val_min_alt;
105 uint val_max_alt;
106 int val_valid_alt;
107 uint addr;
108};
109
110static sysmon_table_t sysmon_table[] =
111{
112 {"Temperature", " C", &sysmon_dspic, NULL, sysmon_backlight_disable,
113 1, 1, -32768, 32767, 0xFFFF, 0x8000-40, 0x8000+85, 0,
114 0x8000-30, 0x8000+80, 0, 0x12BC},
115
116 {"+ 5 V", "V", &sysmon_dspic, NULL, NULL,
117 100, 1000, -0x8000, 0x7FFF, 0xFFFF, 0x8000+4750, 0x8000+5250, 0,
118 0x8000+4750, 0x8000+5250, 0, 0x12CA},
119
Yuri Tikhonov0f855a12008-03-18 13:27:57 +0100120 {"+ 5 V standby", "V", &sysmon_dspic, NULL, NULL,
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100121 100, 1000, -0x8000, 0x7FFF, 0xFFFF, 0x8000+4750, 0x8000+5250, 0,
122 0x8000+4750, 0x8000+5250, 0, 0x12C6},
123};
124static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]);
125
126int sysmon_init_f (void)
127{
128 sysmon_t ** l;
129
130 for (l = sysmon_list; *l; l++)
131 (*l)->init(*l);
132
133 return 0;
134}
135
136void sysmon_reloc (void)
137{
138 sysmon_t ** l;
139 sysmon_table_t * t;
140
141 for (l = sysmon_list; *l; l++) {
142 RELOC(*l);
143 RELOC((*l)->init);
144 RELOC((*l)->read);
145 }
146
147 for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
148 RELOC(t->exec_before);
149 RELOC(t->exec_after);
150 RELOC(t->sysmon);
151 }
152}
153
154static char *sysmon_unit_value (sysmon_table_t *s, uint val)
155{
156 static char buf[32];
157 char *p, sign;
158 int decimal, frac;
159 int unit_val;
160
161 unit_val =
162 s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
163
164 if (val == -1)
165 return "I/O ERROR";
166
167 if (unit_val < 0) {
168 sign = '-';
169 unit_val = -unit_val;
170 } else
171 sign = '+';
172
173 p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
174
175
176 frac = unit_val % s->unit_div;
177
178 frac /= (s->unit_div / s->unit_precision);
179
180 decimal = s->unit_precision;
181
182 if (decimal != 1)
183 *p++ = '.';
184 for (decimal /= 10; decimal != 0; decimal /= 10)
185 *p++ = '0' + (frac / decimal) % 10;
186 strcpy(p, s->unit_name);
187
188 return buf;
189}
190
191static void sysmon_dspic_init (sysmon_t * this)
192{
193}
194
195static int sysmon_dspic_read (sysmon_t * this, uint addr)
196{
197 int res = dspic_read(addr);
198
199 /* To fit into the table range we should add 0x8000 */
200 return (res == -1) ? -1 : (res + 0x8000);
201}
202
203static void sysmon_backlight_disable (sysmon_table_t * this)
204{
Yuri Tikhonov0f855a12008-03-18 13:27:57 +0100205#if defined(CONFIG_VIDEO)
206 board_backlight_switch(this->val_valid_alt);
207#endif
Yuri Tikhonov65b20dc2008-02-04 14:10:42 +0100208}
209
210int sysmon_post_test (int flags)
211{
212 int res = 0;
213 sysmon_table_t * t;
214 int val;
215
216 for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
217 if (t->exec_before)
218 t->exec_before(t);
219
220 val = t->sysmon->read(t->sysmon, t->addr);
221 if (val != -1) {
222 t->val_valid = val >= t->val_min && val <= t->val_max;
223 t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
224 } else {
225 t->val_valid = 0;
226 t->val_valid_alt = 0;
227 }
228
229 if (t->exec_after)
230 t->exec_after(t);
231
232 if ((!t->val_valid) || (flags & POST_MANUAL)) {
233 printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
234 printf("allowed range");
235 printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
236 printf(" %-8s", sysmon_unit_value(t, t->val_max));
237 printf(" %s\n", t->val_valid ? "OK" : "FAIL");
238 }
239
240 if (!t->val_valid)
241 res = 1;
242 }
243
244 return res;
245}
246
247#endif /* CONFIG_POST & CFG_POST_SYSMON */
248#endif /* CONFIG_POST */