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