misc.c 9.3 KB

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