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