Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008 Dmitry Rakhchev, EmCraft Systems, rda@emcraft.com |
| 3 | * |
| 4 | * Developed for DENX Software Engineering GmbH |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <post.h> |
| 26 | #include <common.h> |
| 27 | |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 28 | /* |
| 29 | * SYSMON test |
| 30 | * |
| 31 | * This test performs the system hardware monitoring. |
| 32 | * The test passes when all the following voltages and temperatures |
| 33 | * are within allowed ranges: |
| 34 | * |
Sascha Laue | 6aee00f | 2008-04-01 10:10:18 +0200 | [diff] [blame] | 35 | * Temperature -40 .. +90 C |
| 36 | * +5V +4.50 .. +5.50 V |
| 37 | * +5V standby +3.50 .. +5.50 V |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 38 | * |
| 39 | * LCD backlight is not enabled if temperature values are not within |
| 40 | * allowed ranges (-30 .. + 80). The brightness of backlite can be |
| 41 | * controlled by setting "brightness" enviroment variable. Default value is 50% |
| 42 | * |
| 43 | * See the list of all parameters in the sysmon_table below |
| 44 | */ |
| 45 | |
| 46 | #include <post.h> |
| 47 | #include <watchdog.h> |
| 48 | #include <i2c.h> |
| 49 | |
Yuri Tikhonov | 0f855a1 | 2008-03-18 13:27:57 +0100 | [diff] [blame] | 50 | #if defined(CONFIG_VIDEO) |
| 51 | #include <mb862xx.h> |
| 52 | #endif |
| 53 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 54 | #if CONFIG_POST & CONFIG_SYS_POST_SYSMON |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 55 | |
| 56 | DECLARE_GLOBAL_DATA_PTR; |
| 57 | |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 58 | /* from dspic.c */ |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 59 | extern int dspic_read(ushort reg, ushort *data); |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 60 | |
Sascha Laue | 6aee00f | 2008-04-01 10:10:18 +0200 | [diff] [blame] | 61 | #define REG_TEMPERATURE 0x12BC |
| 62 | #define REG_VOLTAGE_5V 0x12CA |
| 63 | #define REG_VOLTAGE_5V_STANDBY 0x12C6 |
| 64 | |
| 65 | #define TEMPERATURE_MIN (-40) /* degr. C */ |
| 66 | #define TEMPERATURE_MAX (+90) /* degr. C */ |
| 67 | #define TEMPERATURE_DISPLAY_MIN (-35) /* degr. C */ |
Wolfgang Denk | 6adf61d | 2008-05-04 12:10:33 +0200 | [diff] [blame] | 68 | #define TEMPERATURE_DISPLAY_MAX (+85) /* degr. C */ |
Sascha Laue | 6aee00f | 2008-04-01 10:10:18 +0200 | [diff] [blame] | 69 | |
| 70 | #define VOLTAGE_5V_MIN (+4500) /* mV */ |
| 71 | #define VOLTAGE_5V_MAX (+5500) /* mV */ |
| 72 | |
| 73 | #define VOLTAGE_5V_STANDBY_MIN (+3500) /* mV */ |
| 74 | #define VOLTAGE_5V_STANDBY_MAX (+5500) /* mV */ |
| 75 | |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 76 | typedef struct sysmon_s sysmon_t; |
| 77 | typedef struct sysmon_table_s sysmon_table_t; |
| 78 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 79 | static void sysmon_dspic_init(sysmon_t *this); |
| 80 | static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val); |
| 81 | static int sysmon_dspic_read_sgn(sysmon_t *this, uint addr, int *val); |
| 82 | static void sysmon_backlight_disable(sysmon_table_t *this); |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 83 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 84 | struct sysmon_s { |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 85 | uchar chip; |
| 86 | void (*init)(sysmon_t *); |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 87 | int (*read)(sysmon_t *, uint, int *); |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 90 | static sysmon_t sysmon_dspic = { |
| 91 | CONFIG_SYS_I2C_DSPIC_IO_ADDR, |
| 92 | sysmon_dspic_init, |
| 93 | sysmon_dspic_read |
| 94 | }; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 95 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 96 | static sysmon_t sysmon_dspic_sgn = { |
| 97 | CONFIG_SYS_I2C_DSPIC_IO_ADDR, |
| 98 | sysmon_dspic_init, |
| 99 | sysmon_dspic_read_sgn |
| 100 | }; |
| 101 | |
| 102 | static sysmon_t *sysmon_list[] = { |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 103 | &sysmon_dspic, |
| 104 | NULL |
| 105 | }; |
| 106 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 107 | struct sysmon_table_s { |
| 108 | char *name; |
| 109 | char *unit_name; |
| 110 | sysmon_t *sysmon; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 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 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 128 | static sysmon_table_t sysmon_table[] = { |
Sascha Laue | 6aee00f | 2008-04-01 10:10:18 +0200 | [diff] [blame] | 129 | { |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 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, |
Sascha Laue | 6aee00f | 2008-04-01 10:10:18 +0200 | [diff] [blame] | 135 | }, |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 136 | |
Sascha Laue | 6aee00f | 2008-04-01 10:10:18 +0200 | [diff] [blame] | 137 | { |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 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, |
Sascha Laue | 6aee00f | 2008-04-01 10:10:18 +0200 | [diff] [blame] | 143 | }, |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 144 | |
Sascha Laue | 6aee00f | 2008-04-01 10:10:18 +0200 | [diff] [blame] | 145 | { |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 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, |
Sascha Laue | 6aee00f | 2008-04-01 10:10:18 +0200 | [diff] [blame] | 159 | }, |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 160 | }; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 161 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 162 | int sysmon_init_f(void) |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 163 | { |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 164 | sysmon_t **l; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 165 | |
| 166 | for (l = sysmon_list; *l; l++) |
| 167 | (*l)->init(*l); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 172 | void sysmon_reloc(void) |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 173 | { |
Peter Tyser | 80f73b9 | 2009-09-21 11:20:34 -0500 | [diff] [blame] | 174 | /* Do nothing for now, sysmon_reloc() is required by the sysmon post */ |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 177 | static char *sysmon_unit_value(sysmon_table_t *s, uint val) |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 178 | { |
| 179 | static char buf[32]; |
| 180 | char *p, sign; |
| 181 | int decimal, frac; |
| 182 | int unit_val; |
| 183 | |
Wolfgang Denk | 7ed4011 | 2008-04-26 00:34:42 +0200 | [diff] [blame] | 184 | unit_val = s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 185 | |
| 186 | if (val == -1) |
| 187 | return "I/O ERROR"; |
| 188 | |
| 189 | if (unit_val < 0) { |
| 190 | sign = '-'; |
| 191 | unit_val = -unit_val; |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 192 | } else { |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 193 | sign = '+'; |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 194 | } |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 195 | |
| 196 | p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div); |
| 197 | |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 198 | frac = unit_val % s->unit_div; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 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 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 212 | static void sysmon_dspic_init(sysmon_t *this) |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 213 | { |
| 214 | } |
| 215 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 216 | static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val) |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 217 | { |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 218 | ushort data; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 219 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 220 | if (dspic_read(addr, &data) == 0){ |
| 221 | /* To fit into the table range we should add 0x8000 */ |
| 222 | *val = data + 0x8000; |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | return -1; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 229 | static 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 | /* To fit into the table range we should add 0x8000 */ |
| 235 | *val = (signed short)data + 0x8000; |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | static void sysmon_backlight_disable(sysmon_table_t *this) |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 243 | { |
Yuri Tikhonov | 0f855a1 | 2008-03-18 13:27:57 +0100 | [diff] [blame] | 244 | #if defined(CONFIG_VIDEO) |
| 245 | board_backlight_switch(this->val_valid_alt); |
| 246 | #endif |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 249 | int sysmon_post_test(int flags) |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 250 | { |
| 251 | int res = 0; |
| 252 | sysmon_table_t * t; |
| 253 | int val; |
| 254 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 255 | for (t = sysmon_table; t < sysmon_table + ARRAY_SIZE(sysmon_table); t++) { |
| 256 | t->val_valid = 1; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 257 | if (t->exec_before) |
| 258 | t->exec_before(t); |
| 259 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 260 | if (t->sysmon->read(t->sysmon, t->addr, &val) != 0) { |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 261 | t->val_valid = 0; |
| 262 | t->val_valid_alt = 0; |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 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; |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if (t->exec_after) |
| 274 | t->exec_after(t); |
| 275 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 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"); |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 284 | if (!t->val_valid) { |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 285 | res = 1; |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 286 | break; |
| 287 | } |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 288 | } |
Sascha Laue | f14ae41 | 2010-08-19 09:38:56 +0200 | [diff] [blame] | 289 | post_log("\n"); |
Yuri Tikhonov | 65b20dc | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 290 | |
| 291 | return res; |
| 292 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 293 | #endif /* CONFIG_POST & CONFIG_SYS_POST_SYSMON */ |