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