blob: 7d228da8e5b73181adf28754e0cc2f0b6a14844a [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>
Marek Vasuteae88ad2023-12-16 06:42:29 +010014#include <asm/arch/ddr.h>
Marek Vasutf670cd72022-05-21 16:56:26 +020015#include <asm/arch/imx8mp_pins.h>
16#include <asm/arch/sys_proto.h>
17#include <asm/mach-imx/boot_mode.h>
18#include <asm/arch/ddr.h>
Shiji Yangbb112342023-08-03 09:47:16 +080019#include <asm/sections.h>
Marek Vasutf670cd72022-05-21 16:56:26 +020020
21#include <dm/uclass.h>
22#include <dm/device.h>
23#include <dm/uclass-internal.h>
24#include <dm/device-internal.h>
25
Marek Vasut5ca41212023-09-21 20:44:17 +020026#include <linux/bitfield.h>
27
Marek Vasutf670cd72022-05-21 16:56:26 +020028#include <power/pmic.h>
29#include <power/pca9450.h>
30
31#include "lpddr4_timing.h"
32
33DECLARE_GLOBAL_DATA_PTR;
34
35#define UART_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_FSEL1)
36#define WDOG_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_ODE | PAD_CTL_PUE | PAD_CTL_PE)
37
38static const iomux_v3_cfg_t uart_pads[] = {
39 MX8MP_PAD_SAI2_RXFS__UART1_DCE_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
40 MX8MP_PAD_SAI2_RXC__UART1_DCE_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
41};
42
43static const iomux_v3_cfg_t wdog_pads[] = {
44 MX8MP_PAD_GPIO1_IO02__WDOG1_WDOG_B | MUX_PAD_CTRL(WDOG_PAD_CTRL),
45};
46
Marek Vasut5ca41212023-09-21 20:44:17 +020047static bool dh_gigabit_eqos, dh_gigabit_fec;
Marek Vasuteaee3032023-09-21 20:44:20 +020048static u8 dh_som_rev;
Marek Vasut5ca41212023-09-21 20:44:17 +020049
Marek Vasutf670cd72022-05-21 16:56:26 +020050static void dh_imx8mp_early_init_f(void)
51{
52 struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR;
53
54 imx_iomux_v3_setup_multiple_pads(wdog_pads, ARRAY_SIZE(wdog_pads));
55
56 set_wdog_reset(wdog);
57
58 imx_iomux_v3_setup_multiple_pads(uart_pads, ARRAY_SIZE(uart_pads));
59}
60
61static int dh_imx8mp_board_power_init(void)
62{
63 struct udevice *dev;
64 int ret;
65
66 ret = pmic_get("pmic@25", &dev);
67 if (ret == -ENODEV) {
68 puts("Failed to get PMIC\n");
69 return 0;
70 }
71 if (ret != 0)
72 return ret;
73
74 /* BUCKxOUT_DVS0/1 control BUCK123 output. */
75 pmic_reg_write(dev, PCA9450_BUCK123_DVS, 0x29);
76
77 /* Increase VDD_SOC to typical value 0.95V before first DRAM access. */
78 if (IS_ENABLED(CONFIG_IMX8M_VDD_SOC_850MV))
79 /* Set DVS0 to 0.85V for special case. */
80 pmic_reg_write(dev, PCA9450_BUCK1OUT_DVS0, 0x14);
81 else
82 pmic_reg_write(dev, PCA9450_BUCK1OUT_DVS0, 0x1c);
83
84 /* Set DVS1 to 0.85v for suspend. */
85 pmic_reg_write(dev, PCA9450_BUCK1OUT_DVS1, 0x14);
86
87 /*
88 * Enable DVS control through PMIC_STBY_REQ and
89 * set B1_ENMODE=1 (ON by PMIC_ON_REQ=H).
90 */
91 pmic_reg_write(dev, PCA9450_BUCK1CTRL, 0x59);
92
93 /* Kernel uses OD/OD frequency for SoC. */
94
95 /* To avoid timing risk from SoC to ARM, increase VDD_ARM to OD voltage 0.95V */
96 pmic_reg_write(dev, PCA9450_BUCK2OUT_DVS0, 0x1c);
97
Marek Vasutd99c1652023-12-16 06:42:28 +010098 /* DRAM Vdd1 always FPWM */
99 pmic_reg_write(dev, PCA9450_BUCK5CTRL, 0x0d);
100 /* DRAM Vdd2/Vddq always FPWM */
101 pmic_reg_write(dev, PCA9450_BUCK6CTRL, 0x0d);
102
Marek Vasutf670cd72022-05-21 16:56:26 +0200103 /* Set LDO4 and CONFIG2 to enable the I2C level translator. */
104 pmic_reg_write(dev, PCA9450_LDO4CTRL, 0x59);
105 pmic_reg_write(dev, PCA9450_CONFIG2, 0x1);
106
107 return 0;
108}
109
110static struct dram_timing_info *dram_timing_info[8] = {
111 NULL, /* 512 MiB */
112 NULL, /* 1024 MiB */
113 NULL, /* 1536 MiB */
Marek Vasut9fa78d62023-02-11 22:49:01 +0100114 &dh_imx8mp_dhcom_dram_timing_16g_x32, /* 2048 MiB */
Marek Vasutf670cd72022-05-21 16:56:26 +0200115 NULL, /* 3072 MiB */
116 &dh_imx8mp_dhcom_dram_timing_32g_x32, /* 4096 MiB */
117 NULL, /* 6144 MiB */
118 NULL, /* 8192 MiB */
119};
120
121static void spl_dram_init(void)
122{
123 const u16 size[] = { 512, 1024, 1536, 2048, 3072, 4096, 6144, 8192 };
124 u8 memcfg = dh_get_memcfg();
125 int i;
126
127 printf("DDR: %d MiB [0x%x]\n", size[memcfg], memcfg);
128
129 if (!dram_timing_info[memcfg]) {
130 printf("Unsupported DRAM strapping, trying lowest supported. MEMCFG=0x%x\n",
131 memcfg);
132 for (i = 0; i < ARRAY_SIZE(dram_timing_info); i++)
133 if (dram_timing_info[i]) /* Configuration found */
134 break;
135 }
136
137 ddr_init(dram_timing_info[memcfg]);
Marek Vasuteae88ad2023-12-16 06:42:29 +0100138
139 printf("DDR: Inline ECC %sabled\n",
140 (readl(DDRC_ECCCFG0(0)) & DDRC_ECCCFG0_ECC_MODE_MASK) ?
141 "en" : "dis");
142}
143
144#if IS_ENABLED(CONFIG_IMX8M_DRAM_INLINE_ECC)
145static const scrub_func_t dram_scrub_fn[8] = {
146 NULL, /* 512 MiB */
147 NULL, /* 1024 MiB */
148 NULL, /* 1536 MiB */
149 dh_imx8mp_dhcom_dram_scrub_16g_x32, /* 2048 MiB */
150 NULL, /* 3072 MiB */
151 dh_imx8mp_dhcom_dram_scrub_32g_x32, /* 4096 MiB */
152 NULL, /* 6144 MiB */
153 NULL, /* 8192 MiB */
154};
155
156void board_dram_ecc_scrub(void)
157{
158 u8 memcfg = dh_get_memcfg();
159
160 if (!dram_scrub_fn[memcfg])
161 return;
162
163 dram_scrub_fn[memcfg]();
Marek Vasutf670cd72022-05-21 16:56:26 +0200164}
Marek Vasuteae88ad2023-12-16 06:42:29 +0100165#endif
Marek Vasutf670cd72022-05-21 16:56:26 +0200166
167void spl_board_init(void)
168{
169 /*
170 * Set GIC clock to 500 MHz for OD VDD_SOC. Kernel driver does not
171 * allow to change it. Should set the clock after PMIC setting done.
172 * Default is 400 MHz (system_pll1_800m with div = 2) set by ROM for
173 * ND VDD_SOC.
174 */
175 clock_enable(CCGR_GIC, 0);
176 clock_set_target_val(GIC_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(5));
177 clock_enable(CCGR_GIC, 1);
178}
179
180int spl_board_boot_device(enum boot_device boot_dev_spl)
181{
182 return BOOT_DEVICE_BOOTROM;
183}
184
Marek Vasut5ca41212023-09-21 20:44:17 +0200185int board_spl_fit_append_fdt_skip(const char *name)
186{
187 if (!dh_gigabit_eqos) { /* 1x or 2x RMII PHY SoM */
188 if (dh_gigabit_fec) { /* 1x RMII PHY SoM */
189 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-som-overlay-eth1xfast"))
190 return 0;
191 } else { /* 2x RMII PHY SoM */
192 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-som-overlay-eth2xfast"))
193 return 0;
194 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-pdk-overlay-eth2xfast")) {
195 /* 2x RMII PHY SoM on PDK2 or PDK3 */
196 if (of_machine_is_compatible("dh,imx8mp-dhcom-pdk2") ||
197 of_machine_is_compatible("dh,imx8mp-dhcom-pdk3"))
198 return 0;
199 }
200 }
201 }
202
Marek Vasuteaee3032023-09-21 20:44:20 +0200203 if (dh_som_rev == 0x0) { /* Prototype SoM rev.100 */
204 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-som-overlay-rev100"))
205 return 0;
206
207 if (!strcmp(name, "fdt-dto-imx8mp-dhcom-pdk3-overlay-rev100") &&
208 of_machine_is_compatible("dh,imx8mp-dhcom-pdk3"))
209 return 0;
210 }
211
Marek Vasut5ca41212023-09-21 20:44:17 +0200212 return 1; /* Skip this DTO */
213}
214
215static void dh_imx8mp_board_cache_config(void)
216{
217 const void __iomem *mux_base = (void __iomem *)IOMUXC_BASE_ADDR;
218 const u32 mux_sion[] = {
219 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_ENET_RX_CTL__GPIO1_IO24),
220 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_SAI1_TXFS__GPIO4_IO10),
Marek Vasuteaee3032023-09-21 20:44:20 +0200221 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_NAND_DQS__GPIO3_IO14),
222 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_SAI1_TXD7__GPIO4_IO19),
223 FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_SAI5_MCLK__GPIO3_IO25),
Marek Vasut5ca41212023-09-21 20:44:17 +0200224 };
225 int i;
226
227 for (i = 0; i < ARRAY_SIZE(mux_sion); i++)
228 setbits_le32(mux_base + mux_sion[i], IOMUX_CONFIG_SION);
229
230 dh_gigabit_eqos = !(readl(GPIO1_BASE_ADDR) & BIT(24));
231 dh_gigabit_fec = !(readl(GPIO4_BASE_ADDR) & BIT(10));
Marek Vasuteaee3032023-09-21 20:44:20 +0200232 dh_som_rev = !!(readl(GPIO3_BASE_ADDR) & BIT(14));
233 dh_som_rev |= !!(readl(GPIO4_BASE_ADDR) & BIT(19)) << 1;
234 dh_som_rev |= !!(readl(GPIO3_BASE_ADDR) & BIT(25)) << 2;
Marek Vasut5ca41212023-09-21 20:44:17 +0200235
236 for (i = 0; i < ARRAY_SIZE(mux_sion); i++)
237 clrbits_le32(mux_base + mux_sion[i], IOMUX_CONFIG_SION);
238}
239
Marek Vasutf670cd72022-05-21 16:56:26 +0200240void board_init_f(ulong dummy)
241{
242 struct udevice *dev;
243 int ret;
244
245 arch_cpu_init();
246
247 init_uart_clk(0);
248
249 dh_imx8mp_early_init_f();
250
251 preloader_console_init();
252
253 /* Clear the BSS. */
254 memset(__bss_start, 0, __bss_end - __bss_start);
255
256 ret = spl_early_init();
257 if (ret) {
258 debug("spl_early_init() failed: %d\n", ret);
259 hang();
260 }
261
262 ret = uclass_get_device_by_name(UCLASS_CLK,
263 "clock-controller@30380000",
264 &dev);
265 if (ret < 0) {
266 printf("Failed to find clock node. Check device tree\n");
267 hang();
268 }
269
270 enable_tzc380();
271
272 dh_imx8mp_board_power_init();
273
274 /* DDR initialization */
275 spl_dram_init();
276
Marek Vasut5ca41212023-09-21 20:44:17 +0200277 dh_imx8mp_board_cache_config();
278
Marek Vasutf670cd72022-05-21 16:56:26 +0200279 board_init_r(NULL, 0);
280}