post.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #include <common.h>
  24. #include <stdio_dev.h>
  25. #include <watchdog.h>
  26. #include <post.h>
  27. #ifdef CONFIG_LOGBUFFER
  28. #include <logbuff.h>
  29. #endif
  30. DECLARE_GLOBAL_DATA_PTR;
  31. #define POST_MAX_NUMBER 32
  32. #define BOOTMODE_MAGIC 0xDEAD0000
  33. int post_init_f (void)
  34. {
  35. int res = 0;
  36. unsigned int i;
  37. for (i = 0; i < post_list_size; i++) {
  38. struct post_test *test = post_list + i;
  39. if (test->init_f && test->init_f()) {
  40. res = -1;
  41. }
  42. }
  43. gd->post_init_f_time = post_time_ms(0);
  44. if (!gd->post_init_f_time)
  45. {
  46. printf("post/post.c: post_time_ms seems not to be implemented\n");
  47. }
  48. return res;
  49. }
  50. /*
  51. * Supply a default implementation for post_hotkeys_pressed() for boards
  52. * without hotkey support. We always return 0 here, so that the
  53. * long-running tests won't be started.
  54. *
  55. * Boards with hotkey support can override this weak default function
  56. * by defining one in their board specific code.
  57. */
  58. int __post_hotkeys_pressed(void)
  59. {
  60. return 0; /* No hotkeys supported */
  61. }
  62. int post_hotkeys_pressed(void)
  63. __attribute__((weak, alias("__post_hotkeys_pressed")));
  64. void post_bootmode_init (void)
  65. {
  66. int bootmode = post_bootmode_get (0);
  67. int newword;
  68. if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
  69. newword = BOOTMODE_MAGIC | POST_SLOWTEST;
  70. } else if (bootmode == 0) {
  71. newword = BOOTMODE_MAGIC | POST_POWERON;
  72. } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
  73. newword = BOOTMODE_MAGIC | POST_NORMAL;
  74. } else {
  75. /* Use old value */
  76. newword = post_word_load () & ~POST_COLDBOOT;
  77. }
  78. if (bootmode == 0)
  79. {
  80. /* We are booting after power-on */
  81. newword |= POST_COLDBOOT;
  82. }
  83. post_word_store (newword);
  84. /* Reset activity record */
  85. gd->post_log_word = 0;
  86. }
  87. int post_bootmode_get (unsigned int *last_test)
  88. {
  89. unsigned long word = post_word_load ();
  90. int bootmode;
  91. if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
  92. return 0;
  93. }
  94. bootmode = word & 0x7F;
  95. if (last_test && (bootmode & POST_POWERTEST)) {
  96. *last_test = (word >> 8) & 0xFF;
  97. }
  98. return bootmode;
  99. }
  100. /* POST tests run before relocation only mark status bits .... */
  101. static void post_log_mark_start ( unsigned long testid )
  102. {
  103. gd->post_log_word |= (testid)<<16;
  104. }
  105. static void post_log_mark_succ ( unsigned long testid )
  106. {
  107. gd->post_log_word |= testid;
  108. }
  109. /* ... and the messages are output once we are relocated */
  110. void post_output_backlog ( void )
  111. {
  112. int j;
  113. for (j = 0; j < post_list_size; j++) {
  114. if (gd->post_log_word & (post_list[j].testid<<16)) {
  115. post_log ("POST %s ", post_list[j].cmd);
  116. if (gd->post_log_word & post_list[j].testid)
  117. post_log ("PASSED\n");
  118. else {
  119. post_log ("FAILED\n");
  120. show_boot_progress (-31);
  121. }
  122. }
  123. }
  124. }
  125. static void post_bootmode_test_on (unsigned int last_test)
  126. {
  127. unsigned long word = post_word_load ();
  128. word |= POST_POWERTEST;
  129. word |= (last_test & 0xFF) << 8;
  130. post_word_store (word);
  131. }
  132. static void post_bootmode_test_off (void)
  133. {
  134. unsigned long word = post_word_load ();
  135. word &= ~POST_POWERTEST;
  136. post_word_store (word);
  137. }
  138. static void post_get_flags (int *test_flags)
  139. {
  140. int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
  141. POST_CRITICAL };
  142. char *var[] = { "post_poweron", "post_normal", "post_slowtest",
  143. "post_critical" };
  144. int varnum = sizeof (var) / sizeof (var[0]);
  145. char list[128]; /* long enough for POST list */
  146. char *name;
  147. char *s;
  148. int last;
  149. int i, j;
  150. for (j = 0; j < post_list_size; j++) {
  151. test_flags[j] = post_list[j].flags;
  152. }
  153. for (i = 0; i < varnum; i++) {
  154. if (getenv_r (var[i], list, sizeof (list)) <= 0)
  155. continue;
  156. for (j = 0; j < post_list_size; j++) {
  157. test_flags[j] &= ~flag[i];
  158. }
  159. last = 0;
  160. name = list;
  161. while (!last) {
  162. while (*name && *name == ' ')
  163. name++;
  164. if (*name == 0)
  165. break;
  166. s = name + 1;
  167. while (*s && *s != ' ')
  168. s++;
  169. if (*s == 0)
  170. last = 1;
  171. else
  172. *s = 0;
  173. for (j = 0; j < post_list_size; j++) {
  174. if (strcmp (post_list[j].cmd, name) == 0) {
  175. test_flags[j] |= flag[i];
  176. break;
  177. }
  178. }
  179. if (j == post_list_size) {
  180. printf ("No such test: %s\n", name);
  181. }
  182. name = s + 1;
  183. }
  184. }
  185. for (j = 0; j < post_list_size; j++) {
  186. if (test_flags[j] & POST_POWERON) {
  187. test_flags[j] |= POST_SLOWTEST;
  188. }
  189. }
  190. }
  191. static int post_run_single (struct post_test *test,
  192. int test_flags, int flags, unsigned int i)
  193. {
  194. if ((flags & test_flags & POST_ALWAYS) &&
  195. (flags & test_flags & POST_MEM)) {
  196. WATCHDOG_RESET ();
  197. if (!(flags & POST_REBOOT)) {
  198. if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
  199. post_bootmode_test_on (
  200. (gd->flags & GD_FLG_POSTFAIL) ?
  201. POST_FAIL_SAVE | i : i);
  202. }
  203. if (test_flags & POST_PREREL)
  204. post_log_mark_start ( test->testid );
  205. else
  206. post_log ("POST %s ", test->cmd);
  207. }
  208. if (test_flags & POST_PREREL) {
  209. if ((*test->test) (flags) == 0)
  210. post_log_mark_succ ( test->testid );
  211. else {
  212. if (test_flags & POST_CRITICAL)
  213. gd->flags |= GD_FLG_POSTFAIL;
  214. if (test_flags & POST_STOP)
  215. gd->flags |= GD_FLG_POSTSTOP;
  216. }
  217. } else {
  218. if ((*test->test) (flags) != 0) {
  219. post_log ("FAILED\n");
  220. show_boot_progress (-32);
  221. if (test_flags & POST_CRITICAL)
  222. gd->flags |= GD_FLG_POSTFAIL;
  223. if (test_flags & POST_STOP)
  224. gd->flags |= GD_FLG_POSTSTOP;
  225. }
  226. else
  227. post_log ("PASSED\n");
  228. }
  229. if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
  230. post_bootmode_test_off ();
  231. }
  232. return 0;
  233. } else {
  234. return -1;
  235. }
  236. }
  237. int post_run (char *name, int flags)
  238. {
  239. unsigned int i;
  240. int test_flags[POST_MAX_NUMBER];
  241. post_get_flags (test_flags);
  242. if (name == NULL) {
  243. unsigned int last;
  244. if (gd->flags & GD_FLG_POSTSTOP)
  245. return 0;
  246. if (post_bootmode_get (&last) & POST_POWERTEST) {
  247. if (last & POST_FAIL_SAVE) {
  248. last &= ~POST_FAIL_SAVE;
  249. gd->flags |= GD_FLG_POSTFAIL;
  250. }
  251. if (last < post_list_size &&
  252. (flags & test_flags[last] & POST_ALWAYS) &&
  253. (flags & test_flags[last] & POST_MEM)) {
  254. post_run_single (post_list + last,
  255. test_flags[last],
  256. flags | POST_REBOOT, last);
  257. for (i = last + 1; i < post_list_size; i++) {
  258. if (gd->flags & GD_FLG_POSTSTOP)
  259. break;
  260. post_run_single (post_list + i,
  261. test_flags[i],
  262. flags, i);
  263. }
  264. }
  265. } else {
  266. for (i = 0; i < post_list_size; i++) {
  267. if (gd->flags & GD_FLG_POSTSTOP)
  268. break;
  269. post_run_single (post_list + i,
  270. test_flags[i],
  271. flags, i);
  272. }
  273. }
  274. return 0;
  275. } else {
  276. for (i = 0; i < post_list_size; i++) {
  277. if (strcmp (post_list[i].cmd, name) == 0)
  278. break;
  279. }
  280. if (i < post_list_size) {
  281. WATCHDOG_RESET();
  282. return post_run_single (post_list + i,
  283. test_flags[i],
  284. flags, i);
  285. } else {
  286. return -1;
  287. }
  288. }
  289. }
  290. static int post_info_single (struct post_test *test, int full)
  291. {
  292. if (test->flags & POST_MANUAL) {
  293. if (full)
  294. printf ("%s - %s\n"
  295. " %s\n", test->cmd, test->name, test->desc);
  296. else
  297. printf (" %-15s - %s\n", test->cmd, test->name);
  298. return 0;
  299. } else {
  300. return -1;
  301. }
  302. }
  303. int post_info (char *name)
  304. {
  305. unsigned int i;
  306. if (name == NULL) {
  307. for (i = 0; i < post_list_size; i++) {
  308. post_info_single (post_list + i, 0);
  309. }
  310. return 0;
  311. } else {
  312. for (i = 0; i < post_list_size; i++) {
  313. if (strcmp (post_list[i].cmd, name) == 0)
  314. break;
  315. }
  316. if (i < post_list_size) {
  317. return post_info_single (post_list + i, 1);
  318. } else {
  319. return -1;
  320. }
  321. }
  322. }
  323. int post_log (char *format, ...)
  324. {
  325. va_list args;
  326. uint i;
  327. char printbuffer[CONFIG_SYS_PBSIZE];
  328. va_start (args, format);
  329. /* For this to work, printbuffer must be larger than
  330. * anything we ever want to print.
  331. */
  332. i = vsprintf (printbuffer, format, args);
  333. va_end (args);
  334. #ifdef CONFIG_LOGBUFFER
  335. /* Send to the logbuffer */
  336. logbuff_log (printbuffer);
  337. #else
  338. /* Send to the stdout file */
  339. puts (printbuffer);
  340. #endif
  341. return 0;
  342. }
  343. #ifndef CONFIG_RELOC_FIXUP_WORKS
  344. void post_reloc (void)
  345. {
  346. unsigned int i;
  347. /*
  348. * We have to relocate the test table manually
  349. */
  350. for (i = 0; i < post_list_size; i++) {
  351. ulong addr;
  352. struct post_test *test = post_list + i;
  353. if (test->name) {
  354. addr = (ulong) (test->name) + gd->reloc_off;
  355. test->name = (char *) addr;
  356. }
  357. if (test->cmd) {
  358. addr = (ulong) (test->cmd) + gd->reloc_off;
  359. test->cmd = (char *) addr;
  360. }
  361. if (test->desc) {
  362. addr = (ulong) (test->desc) + gd->reloc_off;
  363. test->desc = (char *) addr;
  364. }
  365. if (test->test) {
  366. addr = (ulong) (test->test) + gd->reloc_off;
  367. test->test = (int (*)(int flags)) addr;
  368. }
  369. if (test->init_f) {
  370. addr = (ulong) (test->init_f) + gd->reloc_off;
  371. test->init_f = (int (*)(void)) addr;
  372. }
  373. if (test->reloc) {
  374. addr = (ulong) (test->reloc) + gd->reloc_off;
  375. test->reloc = (void (*)(void)) addr;
  376. test->reloc();
  377. }
  378. }
  379. }
  380. #endif
  381. /*
  382. * Some tests (e.g. SYSMON) need the time when post_init_f started,
  383. * but we cannot use get_timer() at this point.
  384. *
  385. * On PowerPC we implement it using the timebase register.
  386. */
  387. unsigned long post_time_ms (unsigned long base)
  388. {
  389. #ifdef CONFIG_PPC
  390. return (unsigned long)(get_ticks () / (get_tbclk () / CONFIG_SYS_HZ)) - base;
  391. #else
  392. #warning "Not implemented yet"
  393. return 0; /* Not implemented yet */
  394. #endif
  395. }