post.c 9.7 KB

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