wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 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 <common.h> |
| 25 | #include <console.h> |
| 26 | #include <watchdog.h> |
| 27 | #include <post.h> |
| 28 | |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 29 | #ifdef CONFIG_LOGBUFFER |
| 30 | #include <logbuff.h> |
| 31 | #endif |
| 32 | |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 33 | #ifdef CONFIG_POST |
| 34 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 35 | DECLARE_GLOBAL_DATA_PTR; |
| 36 | |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 37 | #define POST_MAX_NUMBER 32 |
| 38 | |
| 39 | #define BOOTMODE_MAGIC 0xDEAD0000 |
| 40 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 41 | int post_init_f (void) |
| 42 | { |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 43 | int res = 0; |
| 44 | unsigned int i; |
| 45 | |
| 46 | for (i = 0; i < post_list_size; i++) { |
| 47 | struct post_test *test = post_list + i; |
| 48 | |
| 49 | if (test->init_f && test->init_f()) { |
| 50 | res = -1; |
| 51 | } |
| 52 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 53 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 54 | gd->post_init_f_time = post_time_ms(0); |
| 55 | if (!gd->post_init_f_time) |
| 56 | { |
| 57 | printf("post/post.c: post_time_ms seems not to be implemented\n"); |
| 58 | } |
| 59 | |
| 60 | return res; |
| 61 | } |
| 62 | |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 63 | void post_bootmode_init (void) |
| 64 | { |
| 65 | int bootmode = post_bootmode_get (0); |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 66 | int newword; |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 67 | |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 68 | if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) { |
| 69 | newword = BOOTMODE_MAGIC | POST_SLOWTEST; |
wdenk | 6dff552 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 70 | } else if (bootmode == 0) { |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 71 | newword = BOOTMODE_MAGIC | POST_POWERON; |
wdenk | 6dff552 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 72 | } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) { |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 73 | newword = BOOTMODE_MAGIC | POST_NORMAL; |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 74 | } else { |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 75 | /* Use old value */ |
| 76 | newword = post_word_load () & ~POST_COLDBOOT; |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 77 | } |
| 78 | |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 79 | if (bootmode == 0) |
| 80 | { |
| 81 | /* We are booting after power-on */ |
| 82 | newword |= POST_COLDBOOT; |
| 83 | } |
| 84 | |
| 85 | post_word_store (newword); |
| 86 | |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 87 | /* Reset activity record */ |
| 88 | gd->post_log_word = 0; |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int post_bootmode_get (unsigned int *last_test) |
| 92 | { |
| 93 | unsigned long word = post_word_load (); |
| 94 | int bootmode; |
| 95 | |
| 96 | if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) { |
| 97 | return 0; |
| 98 | } |
| 99 | |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 100 | bootmode = word & 0x7F; |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 101 | |
| 102 | if (last_test && (bootmode & POST_POWERTEST)) { |
| 103 | *last_test = (word >> 8) & 0xFF; |
| 104 | } |
| 105 | |
| 106 | return bootmode; |
| 107 | } |
| 108 | |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 109 | /* POST tests run before relocation only mark status bits .... */ |
| 110 | static void post_log_mark_start ( unsigned long testid ) |
| 111 | { |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 112 | gd->post_log_word |= (testid)<<16; |
| 113 | } |
| 114 | |
| 115 | static void post_log_mark_succ ( unsigned long testid ) |
| 116 | { |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 117 | gd->post_log_word |= testid; |
| 118 | } |
| 119 | |
| 120 | /* ... and the messages are output once we are relocated */ |
| 121 | void post_output_backlog ( void ) |
| 122 | { |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 123 | int j; |
| 124 | |
| 125 | for (j = 0; j < post_list_size; j++) { |
| 126 | if (gd->post_log_word & (post_list[j].testid<<16)) { |
| 127 | post_log ("POST %s ", post_list[j].cmd); |
| 128 | if (gd->post_log_word & post_list[j].testid) |
| 129 | post_log ("PASSED\n"); |
wdenk | 63e73c9 | 2004-02-23 22:22:28 +0000 | [diff] [blame] | 130 | else { |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 131 | post_log ("FAILED\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 132 | show_boot_progress (-31); |
wdenk | 63e73c9 | 2004-02-23 22:22:28 +0000 | [diff] [blame] | 133 | } |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 138 | static void post_bootmode_test_on (unsigned int last_test) |
| 139 | { |
| 140 | unsigned long word = post_word_load (); |
| 141 | |
| 142 | word |= POST_POWERTEST; |
| 143 | |
| 144 | word |= (last_test & 0xFF) << 8; |
| 145 | |
| 146 | post_word_store (word); |
| 147 | } |
| 148 | |
| 149 | static void post_bootmode_test_off (void) |
| 150 | { |
| 151 | unsigned long word = post_word_load (); |
| 152 | |
| 153 | word &= ~POST_POWERTEST; |
| 154 | |
| 155 | post_word_store (word); |
| 156 | } |
| 157 | |
| 158 | static void post_get_flags (int *test_flags) |
| 159 | { |
Yuri Tikhonov | e262efe | 2008-02-04 14:11:03 +0100 | [diff] [blame^] | 160 | int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST, |
| 161 | POST_CRITICAL }; |
| 162 | char *var[] = { "post_poweron", "post_normal", "post_slowtest", |
| 163 | "post_critical" }; |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 164 | int varnum = sizeof (var) / sizeof (var[0]); |
| 165 | char list[128]; /* long enough for POST list */ |
| 166 | char *name; |
| 167 | char *s; |
| 168 | int last; |
| 169 | int i, j; |
| 170 | |
| 171 | for (j = 0; j < post_list_size; j++) { |
| 172 | test_flags[j] = post_list[j].flags; |
| 173 | } |
| 174 | |
| 175 | for (i = 0; i < varnum; i++) { |
| 176 | if (getenv_r (var[i], list, sizeof (list)) <= 0) |
| 177 | continue; |
| 178 | |
| 179 | for (j = 0; j < post_list_size; j++) { |
| 180 | test_flags[j] &= ~flag[i]; |
| 181 | } |
| 182 | |
| 183 | last = 0; |
| 184 | name = list; |
| 185 | while (!last) { |
| 186 | while (*name && *name == ' ') |
| 187 | name++; |
| 188 | if (*name == 0) |
| 189 | break; |
| 190 | s = name + 1; |
| 191 | while (*s && *s != ' ') |
| 192 | s++; |
| 193 | if (*s == 0) |
| 194 | last = 1; |
| 195 | else |
| 196 | *s = 0; |
| 197 | |
| 198 | for (j = 0; j < post_list_size; j++) { |
| 199 | if (strcmp (post_list[j].cmd, name) == 0) { |
| 200 | test_flags[j] |= flag[i]; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (j == post_list_size) { |
| 206 | printf ("No such test: %s\n", name); |
| 207 | } |
| 208 | |
| 209 | name = s + 1; |
| 210 | } |
| 211 | } |
wdenk | 6dff552 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 212 | |
| 213 | for (j = 0; j < post_list_size; j++) { |
| 214 | if (test_flags[j] & POST_POWERON) { |
| 215 | test_flags[j] |= POST_SLOWTEST; |
| 216 | } |
| 217 | } |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int post_run_single (struct post_test *test, |
| 221 | int test_flags, int flags, unsigned int i) |
| 222 | { |
| 223 | if ((flags & test_flags & POST_ALWAYS) && |
| 224 | (flags & test_flags & POST_MEM)) { |
| 225 | WATCHDOG_RESET (); |
| 226 | |
| 227 | if (!(flags & POST_REBOOT)) { |
| 228 | if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) { |
Yuri Tikhonov | e262efe | 2008-02-04 14:11:03 +0100 | [diff] [blame^] | 229 | post_bootmode_test_on ( |
| 230 | (gd->flags & GD_FLG_POSTFAIL) ? |
| 231 | POST_FAIL_SAVE | i : i); |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 232 | } |
| 233 | |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 234 | if (test_flags & POST_PREREL) |
| 235 | post_log_mark_start ( test->testid ); |
| 236 | else |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 237 | post_log ("POST %s ", test->cmd); |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 238 | } |
| 239 | |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 240 | if (test_flags & POST_PREREL) { |
| 241 | if ((*test->test) (flags) == 0) |
| 242 | post_log_mark_succ ( test->testid ); |
Yuri Tikhonov | e262efe | 2008-02-04 14:11:03 +0100 | [diff] [blame^] | 243 | else if (test_flags & POST_CRITICAL) |
| 244 | gd->flags |= GD_FLG_POSTFAIL; |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 245 | } else { |
wdenk | 63e73c9 | 2004-02-23 22:22:28 +0000 | [diff] [blame] | 246 | if ((*test->test) (flags) != 0) { |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 247 | post_log ("FAILED\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 248 | show_boot_progress (-32); |
Yuri Tikhonov | e262efe | 2008-02-04 14:11:03 +0100 | [diff] [blame^] | 249 | if (test_flags & POST_CRITICAL) |
| 250 | gd->flags |= GD_FLG_POSTFAIL; |
wdenk | 63e73c9 | 2004-02-23 22:22:28 +0000 | [diff] [blame] | 251 | } |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 252 | else |
| 253 | post_log ("PASSED\n"); |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 254 | } |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 255 | |
| 256 | if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) { |
| 257 | post_bootmode_test_off (); |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } else { |
| 262 | return -1; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | int post_run (char *name, int flags) |
| 267 | { |
| 268 | unsigned int i; |
| 269 | int test_flags[POST_MAX_NUMBER]; |
| 270 | |
| 271 | post_get_flags (test_flags); |
| 272 | |
| 273 | if (name == NULL) { |
| 274 | unsigned int last; |
| 275 | |
| 276 | if (post_bootmode_get (&last) & POST_POWERTEST) { |
Yuri Tikhonov | e262efe | 2008-02-04 14:11:03 +0100 | [diff] [blame^] | 277 | if (last & POST_FAIL_SAVE) { |
| 278 | last &= ~POST_FAIL_SAVE; |
| 279 | gd->flags |= GD_FLG_POSTFAIL; |
| 280 | } |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 281 | if (last < post_list_size && |
| 282 | (flags & test_flags[last] & POST_ALWAYS) && |
| 283 | (flags & test_flags[last] & POST_MEM)) { |
| 284 | |
wdenk | ea909b7 | 2002-11-21 23:11:29 +0000 | [diff] [blame] | 285 | post_run_single (post_list + last, |
| 286 | test_flags[last], |
| 287 | flags | POST_REBOOT, last); |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 288 | |
| 289 | for (i = last + 1; i < post_list_size; i++) { |
wdenk | ea909b7 | 2002-11-21 23:11:29 +0000 | [diff] [blame] | 290 | post_run_single (post_list + i, |
| 291 | test_flags[i], |
| 292 | flags, i); |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } else { |
| 296 | for (i = 0; i < post_list_size; i++) { |
wdenk | ea909b7 | 2002-11-21 23:11:29 +0000 | [diff] [blame] | 297 | post_run_single (post_list + i, |
| 298 | test_flags[i], |
| 299 | flags, i); |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } else { |
| 305 | for (i = 0; i < post_list_size; i++) { |
| 306 | if (strcmp (post_list[i].cmd, name) == 0) |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | if (i < post_list_size) { |
| 311 | return post_run_single (post_list + i, |
| 312 | test_flags[i], |
| 313 | flags, i); |
| 314 | } else { |
| 315 | return -1; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | static int post_info_single (struct post_test *test, int full) |
| 321 | { |
| 322 | if (test->flags & POST_MANUAL) { |
| 323 | if (full) |
| 324 | printf ("%s - %s\n" |
| 325 | " %s\n", test->cmd, test->name, test->desc); |
| 326 | else |
| 327 | printf (" %-15s - %s\n", test->cmd, test->name); |
| 328 | |
| 329 | return 0; |
| 330 | } else { |
| 331 | return -1; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | int post_info (char *name) |
| 336 | { |
| 337 | unsigned int i; |
| 338 | |
| 339 | if (name == NULL) { |
| 340 | for (i = 0; i < post_list_size; i++) { |
| 341 | post_info_single (post_list + i, 0); |
| 342 | } |
| 343 | |
| 344 | return 0; |
| 345 | } else { |
| 346 | for (i = 0; i < post_list_size; i++) { |
| 347 | if (strcmp (post_list[i].cmd, name) == 0) |
| 348 | break; |
| 349 | } |
| 350 | |
| 351 | if (i < post_list_size) { |
| 352 | return post_info_single (post_list + i, 1); |
| 353 | } else { |
| 354 | return -1; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | int post_log (char *format, ...) |
| 360 | { |
| 361 | va_list args; |
| 362 | uint i; |
| 363 | char printbuffer[CFG_PBSIZE]; |
| 364 | |
| 365 | va_start (args, format); |
| 366 | |
| 367 | /* For this to work, printbuffer must be larger than |
| 368 | * anything we ever want to print. |
| 369 | */ |
| 370 | i = vsprintf (printbuffer, format, args); |
| 371 | va_end (args); |
| 372 | |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 373 | #ifdef CONFIG_LOGBUFFER |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 374 | /* Send to the logbuffer */ |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 375 | logbuff_log (printbuffer); |
| 376 | #else |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 377 | /* Send to the stdout file */ |
| 378 | puts (printbuffer); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 379 | #endif |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | void post_reloc (void) |
| 385 | { |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 386 | unsigned int i; |
| 387 | |
| 388 | /* |
| 389 | * We have to relocate the test table manually |
| 390 | */ |
| 391 | for (i = 0; i < post_list_size; i++) { |
| 392 | ulong addr; |
| 393 | struct post_test *test = post_list + i; |
| 394 | |
| 395 | if (test->name) { |
| 396 | addr = (ulong) (test->name) + gd->reloc_off; |
| 397 | test->name = (char *) addr; |
| 398 | } |
| 399 | |
| 400 | if (test->cmd) { |
| 401 | addr = (ulong) (test->cmd) + gd->reloc_off; |
| 402 | test->cmd = (char *) addr; |
| 403 | } |
| 404 | |
| 405 | if (test->desc) { |
| 406 | addr = (ulong) (test->desc) + gd->reloc_off; |
| 407 | test->desc = (char *) addr; |
| 408 | } |
| 409 | |
| 410 | if (test->test) { |
| 411 | addr = (ulong) (test->test) + gd->reloc_off; |
| 412 | test->test = (int (*)(int flags)) addr; |
| 413 | } |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 414 | |
| 415 | if (test->init_f) { |
| 416 | addr = (ulong) (test->init_f) + gd->reloc_off; |
| 417 | test->init_f = (int (*)(void)) addr; |
| 418 | } |
| 419 | |
| 420 | if (test->reloc) { |
| 421 | addr = (ulong) (test->reloc) + gd->reloc_off; |
| 422 | test->reloc = (void (*)(void)) addr; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 423 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 424 | test->reloc(); |
| 425 | } |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * Some tests (e.g. SYSMON) need the time when post_init_f started, |
| 432 | * but we cannot use get_timer() at this point. |
| 433 | * |
| 434 | * On PowerPC we implement it using the timebase register. |
| 435 | */ |
| 436 | unsigned long post_time_ms (unsigned long base) |
| 437 | { |
| 438 | #ifdef CONFIG_PPC |
Igor Lisitsin | a11e069 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 439 | return (unsigned long)(get_ticks () / (get_tbclk () / CFG_HZ)) - base; |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 440 | #else |
Wolfgang Denk | ad5bb45 | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 441 | #warning "Not implemented yet" |
wdenk | 4532cb6 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 442 | return 0; /* Not implemented yet */ |
| 443 | #endif |
| 444 | } |
| 445 | |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 446 | #endif /* CONFIG_POST */ |