post.c 9.3 KB

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