Yuri Tikhonov | 8f15d4a | 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 | |
| 28 | #ifdef CONFIG_POST |
| 29 | |
| 30 | /* |
| 31 | * SYSMON test |
| 32 | * |
| 33 | * This test performs the system hardware monitoring. |
| 34 | * The test passes when all the following voltages and temperatures |
| 35 | * are within allowed ranges: |
| 36 | * |
Wolfgang Denk | aa6f6d1 | 2008-03-26 00:52:10 +0100 | [diff] [blame] | 37 | * Temperature -40 .. +85 C |
| 38 | * +5V +4.75 .. +5.25 V |
| 39 | * +5V standby +4.75 .. +5.25 V |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 40 | * |
| 41 | * LCD backlight is not enabled if temperature values are not within |
| 42 | * allowed ranges (-30 .. + 80). The brightness of backlite can be |
| 43 | * controlled by setting "brightness" enviroment variable. Default value is 50% |
| 44 | * |
| 45 | * See the list of all parameters in the sysmon_table below |
| 46 | */ |
| 47 | |
| 48 | #include <post.h> |
| 49 | #include <watchdog.h> |
| 50 | #include <i2c.h> |
| 51 | |
Yuri Tikhonov | 46bc0a9 | 2008-03-18 13:27:57 +0100 | [diff] [blame] | 52 | #if defined(CONFIG_VIDEO) |
| 53 | #include <mb862xx.h> |
| 54 | #endif |
| 55 | |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 56 | #if CONFIG_POST & CFG_POST_SYSMON |
| 57 | |
| 58 | DECLARE_GLOBAL_DATA_PTR; |
| 59 | |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 60 | /* from dspic.c */ |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 61 | extern int dspic_read(ushort reg, ushort *data); |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 62 | |
| 63 | #define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off) |
| 64 | |
| 65 | typedef struct sysmon_s sysmon_t; |
| 66 | typedef struct sysmon_table_s sysmon_table_t; |
| 67 | |
| 68 | static void sysmon_dspic_init (sysmon_t * this); |
| 69 | static int sysmon_dspic_read (sysmon_t * this, uint addr); |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 70 | static int sysmon_dspic_read_sgn (sysmon_t * this, uint addr); |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 71 | static void sysmon_backlight_disable (sysmon_table_t * this); |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 72 | |
| 73 | struct sysmon_s |
| 74 | { |
| 75 | uchar chip; |
| 76 | void (*init)(sysmon_t *); |
| 77 | int (*read)(sysmon_t *, uint); |
| 78 | }; |
| 79 | |
| 80 | static sysmon_t sysmon_dspic = |
| 81 | {CFG_I2C_DSPIC_IO_ADDR, sysmon_dspic_init, sysmon_dspic_read}; |
| 82 | |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 83 | static sysmon_t sysmon_dspic_sgn = |
| 84 | {CFG_I2C_DSPIC_IO_ADDR, sysmon_dspic_init, sysmon_dspic_read_sgn}; |
| 85 | |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 86 | static sysmon_t * sysmon_list[] = |
| 87 | { |
| 88 | &sysmon_dspic, |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 89 | &sysmon_dspic_sgn, |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 90 | NULL |
| 91 | }; |
| 92 | |
| 93 | struct sysmon_table_s |
| 94 | { |
| 95 | char * name; |
| 96 | char * unit_name; |
| 97 | sysmon_t * sysmon; |
| 98 | void (*exec_before)(sysmon_table_t *); |
| 99 | void (*exec_after)(sysmon_table_t *); |
| 100 | |
| 101 | int unit_precision; |
| 102 | int unit_div; |
| 103 | int unit_min; |
| 104 | int unit_max; |
| 105 | uint val_mask; |
| 106 | uint val_min; |
| 107 | uint val_max; |
| 108 | int val_valid; |
| 109 | uint val_min_alt; |
| 110 | uint val_max_alt; |
| 111 | int val_valid_alt; |
| 112 | uint addr; |
| 113 | }; |
| 114 | |
| 115 | static sysmon_table_t sysmon_table[] = |
| 116 | { |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 117 | {"Temperature", " C", &sysmon_dspic_sgn, NULL, sysmon_backlight_disable, |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 118 | 1, 1, -32768, 32767, 0xFFFF, 0x8000-40, 0x8000+85, 0, |
Wolfgang Denk | aa6f6d1 | 2008-03-26 00:52:10 +0100 | [diff] [blame] | 119 | 0x8000-30, 0x8000+80, 0, 0x12BC}, |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 120 | |
| 121 | {"+ 5 V", "V", &sysmon_dspic, NULL, NULL, |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 122 | 100, 1000, 0, 0xFFFF, 0xFFFF, 4750, 5250, 0, |
Wolfgang Denk | aa6f6d1 | 2008-03-26 00:52:10 +0100 | [diff] [blame] | 123 | 4750, 5250, 0, 0x12CA}, |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 124 | |
Yuri Tikhonov | 46bc0a9 | 2008-03-18 13:27:57 +0100 | [diff] [blame] | 125 | {"+ 5 V standby", "V", &sysmon_dspic, NULL, NULL, |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 126 | 100, 1000, 0, 0xFFFF, 0xFFFF, 4750, 5250, 0, |
Wolfgang Denk | aa6f6d1 | 2008-03-26 00:52:10 +0100 | [diff] [blame] | 127 | 4750, 5250, 0, 0x12C6}, |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 128 | }; |
| 129 | static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]); |
| 130 | |
| 131 | int sysmon_init_f (void) |
| 132 | { |
| 133 | sysmon_t ** l; |
| 134 | |
| 135 | for (l = sysmon_list; *l; l++) |
| 136 | (*l)->init(*l); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | void sysmon_reloc (void) |
| 142 | { |
| 143 | sysmon_t ** l; |
| 144 | sysmon_table_t * t; |
| 145 | |
| 146 | for (l = sysmon_list; *l; l++) { |
| 147 | RELOC(*l); |
| 148 | RELOC((*l)->init); |
| 149 | RELOC((*l)->read); |
| 150 | } |
| 151 | |
| 152 | for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) { |
| 153 | RELOC(t->exec_before); |
| 154 | RELOC(t->exec_after); |
| 155 | RELOC(t->sysmon); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static char *sysmon_unit_value (sysmon_table_t *s, uint val) |
| 160 | { |
| 161 | static char buf[32]; |
| 162 | char *p, sign; |
| 163 | int decimal, frac; |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 164 | int unit_val = |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 165 | s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask; |
| 166 | |
| 167 | if (val == -1) |
| 168 | return "I/O ERROR"; |
| 169 | |
| 170 | if (unit_val < 0) { |
| 171 | sign = '-'; |
| 172 | unit_val = -unit_val; |
| 173 | } else |
| 174 | sign = '+'; |
| 175 | |
| 176 | p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div); |
| 177 | |
| 178 | |
| 179 | frac = unit_val % s->unit_div; |
| 180 | |
| 181 | frac /= (s->unit_div / s->unit_precision); |
| 182 | |
| 183 | decimal = s->unit_precision; |
| 184 | |
| 185 | if (decimal != 1) |
| 186 | *p++ = '.'; |
| 187 | for (decimal /= 10; decimal != 0; decimal /= 10) |
| 188 | *p++ = '0' + (frac / decimal) % 10; |
| 189 | strcpy(p, s->unit_name); |
| 190 | |
| 191 | return buf; |
| 192 | } |
| 193 | |
| 194 | static void sysmon_dspic_init (sysmon_t * this) |
| 195 | { |
| 196 | } |
| 197 | |
| 198 | static int sysmon_dspic_read (sysmon_t * this, uint addr) |
| 199 | { |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 200 | ushort data; |
| 201 | |
| 202 | return (dspic_read(addr, &data)) ? -1 : data; |
| 203 | } |
| 204 | |
| 205 | static int sysmon_dspic_read_sgn (sysmon_t * this, uint addr) |
| 206 | { |
| 207 | ushort data; |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 208 | |
| 209 | /* To fit into the table range we should add 0x8000 */ |
Yuri Tikhonov | ff2bdfb | 2008-03-24 11:29:14 +0100 | [diff] [blame] | 210 | return (dspic_read(addr, &data)) ? -1 : |
| 211 | (signed short)data + 0x8000; |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void sysmon_backlight_disable (sysmon_table_t * this) |
| 215 | { |
Yuri Tikhonov | 46bc0a9 | 2008-03-18 13:27:57 +0100 | [diff] [blame] | 216 | #if defined(CONFIG_VIDEO) |
| 217 | board_backlight_switch(this->val_valid_alt); |
| 218 | #endif |
Yuri Tikhonov | 8f15d4a | 2008-02-04 14:10:42 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | int sysmon_post_test (int flags) |
| 222 | { |
| 223 | int res = 0; |
| 224 | sysmon_table_t * t; |
| 225 | int val; |
| 226 | |
| 227 | for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) { |
| 228 | if (t->exec_before) |
| 229 | t->exec_before(t); |
| 230 | |
| 231 | val = t->sysmon->read(t->sysmon, t->addr); |
| 232 | if (val != -1) { |
| 233 | t->val_valid = val >= t->val_min && val <= t->val_max; |
| 234 | t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt; |
| 235 | } else { |
| 236 | t->val_valid = 0; |
| 237 | t->val_valid_alt = 0; |
| 238 | } |
| 239 | |
| 240 | if (t->exec_after) |
| 241 | t->exec_after(t); |
| 242 | |
| 243 | if ((!t->val_valid) || (flags & POST_MANUAL)) { |
| 244 | printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val)); |
| 245 | printf("allowed range"); |
| 246 | printf(" %-8s ..", sysmon_unit_value(t, t->val_min)); |
| 247 | printf(" %-8s", sysmon_unit_value(t, t->val_max)); |
| 248 | printf(" %s\n", t->val_valid ? "OK" : "FAIL"); |
| 249 | } |
| 250 | |
| 251 | if (!t->val_valid) |
| 252 | res = 1; |
| 253 | } |
| 254 | |
| 255 | return res; |
| 256 | } |
| 257 | |
| 258 | #endif /* CONFIG_POST & CFG_POST_SYSMON */ |
| 259 | #endif /* CONFIG_POST */ |