misc.c 9.2 KB

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