post.c 8.7 KB

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