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 <init.h>
10 #include <log.h>
11 #include <miiphy.h>
12 #include <phy_interface.h>
13 #include <ram.h>
14 #include <serial.h>
15 #include <spl.h>
16 #include <splash.h>
17 #include <st_logo_data.h>
18 #include <video.h>
19 #include <asm/global_data.h>
20 #include <asm/io.h>
21 #include <asm/armv7m.h>
22 #include <asm/arch/stm32.h>
23 #include <asm/arch/syscfg.h>
24 #include <asm/gpio.h>
25 #include <linux/delay.h>
26 
27 DECLARE_GLOBAL_DATA_PTR;
28 
dram_init(void)29 int dram_init(void)
30 {
31 #ifndef CONFIG_SPL_BUILD
32 	int rv;
33 	struct udevice *dev;
34 	rv = uclass_get_device(UCLASS_RAM, 0, &dev);
35 	if (rv) {
36 		debug("DRAM init failed: %d\n", rv);
37 		return rv;
38 	}
39 
40 #endif
41 	return fdtdec_setup_mem_size_base();
42 }
43 
dram_init_banksize(void)44 int dram_init_banksize(void)
45 {
46 	return fdtdec_setup_memory_banksize();
47 }
48 
49 #ifdef CONFIG_SPL_BUILD
50 #ifdef CONFIG_SPL_OS_BOOT
spl_start_uboot(void)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 
spl_dram_init(void)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 }
spl_board_init(void)68 void spl_board_init(void)
69 {
70 	preloader_console_init();
71 	spl_dram_init();
72 	arch_cpu_init(); /* to configure mpu for sdram rw permissions */
73 }
spl_boot_device(void)74 u32 spl_boot_device(void)
75 {
76 	return BOOT_DEVICE_XIP;
77 }
78 #endif
79 
board_late_init(void)80 int board_late_init(void)
81 {
82 	struct gpio_desc gpio = {};
83 	int node;
84 
85 	node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "st,led1");
86 	if (node < 0)
87 		return -1;
88 
89 	gpio_request_by_name_nodev(offset_to_ofnode(node), "led-gpio", 0, &gpio,
90 				   GPIOD_IS_OUT);
91 
92 	if (dm_gpio_is_valid(&gpio)) {
93 		dm_gpio_set_value(&gpio, 0);
94 		mdelay(10);
95 		dm_gpio_set_value(&gpio, 1);
96 	}
97 
98 	/* read button 1*/
99 	node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "st,button1");
100 	if (node < 0)
101 		return -1;
102 
103 	gpio_request_by_name_nodev(offset_to_ofnode(node), "button-gpio", 0,
104 				   &gpio, GPIOD_IS_IN);
105 
106 	if (dm_gpio_is_valid(&gpio)) {
107 		if (dm_gpio_get_value(&gpio))
108 			puts("usr button is at HIGH LEVEL\n");
109 		else
110 			puts("usr button is at LOW LEVEL\n");
111 	}
112 
113 	return 0;
114 }
115 
board_init(void)116 int board_init(void)
117 {
118 #ifdef CONFIG_ETH_DESIGNWARE
119 	ofnode node;
120 
121 	node = ofnode_by_compatible(ofnode_null(), "st,stm32-dwmac");
122 	if (!ofnode_valid(node))
123 		return -1;
124 
125 	switch (ofnode_read_phy_mode(node)) {
126 	case PHY_INTERFACE_MODE_RMII:
127 		STM32_SYSCFG->pmc |= SYSCFG_PMC_MII_RMII_SEL;
128 		break;
129 	case PHY_INTERFACE_MODE_MII:
130 		STM32_SYSCFG->pmc &= ~SYSCFG_PMC_MII_RMII_SEL;
131 		break;
132 	default:
133 		printf("Unsupported PHY interface!\n");
134 	}
135 #endif
136 
137 #if defined(CONFIG_CMD_BMP)
138 	bmp_display((ulong)stmicroelectronics_uboot_logo_8bit_rle,
139 		    BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
140 #endif /* CONFIG_CMD_BMP */
141 
142 	return 0;
143 }
144