blob: e28de904844474bb86e9aa0660e2297cdc3d37f9 [file] [log] [blame]
wdenk4532cb62003-04-27 22:52:51 +00001/*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <post.h>
25#include <common.h>
26
27#ifdef CONFIG_POST
28
29/*
30 * SYSMON test
31 *
32 * This test performs the system hardware monitoring.
33 * The test passes when all the following voltages and temperatures
34 * are within allowed ranges:
wdenk8bde7f72003-06-27 21:31:46 +000035 *
wdenk4532cb62003-04-27 22:52:51 +000036 * Board temperature
37 * Front temperature
38 * +3.3V CPU logic
39 * +5V logic
40 * +12V PCMCIA
41 * +12V CCFL
42 * +5V standby
wdenk8bde7f72003-06-27 21:31:46 +000043 *
wdenk4532cb62003-04-27 22:52:51 +000044 * CCFL is not enabled if temperature values are not within allowed ranges
45 *
46 * See the list off all parameters in the sysmon_table below
47 */
48
49#include <post.h>
50#include <watchdog.h>
51#include <i2c.h>
52
wdenk4532cb62003-04-27 22:52:51 +000053#if CONFIG_POST & CFG_POST_SYSMON
54
wdenk45219c42003-05-12 21:50:16 +000055static int sysmon_temp_invalid = 0;
56
wdenk4532cb62003-04-27 22:52:51 +000057/* #define DEBUG */
58
59#define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off)
60
61typedef struct sysmon_s sysmon_t;
62typedef struct sysmon_table_s sysmon_table_t;
63
64static void sysmon_lm87_init (sysmon_t * this);
65static void sysmon_pic_init (sysmon_t * this);
66static uint sysmon_i2c_read (sysmon_t * this, uint addr);
67static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr);
68static void sysmon_ccfl_disable (sysmon_table_t * this);
69static void sysmon_ccfl_enable (sysmon_table_t * this);
70
71struct sysmon_s
72{
73 uchar chip;
74 void (*init)(sysmon_t *);
75 uint (*read)(sysmon_t *, uint);
76};
77
78static sysmon_t sysmon_lm87 =
79 {CFG_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read};
80static sysmon_t sysmon_lm87_sgn =
81 {CFG_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read_sgn};
82static sysmon_t sysmon_pic =
83 {CFG_I2C_PICIO_ADDR, sysmon_pic_init, sysmon_i2c_read};
84
85static sysmon_t * sysmon_list[] =
86{
87 &sysmon_lm87,
88 &sysmon_lm87_sgn,
89 &sysmon_pic,
90 NULL
91};
92
93struct sysmon_table_s
94{
95 char * name;
96 char * unit_name;
97 sysmon_t * sysmon;
98 void (*exec_before)(sysmon_table_t *);
99 void (*exec_after)(sysmon_table_t *);
100
wdenk8564acf2003-07-14 22:13:32 +0000101 int unit_precision;
wdenk4532cb62003-04-27 22:52:51 +0000102 int unit_div;
103 int unit_min;
104 int unit_max;
105 uint val_mask;
106 uint val_min;
107 uint val_max;
108 int val_valid;
wdenk8564acf2003-07-14 22:13:32 +0000109 uint val_min_alt;
110 uint val_max_alt;
111 int val_valid_alt;
wdenk4532cb62003-04-27 22:52:51 +0000112 uint addr;
113};
114
115static sysmon_table_t sysmon_table[] =
116{
117 {"Board temperature", " C", &sysmon_lm87_sgn, NULL, sysmon_ccfl_disable,
wdenk8564acf2003-07-14 22:13:32 +0000118 1, 1, -128, 127, 0xFF, 0x58, 0xD5, 0, 0x67, 0xC6, 0, 0x27},
wdenk4532cb62003-04-27 22:52:51 +0000119
120 {"Front temperature", " C", &sysmon_lm87, NULL, sysmon_ccfl_disable,
wdenk8564acf2003-07-14 22:13:32 +0000121 1, 100, -27316, 8984, 0xFF, 0xA4, 0xFC, 0, 0xAE, 0xF1, 0, 0x29},
wdenk4532cb62003-04-27 22:52:51 +0000122
123 {"+3.3V CPU logic", "V", &sysmon_lm87, NULL, NULL,
wdenk8564acf2003-07-14 22:13:32 +0000124 100, 1000, 0, 4386, 0xFF, 0xB6, 0xC9, 0, 0xB6, 0xC9, 0, 0x22},
wdenk4532cb62003-04-27 22:52:51 +0000125
wdenk8564acf2003-07-14 22:13:32 +0000126 {"+ 5 V logic", "V", &sysmon_lm87, NULL, NULL,
127 100, 1000, 0, 6630, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x23},
wdenk4532cb62003-04-27 22:52:51 +0000128
wdenk8564acf2003-07-14 22:13:32 +0000129 {"+12 V PCMCIA", "V", &sysmon_lm87, NULL, NULL,
130 100, 1000, 0, 15460, 0xFF, 0xBC, 0xD0, 0, 0xBC, 0xD0, 0, 0x21},
wdenk4532cb62003-04-27 22:52:51 +0000131
wdenk8564acf2003-07-14 22:13:32 +0000132 {"+12 V CCFL", "V", &sysmon_lm87, NULL, sysmon_ccfl_enable,
133 100, 1000, 0, 15900, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x24},
wdenk4532cb62003-04-27 22:52:51 +0000134
wdenk8564acf2003-07-14 22:13:32 +0000135 {"+ 5 V standby", "V", &sysmon_pic, NULL, NULL,
136 100, 1000, 0, 6040, 0xFF, 0xC8, 0xDE, 0, 0xC8, 0xDE, 0, 0x7C},
wdenk4532cb62003-04-27 22:52:51 +0000137};
138static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]);
139
140static int conversion_done = 0;
141
142
143int sysmon_init_f (void)
144{
145 sysmon_t ** l;
146 ulong reg;
147
148 /* Power on CCFL, PCMCIA */
149 reg = pic_read (0x60);
150 reg |= 0x09;
151 pic_write (0x60, reg);
152
153 for (l = sysmon_list; *l; l++)
154 {
155 (*l)->init(*l);
156 }
wdenk8bde7f72003-06-27 21:31:46 +0000157
wdenk4532cb62003-04-27 22:52:51 +0000158 return 0;
159}
160
161void sysmon_reloc (void)
162{
163 DECLARE_GLOBAL_DATA_PTR;
164
165 sysmon_t ** l;
166 sysmon_table_t * t;
167
168 for (l = sysmon_list; *l; l++)
169 {
170 RELOC(*l);
171 RELOC((*l)->init);
172 RELOC((*l)->read);
173 }
174
175 for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++)
176 {
177 RELOC(t->exec_before);
178 RELOC(t->exec_after);
179 RELOC(t->sysmon);
180 }
181}
182
wdenk8564acf2003-07-14 22:13:32 +0000183static char *sysmon_unit_value (sysmon_table_t *s, uint val)
wdenk4532cb62003-04-27 22:52:51 +0000184{
185 static char buf[32];
186 int unit_val =
187 s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
wdenk8564acf2003-07-14 22:13:32 +0000188 char *p, sign;
wdenk4532cb62003-04-27 22:52:51 +0000189 int dec, frac;
190
wdenk8564acf2003-07-14 22:13:32 +0000191 if (unit_val < 0) {
192 sign = '-';
193 unit_val = -unit_val;
194 } else {
195 sign = '+';
196 }
wdenk8bde7f72003-06-27 21:31:46 +0000197
wdenk8564acf2003-07-14 22:13:32 +0000198 p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
wdenk8bde7f72003-06-27 21:31:46 +0000199
wdenk8564acf2003-07-14 22:13:32 +0000200
201 frac = unit_val % s->unit_div;
202
203 frac /= (s->unit_div / s->unit_precision);
204
205 dec = s->unit_precision;
wdenk8bde7f72003-06-27 21:31:46 +0000206
wdenk4532cb62003-04-27 22:52:51 +0000207 if (dec != 1)
208 {
209 *p++ = '.';
210 }
wdenk4532cb62003-04-27 22:52:51 +0000211 for (dec /= 10; dec != 0; dec /= 10)
212 {
wdenk8564acf2003-07-14 22:13:32 +0000213 *p++ = '0' + (frac / dec) % 10;
wdenk4532cb62003-04-27 22:52:51 +0000214 }
wdenk4532cb62003-04-27 22:52:51 +0000215 strcpy(p, s->unit_name);
wdenk8bde7f72003-06-27 21:31:46 +0000216
wdenk4532cb62003-04-27 22:52:51 +0000217 return buf;
218}
219
220static void sysmon_lm87_init (sysmon_t * this)
221{
222 uchar val;
223
224 /* Detect LM87 chip */
225 if (i2c_read(this->chip, 0x40, 1, &val, 1) || (val & 0x80) != 0 ||
226 i2c_read(this->chip, 0x3E, 1, &val, 1) || val != 0x02)
227 {
228 printf("Error: LM87 not found at 0x%02X\n", this->chip);
229 return;
230 }
wdenk8bde7f72003-06-27 21:31:46 +0000231
wdenk4532cb62003-04-27 22:52:51 +0000232 /* Configure pins 5,6 as AIN */
233 val = 0x03;
234 if (i2c_write(this->chip, 0x16, 1, &val, 1))
235 {
236 printf("Error: can't write LM87 config register\n");
237 return;
238 }
239
240 /* Start monitoring */
241 val = 0x01;
242 if (i2c_write(this->chip, 0x40, 1, &val, 1))
243 {
244 printf("Error: can't write LM87 config register\n");
245 return;
246 }
247}
248
249static void sysmon_pic_init (sysmon_t * this)
250{
251}
252
253static uint sysmon_i2c_read (sysmon_t * this, uint addr)
254{
255 uchar val;
256 uint res = i2c_read(this->chip, addr, 1, &val, 1);
257
258 return res == 0 ? val : -1;
259}
260
261static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr)
262{
263 uchar val;
264 return i2c_read(this->chip, addr, 1, &val, 1) == 0 ?
265 128 + (signed char)val : -1;
266}
267
268static void sysmon_ccfl_disable (sysmon_table_t * this)
269{
wdenk8564acf2003-07-14 22:13:32 +0000270 if (!this->val_valid_alt)
wdenk4532cb62003-04-27 22:52:51 +0000271 {
272 sysmon_temp_invalid = 1;
273 }
274}
275
276static void sysmon_ccfl_enable (sysmon_table_t * this)
277{
278 ulong reg;
279
280 if (!sysmon_temp_invalid)
281 {
282 reg = pic_read (0x60);
283 reg |= 0x02;
284 pic_write (0x60, reg);
285 }
286}
287
288int sysmon_post_test (int flags)
289{
290 DECLARE_GLOBAL_DATA_PTR;
291
292 int res = 0;
293 sysmon_table_t * t;
294 uint val;
295
296 /*
297 * The A/D conversion on the LM87 sensor takes 300 ms.
298 */
299 if (! conversion_done)
300 {
301 while (post_time_ms(gd->post_init_f_time) < 300) WATCHDOG_RESET ();
302 conversion_done = 1;
303 }
304
305 for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++)
306 {
307 if (t->exec_before)
308 {
309 t->exec_before(t);
310 }
311
312 val = t->sysmon->read(t->sysmon, t->addr);
313 t->val_valid = val >= t->val_min && val <= t->val_max;
wdenk8564acf2003-07-14 22:13:32 +0000314 t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
wdenk4532cb62003-04-27 22:52:51 +0000315
316 if (t->exec_after)
317 {
318 t->exec_after(t);
319 }
320
wdenkd1cbe852003-06-28 17:24:46 +0000321 if ((!t->val_valid) || (flags & POST_MANUAL))
wdenk4532cb62003-04-27 22:52:51 +0000322 {
323 printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
324 printf("allowed range");
325 printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
326 printf(" %-8s", sysmon_unit_value(t, t->val_max));
327 printf(" %s\n", t->val_valid ? "OK" : "FAIL");
328 }
329
330 if (!t->val_valid)
331 {
332 res = -1;
333 }
334 }
335
336 return res;
337}
338
339#endif /* CONFIG_POST & CFG_POST_SYSMON */
340#endif /* CONFIG_POST */