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 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 55 | DECLARE_GLOBAL_DATA_PTR; |
| 56 | |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 57 | static int sysmon_temp_invalid = 0; |
| 58 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 59 | /* #define DEBUG */ |
| 60 | |
| 61 | #define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off) |
| 62 | |
| 63 | typedef struct sysmon_s sysmon_t; |
| 64 | typedef struct sysmon_table_s sysmon_table_t; |
| 65 | |
| 66 | static void sysmon_lm87_init (sysmon_t * this); |
| 67 | static void sysmon_pic_init (sysmon_t * this); |
| 68 | static uint sysmon_i2c_read (sysmon_t * this, uint addr); |
| 69 | static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr); |
| 70 | static void sysmon_ccfl_disable (sysmon_table_t * this); |
| 71 | static void sysmon_ccfl_enable (sysmon_table_t * this); |
| 72 | |
| 73 | struct sysmon_s |
| 74 | { |
| 75 | uchar chip; |
| 76 | void (*init)(sysmon_t *); |
| 77 | uint (*read)(sysmon_t *, uint); |
| 78 | }; |
| 79 | |
| 80 | static sysmon_t sysmon_lm87 = |
| 81 | {CFG_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read}; |
| 82 | static sysmon_t sysmon_lm87_sgn = |
| 83 | {CFG_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read_sgn}; |
| 84 | static sysmon_t sysmon_pic = |
| 85 | {CFG_I2C_PICIO_ADDR, sysmon_pic_init, sysmon_i2c_read}; |
| 86 | |
| 87 | static sysmon_t * sysmon_list[] = |
| 88 | { |
| 89 | &sysmon_lm87, |
| 90 | &sysmon_lm87_sgn, |
| 91 | &sysmon_pic, |
| 92 | NULL |
| 93 | }; |
| 94 | |
| 95 | struct sysmon_table_s |
| 96 | { |
| 97 | char * name; |
| 98 | char * unit_name; |
| 99 | sysmon_t * sysmon; |
| 100 | void (*exec_before)(sysmon_table_t *); |
| 101 | void (*exec_after)(sysmon_table_t *); |
| 102 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 103 | int unit_precision; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 104 | int unit_div; |
| 105 | int unit_min; |
| 106 | int unit_max; |
| 107 | uint val_mask; |
| 108 | uint val_min; |
| 109 | uint val_max; |
| 110 | int val_valid; |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 111 | uint val_min_alt; |
| 112 | uint val_max_alt; |
| 113 | int val_valid_alt; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 114 | uint addr; |
| 115 | }; |
| 116 | |
| 117 | static sysmon_table_t sysmon_table[] = |
| 118 | { |
| 119 | {"Board temperature", " C", &sysmon_lm87_sgn, NULL, sysmon_ccfl_disable, |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 120 | 1, 1, -128, 127, 0xFF, 0x58, 0xD5, 0, 0x6C, 0xC6, 0, 0x27}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 121 | |
| 122 | {"Front temperature", " C", &sysmon_lm87, NULL, sysmon_ccfl_disable, |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 123 | 1, 100, -27316, 8984, 0xFF, 0xA4, 0xFC, 0, 0xB2, 0xF1, 0, 0x29}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 124 | |
| 125 | {"+3.3V CPU logic", "V", &sysmon_lm87, NULL, NULL, |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 126 | 100, 1000, 0, 4386, 0xFF, 0xB6, 0xC9, 0, 0xB6, 0xC9, 0, 0x22}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 127 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 128 | {"+ 5 V logic", "V", &sysmon_lm87, NULL, NULL, |
| 129 | 100, 1000, 0, 6630, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x23}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 130 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 131 | {"+12 V PCMCIA", "V", &sysmon_lm87, NULL, NULL, |
| 132 | 100, 1000, 0, 15460, 0xFF, 0xBC, 0xD0, 0, 0xBC, 0xD0, 0, 0x21}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 133 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 134 | {"+12 V CCFL", "V", &sysmon_lm87, NULL, sysmon_ccfl_enable, |
| 135 | 100, 1000, 0, 15900, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x24}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 136 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 137 | {"+ 5 V standby", "V", &sysmon_pic, NULL, NULL, |
| 138 | 100, 1000, 0, 6040, 0xFF, 0xC8, 0xDE, 0, 0xC8, 0xDE, 0, 0x7C}, |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 139 | }; |
| 140 | static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]); |
| 141 | |
| 142 | static int conversion_done = 0; |
| 143 | |
| 144 | |
| 145 | int sysmon_init_f (void) |
| 146 | { |
| 147 | sysmon_t ** l; |
| 148 | ulong reg; |
| 149 | |
| 150 | /* Power on CCFL, PCMCIA */ |
| 151 | reg = pic_read (0x60); |
| 152 | reg |= 0x09; |
| 153 | pic_write (0x60, reg); |
| 154 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 155 | for (l = sysmon_list; *l; l++) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 156 | (*l)->init(*l); |
| 157 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 158 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | void sysmon_reloc (void) |
| 163 | { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 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 | |
Wolfgang Denk | 15f36a5 | 2005-07-28 10:42:26 +0200 | [diff] [blame] | 188 | if (val == -1) { |
| 189 | return "I/O ERROR"; |
| 190 | } |
| 191 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 192 | if (unit_val < 0) { |
| 193 | sign = '-'; |
| 194 | unit_val = -unit_val; |
| 195 | } else { |
| 196 | sign = '+'; |
| 197 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 198 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 199 | p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 200 | |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 201 | |
| 202 | frac = unit_val % s->unit_div; |
| 203 | |
| 204 | frac /= (s->unit_div / s->unit_precision); |
| 205 | |
| 206 | dec = s->unit_precision; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 207 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 208 | if (dec != 1) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 209 | *p++ = '.'; |
| 210 | } |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 211 | for (dec /= 10; dec != 0; dec /= 10) { |
wdenk | 8564acf | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 212 | *p++ = '0' + (frac / dec) % 10; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 213 | } |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 214 | strcpy(p, s->unit_name); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 215 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 216 | return buf; |
| 217 | } |
| 218 | |
| 219 | static void sysmon_lm87_init (sysmon_t * this) |
| 220 | { |
| 221 | uchar val; |
| 222 | |
| 223 | /* Detect LM87 chip */ |
| 224 | if (i2c_read(this->chip, 0x40, 1, &val, 1) || (val & 0x80) != 0 || |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 225 | i2c_read(this->chip, 0x3E, 1, &val, 1) || val != 0x02) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 226 | printf("Error: LM87 not found at 0x%02X\n", this->chip); |
| 227 | return; |
| 228 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 229 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 230 | /* Configure pins 5,6 as AIN */ |
| 231 | val = 0x03; |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 232 | if (i2c_write(this->chip, 0x16, 1, &val, 1)) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 233 | printf("Error: can't write LM87 config register\n"); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | /* Start monitoring */ |
| 238 | val = 0x01; |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 239 | if (i2c_write(this->chip, 0x40, 1, &val, 1)) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 240 | printf("Error: can't write LM87 config register\n"); |
| 241 | return; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | static void sysmon_pic_init (sysmon_t * this) |
| 246 | { |
| 247 | } |
| 248 | |
| 249 | static uint sysmon_i2c_read (sysmon_t * this, uint addr) |
| 250 | { |
| 251 | uchar val; |
| 252 | uint res = i2c_read(this->chip, addr, 1, &val, 1); |
| 253 | |
| 254 | return res == 0 ? val : -1; |
| 255 | } |
| 256 | |
| 257 | static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr) |
| 258 | { |
| 259 | uchar val; |
| 260 | return i2c_read(this->chip, addr, 1, &val, 1) == 0 ? |
| 261 | 128 + (signed char)val : -1; |
| 262 | } |
| 263 | |
| 264 | static void sysmon_ccfl_disable (sysmon_table_t * this) |
| 265 | { |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 266 | if (!this->val_valid_alt) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 267 | sysmon_temp_invalid = 1; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static void sysmon_ccfl_enable (sysmon_table_t * this) |
| 272 | { |
| 273 | ulong reg; |
| 274 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 275 | if (!sysmon_temp_invalid) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 276 | reg = pic_read (0x60); |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 277 | reg |= 0x06; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 278 | pic_write (0x60, reg); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | int sysmon_post_test (int flags) |
| 283 | { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 284 | int res = 0; |
| 285 | sysmon_table_t * t; |
| 286 | uint val; |
| 287 | |
| 288 | /* |
| 289 | * The A/D conversion on the LM87 sensor takes 300 ms. |
| 290 | */ |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 291 | if (! conversion_done) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 292 | while (post_time_ms(gd->post_init_f_time) < 300) WATCHDOG_RESET (); |
| 293 | conversion_done = 1; |
| 294 | } |
| 295 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 296 | for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) { |
| 297 | if (t->exec_before) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 298 | t->exec_before(t); |
| 299 | } |
| 300 | |
| 301 | val = t->sysmon->read(t->sysmon, t->addr); |
Wolfgang Denk | 15f36a5 | 2005-07-28 10:42:26 +0200 | [diff] [blame] | 302 | if (val != -1) { |
| 303 | t->val_valid = val >= t->val_min && val <= t->val_max; |
| 304 | t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt; |
| 305 | } else { |
| 306 | t->val_valid = 0; |
| 307 | t->val_valid_alt = 0; |
| 308 | } |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 309 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 310 | if (t->exec_after) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 311 | t->exec_after(t); |
| 312 | } |
| 313 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 314 | if ((!t->val_valid) || (flags & POST_MANUAL)) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 315 | printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val)); |
| 316 | printf("allowed range"); |
| 317 | printf(" %-8s ..", sysmon_unit_value(t, t->val_min)); |
| 318 | printf(" %-8s", sysmon_unit_value(t, t->val_max)); |
| 319 | printf(" %s\n", t->val_valid ? "OK" : "FAIL"); |
| 320 | } |
| 321 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 322 | if (!t->val_valid) { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 323 | res = -1; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return res; |
| 328 | } |
| 329 | |
| 330 | #endif /* CONFIG_POST & CFG_POST_SYSMON */ |
| 331 | #endif /* CONFIG_POST */ |