wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <post.h> |
| 25 | #include <common.h> |
| 26 | |
| 27 | #ifdef CONFIG_POST |
| 28 | |
| 29 | /* |
| 30 | * SYSMON test |
| 31 | * |
| 32 | * This test performs the system hardware monitoring. |
| 33 | * The test passes when all the following voltages and temperatures |
| 34 | * are within allowed ranges: |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 35 | * |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 36 | * Board temperature |
| 37 | * Front temperature |
| 38 | * +3.3V CPU logic |
| 39 | * +5V logic |
| 40 | * +12V PCMCIA |
| 41 | * +12V CCFL |
| 42 | * +5V standby |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 43 | * |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 44 | * CCFL is not enabled if temperature values are not within allowed ranges |
| 45 | * |
| 46 | * See the list off all parameters in the sysmon_table below |
| 47 | */ |
| 48 | |
| 49 | #include <post.h> |
| 50 | #include <watchdog.h> |
| 51 | #include <i2c.h> |
| 52 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 53 | #if CONFIG_POST & CFG_POST_SYSMON |
| 54 | |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 55 | static int sysmon_temp_invalid = 0; |
| 56 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 57 | /* #define DEBUG */ |
| 58 | |
| 59 | #define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off) |
| 60 | |
| 61 | typedef struct sysmon_s sysmon_t; |
| 62 | typedef struct sysmon_table_s sysmon_table_t; |
| 63 | |
| 64 | static void sysmon_lm87_init (sysmon_t * this); |
| 65 | static void sysmon_pic_init (sysmon_t * this); |
| 66 | static uint sysmon_i2c_read (sysmon_t * this, uint addr); |
| 67 | static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr); |
| 68 | static void sysmon_ccfl_disable (sysmon_table_t * this); |
| 69 | static void sysmon_ccfl_enable (sysmon_table_t * this); |
| 70 | |
| 71 | struct sysmon_s |
| 72 | { |
| 73 | uchar chip; |
| 74 | void (*init)(sysmon_t *); |
| 75 | uint (*read)(sysmon_t *, uint); |
| 76 | }; |
| 77 | |
| 78 | static sysmon_t sysmon_lm87 = |
| 79 | {CFG_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read}; |
| 80 | static sysmon_t sysmon_lm87_sgn = |
| 81 | {CFG_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read_sgn}; |
| 82 | static sysmon_t sysmon_pic = |
| 83 | {CFG_I2C_PICIO_ADDR, sysmon_pic_init, sysmon_i2c_read}; |
| 84 | |
| 85 | static sysmon_t * sysmon_list[] = |
| 86 | { |
| 87 | &sysmon_lm87, |
| 88 | &sysmon_lm87_sgn, |
| 89 | &sysmon_pic, |
| 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 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 101 | int unit_precision; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 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; |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 109 | uint val_min_alt; |
| 110 | uint val_max_alt; |
| 111 | int val_valid_alt; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 112 | uint addr; |
| 113 | }; |
| 114 | |
| 115 | static sysmon_table_t sysmon_table[] = |
| 116 | { |
| 117 | {"Board temperature", " C", &sysmon_lm87_sgn, NULL, sysmon_ccfl_disable, |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 118 | 1, 1, -128, 127, 0xFF, 0x58, 0xD5, 0, 0x6C, 0xC6, 0, 0x27}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 119 | |
| 120 | {"Front temperature", " C", &sysmon_lm87, NULL, sysmon_ccfl_disable, |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 121 | 1, 100, -27316, 8984, 0xFF, 0xA4, 0xFC, 0, 0xB2, 0xF1, 0, 0x29}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 122 | |
| 123 | {"+3.3V CPU logic", "V", &sysmon_lm87, NULL, NULL, |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 124 | 100, 1000, 0, 4386, 0xFF, 0xB6, 0xC9, 0, 0xB6, 0xC9, 0, 0x22}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 125 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 126 | {"+ 5 V logic", "V", &sysmon_lm87, NULL, NULL, |
| 127 | 100, 1000, 0, 6630, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x23}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 128 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 129 | {"+12 V PCMCIA", "V", &sysmon_lm87, NULL, NULL, |
| 130 | 100, 1000, 0, 15460, 0xFF, 0xBC, 0xD0, 0, 0xBC, 0xD0, 0, 0x21}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 131 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 132 | {"+12 V CCFL", "V", &sysmon_lm87, NULL, sysmon_ccfl_enable, |
| 133 | 100, 1000, 0, 15900, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x24}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 134 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 135 | {"+ 5 V standby", "V", &sysmon_pic, NULL, NULL, |
| 136 | 100, 1000, 0, 6040, 0xFF, 0xC8, 0xDE, 0, 0xC8, 0xDE, 0, 0x7C}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 137 | }; |
| 138 | static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]); |
| 139 | |
| 140 | static int conversion_done = 0; |
| 141 | |
| 142 | |
| 143 | int sysmon_init_f (void) |
| 144 | { |
| 145 | sysmon_t ** l; |
| 146 | ulong reg; |
| 147 | |
| 148 | /* Power on CCFL, PCMCIA */ |
| 149 | reg = pic_read (0x60); |
| 150 | reg |= 0x09; |
| 151 | pic_write (0x60, reg); |
| 152 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 153 | for (l = sysmon_list; *l; l++) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 154 | (*l)->init(*l); |
| 155 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 156 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | void sysmon_reloc (void) |
| 161 | { |
| 162 | DECLARE_GLOBAL_DATA_PTR; |
| 163 | |
| 164 | sysmon_t ** l; |
| 165 | sysmon_table_t * t; |
| 166 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 167 | for (l = sysmon_list; *l; l++) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 168 | RELOC(*l); |
| 169 | RELOC((*l)->init); |
| 170 | RELOC((*l)->read); |
| 171 | } |
| 172 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 173 | for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 174 | RELOC(t->exec_before); |
| 175 | RELOC(t->exec_after); |
| 176 | RELOC(t->sysmon); |
| 177 | } |
| 178 | } |
| 179 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 180 | static char *sysmon_unit_value (sysmon_table_t *s, uint val) |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 181 | { |
| 182 | static char buf[32]; |
| 183 | int unit_val = |
| 184 | s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask; |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 185 | char *p, sign; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 186 | int dec, frac; |
| 187 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 188 | if (unit_val < 0) { |
| 189 | sign = '-'; |
| 190 | unit_val = -unit_val; |
| 191 | } else { |
| 192 | sign = '+'; |
| 193 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 194 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 195 | p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 196 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 197 | |
| 198 | frac = unit_val % s->unit_div; |
| 199 | |
| 200 | frac /= (s->unit_div / s->unit_precision); |
| 201 | |
| 202 | dec = s->unit_precision; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 203 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 204 | if (dec != 1) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 205 | *p++ = '.'; |
| 206 | } |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 207 | for (dec /= 10; dec != 0; dec /= 10) { |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 208 | *p++ = '0' + (frac / dec) % 10; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 209 | } |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 210 | strcpy(p, s->unit_name); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 211 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 212 | return buf; |
| 213 | } |
| 214 | |
| 215 | static void sysmon_lm87_init (sysmon_t * this) |
| 216 | { |
| 217 | uchar val; |
| 218 | |
| 219 | /* Detect LM87 chip */ |
| 220 | if (i2c_read(this->chip, 0x40, 1, &val, 1) || (val & 0x80) != 0 || |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 221 | i2c_read(this->chip, 0x3E, 1, &val, 1) || val != 0x02) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 222 | printf("Error: LM87 not found at 0x%02X\n", this->chip); |
| 223 | return; |
| 224 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 225 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 226 | /* Configure pins 5,6 as AIN */ |
| 227 | val = 0x03; |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 228 | if (i2c_write(this->chip, 0x16, 1, &val, 1)) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 229 | printf("Error: can't write LM87 config register\n"); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | /* Start monitoring */ |
| 234 | val = 0x01; |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 235 | if (i2c_write(this->chip, 0x40, 1, &val, 1)) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 236 | printf("Error: can't write LM87 config register\n"); |
| 237 | return; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static void sysmon_pic_init (sysmon_t * this) |
| 242 | { |
| 243 | } |
| 244 | |
| 245 | static uint sysmon_i2c_read (sysmon_t * this, uint addr) |
| 246 | { |
| 247 | uchar val; |
| 248 | uint res = i2c_read(this->chip, addr, 1, &val, 1); |
| 249 | |
| 250 | return res == 0 ? val : -1; |
| 251 | } |
| 252 | |
| 253 | static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr) |
| 254 | { |
| 255 | uchar val; |
| 256 | return i2c_read(this->chip, addr, 1, &val, 1) == 0 ? |
| 257 | 128 + (signed char)val : -1; |
| 258 | } |
| 259 | |
| 260 | static void sysmon_ccfl_disable (sysmon_table_t * this) |
| 261 | { |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 262 | if (!this->val_valid_alt) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 263 | sysmon_temp_invalid = 1; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static void sysmon_ccfl_enable (sysmon_table_t * this) |
| 268 | { |
| 269 | ulong reg; |
| 270 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 271 | if (!sysmon_temp_invalid) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 272 | reg = pic_read (0x60); |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 273 | reg |= 0x06; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 274 | pic_write (0x60, reg); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | int sysmon_post_test (int flags) |
| 279 | { |
| 280 | DECLARE_GLOBAL_DATA_PTR; |
| 281 | |
| 282 | int res = 0; |
| 283 | sysmon_table_t * t; |
| 284 | uint val; |
| 285 | |
| 286 | /* |
| 287 | * The A/D conversion on the LM87 sensor takes 300 ms. |
| 288 | */ |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 289 | if (! conversion_done) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 290 | while (post_time_ms(gd->post_init_f_time) < 300) WATCHDOG_RESET (); |
| 291 | conversion_done = 1; |
| 292 | } |
| 293 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 294 | for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) { |
| 295 | if (t->exec_before) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 296 | t->exec_before(t); |
| 297 | } |
| 298 | |
| 299 | val = t->sysmon->read(t->sysmon, t->addr); |
| 300 | t->val_valid = val >= t->val_min && val <= t->val_max; |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 301 | t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 302 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 303 | if (t->exec_after) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 304 | t->exec_after(t); |
| 305 | } |
| 306 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 307 | if ((!t->val_valid) || (flags & POST_MANUAL)) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 308 | printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val)); |
| 309 | printf("allowed range"); |
| 310 | printf(" %-8s ..", sysmon_unit_value(t, t->val_min)); |
| 311 | printf(" %-8s", sysmon_unit_value(t, t->val_max)); |
| 312 | printf(" %s\n", t->val_valid ? "OK" : "FAIL"); |
| 313 | } |
| 314 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 315 | if (!t->val_valid) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 316 | res = -1; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return res; |
| 321 | } |
| 322 | |
| 323 | #endif /* CONFIG_POST & CFG_POST_SYSMON */ |
| 324 | #endif /* CONFIG_POST */ |