post.c 11 KB

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