post.c 9.7 KB

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