misc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright (C) 2013 Samsung Electronics
  3. * Przemyslaw Marczak <p.marczak@samsung.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <lcd.h>
  9. #include <libtizen.h>
  10. #include <samsung/misc.h>
  11. #include <errno.h>
  12. #include <version.h>
  13. #include <malloc.h>
  14. #include <memalign.h>
  15. #include <linux/sizes.h>
  16. #include <asm/arch/cpu.h>
  17. #include <asm/gpio.h>
  18. #include <linux/input.h>
  19. #include <dm.h>
  20. #include <power/pmic.h>
  21. #include <mmc.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #ifdef CONFIG_SET_DFU_ALT_INFO
  24. void set_dfu_alt_info(char *interface, char *devstr)
  25. {
  26. size_t buf_size = CONFIG_SET_DFU_ALT_BUF_LEN;
  27. ALLOC_CACHE_ALIGN_BUFFER(char, buf, buf_size);
  28. char *alt_info = "Settings not found!";
  29. char *status = "error!\n";
  30. char *alt_setting;
  31. char *alt_sep;
  32. int offset = 0;
  33. puts("DFU alt info setting: ");
  34. alt_setting = get_dfu_alt_boot(interface, devstr);
  35. if (alt_setting) {
  36. setenv("dfu_alt_boot", alt_setting);
  37. offset = snprintf(buf, buf_size, "%s", alt_setting);
  38. }
  39. alt_setting = get_dfu_alt_system(interface, devstr);
  40. if (alt_setting) {
  41. if (offset)
  42. alt_sep = ";";
  43. else
  44. alt_sep = "";
  45. offset += snprintf(buf + offset, buf_size - offset,
  46. "%s%s", alt_sep, alt_setting);
  47. }
  48. if (offset) {
  49. alt_info = buf;
  50. status = "done\n";
  51. }
  52. setenv("dfu_alt_info", alt_info);
  53. puts(status);
  54. }
  55. #endif
  56. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  57. void set_board_info(void)
  58. {
  59. char info[64];
  60. snprintf(info, ARRAY_SIZE(info), "%u.%u", (s5p_cpu_rev & 0xf0) >> 4,
  61. s5p_cpu_rev & 0xf);
  62. setenv("soc_rev", info);
  63. snprintf(info, ARRAY_SIZE(info), "%x", s5p_cpu_id);
  64. setenv("soc_id", info);
  65. #ifdef CONFIG_REVISION_TAG
  66. snprintf(info, ARRAY_SIZE(info), "%x", get_board_rev());
  67. setenv("board_rev", info);
  68. #endif
  69. #ifdef CONFIG_OF_LIBFDT
  70. const char *bdtype = "";
  71. const char *bdname = CONFIG_SYS_BOARD;
  72. #ifdef CONFIG_BOARD_TYPES
  73. bdtype = get_board_type();
  74. if (!bdtype)
  75. bdtype = "";
  76. sprintf(info, "%s%s", bdname, bdtype);
  77. setenv("boardname", info);
  78. #endif
  79. snprintf(info, ARRAY_SIZE(info), "%s%x-%s%s.dtb",
  80. CONFIG_SYS_SOC, s5p_cpu_id, bdname, bdtype);
  81. setenv("fdtfile", info);
  82. #endif
  83. }
  84. #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
  85. #ifdef CONFIG_LCD_MENU
  86. static int power_key_pressed(u32 reg)
  87. {
  88. #ifndef CONFIG_DM_I2C /* TODO(maintainer): Convert to driver model */
  89. struct pmic *pmic;
  90. u32 status;
  91. u32 mask;
  92. pmic = pmic_get(KEY_PWR_PMIC_NAME);
  93. if (!pmic) {
  94. printf("%s: Not found\n", KEY_PWR_PMIC_NAME);
  95. return 0;
  96. }
  97. if (pmic_probe(pmic))
  98. return 0;
  99. if (reg == KEY_PWR_STATUS_REG)
  100. mask = KEY_PWR_STATUS_MASK;
  101. else
  102. mask = KEY_PWR_INTERRUPT_MASK;
  103. if (pmic_reg_read(pmic, reg, &status))
  104. return 0;
  105. return !!(status & mask);
  106. #else
  107. return 0;
  108. #endif
  109. }
  110. static int key_pressed(int key)
  111. {
  112. int value;
  113. switch (key) {
  114. case KEY_POWER:
  115. value = power_key_pressed(KEY_PWR_INTERRUPT_REG);
  116. break;
  117. case KEY_VOLUMEUP:
  118. value = !gpio_get_value(KEY_VOL_UP_GPIO);
  119. break;
  120. case KEY_VOLUMEDOWN:
  121. value = !gpio_get_value(KEY_VOL_DOWN_GPIO);
  122. break;
  123. default:
  124. value = 0;
  125. break;
  126. }
  127. return value;
  128. }
  129. #ifdef CONFIG_LCD
  130. static int check_keys(void)
  131. {
  132. int keys = 0;
  133. if (key_pressed(KEY_POWER))
  134. keys += KEY_POWER;
  135. if (key_pressed(KEY_VOLUMEUP))
  136. keys += KEY_VOLUMEUP;
  137. if (key_pressed(KEY_VOLUMEDOWN))
  138. keys += KEY_VOLUMEDOWN;
  139. return keys;
  140. }
  141. /*
  142. * 0 BOOT_MODE_INFO
  143. * 1 BOOT_MODE_THOR
  144. * 2 BOOT_MODE_UMS
  145. * 3 BOOT_MODE_DFU
  146. * 4 BOOT_MODE_EXIT
  147. */
  148. static char *
  149. mode_name[BOOT_MODE_EXIT + 1][2] = {
  150. {"DEVICE", ""},
  151. {"THOR", "thor"},
  152. {"UMS", "ums"},
  153. {"DFU", "dfu"},
  154. {"GPT", "gpt"},
  155. {"ENV", "env"},
  156. {"EXIT", ""},
  157. };
  158. static char *
  159. mode_info[BOOT_MODE_EXIT + 1] = {
  160. "info",
  161. "downloader",
  162. "mass storage",
  163. "firmware update",
  164. "restore",
  165. "default",
  166. "and run normal boot"
  167. };
  168. static char *
  169. mode_cmd[BOOT_MODE_EXIT + 1] = {
  170. "",
  171. "thor 0 mmc 0",
  172. "ums 0 mmc 0",
  173. "dfu 0 mmc 0",
  174. "gpt write mmc 0 $partitions",
  175. "env default -a; saveenv",
  176. "",
  177. };
  178. static void display_board_info(void)
  179. {
  180. #ifdef CONFIG_GENERIC_MMC
  181. struct mmc *mmc = find_mmc_device(0);
  182. #endif
  183. vidinfo_t *vid = &panel_info;
  184. lcd_position_cursor(4, 4);
  185. lcd_printf("%s\n\t", U_BOOT_VERSION);
  186. lcd_puts("\n\t\tBoard Info:\n");
  187. #ifdef CONFIG_SYS_BOARD
  188. lcd_printf("\tBoard name: %s\n", CONFIG_SYS_BOARD);
  189. #endif
  190. #ifdef CONFIG_REVISION_TAG
  191. lcd_printf("\tBoard rev: %u\n", get_board_rev());
  192. #endif
  193. lcd_printf("\tDRAM banks: %u\n", CONFIG_NR_DRAM_BANKS);
  194. lcd_printf("\tDRAM size: %u MB\n", gd->ram_size / SZ_1M);
  195. #ifdef CONFIG_GENERIC_MMC
  196. if (mmc) {
  197. if (!mmc->capacity)
  198. mmc_init(mmc);
  199. lcd_printf("\teMMC size: %llu MB\n", mmc->capacity / SZ_1M);
  200. }
  201. #endif
  202. if (vid)
  203. lcd_printf("\tDisplay resolution: %u x % u\n",
  204. vid->vl_col, vid->vl_row);
  205. lcd_printf("\tDisplay BPP: %u\n", 1 << vid->vl_bpix);
  206. }
  207. #endif
  208. static int mode_leave_menu(int mode)
  209. {
  210. #ifdef CONFIG_LCD
  211. char *exit_option;
  212. char *exit_reset = "reset";
  213. char *exit_back = "back";
  214. cmd_tbl_t *cmd;
  215. int cmd_result;
  216. int leave;
  217. lcd_clear();
  218. switch (mode) {
  219. case BOOT_MODE_EXIT:
  220. return 1;
  221. case BOOT_MODE_INFO:
  222. display_board_info();
  223. exit_option = exit_back;
  224. leave = 0;
  225. break;
  226. default:
  227. cmd = find_cmd(mode_name[mode][1]);
  228. if (cmd) {
  229. printf("Enter: %s %s\n", mode_name[mode][0],
  230. mode_info[mode]);
  231. lcd_printf("\n\n\t%s %s\n", mode_name[mode][0],
  232. mode_info[mode]);
  233. lcd_puts("\n\tDo not turn off device before finish!\n");
  234. cmd_result = run_command(mode_cmd[mode], 0);
  235. if (cmd_result == CMD_RET_SUCCESS) {
  236. printf("Command finished\n");
  237. lcd_clear();
  238. lcd_printf("\n\n\t%s finished\n",
  239. mode_name[mode][0]);
  240. exit_option = exit_reset;
  241. leave = 1;
  242. } else {
  243. printf("Command error\n");
  244. lcd_clear();
  245. lcd_printf("\n\n\t%s command error\n",
  246. mode_name[mode][0]);
  247. exit_option = exit_back;
  248. leave = 0;
  249. }
  250. } else {
  251. lcd_puts("\n\n\tThis mode is not supported.\n");
  252. exit_option = exit_back;
  253. leave = 0;
  254. }
  255. }
  256. lcd_printf("\n\n\tPress POWER KEY to %s\n", exit_option);
  257. /* Clear PWR button Rising edge interrupt status flag */
  258. power_key_pressed(KEY_PWR_INTERRUPT_REG);
  259. /* Wait for PWR key */
  260. while (!key_pressed(KEY_POWER))
  261. mdelay(1);
  262. lcd_clear();
  263. return leave;
  264. #else
  265. return 0;
  266. #endif
  267. }
  268. #ifdef CONFIG_LCD
  269. static void display_download_menu(int mode)
  270. {
  271. char *selection[BOOT_MODE_EXIT + 1];
  272. int i;
  273. for (i = 0; i <= BOOT_MODE_EXIT; i++)
  274. selection[i] = "[ ]";
  275. selection[mode] = "[=>]";
  276. lcd_clear();
  277. lcd_printf("\n\n\t\tDownload Mode Menu\n\n");
  278. for (i = 0; i <= BOOT_MODE_EXIT; i++)
  279. lcd_printf("\t%s %s - %s\n\n", selection[i],
  280. mode_name[i][0], mode_info[i]);
  281. }
  282. #endif
  283. static void download_menu(void)
  284. {
  285. #ifdef CONFIG_LCD
  286. int mode = 0;
  287. int last_mode = 0;
  288. int run;
  289. int key = 0;
  290. int timeout = 15; /* sec */
  291. int i;
  292. display_download_menu(mode);
  293. lcd_puts("\n");
  294. /* Start count if no key is pressed */
  295. while (check_keys())
  296. continue;
  297. while (timeout--) {
  298. lcd_printf("\r\tNormal boot will start in: %2.d seconds.",
  299. timeout);
  300. /* about 1000 ms in for loop */
  301. for (i = 0; i < 10; i++) {
  302. mdelay(100);
  303. key = check_keys();
  304. if (key)
  305. break;
  306. }
  307. if (key)
  308. break;
  309. }
  310. if (!key) {
  311. lcd_clear();
  312. return;
  313. }
  314. while (1) {
  315. run = 0;
  316. if (mode != last_mode)
  317. display_download_menu(mode);
  318. last_mode = mode;
  319. mdelay(200);
  320. key = check_keys();
  321. switch (key) {
  322. case KEY_POWER:
  323. run = 1;
  324. break;
  325. case KEY_VOLUMEUP:
  326. if (mode > 0)
  327. mode--;
  328. break;
  329. case KEY_VOLUMEDOWN:
  330. if (mode < BOOT_MODE_EXIT)
  331. mode++;
  332. break;
  333. default:
  334. break;
  335. }
  336. if (run) {
  337. if (mode_leave_menu(mode))
  338. run_command("reset", 0);
  339. display_download_menu(mode);
  340. }
  341. }
  342. lcd_clear();
  343. #endif
  344. }
  345. void check_boot_mode(void)
  346. {
  347. int pwr_key;
  348. pwr_key = power_key_pressed(KEY_PWR_STATUS_REG);
  349. if (!pwr_key)
  350. return;
  351. /* Clear PWR button Rising edge interrupt status flag */
  352. power_key_pressed(KEY_PWR_INTERRUPT_REG);
  353. if (key_pressed(KEY_VOLUMEUP))
  354. download_menu();
  355. else if (key_pressed(KEY_VOLUMEDOWN))
  356. mode_leave_menu(BOOT_MODE_THOR);
  357. }
  358. void keys_init(void)
  359. {
  360. /* Set direction to input */
  361. gpio_request(KEY_VOL_UP_GPIO, "volume-up");
  362. gpio_request(KEY_VOL_DOWN_GPIO, "volume-down");
  363. gpio_direction_input(KEY_VOL_UP_GPIO);
  364. gpio_direction_input(KEY_VOL_DOWN_GPIO);
  365. }
  366. #endif /* CONFIG_LCD_MENU */
  367. #ifdef CONFIG_CMD_BMP
  368. void draw_logo(void)
  369. {
  370. int x, y;
  371. ulong addr;
  372. addr = panel_info.logo_addr;
  373. if (!addr) {
  374. error("There is no logo data.");
  375. return;
  376. }
  377. if (panel_info.vl_width >= panel_info.logo_width) {
  378. x = ((panel_info.vl_width - panel_info.logo_width) >> 1);
  379. x += panel_info.logo_x_offset; /* For X center align */
  380. } else {
  381. x = 0;
  382. printf("Warning: image width is bigger than display width\n");
  383. }
  384. if (panel_info.vl_height >= panel_info.logo_height) {
  385. y = ((panel_info.vl_height - panel_info.logo_height) >> 1);
  386. y += panel_info.logo_y_offset; /* For Y center align */
  387. } else {
  388. y = 0;
  389. printf("Warning: image height is bigger than display height\n");
  390. }
  391. bmp_display(addr, x, y);
  392. }
  393. #endif /* CONFIG_CMD_BMP */