2 * (C) Copyright 2008 Dmitry Rakhchev, EmCraft Systems, rda@emcraft.com
4 * Developed for DENX Software Engineering GmbH
6 * SPDX-License-Identifier: GPL-2.0+
15 * This test performs the system hardware monitoring.
16 * The test passes when all the following voltages and temperatures
17 * are within allowed ranges:
19 * Temperature -40 .. +90 C
20 * +5V +4.50 .. +5.50 V
21 * +5V standby +3.50 .. +5.50 V
23 * LCD backlight is not enabled if temperature values are not within
24 * allowed ranges (-30 .. + 80). The brightness of backlite can be
25 * controlled by setting "brightness" environment variable. Default value is 50%
27 * See the list of all parameters in the sysmon_table below
34 #if defined(CONFIG_VIDEO)
38 #if CONFIG_POST & CONFIG_SYS_POST_SYSMON
40 DECLARE_GLOBAL_DATA_PTR;
43 extern int dspic_read(ushort reg, ushort *data);
45 #define REG_TEMPERATURE 0x12BC
46 #define REG_VOLTAGE_5V 0x12CA
47 #define REG_VOLTAGE_5V_STANDBY 0x12C6
49 #define TEMPERATURE_MIN (-40) /* degr. C */
50 #define TEMPERATURE_MAX (+90) /* degr. C */
51 #define TEMPERATURE_DISPLAY_MIN (-35) /* degr. C */
52 #define TEMPERATURE_DISPLAY_MAX (+85) /* degr. C */
54 #define VOLTAGE_5V_MIN (+4500) /* mV */
55 #define VOLTAGE_5V_MAX (+5500) /* mV */
57 #define VOLTAGE_5V_STANDBY_MIN (+3500) /* mV */
58 #define VOLTAGE_5V_STANDBY_MAX (+5500) /* mV */
60 typedef struct sysmon_s sysmon_t;
61 typedef struct sysmon_table_s sysmon_table_t;
63 static void sysmon_dspic_init(sysmon_t *this);
64 static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val);
65 static int sysmon_dspic_read_sgn(sysmon_t *this, uint addr, int *val);
66 static void sysmon_backlight_disable(sysmon_table_t *this);
70 void (*init)(sysmon_t *);
71 int (*read)(sysmon_t *, uint, int *);
74 static sysmon_t sysmon_dspic = {
75 CONFIG_SYS_I2C_DSPIC_IO_ADDR,
80 static sysmon_t sysmon_dspic_sgn = {
81 CONFIG_SYS_I2C_DSPIC_IO_ADDR,
86 static sysmon_t *sysmon_list[] = {
91 struct sysmon_table_s {
95 void (*exec_before)(sysmon_table_t *);
96 void (*exec_after)(sysmon_table_t *);
112 static sysmon_table_t sysmon_table[] = {
114 "Temperature", " C", &sysmon_dspic, NULL, sysmon_backlight_disable,
115 1, 1, -32768, 32767, 0xFFFF,
116 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
117 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
122 "+ 5 V", "V", &sysmon_dspic, NULL, NULL,
123 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
124 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
125 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
130 "+ 5 V standby", "V", &sysmon_dspic, NULL, NULL,
131 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
132 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
133 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
134 REG_VOLTAGE_5V_STANDBY,
138 "Temperature", "°C", &sysmon_dspic_sgn, NULL, sysmon_backlight_disable,
139 1, 1, -32768, 32767, 0xFFFF,
140 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
141 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
146 int sysmon_init_f(void)
150 for (l = sysmon_list; *l; l++)
156 void sysmon_reloc(void)
158 /* Do nothing for now, sysmon_reloc() is required by the sysmon post */
161 static char *sysmon_unit_value(sysmon_table_t *s, uint val)
168 unit_val = s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
175 unit_val = -unit_val;
180 p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
182 frac = unit_val % s->unit_div;
183 frac /= (s->unit_div / s->unit_precision);
185 decimal = s->unit_precision;
189 for (decimal /= 10; decimal != 0; decimal /= 10)
190 *p++ = '0' + (frac / decimal) % 10;
191 strcpy(p, s->unit_name);
196 static void sysmon_dspic_init(sysmon_t *this)
200 static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val)
204 if (dspic_read(addr, &data) == 0){
205 /* To fit into the table range we should add 0x8000 */
206 *val = data + 0x8000;
213 static int sysmon_dspic_read_sgn(sysmon_t *this, uint addr, int *val)
217 if (dspic_read(addr, &data) == 0){
218 /* To fit into the table range we should add 0x8000 */
219 *val = (signed short)data + 0x8000;
226 static void sysmon_backlight_disable(sysmon_table_t *this)
228 #if defined(CONFIG_VIDEO)
229 board_backlight_switch(this->val_valid_alt);
233 int sysmon_post_test(int flags)
239 for (t = sysmon_table; t < sysmon_table + ARRAY_SIZE(sysmon_table); t++) {
244 if (t->sysmon->read(t->sysmon, t->addr, &val) != 0) {
246 t->val_valid_alt = 0;
247 post_log(": read failed\n");
252 if (t->val_valid != 0) {
253 t->val_valid = val >= t->val_min && val <= t->val_max;
254 t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
260 if ((!t->val_valid) || (flags)) {
261 post_log("\n\t%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
262 post_log("allowed range");
263 post_log(" %-8s ..", sysmon_unit_value(t, t->val_min));
264 post_log(" %-8s", sysmon_unit_value(t, t->val_max));
265 post_log(" %s", t->val_valid ? "OK" : "FAIL");
277 #endif /* CONFIG_POST & CONFIG_SYS_POST_SYSMON */