blob: a58a75dc9589200849317fb99638b7e8bec4b671 [file] [log] [blame]
Frieder Schrempf9cab87f2021-09-29 16:42:42 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Kontron Electronics GmbH
4 */
5
6#include <asm/arch/imx8mm_pins.h>
7#include <asm/arch/clock.h>
8#include <asm/arch/ddr.h>
9#include <asm/arch/imx-regs.h>
10#include <asm/arch/sys_proto.h>
11#include <asm/global_data.h>
12#include <asm/gpio.h>
13#include <asm/mach-imx/boot_mode.h>
14#include <asm/mach-imx/iomux-v3.h>
15#include <dm/uclass.h>
16#include <hang.h>
17#include <i2c.h>
18#include <init.h>
19#include <linux/errno.h>
20#include <linux/delay.h>
21#include <power/pca9450.h>
22#include <power/pmic.h>
23#include <spl.h>
24
25DECLARE_GLOBAL_DATA_PTR;
26
27enum {
28 BOARD_TYPE_KTN_N801X,
29 BOARD_TYPE_KTN_N801X_LVDS,
30 BOARD_TYPE_MAX
31};
32
33#define GPIO_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_ODE | PAD_CTL_PUE | PAD_CTL_PE)
34#define I2C_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_HYS | PAD_CTL_PUE)
Frieder Schrempf9cab87f2021-09-29 16:42:42 +020035#define WDOG_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_ODE | PAD_CTL_PUE | PAD_CTL_PE)
36
37#define TOUCH_RESET_GPIO IMX_GPIO_NR(3, 23)
38
39static iomux_v3_cfg_t const i2c1_pads[] = {
40 IMX8MM_PAD_I2C1_SCL_I2C1_SCL | MUX_PAD_CTRL(I2C_PAD_CTRL) | MUX_MODE_SION,
41 IMX8MM_PAD_I2C1_SDA_I2C1_SDA | MUX_PAD_CTRL(I2C_PAD_CTRL) | MUX_MODE_SION
42};
43
44static iomux_v3_cfg_t const i2c2_pads[] = {
45 IMX8MM_PAD_I2C2_SCL_I2C2_SCL | MUX_PAD_CTRL(I2C_PAD_CTRL) | MUX_MODE_SION,
46 IMX8MM_PAD_I2C2_SDA_I2C2_SDA | MUX_PAD_CTRL(I2C_PAD_CTRL) | MUX_MODE_SION
47};
48
49static iomux_v3_cfg_t const touch_gpio[] = {
50 IMX8MM_PAD_SAI5_RXD2_GPIO3_IO23 | MUX_PAD_CTRL(GPIO_PAD_CTRL)
51};
52
Frieder Schrempf9cab87f2021-09-29 16:42:42 +020053static iomux_v3_cfg_t const wdog_pads[] = {
54 IMX8MM_PAD_GPIO1_IO02_WDOG1_WDOG_B | MUX_PAD_CTRL(WDOG_PAD_CTRL),
55};
56
57int spl_board_boot_device(enum boot_device boot_dev_spl)
58{
59 switch (boot_dev_spl) {
60 case USB_BOOT:
61 return BOOT_DEVICE_BOARD;
62 case SPI_NOR_BOOT:
63 return BOOT_DEVICE_SPI;
64 case SD1_BOOT:
65 case MMC1_BOOT:
66 return BOOT_DEVICE_MMC1;
67 case SD2_BOOT:
68 case MMC2_BOOT:
69 return BOOT_DEVICE_MMC2;
70 default:
71 return BOOT_DEVICE_NONE;
72 }
73}
74
75bool check_ram_available(long size)
76{
77 long sz = get_ram_size((long *)PHYS_SDRAM, size);
78
79 if (sz == size)
80 return true;
81
82 return false;
83}
84
85static void spl_dram_init(void)
86{
87 u32 size = 0;
88
89 /*
90 * Try the default DDR settings in lpddr4_timing.c to
91 * comply with the Micron 4GB DDR.
92 */
93 if (!ddr_init(&dram_timing) && check_ram_available(SZ_4G)) {
94 size = 4;
95 } else {
96 /*
97 * Overwrite some values to comply with the Micron 1GB/2GB DDRs.
98 */
99 dram_timing.ddrc_cfg[2].val = 0xa1080020;
100 dram_timing.ddrc_cfg[37].val = 0x1f;
101
102 dram_timing.fsp_msg[0].fsp_cfg[9].val = 0x110;
103 dram_timing.fsp_msg[0].fsp_cfg[21].val = 0x1;
104 dram_timing.fsp_msg[1].fsp_cfg[10].val = 0x110;
105 dram_timing.fsp_msg[1].fsp_cfg[22].val = 0x1;
106 dram_timing.fsp_msg[2].fsp_cfg[10].val = 0x110;
107 dram_timing.fsp_msg[2].fsp_cfg[22].val = 0x1;
108 dram_timing.fsp_msg[3].fsp_cfg[10].val = 0x110;
109 dram_timing.fsp_msg[3].fsp_cfg[22].val = 0x1;
110
111 if (!ddr_init(&dram_timing)) {
112 if (check_ram_available(SZ_2G))
113 size = 2;
114 else if (check_ram_available(SZ_1G))
115 size = 1;
116 }
117 }
118
119 if (size == 0) {
120 printf("Failed to initialize DDR RAM!\n");
121 size = 1;
122 }
123
124 printf("Kontron SL i.MX8MM (N801X) module, %u GB RAM detected\n", size);
125 writel(size, M4_BOOTROM_BASE_ADDR);
126}
127
128static void touch_reset(void)
129{
130 /*
131 * Toggle the reset of the touch panel.
132 */
133 imx_iomux_v3_setup_multiple_pads(touch_gpio, ARRAY_SIZE(touch_gpio));
134
135 gpio_request(TOUCH_RESET_GPIO, "touch_reset");
136 gpio_direction_output(TOUCH_RESET_GPIO, 0);
137 mdelay(20);
138 gpio_direction_output(TOUCH_RESET_GPIO, 1);
139 mdelay(20);
140}
141
142static int i2c_detect(u8 bus, u16 addr)
143{
144 struct udevice *udev;
145 int ret;
146
147 /*
148 * Try to probe the touch controller to check if an LVDS panel is
149 * connected.
150 */
151 ret = i2c_get_chip_for_busnum(bus, addr, 0, &udev);
152 if (ret == 0)
153 return 0;
154
155 return 1;
156}
157
158int do_board_detect(void)
159{
160 bool lvds = false;
161
162 /*
163 * Check the I2C touch controller to detect a LVDS panel.
164 */
165 imx_iomux_v3_setup_multiple_pads(i2c2_pads, ARRAY_SIZE(i2c2_pads));
166 touch_reset();
167
168 if (i2c_detect(1, 0x5d) == 0) {
169 printf("Touch controller detected, assuming LVDS panel...\n");
170 lvds = true;
171 }
172
173 /*
174 * Check the I2C PMIC to detect the deprecated SoM with DA9063.
175 */
176 imx_iomux_v3_setup_multiple_pads(i2c1_pads, ARRAY_SIZE(i2c1_pads));
177
178 if (i2c_detect(0, 0x58) == 0) {
179 printf("### ATTENTION: DEPRECATED SOM REVISION (N8010 Rev0) DETECTED! ###\n");
180 printf("### THIS HW IS NOT SUPPRTED AND BOOTING WILL PROBABLY FAIL ###\n");
181 printf("### PLEASE UPGRADE TO LATEST MODULE ###\n");
182 }
183
184 if (lvds)
185 gd->board_type = BOARD_TYPE_KTN_N801X_LVDS;
186 else
187 gd->board_type = BOARD_TYPE_KTN_N801X;
188
189 return 0;
190}
191
192int board_fit_config_name_match(const char *name)
193{
194 if (gd->board_type == BOARD_TYPE_KTN_N801X_LVDS && is_imx8mm() &&
195 !strncmp(name, "imx8mm-kontron-n801x-s-lvds", 27))
196 return 0;
197
198 if (gd->board_type == BOARD_TYPE_KTN_N801X && is_imx8mm() &&
199 !strncmp(name, "imx8mm-kontron-n801x-s", 22))
200 return 0;
201
202 return -1;
203}
204
205void spl_board_init(void)
206{
207 struct udevice *dev;
208 int ret;
209
210 puts("Normal Boot\n");
211
212 ret = uclass_get_device_by_name(UCLASS_CLK,
213 "clock-controller@30380000",
214 &dev);
215 if (ret < 0)
216 printf("Failed to find clock node. Check device tree\n");
217}
218
219int board_early_init_f(void)
220{
221 struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR;
222
223 imx_iomux_v3_setup_multiple_pads(wdog_pads, ARRAY_SIZE(wdog_pads));
224
225 set_wdog_reset(wdog);
226
Frieder Schrempf9cab87f2021-09-29 16:42:42 +0200227 return 0;
228}
229
230static int power_init_board(void)
231{
232 struct udevice *dev;
233 int ret = pmic_get("pmic@25", &dev);
234
235 if (ret == -ENODEV)
236 puts("No pmic found\n");
237
238 if (ret)
239 return ret;
240
241 /* BUCKxOUT_DVS0/1 control BUCK123 output, clear PRESET_EN */
242 pmic_reg_write(dev, PCA9450_BUCK123_DVS, 0x29);
243
244 /* increase VDD_DRAM to 0.95V for 1.5GHz DDR */
245 pmic_reg_write(dev, PCA9450_BUCK3OUT_DVS0, 0x1c);
246
247 /* set VDD_SNVS_0V8 from default 0.85V to 0.8V */
248 pmic_reg_write(dev, PCA9450_LDO2CTRL, 0xC0);
249
250 /* set WDOG_B_CFG to cold reset */
251 pmic_reg_write(dev, PCA9450_RESET_CTRL, 0xA1);
252
253 return 0;
254}
255
256void board_init_f(ulong dummy)
257{
258 int ret;
259
260 arch_cpu_init();
261
262 init_uart_clk(2);
263
264 board_early_init_f();
265
266 timer_init();
267
Frieder Schrempf9cab87f2021-09-29 16:42:42 +0200268 /* Clear the BSS. */
269 memset(__bss_start, 0, __bss_end - __bss_start);
270
271 ret = spl_init();
272 if (ret) {
273 debug("spl_init() failed: %d\n", ret);
274 hang();
275 }
276
Peng Fan0a16da82022-06-11 20:21:00 +0800277 preloader_console_init();
278
Frieder Schrempf9cab87f2021-09-29 16:42:42 +0200279 enable_tzc380();
280
281 /* PMIC initialization */
282 power_init_board();
283
284 /* DDR initialization */
285 spl_dram_init();
286
287 /* Detect the board type */
288 do_board_detect();
289
290 board_init_r(NULL, 0);
291}
292
293void board_boot_order(u32 *spl_boot_list)
294{
295 u32 bootdev = spl_boot_device();
296
297 /*
298 * The default boot fuse settings use the SD card (MMC2) as primary
299 * boot device, but allow SPI NOR as a fallback boot device.
300 * We can't detect the fallback case and spl_boot_device() will return
301 * BOOT_DEVICE_MMC2 despite the actual boot device being SPI NOR.
302 * Therefore we try to load U-Boot proper vom SPI NOR after loading
303 * from MMC has failed.
304 */
305 spl_boot_list[0] = bootdev;
306
307 switch (bootdev) {
308 case BOOT_DEVICE_MMC1:
309 case BOOT_DEVICE_MMC2:
310 spl_boot_list[1] = BOOT_DEVICE_SPI;
311 break;
312 }
313}