Marek Vasut | f670cd7 | 2022-05-21 16:56:26 +0200 | [diff] [blame] | 1 | // 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 Yang | bb11234 | 2023-08-03 09:47:16 +0800 | [diff] [blame] | 18 | #include <asm/sections.h> |
Marek Vasut | f670cd7 | 2022-05-21 16:56:26 +0200 | [diff] [blame] | 19 | |
| 20 | #include <dm/uclass.h> |
| 21 | #include <dm/device.h> |
| 22 | #include <dm/uclass-internal.h> |
| 23 | #include <dm/device-internal.h> |
| 24 | |
Marek Vasut | 5ca4121 | 2023-09-21 20:44:17 +0200 | [diff] [blame] | 25 | #include <linux/bitfield.h> |
| 26 | |
Marek Vasut | f670cd7 | 2022-05-21 16:56:26 +0200 | [diff] [blame] | 27 | #include <power/pmic.h> |
| 28 | #include <power/pca9450.h> |
| 29 | |
| 30 | #include "lpddr4_timing.h" |
| 31 | |
| 32 | DECLARE_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 | |
| 37 | static 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 | |
| 42 | static const iomux_v3_cfg_t wdog_pads[] = { |
| 43 | MX8MP_PAD_GPIO1_IO02__WDOG1_WDOG_B | MUX_PAD_CTRL(WDOG_PAD_CTRL), |
| 44 | }; |
| 45 | |
Marek Vasut | 5ca4121 | 2023-09-21 20:44:17 +0200 | [diff] [blame] | 46 | static bool dh_gigabit_eqos, dh_gigabit_fec; |
| 47 | |
Marek Vasut | f670cd7 | 2022-05-21 16:56:26 +0200 | [diff] [blame] | 48 | static void dh_imx8mp_early_init_f(void) |
| 49 | { |
| 50 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR; |
| 51 | |
| 52 | imx_iomux_v3_setup_multiple_pads(wdog_pads, ARRAY_SIZE(wdog_pads)); |
| 53 | |
| 54 | set_wdog_reset(wdog); |
| 55 | |
| 56 | imx_iomux_v3_setup_multiple_pads(uart_pads, ARRAY_SIZE(uart_pads)); |
| 57 | } |
| 58 | |
| 59 | static int dh_imx8mp_board_power_init(void) |
| 60 | { |
| 61 | struct udevice *dev; |
| 62 | int ret; |
| 63 | |
| 64 | ret = pmic_get("pmic@25", &dev); |
| 65 | if (ret == -ENODEV) { |
| 66 | puts("Failed to get PMIC\n"); |
| 67 | return 0; |
| 68 | } |
| 69 | if (ret != 0) |
| 70 | return ret; |
| 71 | |
| 72 | /* BUCKxOUT_DVS0/1 control BUCK123 output. */ |
| 73 | pmic_reg_write(dev, PCA9450_BUCK123_DVS, 0x29); |
| 74 | |
| 75 | /* Increase VDD_SOC to typical value 0.95V before first DRAM access. */ |
| 76 | if (IS_ENABLED(CONFIG_IMX8M_VDD_SOC_850MV)) |
| 77 | /* Set DVS0 to 0.85V for special case. */ |
| 78 | pmic_reg_write(dev, PCA9450_BUCK1OUT_DVS0, 0x14); |
| 79 | else |
| 80 | pmic_reg_write(dev, PCA9450_BUCK1OUT_DVS0, 0x1c); |
| 81 | |
| 82 | /* Set DVS1 to 0.85v for suspend. */ |
| 83 | pmic_reg_write(dev, PCA9450_BUCK1OUT_DVS1, 0x14); |
| 84 | |
| 85 | /* |
| 86 | * Enable DVS control through PMIC_STBY_REQ and |
| 87 | * set B1_ENMODE=1 (ON by PMIC_ON_REQ=H). |
| 88 | */ |
| 89 | pmic_reg_write(dev, PCA9450_BUCK1CTRL, 0x59); |
| 90 | |
| 91 | /* Kernel uses OD/OD frequency for SoC. */ |
| 92 | |
| 93 | /* To avoid timing risk from SoC to ARM, increase VDD_ARM to OD voltage 0.95V */ |
| 94 | pmic_reg_write(dev, PCA9450_BUCK2OUT_DVS0, 0x1c); |
| 95 | |
Marek Vasut | f670cd7 | 2022-05-21 16:56:26 +0200 | [diff] [blame] | 96 | /* Set LDO4 and CONFIG2 to enable the I2C level translator. */ |
| 97 | pmic_reg_write(dev, PCA9450_LDO4CTRL, 0x59); |
| 98 | pmic_reg_write(dev, PCA9450_CONFIG2, 0x1); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static struct dram_timing_info *dram_timing_info[8] = { |
| 104 | NULL, /* 512 MiB */ |
| 105 | NULL, /* 1024 MiB */ |
| 106 | NULL, /* 1536 MiB */ |
Marek Vasut | 9fa78d6 | 2023-02-11 22:49:01 +0100 | [diff] [blame] | 107 | &dh_imx8mp_dhcom_dram_timing_16g_x32, /* 2048 MiB */ |
Marek Vasut | f670cd7 | 2022-05-21 16:56:26 +0200 | [diff] [blame] | 108 | NULL, /* 3072 MiB */ |
| 109 | &dh_imx8mp_dhcom_dram_timing_32g_x32, /* 4096 MiB */ |
| 110 | NULL, /* 6144 MiB */ |
| 111 | NULL, /* 8192 MiB */ |
| 112 | }; |
| 113 | |
| 114 | static void spl_dram_init(void) |
| 115 | { |
| 116 | const u16 size[] = { 512, 1024, 1536, 2048, 3072, 4096, 6144, 8192 }; |
| 117 | u8 memcfg = dh_get_memcfg(); |
| 118 | int i; |
| 119 | |
| 120 | printf("DDR: %d MiB [0x%x]\n", size[memcfg], memcfg); |
| 121 | |
| 122 | if (!dram_timing_info[memcfg]) { |
| 123 | printf("Unsupported DRAM strapping, trying lowest supported. MEMCFG=0x%x\n", |
| 124 | memcfg); |
| 125 | for (i = 0; i < ARRAY_SIZE(dram_timing_info); i++) |
| 126 | if (dram_timing_info[i]) /* Configuration found */ |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | ddr_init(dram_timing_info[memcfg]); |
| 131 | } |
| 132 | |
| 133 | void spl_board_init(void) |
| 134 | { |
| 135 | /* |
| 136 | * Set GIC clock to 500 MHz for OD VDD_SOC. Kernel driver does not |
| 137 | * allow to change it. Should set the clock after PMIC setting done. |
| 138 | * Default is 400 MHz (system_pll1_800m with div = 2) set by ROM for |
| 139 | * ND VDD_SOC. |
| 140 | */ |
| 141 | clock_enable(CCGR_GIC, 0); |
| 142 | clock_set_target_val(GIC_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(5)); |
| 143 | clock_enable(CCGR_GIC, 1); |
| 144 | } |
| 145 | |
| 146 | int spl_board_boot_device(enum boot_device boot_dev_spl) |
| 147 | { |
| 148 | return BOOT_DEVICE_BOOTROM; |
| 149 | } |
| 150 | |
Marek Vasut | 5ca4121 | 2023-09-21 20:44:17 +0200 | [diff] [blame] | 151 | int board_spl_fit_append_fdt_skip(const char *name) |
| 152 | { |
| 153 | if (!dh_gigabit_eqos) { /* 1x or 2x RMII PHY SoM */ |
| 154 | if (dh_gigabit_fec) { /* 1x RMII PHY SoM */ |
| 155 | if (!strcmp(name, "fdt-dto-imx8mp-dhcom-som-overlay-eth1xfast")) |
| 156 | return 0; |
| 157 | } else { /* 2x RMII PHY SoM */ |
| 158 | if (!strcmp(name, "fdt-dto-imx8mp-dhcom-som-overlay-eth2xfast")) |
| 159 | return 0; |
| 160 | if (!strcmp(name, "fdt-dto-imx8mp-dhcom-pdk-overlay-eth2xfast")) { |
| 161 | /* 2x RMII PHY SoM on PDK2 or PDK3 */ |
| 162 | if (of_machine_is_compatible("dh,imx8mp-dhcom-pdk2") || |
| 163 | of_machine_is_compatible("dh,imx8mp-dhcom-pdk3")) |
| 164 | return 0; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return 1; /* Skip this DTO */ |
| 170 | } |
| 171 | |
| 172 | static void dh_imx8mp_board_cache_config(void) |
| 173 | { |
| 174 | const void __iomem *mux_base = (void __iomem *)IOMUXC_BASE_ADDR; |
| 175 | const u32 mux_sion[] = { |
| 176 | FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_ENET_RX_CTL__GPIO1_IO24), |
| 177 | FIELD_GET(MUX_CTRL_OFS_MASK, MX8MP_PAD_SAI1_TXFS__GPIO4_IO10), |
| 178 | }; |
| 179 | int i; |
| 180 | |
| 181 | for (i = 0; i < ARRAY_SIZE(mux_sion); i++) |
| 182 | setbits_le32(mux_base + mux_sion[i], IOMUX_CONFIG_SION); |
| 183 | |
| 184 | dh_gigabit_eqos = !(readl(GPIO1_BASE_ADDR) & BIT(24)); |
| 185 | dh_gigabit_fec = !(readl(GPIO4_BASE_ADDR) & BIT(10)); |
| 186 | |
| 187 | for (i = 0; i < ARRAY_SIZE(mux_sion); i++) |
| 188 | clrbits_le32(mux_base + mux_sion[i], IOMUX_CONFIG_SION); |
| 189 | } |
| 190 | |
Marek Vasut | f670cd7 | 2022-05-21 16:56:26 +0200 | [diff] [blame] | 191 | void board_init_f(ulong dummy) |
| 192 | { |
| 193 | struct udevice *dev; |
| 194 | int ret; |
| 195 | |
| 196 | arch_cpu_init(); |
| 197 | |
| 198 | init_uart_clk(0); |
| 199 | |
| 200 | dh_imx8mp_early_init_f(); |
| 201 | |
| 202 | preloader_console_init(); |
| 203 | |
| 204 | /* Clear the BSS. */ |
| 205 | memset(__bss_start, 0, __bss_end - __bss_start); |
| 206 | |
| 207 | ret = spl_early_init(); |
| 208 | if (ret) { |
| 209 | debug("spl_early_init() failed: %d\n", ret); |
| 210 | hang(); |
| 211 | } |
| 212 | |
| 213 | ret = uclass_get_device_by_name(UCLASS_CLK, |
| 214 | "clock-controller@30380000", |
| 215 | &dev); |
| 216 | if (ret < 0) { |
| 217 | printf("Failed to find clock node. Check device tree\n"); |
| 218 | hang(); |
| 219 | } |
| 220 | |
| 221 | enable_tzc380(); |
| 222 | |
| 223 | dh_imx8mp_board_power_init(); |
| 224 | |
| 225 | /* DDR initialization */ |
| 226 | spl_dram_init(); |
| 227 | |
Marek Vasut | 5ca4121 | 2023-09-21 20:44:17 +0200 | [diff] [blame] | 228 | dh_imx8mp_board_cache_config(); |
| 229 | |
Marek Vasut | f670cd7 | 2022-05-21 16:56:26 +0200 | [diff] [blame] | 230 | board_init_r(NULL, 0); |
| 231 | } |