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