post.c 9.7 KB

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