post.c 11 KB

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