1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <post.h>
26#include <common.h>
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46#include <post.h>
47#include <watchdog.h>
48#include <i2c.h>
49
50#if defined(CONFIG_VIDEO)
51#include <mb862xx.h>
52#endif
53
54#if CONFIG_POST & CONFIG_SYS_POST_SYSMON
55
56DECLARE_GLOBAL_DATA_PTR;
57
58
59extern int dspic_read(ushort reg, ushort *data);
60
61#define REG_TEMPERATURE 0x12BC
62#define REG_VOLTAGE_5V 0x12CA
63#define REG_VOLTAGE_5V_STANDBY 0x12C6
64
65#define TEMPERATURE_MIN (-40)
66#define TEMPERATURE_MAX (+90)
67#define TEMPERATURE_DISPLAY_MIN (-35)
68#define TEMPERATURE_DISPLAY_MAX (+85)
69
70#define VOLTAGE_5V_MIN (+4500)
71#define VOLTAGE_5V_MAX (+5500)
72
73#define VOLTAGE_5V_STANDBY_MIN (+3500)
74#define VOLTAGE_5V_STANDBY_MAX (+5500)
75
76typedef struct sysmon_s sysmon_t;
77typedef struct sysmon_table_s sysmon_table_t;
78
79static void sysmon_dspic_init(sysmon_t *this);
80static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val);
81static int sysmon_dspic_read_sgn(sysmon_t *this, uint addr, int *val);
82static void sysmon_backlight_disable(sysmon_table_t *this);
83
84struct sysmon_s {
85 uchar chip;
86 void (*init)(sysmon_t *);
87 int (*read)(sysmon_t *, uint, int *);
88};
89
90static sysmon_t sysmon_dspic = {
91 CONFIG_SYS_I2C_DSPIC_IO_ADDR,
92 sysmon_dspic_init,
93 sysmon_dspic_read
94};
95
96static sysmon_t sysmon_dspic_sgn = {
97 CONFIG_SYS_I2C_DSPIC_IO_ADDR,
98 sysmon_dspic_init,
99 sysmon_dspic_read_sgn
100};
101
102static sysmon_t *sysmon_list[] = {
103 &sysmon_dspic,
104 NULL
105};
106
107struct sysmon_table_s {
108 char *name;
109 char *unit_name;
110 sysmon_t *sysmon;
111 void (*exec_before)(sysmon_table_t *);
112 void (*exec_after)(sysmon_table_t *);
113
114 int unit_precision;
115 int unit_div;
116 int unit_min;
117 int unit_max;
118 uint val_mask;
119 uint val_min;
120 uint val_max;
121 int val_valid;
122 uint val_min_alt;
123 uint val_max_alt;
124 int val_valid_alt;
125 uint addr;
126};
127
128static sysmon_table_t sysmon_table[] = {
129 {
130 "Temperature", " C", &sysmon_dspic, NULL, sysmon_backlight_disable,
131 1, 1, -32768, 32767, 0xFFFF,
132 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
133 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
134 REG_TEMPERATURE,
135 },
136
137 {
138 "+ 5 V", "V", &sysmon_dspic, NULL, NULL,
139 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
140 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
141 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
142 REG_VOLTAGE_5V,
143 },
144
145 {
146 "+ 5 V standby", "V", &sysmon_dspic, NULL, NULL,
147 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
148 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
149 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
150 REG_VOLTAGE_5V_STANDBY,
151 },
152
153 {
154 "Temperature", "°C", &sysmon_dspic_sgn, NULL, sysmon_backlight_disable,
155 1, 1, -32768, 32767, 0xFFFF,
156 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
157 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
158 REG_TEMPERATURE,
159 },
160};
161
162int sysmon_init_f(void)
163{
164 sysmon_t **l;
165
166 for (l = sysmon_list; *l; l++)
167 (*l)->init(*l);
168
169 return 0;
170}
171
172void sysmon_reloc(void)
173{
174
175}
176
177static char *sysmon_unit_value(sysmon_table_t *s, uint val)
178{
179 static char buf[32];
180 char *p, sign;
181 int decimal, frac;
182 int unit_val;
183
184 unit_val = s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
185
186 if (val == -1)
187 return "I/O ERROR";
188
189 if (unit_val < 0) {
190 sign = '-';
191 unit_val = -unit_val;
192 } else {
193 sign = '+';
194 }
195
196 p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
197
198 frac = unit_val % s->unit_div;
199 frac /= (s->unit_div / s->unit_precision);
200
201 decimal = s->unit_precision;
202
203 if (decimal != 1)
204 *p++ = '.';
205 for (decimal /= 10; decimal != 0; decimal /= 10)
206 *p++ = '0' + (frac / decimal) % 10;
207 strcpy(p, s->unit_name);
208
209 return buf;
210}
211
212static void sysmon_dspic_init(sysmon_t *this)
213{
214}
215
216static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val)
217{
218 ushort data;
219
220 if (dspic_read(addr, &data) == 0){
221
222 *val = data + 0x8000;
223 return 0;
224 }
225
226 return -1;
227}
228
229static int sysmon_dspic_read_sgn(sysmon_t *this, uint addr, int *val)
230{
231 ushort data;
232
233 if (dspic_read(addr, &data) == 0){
234
235 *val = (signed short)data + 0x8000;
236 return 0;
237 }
238
239 return -1;
240}
241
242static void sysmon_backlight_disable(sysmon_table_t *this)
243{
244#if defined(CONFIG_VIDEO)
245 board_backlight_switch(this->val_valid_alt);
246#endif
247}
248
249int sysmon_post_test(int flags)
250{
251 int res = 0;
252 sysmon_table_t * t;
253 int val;
254
255 for (t = sysmon_table; t < sysmon_table + ARRAY_SIZE(sysmon_table); t++) {
256 t->val_valid = 1;
257 if (t->exec_before)
258 t->exec_before(t);
259
260 if (t->sysmon->read(t->sysmon, t->addr, &val) != 0) {
261 t->val_valid = 0;
262 t->val_valid_alt = 0;
263 post_log(": read failed\n");
264 res = 1;
265 break;
266 }
267
268 if (t->val_valid != 0) {
269 t->val_valid = val >= t->val_min && val <= t->val_max;
270 t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
271 }
272
273 if (t->exec_after)
274 t->exec_after(t);
275
276 if ((!t->val_valid) || (flags)) {
277 post_log("\n\t%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
278 post_log("allowed range");
279 post_log(" %-8s ..", sysmon_unit_value(t, t->val_min));
280 post_log(" %-8s", sysmon_unit_value(t, t->val_max));
281 post_log(" %s", t->val_valid ? "OK" : "FAIL");
282 }
283
284 if (!t->val_valid) {
285 res = 1;
286 break;
287 }
288 }
289 post_log("\n");
290
291 return res;
292}
293#endif
294