post.c 9.6 KB

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