]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - post/sysmon.c
* LWMON extensions:
[karo-tx-uboot.git] / post / sysmon.c
1 /*
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:
35  * 
36  * Board temperature
37  * Front temperature
38  * +3.3V CPU logic
39  * +5V logic
40  * +12V PCMCIA
41  * +12V CCFL
42  * +5V standby
43  * 
44  * 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
53 static int sysmon_temp_invalid = 0;
54
55 #if CONFIG_POST & CFG_POST_SYSMON
56
57 /* #define DEBUG */
58
59 #define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off)
60
61 typedef struct sysmon_s sysmon_t;
62 typedef struct sysmon_table_s sysmon_table_t;
63
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);
70
71 struct sysmon_s
72 {
73         uchar   chip;
74         void    (*init)(sysmon_t *);
75         uint    (*read)(sysmon_t *, uint);
76 };
77
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};
84
85 static sysmon_t * sysmon_list[] =
86 {
87         &sysmon_lm87,
88         &sysmon_lm87_sgn,
89         &sysmon_pic,
90         NULL
91 };
92
93 struct 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
101         int             unit_div;
102         int             unit_min;
103         int             unit_max;
104         uint            val_mask;
105         uint            val_min;
106         uint            val_max;
107         int             val_valid;
108         uint            addr;
109 };
110
111 static sysmon_table_t sysmon_table[] =
112 {
113     {"Board temperature", " C", &sysmon_lm87_sgn, NULL, sysmon_ccfl_disable,
114      1, -128, 127, 0xFF, 0x58, 0xD5, 0, 0x27},
115
116     {"Front temperature", " C", &sysmon_lm87, NULL, sysmon_ccfl_disable,
117      100, -27316, 8984, 0xFF, 0xA4, 0xFC, 0, 0x29},
118
119     {"+3.3V CPU logic", "V", &sysmon_lm87, NULL, NULL,
120      1000, 0, 4386, 0xFF, 0xB6, 0xC9, 0, 0x22},
121
122     {"+5V logic", "V", &sysmon_lm87, NULL, NULL,
123      1000, 0, 6630, 0xFF, 0xB6, 0xCA, 0, 0x23},
124
125     {"+12V PCMCIA", "V", &sysmon_lm87, NULL, NULL,
126      1000, 0, 15460, 0xFF, 0xBC, 0xD0, 0, 0x21},
127
128     {"+12V CCFL", "V", &sysmon_lm87, NULL, sysmon_ccfl_enable,
129      1000, 0, 15900, 0xFF, 0xB6, 0xCA, 0, 0x24},
130
131     {"+5V standby", "V", &sysmon_pic, NULL, NULL,
132      1000, 0, 6040, 0xFF, 0xC8, 0xDE, 0, 0x7C},
133 };
134 static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]);
135
136 static int conversion_done = 0;
137
138
139 int sysmon_init_f (void)
140 {
141         sysmon_t ** l;
142         ulong reg;
143
144         /* Power on CCFL, PCMCIA */
145         reg = pic_read  (0x60);
146         reg |= 0x09;
147         pic_write (0x60, reg);
148
149         for (l = sysmon_list; *l; l++)
150         {
151                 (*l)->init(*l);
152         }
153         
154         return 0;
155 }
156
157 void sysmon_reloc (void)
158 {
159         DECLARE_GLOBAL_DATA_PTR;
160
161         sysmon_t ** l;
162         sysmon_table_t * t;
163
164         for (l = sysmon_list; *l; l++)
165         {
166                 RELOC(*l);
167                 RELOC((*l)->init);
168                 RELOC((*l)->read);
169         }
170
171         for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++)
172         {
173                 RELOC(t->exec_before);
174                 RELOC(t->exec_after);
175                 RELOC(t->sysmon);
176         }
177 }
178
179 static char * sysmon_unit_value (sysmon_table_t * s, uint val)
180 {
181         static char buf[32];
182         int unit_val =
183             s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
184         char * p;
185         int dec, frac;
186
187         sprintf(buf, "%+d", unit_val / s->unit_div);
188         
189         frac = (unit_val > 0 ? unit_val : -unit_val) % s->unit_div;
190         p = buf + strlen(buf);
191         
192         dec = s->unit_div;
193         
194         if (dec != 1)
195         {
196                 *p++ = '.';
197         }
198         
199         for (dec /= 10; dec != 0; dec /= 10)
200         {
201                 *p++ = '0' + frac / dec % 10;
202         }
203         
204         strcpy(p, s->unit_name);
205         
206         return buf;
207 }
208
209 static void sysmon_lm87_init (sysmon_t * this)
210 {
211         uchar val;
212
213         /* Detect LM87 chip */
214         if (i2c_read(this->chip, 0x40, 1, &val, 1) || (val & 0x80) != 0 ||
215             i2c_read(this->chip, 0x3E, 1, &val, 1) || val != 0x02)
216         {
217                 printf("Error: LM87 not found at 0x%02X\n", this->chip);
218                 return;
219         }
220         
221         /* Configure pins 5,6 as AIN */
222         val = 0x03;
223         if (i2c_write(this->chip, 0x16, 1, &val, 1))
224         {
225                 printf("Error: can't write LM87 config register\n");
226                 return;
227         }
228
229         /* Start monitoring */
230         val = 0x01;
231         if (i2c_write(this->chip, 0x40, 1, &val, 1))
232         {
233                 printf("Error: can't write LM87 config register\n");
234                 return;
235         }
236 }
237
238 static void sysmon_pic_init (sysmon_t * this)
239 {
240 }
241
242 static uint sysmon_i2c_read (sysmon_t * this, uint addr)
243 {
244         uchar val;
245         uint res = i2c_read(this->chip, addr, 1, &val, 1);
246
247         return res == 0 ? val : -1;
248 }
249
250 static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr)
251 {
252         uchar val;
253         return i2c_read(this->chip, addr, 1, &val, 1) == 0 ?
254                 128 + (signed char)val : -1;
255 }
256
257 static void sysmon_ccfl_disable (sysmon_table_t * this)
258 {
259         if (!this->val_valid)
260         {
261                 sysmon_temp_invalid = 1;
262         }
263 }
264
265 static void sysmon_ccfl_enable (sysmon_table_t * this)
266 {
267         ulong reg;
268
269         if (!sysmon_temp_invalid)
270         {
271                 reg = pic_read  (0x60);
272                 reg |= 0x02;
273                 pic_write (0x60, reg);
274         }
275 }
276
277 int sysmon_post_test (int flags)
278 {
279         DECLARE_GLOBAL_DATA_PTR;
280
281         int res = 0;
282         sysmon_table_t * t;
283         uint val;
284
285         /*
286          * The A/D conversion on the LM87 sensor takes 300 ms.
287          */
288         if (! conversion_done)
289         {
290                 while (post_time_ms(gd->post_init_f_time) < 300) WATCHDOG_RESET ();
291                 conversion_done = 1;
292         }
293
294         for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++)
295         {
296                 if (t->exec_before)
297                 {
298                         t->exec_before(t);
299                 }
300
301                 val = t->sysmon->read(t->sysmon, t->addr);
302                 t->val_valid = val >= t->val_min && val <= t->val_max;
303
304                 if (t->exec_after)
305                 {
306                         t->exec_after(t);
307                 }
308
309 #ifndef DEBUG
310                 if (!t->val_valid)
311 #endif
312                 {
313                         printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
314                         printf("allowed range");
315                         printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
316                         printf(" %-8s", sysmon_unit_value(t, t->val_max));
317                         printf("     %s\n", t->val_valid ? "OK" : "FAIL");
318                 }
319
320                 if (!t->val_valid)
321                 {
322                         res = -1;
323                 }
324         }
325
326         return res;
327 }
328
329 #endif /* CONFIG_POST & CFG_POST_SYSMON */
330 #endif /* CONFIG_POST */