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 | 6405a15 | 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 | c08f158 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 41 | int post_init_f (void) |
| 42 | { |
wdenk | c08f158 | 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 | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 53 | |
wdenk | c08f158 | 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 | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 66 | int newword; |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 67 | |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 68 | if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) { |
| 69 | newword = BOOTMODE_MAGIC | POST_SLOWTEST; |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 70 | } else if (bootmode == 0) { |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 71 | newword = BOOTMODE_MAGIC | POST_POWERON; |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 72 | } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) { |
wdenk | 874ac26 | 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 | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 75 | /* Use old value */ |
| 76 | newword = post_word_load () & ~POST_COLDBOOT; |
| 77 | } |
| 78 | |
| 79 | if (bootmode == 0) |
| 80 | { |
| 81 | /* We are booting after power-on */ |
| 82 | newword |= POST_COLDBOOT; |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 83 | } |
| 84 | |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 85 | post_word_store (newword); |
| 86 | |
wdenk | 9dfa8d1 | 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 | 874ac26 | 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 | 9dfa8d1 | 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 | 9dfa8d1 | 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 | 9dfa8d1 | 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 | 9dfa8d1 | 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 | e97d3d9 | 2004-02-23 22:22:28 +0000 | [diff] [blame] | 130 | else { |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 131 | post_log ("FAILED\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 132 | show_boot_progress (-31); |
wdenk | e97d3d9 | 2004-02-23 22:22:28 +0000 | [diff] [blame] | 133 | } |
wdenk | 9dfa8d1 | 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 | { |
wdenk | dccbda0 | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 160 | int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST }; |
| 161 | char *var[] = { "post_poweron", "post_normal", "post_slowtest" }; |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 162 | int varnum = sizeof (var) / sizeof (var[0]); |
| 163 | char list[128]; /* long enough for POST list */ |
| 164 | char *name; |
| 165 | char *s; |
| 166 | int last; |
| 167 | int i, j; |
| 168 | |
| 169 | for (j = 0; j < post_list_size; j++) { |
| 170 | test_flags[j] = post_list[j].flags; |
| 171 | } |
| 172 | |
| 173 | for (i = 0; i < varnum; i++) { |
| 174 | if (getenv_r (var[i], list, sizeof (list)) <= 0) |
| 175 | continue; |
| 176 | |
| 177 | for (j = 0; j < post_list_size; j++) { |
| 178 | test_flags[j] &= ~flag[i]; |
| 179 | } |
| 180 | |
| 181 | last = 0; |
| 182 | name = list; |
| 183 | while (!last) { |
| 184 | while (*name && *name == ' ') |
| 185 | name++; |
| 186 | if (*name == 0) |
| 187 | break; |
| 188 | s = name + 1; |
| 189 | while (*s && *s != ' ') |
| 190 | s++; |
| 191 | if (*s == 0) |
| 192 | last = 1; |
| 193 | else |
| 194 | *s = 0; |
| 195 | |
| 196 | for (j = 0; j < post_list_size; j++) { |
| 197 | if (strcmp (post_list[j].cmd, name) == 0) { |
| 198 | test_flags[j] |= flag[i]; |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (j == post_list_size) { |
| 204 | printf ("No such test: %s\n", name); |
| 205 | } |
| 206 | |
| 207 | name = s + 1; |
| 208 | } |
| 209 | } |
wdenk | ca9bc76 | 2003-07-15 07:45:49 +0000 | [diff] [blame] | 210 | |
| 211 | for (j = 0; j < post_list_size; j++) { |
| 212 | if (test_flags[j] & POST_POWERON) { |
| 213 | test_flags[j] |= POST_SLOWTEST; |
| 214 | } |
| 215 | } |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static int post_run_single (struct post_test *test, |
| 219 | int test_flags, int flags, unsigned int i) |
| 220 | { |
| 221 | if ((flags & test_flags & POST_ALWAYS) && |
| 222 | (flags & test_flags & POST_MEM)) { |
| 223 | WATCHDOG_RESET (); |
| 224 | |
| 225 | if (!(flags & POST_REBOOT)) { |
| 226 | if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) { |
| 227 | post_bootmode_test_on (i); |
| 228 | } |
| 229 | |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 230 | if (test_flags & POST_PREREL) |
| 231 | post_log_mark_start ( test->testid ); |
| 232 | else |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 233 | post_log ("POST %s ", test->cmd); |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 234 | } |
| 235 | |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 236 | if (test_flags & POST_PREREL) { |
| 237 | if ((*test->test) (flags) == 0) |
| 238 | post_log_mark_succ ( test->testid ); |
| 239 | } else { |
wdenk | e97d3d9 | 2004-02-23 22:22:28 +0000 | [diff] [blame] | 240 | if ((*test->test) (flags) != 0) { |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 241 | post_log ("FAILED\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 242 | show_boot_progress (-32); |
wdenk | e97d3d9 | 2004-02-23 22:22:28 +0000 | [diff] [blame] | 243 | } |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 244 | else |
| 245 | post_log ("PASSED\n"); |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 246 | } |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 247 | |
| 248 | if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) { |
| 249 | post_bootmode_test_off (); |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } else { |
| 254 | return -1; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | int post_run (char *name, int flags) |
| 259 | { |
| 260 | unsigned int i; |
| 261 | int test_flags[POST_MAX_NUMBER]; |
| 262 | |
| 263 | post_get_flags (test_flags); |
| 264 | |
| 265 | if (name == NULL) { |
| 266 | unsigned int last; |
| 267 | |
| 268 | if (post_bootmode_get (&last) & POST_POWERTEST) { |
| 269 | if (last < post_list_size && |
| 270 | (flags & test_flags[last] & POST_ALWAYS) && |
| 271 | (flags & test_flags[last] & POST_MEM)) { |
| 272 | |
wdenk | 2029f4d | 2002-11-21 23:11:29 +0000 | [diff] [blame] | 273 | post_run_single (post_list + last, |
| 274 | test_flags[last], |
| 275 | flags | POST_REBOOT, last); |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 276 | |
| 277 | for (i = last + 1; i < post_list_size; i++) { |
wdenk | 2029f4d | 2002-11-21 23:11:29 +0000 | [diff] [blame] | 278 | post_run_single (post_list + i, |
| 279 | test_flags[i], |
| 280 | flags, i); |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } else { |
| 284 | for (i = 0; i < post_list_size; i++) { |
wdenk | 2029f4d | 2002-11-21 23:11:29 +0000 | [diff] [blame] | 285 | post_run_single (post_list + i, |
| 286 | test_flags[i], |
| 287 | flags, i); |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } else { |
| 293 | for (i = 0; i < post_list_size; i++) { |
| 294 | if (strcmp (post_list[i].cmd, name) == 0) |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | if (i < post_list_size) { |
| 299 | return post_run_single (post_list + i, |
| 300 | test_flags[i], |
| 301 | flags, i); |
| 302 | } else { |
| 303 | return -1; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static int post_info_single (struct post_test *test, int full) |
| 309 | { |
| 310 | if (test->flags & POST_MANUAL) { |
| 311 | if (full) |
| 312 | printf ("%s - %s\n" |
| 313 | " %s\n", test->cmd, test->name, test->desc); |
| 314 | else |
| 315 | printf (" %-15s - %s\n", test->cmd, test->name); |
| 316 | |
| 317 | return 0; |
| 318 | } else { |
| 319 | return -1; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | int post_info (char *name) |
| 324 | { |
| 325 | unsigned int i; |
| 326 | |
| 327 | if (name == NULL) { |
| 328 | for (i = 0; i < post_list_size; i++) { |
| 329 | post_info_single (post_list + i, 0); |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } else { |
| 334 | for (i = 0; i < post_list_size; i++) { |
| 335 | if (strcmp (post_list[i].cmd, name) == 0) |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | if (i < post_list_size) { |
| 340 | return post_info_single (post_list + i, 1); |
| 341 | } else { |
| 342 | return -1; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | int post_log (char *format, ...) |
| 348 | { |
| 349 | va_list args; |
| 350 | uint i; |
| 351 | char printbuffer[CFG_PBSIZE]; |
| 352 | |
| 353 | va_start (args, format); |
| 354 | |
| 355 | /* For this to work, printbuffer must be larger than |
| 356 | * anything we ever want to print. |
| 357 | */ |
| 358 | i = vsprintf (printbuffer, format, args); |
| 359 | va_end (args); |
| 360 | |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 361 | #ifdef CONFIG_LOGBUFFER |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 362 | /* Send to the logbuffer */ |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 363 | logbuff_log (printbuffer); |
| 364 | #else |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 365 | /* Send to the stdout file */ |
| 366 | puts (printbuffer); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 367 | #endif |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | void post_reloc (void) |
| 373 | { |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 374 | unsigned int i; |
| 375 | |
| 376 | /* |
| 377 | * We have to relocate the test table manually |
| 378 | */ |
| 379 | for (i = 0; i < post_list_size; i++) { |
| 380 | ulong addr; |
| 381 | struct post_test *test = post_list + i; |
| 382 | |
| 383 | if (test->name) { |
| 384 | addr = (ulong) (test->name) + gd->reloc_off; |
| 385 | test->name = (char *) addr; |
| 386 | } |
| 387 | |
| 388 | if (test->cmd) { |
| 389 | addr = (ulong) (test->cmd) + gd->reloc_off; |
| 390 | test->cmd = (char *) addr; |
| 391 | } |
| 392 | |
| 393 | if (test->desc) { |
| 394 | addr = (ulong) (test->desc) + gd->reloc_off; |
| 395 | test->desc = (char *) addr; |
| 396 | } |
| 397 | |
| 398 | if (test->test) { |
| 399 | addr = (ulong) (test->test) + gd->reloc_off; |
| 400 | test->test = (int (*)(int flags)) addr; |
| 401 | } |
wdenk | c08f158 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 402 | |
| 403 | if (test->init_f) { |
| 404 | addr = (ulong) (test->init_f) + gd->reloc_off; |
| 405 | test->init_f = (int (*)(void)) addr; |
| 406 | } |
| 407 | |
| 408 | if (test->reloc) { |
| 409 | addr = (ulong) (test->reloc) + gd->reloc_off; |
| 410 | test->reloc = (void (*)(void)) addr; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 411 | |
wdenk | c08f158 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 412 | test->reloc(); |
| 413 | } |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
wdenk | c08f158 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * Some tests (e.g. SYSMON) need the time when post_init_f started, |
| 420 | * but we cannot use get_timer() at this point. |
| 421 | * |
| 422 | * On PowerPC we implement it using the timebase register. |
| 423 | */ |
| 424 | unsigned long post_time_ms (unsigned long base) |
| 425 | { |
| 426 | #ifdef CONFIG_PPC |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 427 | return (unsigned long)(get_ticks () / (get_tbclk () / CFG_HZ)) - base; |
wdenk | c08f158 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 428 | #else |
Wolfgang Denk | b38e0df | 2007-03-06 18:08:43 +0100 | [diff] [blame] | 429 | #warning "Not implemented yet" |
wdenk | c08f158 | 2003-04-27 22:52:51 +0000 | [diff] [blame] | 430 | return 0; /* Not implemented yet */ |
| 431 | #endif |
| 432 | } |
| 433 | |
wdenk | a042ac8 | 2002-09-12 22:36:57 +0000 | [diff] [blame] | 434 | #endif /* CONFIG_POST */ |