1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
4  * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <lcd.h>
10 #include <ram.h>
11 #include <spl.h>
12 #include <splash.h>
13 #include <st_logo_data.h>
14 #include <video.h>
15 #include <asm/io.h>
16 #include <asm/armv7m.h>
17 #include <asm/arch/stm32.h>
18 #include <asm/arch/gpio.h>
19 #include <asm/arch/syscfg.h>
20 #include <asm/gpio.h>
21 
22 DECLARE_GLOBAL_DATA_PTR;
23 
24 int dram_init(void)
25 {
26 #ifndef CONFIG_SUPPORT_SPL
27 	int rv;
28 	struct udevice *dev;
29 	rv = uclass_get_device(UCLASS_RAM, 0, &dev);
30 	if (rv) {
31 		debug("DRAM init failed: %d\n", rv);
32 		return rv;
33 	}
34 
35 #endif
36 	return fdtdec_setup_mem_size_base();
37 }
38 
39 int dram_init_banksize(void)
40 {
41 	return fdtdec_setup_memory_banksize();
42 }
43 
44 int board_early_init_f(void)
45 {
46 	return 0;
47 }
48 
49 #ifdef CONFIG_SPL_BUILD
50 #ifdef CONFIG_SPL_OS_BOOT
51 int spl_start_uboot(void)
52 {
53 	debug("SPL: booting kernel\n");
54 	/* break into full u-boot on 'c' */
55 	return serial_tstc() && serial_getc() == 'c';
56 }
57 #endif
58 
59 int spl_dram_init(void)
60 {
61 	struct udevice *dev;
62 	int rv;
63 	rv = uclass_get_device(UCLASS_RAM, 0, &dev);
64 	if (rv)
65 		debug("DRAM init failed: %d\n", rv);
66 	return rv;
67 }
68 void spl_board_init(void)
69 {
70 	spl_dram_init();
71 	preloader_console_init();
72 	arch_cpu_init(); /* to configure mpu for sdram rw permissions */
73 }
74 u32 spl_boot_device(void)
75 {
76 	return BOOT_DEVICE_XIP;
77 }
78 
79 #endif
80 u32 get_board_rev(void)
81 {
82 	return 0;
83 }
84 
85 int board_late_init(void)
86 {
87 	struct gpio_desc gpio = {};
88 	int node;
89 
90 	node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "st,led1");
91 	if (node < 0)
92 		return -1;
93 
94 	gpio_request_by_name_nodev(offset_to_ofnode(node), "led-gpio", 0, &gpio,
95 				   GPIOD_IS_OUT);
96 
97 	if (dm_gpio_is_valid(&gpio)) {
98 		dm_gpio_set_value(&gpio, 0);
99 		mdelay(10);
100 		dm_gpio_set_value(&gpio, 1);
101 	}
102 
103 	/* read button 1*/
104 	node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "st,button1");
105 	if (node < 0)
106 		return -1;
107 
108 	gpio_request_by_name_nodev(offset_to_ofnode(node), "button-gpio", 0,
109 				   &gpio, GPIOD_IS_IN);
110 
111 	if (dm_gpio_is_valid(&gpio)) {
112 		if (dm_gpio_get_value(&gpio))
113 			puts("usr button is at HIGH LEVEL\n");
114 		else
115 			puts("usr button is at LOW LEVEL\n");
116 	}
117 
118 	return 0;
119 }
120 
121 int board_init(void)
122 {
123 	gd->bd->bi_boot_params = gd->bd->bi_dram[0].start + 0x100;
124 
125 #ifdef CONFIG_ETH_DESIGNWARE
126 	/* Set >RMII mode */
127 	STM32_SYSCFG->pmc |= SYSCFG_PMC_MII_RMII_SEL;
128 #endif
129 
130 #if defined(CONFIG_CMD_BMP)
131 	bmp_display((ulong)stmicroelectronics_uboot_logo_8bit_rle,
132 		    BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
133 #endif /* CONFIG_CMD_BMP */
134 
135 	return 0;
136 }
137