添加了牛皮藓,主要方便不知情的人联系我。有洁癖者自己去除代码,在
[ddnasgpl.git] / post / sysmon.c
blob72fcac3850809489431e052d22c3f6dd122d1e62
1 /*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
24 #include <post.h>
25 #include <common.h>
27 #ifdef CONFIG_POST
30 * SYSMON test
32 * This test performs the system hardware monitoring.
33 * The test passes when all the following voltages and temperatures
34 * are within allowed ranges:
36 * Board temperature
37 * Front temperature
38 * +3.3V CPU logic
39 * +5V logic
40 * +12V PCMCIA
41 * +12V CCFL
42 * +5V standby
44 * CCFL is not enabled if temperature values are not within allowed ranges
46 * See the list off all parameters in the sysmon_table below
49 #include <post.h>
50 #include <watchdog.h>
51 #include <i2c.h>
53 #if CONFIG_POST & CFG_POST_SYSMON
55 static int sysmon_temp_invalid = 0;
57 /* #define DEBUG */
59 #define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off)
61 typedef struct sysmon_s sysmon_t;
62 typedef struct sysmon_table_s sysmon_table_t;
64 static void sysmon_lm87_init (sysmon_t * this);
65 static void sysmon_pic_init (sysmon_t * this);
66 static uint sysmon_i2c_read (sysmon_t * this, uint addr);
67 static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr);
68 static void sysmon_ccfl_disable (sysmon_table_t * this);
69 static void sysmon_ccfl_enable (sysmon_table_t * this);
71 struct sysmon_s
73 uchar chip;
74 void (*init)(sysmon_t *);
75 uint (*read)(sysmon_t *, uint);
78 static sysmon_t sysmon_lm87 =
79 {CFG_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read};
80 static sysmon_t sysmon_lm87_sgn =
81 {CFG_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read_sgn};
82 static sysmon_t sysmon_pic =
83 {CFG_I2C_PICIO_ADDR, sysmon_pic_init, sysmon_i2c_read};
85 static sysmon_t * sysmon_list[] =
87 &sysmon_lm87,
88 &sysmon_lm87_sgn,
89 &sysmon_pic,
90 NULL
93 struct sysmon_table_s
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 *);
101 int unit_precision;
102 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;
109 uint val_min_alt;
110 uint val_max_alt;
111 int val_valid_alt;
112 uint addr;
115 static sysmon_table_t sysmon_table[] =
117 {"Board temperature", " C", &sysmon_lm87_sgn, NULL, sysmon_ccfl_disable,
118 1, 1, -128, 127, 0xFF, 0x58, 0xD5, 0, 0x6C, 0xC6, 0, 0x27},
120 {"Front temperature", " C", &sysmon_lm87, NULL, sysmon_ccfl_disable,
121 1, 100, -27316, 8984, 0xFF, 0xA4, 0xFC, 0, 0xB2, 0xF1, 0, 0x29},
123 {"+3.3V CPU logic", "V", &sysmon_lm87, NULL, NULL,
124 100, 1000, 0, 4386, 0xFF, 0xB6, 0xC9, 0, 0xB6, 0xC9, 0, 0x22},
126 {"+ 5 V logic", "V", &sysmon_lm87, NULL, NULL,
127 100, 1000, 0, 6630, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x23},
129 {"+12 V PCMCIA", "V", &sysmon_lm87, NULL, NULL,
130 100, 1000, 0, 15460, 0xFF, 0xBC, 0xD0, 0, 0xBC, 0xD0, 0, 0x21},
132 {"+12 V CCFL", "V", &sysmon_lm87, NULL, sysmon_ccfl_enable,
133 100, 1000, 0, 15900, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x24},
135 {"+ 5 V standby", "V", &sysmon_pic, NULL, NULL,
136 100, 1000, 0, 6040, 0xFF, 0xC8, 0xDE, 0, 0xC8, 0xDE, 0, 0x7C},
138 static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]);
140 static int conversion_done = 0;
143 int sysmon_init_f (void)
145 sysmon_t ** l;
146 ulong reg;
148 /* Power on CCFL, PCMCIA */
149 reg = pic_read (0x60);
150 reg |= 0x09;
151 pic_write (0x60, reg);
153 for (l = sysmon_list; *l; l++) {
154 (*l)->init(*l);
157 return 0;
160 void sysmon_reloc (void)
162 DECLARE_GLOBAL_DATA_PTR;
164 sysmon_t ** l;
165 sysmon_table_t * t;
167 for (l = sysmon_list; *l; l++) {
168 RELOC(*l);
169 RELOC((*l)->init);
170 RELOC((*l)->read);
173 for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
174 RELOC(t->exec_before);
175 RELOC(t->exec_after);
176 RELOC(t->sysmon);
180 static char *sysmon_unit_value (sysmon_table_t *s, uint val)
182 static char buf[32];
183 int unit_val =
184 s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
185 char *p, sign;
186 int dec, frac;
188 if (val == -1) {
189 return "I/O ERROR";
192 if (unit_val < 0) {
193 sign = '-';
194 unit_val = -unit_val;
195 } else {
196 sign = '+';
199 p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
202 frac = unit_val % s->unit_div;
204 frac /= (s->unit_div / s->unit_precision);
206 dec = s->unit_precision;
208 if (dec != 1) {
209 *p++ = '.';
211 for (dec /= 10; dec != 0; dec /= 10) {
212 *p++ = '0' + (frac / dec) % 10;
214 strcpy(p, s->unit_name);
216 return buf;
219 static void sysmon_lm87_init (sysmon_t * this)
221 uchar val;
223 /* Detect LM87 chip */
224 if (i2c_read(this->chip, 0x40, 1, &val, 1) || (val & 0x80) != 0 ||
225 i2c_read(this->chip, 0x3E, 1, &val, 1) || val != 0x02) {
226 printf("Error: LM87 not found at 0x%02X\n", this->chip);
227 return;
230 /* Configure pins 5,6 as AIN */
231 val = 0x03;
232 if (i2c_write(this->chip, 0x16, 1, &val, 1)) {
233 printf("Error: can't write LM87 config register\n");
234 return;
237 /* Start monitoring */
238 val = 0x01;
239 if (i2c_write(this->chip, 0x40, 1, &val, 1)) {
240 printf("Error: can't write LM87 config register\n");
241 return;
245 static void sysmon_pic_init (sysmon_t * this)
249 static uint sysmon_i2c_read (sysmon_t * this, uint addr)
251 uchar val;
252 uint res = i2c_read(this->chip, addr, 1, &val, 1);
254 return res == 0 ? val : -1;
257 static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr)
259 uchar val;
260 return i2c_read(this->chip, addr, 1, &val, 1) == 0 ?
261 128 + (signed char)val : -1;
264 static void sysmon_ccfl_disable (sysmon_table_t * this)
266 if (!this->val_valid_alt) {
267 sysmon_temp_invalid = 1;
271 static void sysmon_ccfl_enable (sysmon_table_t * this)
273 ulong reg;
275 if (!sysmon_temp_invalid) {
276 reg = pic_read (0x60);
277 reg |= 0x06;
278 pic_write (0x60, reg);
282 int sysmon_post_test (int flags)
284 DECLARE_GLOBAL_DATA_PTR;
286 int res = 0;
287 sysmon_table_t * t;
288 uint val;
291 * The A/D conversion on the LM87 sensor takes 300 ms.
293 if (! conversion_done) {
294 while (post_time_ms(gd->post_init_f_time) < 300) WATCHDOG_RESET ();
295 conversion_done = 1;
298 for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
299 if (t->exec_before) {
300 t->exec_before(t);
303 val = t->sysmon->read(t->sysmon, t->addr);
304 if (val != -1) {
305 t->val_valid = val >= t->val_min && val <= t->val_max;
306 t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
307 } else {
308 t->val_valid = 0;
309 t->val_valid_alt = 0;
312 if (t->exec_after) {
313 t->exec_after(t);
316 if ((!t->val_valid) || (flags & POST_MANUAL)) {
317 printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
318 printf("allowed range");
319 printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
320 printf(" %-8s", sysmon_unit_value(t, t->val_max));
321 printf(" %s\n", t->val_valid ? "OK" : "FAIL");
324 if (!t->val_valid) {
325 res = -1;
329 return res;
332 #endif /* CONFIG_POST & CFG_POST_SYSMON */
333 #endif /* CONFIG_POST */