]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - post/board/lwmon/sysmon.c
socfpga: Move board/socfpga_cyclone5 to board/socfpga
[karo-tx-uboot.git] / post / board / lwmon / 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 /*
28  * SYSMON test
29  *
30  * This test performs the system hardware monitoring.
31  * The test passes when all the following voltages and temperatures
32  * are within allowed ranges:
33  *
34  * Board temperature
35  * Front temperature
36  * +3.3V CPU logic
37  * +5V logic
38  * +12V PCMCIA
39  * +12V CCFL
40  * +5V standby
41  *
42  * CCFL is not enabled if temperature values are not within allowed ranges
43  *
44  * See the list off all parameters in the sysmon_table below
45  */
46
47 #include <post.h>
48 #include <watchdog.h>
49 #include <i2c.h>
50
51 #if CONFIG_POST & CONFIG_SYS_POST_SYSMON
52
53 DECLARE_GLOBAL_DATA_PTR;
54
55 static int sysmon_temp_invalid = 0;
56
57 /* #define DEBUG */
58
59 typedef struct sysmon_s sysmon_t;
60 typedef struct sysmon_table_s sysmon_table_t;
61
62 static void sysmon_lm87_init (sysmon_t * this);
63 static void sysmon_pic_init (sysmon_t * this);
64 static uint sysmon_i2c_read (sysmon_t * this, uint addr);
65 static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr);
66 static void sysmon_ccfl_disable (sysmon_table_t * this);
67 static void sysmon_ccfl_enable (sysmon_table_t * this);
68
69 struct sysmon_s
70 {
71         uchar   chip;
72         void    (*init)(sysmon_t *);
73         uint    (*read)(sysmon_t *, uint);
74 };
75
76 static sysmon_t sysmon_lm87 =
77         {CONFIG_SYS_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read};
78 static sysmon_t sysmon_lm87_sgn =
79         {CONFIG_SYS_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read_sgn};
80 static sysmon_t sysmon_pic =
81         {CONFIG_SYS_I2C_PICIO_ADDR, sysmon_pic_init, sysmon_i2c_read};
82
83 static sysmon_t * sysmon_list[] =
84 {
85         &sysmon_lm87,
86         &sysmon_lm87_sgn,
87         &sysmon_pic,
88         NULL
89 };
90
91 struct sysmon_table_s
92 {
93         char *          name;
94         char *          unit_name;
95         sysmon_t *      sysmon;
96         void            (*exec_before)(sysmon_table_t *);
97         void            (*exec_after)(sysmon_table_t *);
98
99         int             unit_precision;
100         int             unit_div;
101         int             unit_min;
102         int             unit_max;
103         uint            val_mask;
104         uint            val_min;
105         uint            val_max;
106         int             val_valid;
107         uint            val_min_alt;
108         uint            val_max_alt;
109         int             val_valid_alt;
110         uint            addr;
111 };
112
113 static sysmon_table_t sysmon_table[] =
114 {
115     {"Board temperature", " C", &sysmon_lm87_sgn, NULL, sysmon_ccfl_disable,
116      1, 1, -128, 127, 0xFF, 0x58, 0xD5, 0, 0x6C, 0xC6, 0, 0x27},
117
118     {"Front temperature", " C", &sysmon_lm87, NULL, sysmon_ccfl_disable,
119      1, 100, -27316, 8984, 0xFF, 0xA4, 0xFC, 0, 0xB2, 0xF1, 0, 0x29},
120
121     {"+3.3V CPU logic", "V", &sysmon_lm87, NULL, NULL,
122      100, 1000, 0, 4386, 0xFF, 0xB6, 0xC9, 0, 0xB6, 0xC9, 0, 0x22},
123
124     {"+ 5 V logic", "V", &sysmon_lm87, NULL, NULL,
125      100, 1000, 0, 6630, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x23},
126
127     {"+12 V PCMCIA", "V", &sysmon_lm87, NULL, NULL,
128      100, 1000, 0, 15460, 0xFF, 0xBC, 0xD0, 0, 0xBC, 0xD0, 0, 0x21},
129
130     {"+12 V CCFL", "V", &sysmon_lm87, NULL, sysmon_ccfl_enable,
131      100, 1000, 0, 15900, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x24},
132
133     {"+ 5 V standby", "V", &sysmon_pic, NULL, NULL,
134      100, 1000, 0, 6040, 0xFF, 0xC8, 0xDE, 0, 0xC8, 0xDE, 0, 0x7C},
135 };
136 static int sysmon_table_size = ARRAY_SIZE(sysmon_table);
137
138 static int conversion_done = 0;
139
140
141 int sysmon_init_f (void)
142 {
143         sysmon_t ** l;
144         ulong reg;
145
146         /* Power on CCFL, PCMCIA */
147         reg = pic_read  (0x60);
148         reg |= 0x09;
149         pic_write (0x60, reg);
150
151         for (l = sysmon_list; *l; l++) {
152                 (*l)->init(*l);
153         }
154
155         return 0;
156 }
157
158 void sysmon_reloc (void)
159 {
160         /* Do nothing for now, sysmon_reloc() is required by the sysmon post */
161 }
162
163 static char *sysmon_unit_value (sysmon_table_t *s, uint val)
164 {
165         static char buf[32];
166         int unit_val =
167             s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
168         char *p, sign;
169         int dec, frac;
170
171         if (val == -1) {
172                 return "I/O ERROR";
173         }
174
175         if (unit_val < 0) {
176                 sign = '-';
177                 unit_val = -unit_val;
178         } else {
179                 sign = '+';
180         }
181
182         p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
183
184
185         frac = unit_val % s->unit_div;
186
187         frac /= (s->unit_div / s->unit_precision);
188
189         dec = s->unit_precision;
190
191         if (dec != 1) {
192                 *p++ = '.';
193         }
194         for (dec /= 10; dec != 0; dec /= 10) {
195                 *p++ = '0' + (frac / dec) % 10;
196         }
197         strcpy(p, s->unit_name);
198
199         return buf;
200 }
201
202 static void sysmon_lm87_init (sysmon_t * this)
203 {
204         uchar val;
205
206         /* Detect LM87 chip */
207         if (i2c_read(this->chip, 0x40, 1, &val, 1) || (val & 0x80) != 0 ||
208             i2c_read(this->chip, 0x3E, 1, &val, 1) || val != 0x02) {
209                 printf("Error: LM87 not found at 0x%02X\n", this->chip);
210                 return;
211         }
212
213         /* Configure pins 5,6 as AIN */
214         val = 0x03;
215         if (i2c_write(this->chip, 0x16, 1, &val, 1)) {
216                 printf("Error: can't write LM87 config register\n");
217                 return;
218         }
219
220         /* Start monitoring */
221         val = 0x01;
222         if (i2c_write(this->chip, 0x40, 1, &val, 1)) {
223                 printf("Error: can't write LM87 config register\n");
224                 return;
225         }
226 }
227
228 static void sysmon_pic_init (sysmon_t * this)
229 {
230 }
231
232 static uint sysmon_i2c_read (sysmon_t * this, uint addr)
233 {
234         uchar val;
235         uint res = i2c_read(this->chip, addr, 1, &val, 1);
236
237         return res == 0 ? val : -1;
238 }
239
240 static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr)
241 {
242         uchar val;
243         return i2c_read(this->chip, addr, 1, &val, 1) == 0 ?
244                 128 + (signed char)val : -1;
245 }
246
247 static void sysmon_ccfl_disable (sysmon_table_t * this)
248 {
249         if (!this->val_valid_alt) {
250                 sysmon_temp_invalid = 1;
251         }
252 }
253
254 static void sysmon_ccfl_enable (sysmon_table_t * this)
255 {
256         ulong reg;
257
258         if (!sysmon_temp_invalid) {
259                 reg = pic_read  (0x60);
260                 reg |= 0x06;
261                 pic_write (0x60, reg);
262         }
263 }
264
265 int sysmon_post_test (int flags)
266 {
267         int res = 0;
268         sysmon_table_t * t;
269         uint val;
270
271         /*
272          * The A/D conversion on the LM87 sensor takes 300 ms.
273          */
274         if (! conversion_done) {
275                 while (post_time_ms(gd->post_init_f_time) < 300) WATCHDOG_RESET ();
276                 conversion_done = 1;
277         }
278
279         for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
280                 if (t->exec_before) {
281                         t->exec_before(t);
282                 }
283
284                 val = t->sysmon->read(t->sysmon, t->addr);
285                 if (val != -1) {
286                         t->val_valid = val >= t->val_min && val <= t->val_max;
287                         t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
288                 } else {
289                         t->val_valid = 0;
290                         t->val_valid_alt = 0;
291                 }
292
293                 if (t->exec_after) {
294                         t->exec_after(t);
295                 }
296
297                 if ((!t->val_valid) || (flags & POST_MANUAL)) {
298                         printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
299                         printf("allowed range");
300                         printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
301                         printf(" %-8s", sysmon_unit_value(t, t->val_max));
302                         printf("     %s\n", t->val_valid ? "OK" : "FAIL");
303                 }
304
305                 if (!t->val_valid) {
306                         res = -1;
307                 }
308         }
309
310         return res;
311 }
312
313 #endif /* CONFIG_POST & CONFIG_SYS_POST_SYSMON */