blob: bf9a11472d116ff1f921d0371481c97abc2047cf [file] [log] [blame]
Marek Vasut78943112022-12-11 21:17:14 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022 Marek Vasut <marex@denx.de>
4 */
5
6#include <common.h>
7#include <asm-generic/gpio.h>
8#include <asm-generic/sections.h>
9#include <asm/arch/clock.h>
10#include <asm/arch/ddr.h>
11#include <asm/arch/sys_proto.h>
12#include <asm/io.h>
13#include <asm/mach-imx/boot_mode.h>
14#include <asm/mach-imx/iomux-v3.h>
15#include <common.h>
16#include <dm/uclass.h>
17#include <hang.h>
18#include <i2c_eeprom.h>
19#include <image.h>
20#include <init.h>
21#include <net.h>
22#include <spl.h>
23
24#include <dm/uclass.h>
25#include <dm/device.h>
26#include <dm/uclass-internal.h>
27#include <dm/device-internal.h>
28
29DECLARE_GLOBAL_DATA_PTR;
30
31#define WDOG_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_ODE | PAD_CTL_PUE | PAD_CTL_PE)
32
33u8 dmo_get_memcfg(void)
34{
35 struct gpio_desc gpio[4];
36 u8 memcfg = 0;
37 ofnode node;
38 int i, ret;
39
40 node = ofnode_path("/config");
41 if (!ofnode_valid(node)) {
42 printf("%s: no /config node?\n", __func__);
43 return BIT(2) | BIT(0);
44 }
45
46 ret = gpio_request_list_by_name_nodev(node,
47 "dmo,ram-coding-gpios",
48 gpio, ARRAY_SIZE(gpio),
49 GPIOD_IS_IN);
50 for (i = 0; i < ret; i++)
51 memcfg |= !!dm_gpio_get_value(&(gpio[i])) << i;
52
53 gpio_free_list_nodev(gpio, ret);
54
55 return memcfg;
56}
57
58int board_phys_sdram_size(phys_size_t *size)
59{
60 u8 memcfg = dmo_get_memcfg();
61
62 *size = (4ULL >> ((memcfg >> 1) & 0x3)) * SZ_1G;
63
64 return 0;
65}
66
67#ifdef CONFIG_SPL_BUILD
68static void data_modul_imx_edm_sbc_early_init_f(const iomux_v3_cfg_t wdog_pad)
69{
70 struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR;
71
72 imx_iomux_v3_setup_pad(wdog_pad | MUX_PAD_CTRL(WDOG_PAD_CTRL));
73
74 set_wdog_reset(wdog);
75}
76
77__weak int data_modul_imx_edm_sbc_board_power_init(void)
78{
79 return 0;
80}
81
82static void spl_dram_init(struct dram_timing_info *dram_timing_info[8])
83{
84 u8 memcfg = dmo_get_memcfg();
85 int i;
86
87 printf("DDR: %d GiB x%d [0x%x]\n",
88 /* 0..4 GiB, 1..2 GiB, 0..1 GiB */
89 4 >> ((memcfg >> 1) & 0x3),
90 /* 0..x32, 1..x16 */
91 32 >> (memcfg & BIT(0)),
92 memcfg);
93
94 if (!dram_timing_info[memcfg]) {
95 printf("Unsupported DRAM strapping, trying lowest supported. MEMCFG=0x%x\n",
96 memcfg);
97 for (i = 7; i >= 0; i--)
98 if (dram_timing_info[i]) /* Configuration found */
99 break;
100 }
101
102 ddr_init(dram_timing_info[memcfg]);
103}
104
105void dmo_board_init_f(const iomux_v3_cfg_t wdog_pad,
106 struct dram_timing_info *dram_timing_info[8])
107{
108 struct udevice *dev;
109 int ret;
110
111 icache_enable();
112
113 arch_cpu_init();
114
115 init_uart_clk(2);
116
117 data_modul_imx_edm_sbc_early_init_f(wdog_pad);
118
119 /* Clear the BSS. */
120 memset(__bss_start, 0, __bss_end - __bss_start);
121
122 ret = spl_early_init();
123 if (ret) {
124 debug("spl_early_init() failed: %d\n", ret);
125 hang();
126 }
127
128 preloader_console_init();
129
130 ret = uclass_get_device_by_name(UCLASS_CLK,
131 "clock-controller@30380000",
132 &dev);
133 if (ret < 0) {
134 printf("Failed to find clock node. Check device tree\n");
135 hang();
136 }
137
138 enable_tzc380();
139
140 data_modul_imx_edm_sbc_board_power_init();
141
142 /* DDR initialization */
143 spl_dram_init(dram_timing_info);
144
145 board_init_r(NULL, 0);
146}
147#else
148void dmo_setup_boot_device(void)
149{
150 int boot_device = get_boot_device();
151 char *devnum;
152
153 devnum = env_get("devnum");
154 if (devnum) /* devnum is already set */
155 return;
156
157 if (boot_device == MMC3_BOOT) /* eMMC */
158 env_set_ulong("devnum", 0);
159 else
160 env_set_ulong("devnum", 1);
161}
162
163void dmo_setup_mac_address(void)
164{
165 unsigned char enetaddr[6];
166 struct udevice *dev;
167 int off, ret;
168
169 ret = eth_env_get_enetaddr("ethaddr", enetaddr);
170 if (ret) /* ethaddr is already set */
171 return;
172
173 off = fdt_path_offset(gd->fdt_blob, "eeprom0");
174 if (off < 0) {
175 printf("%s: No eeprom0 path offset\n", __func__);
176 return;
177 }
178
179 ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
180 if (ret) {
181 printf("Cannot find EEPROM!\n");
182 return;
183 }
184
185 ret = i2c_eeprom_read(dev, 0xb0, enetaddr, 0x6);
186 if (ret) {
187 printf("Error reading configuration EEPROM!\n");
188 return;
189 }
190
191 if (is_valid_ethaddr(enetaddr))
192 eth_env_set_enetaddr("ethaddr", enetaddr);
193}
194#endif