blob: 1b05da53c352d7cc2889a085cf927f29113aca64 [file] [log] [blame]
Marek Vasutf670cd72022-05-21 16:56:26 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022 Marek Vasut <marex@denx.de>
4 */
5
6#include <common.h>
7#include <hang.h>
8#include <image.h>
9#include <init.h>
10#include <spl.h>
11#include <asm/io.h>
12#include <asm-generic/gpio.h>
13#include <asm/arch/clock.h>
14#include <asm/arch/imx8mp_pins.h>
15#include <asm/arch/sys_proto.h>
16#include <asm/mach-imx/boot_mode.h>
17#include <asm/arch/ddr.h>
Shiji Yangbb112342023-08-03 09:47:16 +080018#include <asm/sections.h>
Marek Vasutf670cd72022-05-21 16:56:26 +020019
20#include <dm/uclass.h>
21#include <dm/device.h>
22#include <dm/uclass-internal.h>
23#include <dm/device-internal.h>
24
Marek Vasut5ca41212023-09-21 20:44:17 +020025#include <linux/bitfield.h>
26
Marek Vasutf670cd72022-05-21 16:56:26 +020027#include <power/pmic.h>
28#include <power/pca9450.h>
29
30#include "lpddr4_timing.h"
31
32DECLARE_GLOBAL_DATA_PTR;
33
34#define UART_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_FSEL1)
35#define WDOG_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_ODE | PAD_CTL_PUE | PAD_CTL_PE)
36
37static const iomux_v3_cfg_t uart_pads[] = {
38 MX8MP_PAD_SAI2_RXFS__UART1_DCE_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
39 MX8MP_PAD_SAI2_RXC__UART1_DCE_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
40};
41
42static const iomux_v3_cfg_t wdog_pads[] = {
43 MX8MP_PAD_GPIO1_IO02__WDOG1_WDOG_B | MUX_PAD_CTRL(WDOG_PAD_CTRL),
44};
45
Marek Vasut5ca41212023-09-21 20:44:17 +020046static bool dh_gigabit_eqos, dh_gigabit_fec;
Marek Vasuteaee3032023-09-21 20:44:20 +020047static u8 dh_som_rev;
Marek Vasut5ca41212023-09-21 20:44:17 +020048
Marek Vasutf670cd72022-05-21 16:56:26 +020049static void dh_imx8mp_early_init_f(void)
50{
51 struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR;
52
53 imx_iomux_v3_setup_multiple_pads(wdog_pads, ARRAY_SIZE(wdog_pads));
54
55 set_wdog_reset(wdog);
56
57 imx_iomux_v3_setup_multiple_pads(uart_pads, ARRAY_SIZE(uart_pads));
58}
59
60static int dh_imx8mp_board_power_init(void)
61{
62 struct udevice *dev;
63 int ret;
64
65 ret = pmic_get("pmic@25", &dev);
66 if (ret == -ENODEV) {
67 puts("Failed to get PMIC\n");
68 return 0;
69 }
70 if (ret != 0)
71 return ret;
72
73 /* BUCKxOUT_DVS0/1 control BUCK123 output. */
74 pmic_reg_write(dev, PCA9450_BUCK123_DVS, 0x29);
75
76 /* Increase VDD_SOC to typical value 0.95V before first DRAM access. */
77 if (IS_ENABLED(CONFIG_IMX8M_VDD_SOC_850MV))
78 /* Set DVS0 to 0.85V for special case. */
79 pmic_reg_write(dev, PCA9450_BUCK1OUT_DVS0, 0x14);
80 else
81 pmic_reg_write(dev, PCA9450_BUCK1OUT_DVS0, 0x1c);
82
83 /* Set DVS1 to 0.85v for suspend. */
84 pmic_reg_write(dev, PCA9450_BUCK1OUT_DVS1, 0x14);
85
86 /*
87 * Enable DVS control through PMIC_STBY_REQ and
88 * set B1_ENMODE=1 (ON by PMIC_ON_REQ=H).
89 */
90 pmic_reg_write(dev, PCA9450_BUCK1CTRL, 0x59);
91
92 /* Kernel uses OD/OD frequency for SoC. */
93
94 /* To avoid timing risk from SoC to ARM, increase VDD_ARM to OD voltage 0.95V */
95 pmic_reg_write(dev, PCA9450_BUCK2OUT_DVS0, 0x1c);
96
Marek Vasutf670cd72022-05-21 16:56:26 +020097 /* Set LDO4 and CONFIG2 to enable the I2C level translator. */
98 pmic_reg_write(dev, PCA9450_LDO4CTRL, 0x59);
99 pmic_reg_write(dev, PCA9450_CONFIG2, 0x1);
100
101 return 0;
102}
103
104static struct dram_timing_info *dram_timing_info[8] = {
105 NULL, /* 512 MiB */
106 NULL, /* 1024 MiB */
107 NULL, /* 1536 MiB */
Marek Vasut9fa78d62023-02-11 22:49:01 +0100108 &dh_imx8mp_dhcom_dram_timing_16g_x32, /* 2048 MiB */
Marek Vasutf670cd72022-05-21 16:56:26 +0200109 NULL, /* 3072 MiB */
110 &dh_imx8mp_dhcom_dram_timing_32g_x32, /* 4096 MiB */
111 NULL, /* 6144 MiB */
112 NULL, /* 8192 MiB */
113};
114
115static void spl_dram_init(void)
116{
117 const u16 size[] = { 512, 1024, 1536, 2048, 3072, 4096, 6144, 8192 };
118 u8 memcfg = dh_get_memcfg();
119 int i;
120
121 printf("DDR: %d MiB [0x%x]\n", size[memcfg], memcfg);
122
123 if (!dram_timing_info[memcfg]) {
124 printf("Unsupported DRAM strapping, trying lowest supported. MEMCFG=0x%x\n",
125 memcfg);
126 for (i = 0; i < ARRAY_SIZE(dram_timing_info); i++)
127 if (dram_timing_info[i]) /* Configuration found */
128 break;
129 }
130
131 ddr_init(dram_timing_info[memcfg]);
132}
133
134void spl_board_init(void)
135{
136 /*
137 * Set GIC clock to 500 MHz for OD VDD_SOC. Kernel driver does not
138 * allow to change it. Should set the clock after PMIC setting done.
139 * Default is 400 MHz (system_pll1_800m with div = 2) set by ROM for
140 * ND VDD_SOC.
141 */
142 clock_enable(CCGR_GIC, 0);
143 clock_set_target_val(GIC_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(5));
144 clock_enable(CCGR_GIC, 1);
145}
146
147int spl_board_boot_device(enum boot_device boot_dev_spl)
148{
149 return BOOT_DEVICE_BOOTROM;
150}
151
Marek Vasut5ca41212023-09-21 20:44:17 +0200152int board_spl_fit_append_fdt_skip(const char *name)
153{
154 if (!dh_gigabit_eqos) { /* 1x or 2x RMII PHY SoM */
155 if (dh_gigabit_fec) { /* 1x RMII PHY SoM */
156 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-som-overlay-eth1xfast"))
157 return 0;
158 } else { /* 2x RMII PHY SoM */
159 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-som-overlay-eth2xfast"))
160 return 0;
161 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-pdk-overlay-eth2xfast")) {
162 /* 2x RMII PHY SoM on PDK2 or PDK3 */
163 if (of_machine_is_compatible("dh,imx8mp-dhcom-pdk2") ||
164 of_machine_is_compatible("dh,imx8mp-dhcom-pdk3"))
165 return 0;
166 }
167 }
168 }
169
Marek Vasuteaee3032023-09-21 20:44:20 +0200170 if (dh_som_rev == 0x0) { /* Prototype SoM rev.100 */
171 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-som-overlay-rev100"))
172 return 0;
173
174 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-pdk3-overlay-rev100") &&
175 of_machine_is_compatible("dh,imx8mp-dhcom-pdk3"))
176 return 0;
177 }
178
Marek Vasut5ca41212023-09-21 20:44:17 +0200179 return 1; /* Skip this DTO */
180}
181
182static void dh_imx8mp_board_cache_config(void)
183{
184 const void __iomem *mux_base = (void __iomem *)IOMUXC_BASE_ADDR;
185 const u32 mux_sion[] = {
186 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_ENET_RX_CTL__GPIO1_IO24),
187 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_SAI1_TXFS__GPIO4_IO10),
Marek Vasuteaee3032023-09-21 20:44:20 +0200188 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_NAND_DQS__GPIO3_IO14),
189 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_SAI1_TXD7__GPIO4_IO19),
190 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_SAI5_MCLK__GPIO3_IO25),
Marek Vasut5ca41212023-09-21 20:44:17 +0200191 };
192 int i;
193
194 for (i = 0; i < ARRAY_SIZE(mux_sion); i++)
195 setbits_le32(mux_base + mux_sion[i], IOMUX_CONFIG_SION);
196
197 dh_gigabit_eqos = !(readl(GPIO1_BASE_ADDR) & BIT(24));
198 dh_gigabit_fec = !(readl(GPIO4_BASE_ADDR) & BIT(10));
Marek Vasuteaee3032023-09-21 20:44:20 +0200199 dh_som_rev = !!(readl(GPIO3_BASE_ADDR) & BIT(14));
200 dh_som_rev |= !!(readl(GPIO4_BASE_ADDR) & BIT(19)) << 1;
201 dh_som_rev |= !!(readl(GPIO3_BASE_ADDR) & BIT(25)) << 2;
Marek Vasut5ca41212023-09-21 20:44:17 +0200202
203 for (i = 0; i < ARRAY_SIZE(mux_sion); i++)
204 clrbits_le32(mux_base + mux_sion[i], IOMUX_CONFIG_SION);
205}
206
Marek Vasutf670cd72022-05-21 16:56:26 +0200207void board_init_f(ulong dummy)
208{
209 struct udevice *dev;
210 int ret;
211
212 arch_cpu_init();
213
214 init_uart_clk(0);
215
216 dh_imx8mp_early_init_f();
217
218 preloader_console_init();
219
220 /* Clear the BSS. */
221 memset(__bss_start, 0, __bss_end - __bss_start);
222
223 ret = spl_early_init();
224 if (ret) {
225 debug("spl_early_init() failed: %d\n", ret);
226 hang();
227 }
228
229 ret = uclass_get_device_by_name(UCLASS_CLK,
230 "clock-controller@30380000",
231 &dev);
232 if (ret < 0) {
233 printf("Failed to find clock node. Check device tree\n");
234 hang();
235 }
236
237 enable_tzc380();
238
239 dh_imx8mp_board_power_init();
240
241 /* DDR initialization */
242 spl_dram_init();
243
Marek Vasut5ca41212023-09-21 20:44:17 +0200244 dh_imx8mp_board_cache_config();
245
Marek Vasutf670cd72022-05-21 16:56:26 +0200246 board_init_r(NULL, 0);
247}