post.c 9.9 KB

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