Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 1 | // 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> |
Fabio Estevam | 75aad88 | 2022-06-09 17:13:31 -0300 | [diff] [blame] | 16 | #include <dm/device.h> |
| 17 | #include <dm/uclass-internal.h> |
| 18 | #include <dm/device-internal.h> |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 19 | #include <hang.h> |
| 20 | #include <i2c.h> |
| 21 | #include <init.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <power/pca9450.h> |
| 25 | #include <power/pmic.h> |
| 26 | #include <spl.h> |
| 27 | |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
| 30 | enum { |
| 31 | BOARD_TYPE_KTN_N801X, |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 32 | BOARD_TYPE_MAX |
| 33 | }; |
| 34 | |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 35 | #define I2C_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_HYS | PAD_CTL_PUE) |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 36 | |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 37 | static iomux_v3_cfg_t const i2c1_pads[] = { |
| 38 | IMX8MM_PAD_I2C1_SCL_I2C1_SCL | MUX_PAD_CTRL(I2C_PAD_CTRL) | MUX_MODE_SION, |
| 39 | IMX8MM_PAD_I2C1_SDA_I2C1_SDA | MUX_PAD_CTRL(I2C_PAD_CTRL) | MUX_MODE_SION |
| 40 | }; |
| 41 | |
| 42 | static iomux_v3_cfg_t const i2c2_pads[] = { |
| 43 | IMX8MM_PAD_I2C2_SCL_I2C2_SCL | MUX_PAD_CTRL(I2C_PAD_CTRL) | MUX_MODE_SION, |
| 44 | IMX8MM_PAD_I2C2_SDA_I2C2_SDA | MUX_PAD_CTRL(I2C_PAD_CTRL) | MUX_MODE_SION |
| 45 | }; |
| 46 | |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 47 | int spl_board_boot_device(enum boot_device boot_dev_spl) |
| 48 | { |
| 49 | switch (boot_dev_spl) { |
| 50 | case USB_BOOT: |
| 51 | return BOOT_DEVICE_BOARD; |
| 52 | case SPI_NOR_BOOT: |
| 53 | return BOOT_DEVICE_SPI; |
| 54 | case SD1_BOOT: |
| 55 | case MMC1_BOOT: |
| 56 | return BOOT_DEVICE_MMC1; |
| 57 | case SD2_BOOT: |
| 58 | case MMC2_BOOT: |
| 59 | return BOOT_DEVICE_MMC2; |
| 60 | default: |
| 61 | return BOOT_DEVICE_NONE; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | bool check_ram_available(long size) |
| 66 | { |
| 67 | long sz = get_ram_size((long *)PHYS_SDRAM, size); |
| 68 | |
| 69 | if (sz == size) |
| 70 | return true; |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | static void spl_dram_init(void) |
| 76 | { |
| 77 | u32 size = 0; |
| 78 | |
| 79 | /* |
| 80 | * Try the default DDR settings in lpddr4_timing.c to |
| 81 | * comply with the Micron 4GB DDR. |
| 82 | */ |
| 83 | if (!ddr_init(&dram_timing) && check_ram_available(SZ_4G)) { |
| 84 | size = 4; |
| 85 | } else { |
| 86 | /* |
| 87 | * Overwrite some values to comply with the Micron 1GB/2GB DDRs. |
| 88 | */ |
| 89 | dram_timing.ddrc_cfg[2].val = 0xa1080020; |
| 90 | dram_timing.ddrc_cfg[37].val = 0x1f; |
| 91 | |
| 92 | dram_timing.fsp_msg[0].fsp_cfg[9].val = 0x110; |
| 93 | dram_timing.fsp_msg[0].fsp_cfg[21].val = 0x1; |
| 94 | dram_timing.fsp_msg[1].fsp_cfg[10].val = 0x110; |
| 95 | dram_timing.fsp_msg[1].fsp_cfg[22].val = 0x1; |
| 96 | dram_timing.fsp_msg[2].fsp_cfg[10].val = 0x110; |
| 97 | dram_timing.fsp_msg[2].fsp_cfg[22].val = 0x1; |
| 98 | dram_timing.fsp_msg[3].fsp_cfg[10].val = 0x110; |
| 99 | dram_timing.fsp_msg[3].fsp_cfg[22].val = 0x1; |
| 100 | |
| 101 | if (!ddr_init(&dram_timing)) { |
| 102 | if (check_ram_available(SZ_2G)) |
| 103 | size = 2; |
| 104 | else if (check_ram_available(SZ_1G)) |
| 105 | size = 1; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (size == 0) { |
| 110 | printf("Failed to initialize DDR RAM!\n"); |
| 111 | size = 1; |
| 112 | } |
| 113 | |
| 114 | printf("Kontron SL i.MX8MM (N801X) module, %u GB RAM detected\n", size); |
| 115 | writel(size, M4_BOOTROM_BASE_ADDR); |
| 116 | } |
| 117 | |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 118 | static int i2c_detect(u8 bus, u16 addr) |
| 119 | { |
| 120 | struct udevice *udev; |
| 121 | int ret; |
| 122 | |
| 123 | /* |
| 124 | * Try to probe the touch controller to check if an LVDS panel is |
| 125 | * connected. |
| 126 | */ |
| 127 | ret = i2c_get_chip_for_busnum(bus, addr, 0, &udev); |
| 128 | if (ret == 0) |
| 129 | return 0; |
| 130 | |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | int do_board_detect(void) |
| 135 | { |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 136 | /* |
| 137 | * Check the I2C PMIC to detect the deprecated SoM with DA9063. |
| 138 | */ |
| 139 | imx_iomux_v3_setup_multiple_pads(i2c1_pads, ARRAY_SIZE(i2c1_pads)); |
| 140 | |
| 141 | if (i2c_detect(0, 0x58) == 0) { |
| 142 | printf("### ATTENTION: DEPRECATED SOM REVISION (N8010 Rev0) DETECTED! ###\n"); |
Frieder Schrempf | a9f8185 | 2022-08-24 15:52:23 +0200 | [diff] [blame^] | 143 | printf("### THIS HW IS NOT SUPPORTED AND BOOTING WILL PROBABLY FAIL ###\n"); |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 144 | printf("### PLEASE UPGRADE TO LATEST MODULE ###\n"); |
| 145 | } |
| 146 | |
Frieder Schrempf | a9f8185 | 2022-08-24 15:52:23 +0200 | [diff] [blame^] | 147 | gd->board_type = BOARD_TYPE_KTN_N801X; |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int board_fit_config_name_match(const char *name) |
| 153 | { |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 154 | if (gd->board_type == BOARD_TYPE_KTN_N801X && is_imx8mm() && |
| 155 | !strncmp(name, "imx8mm-kontron-n801x-s", 22)) |
| 156 | return 0; |
| 157 | |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | void spl_board_init(void) |
| 162 | { |
| 163 | struct udevice *dev; |
| 164 | int ret; |
| 165 | |
Fabio Estevam | 75aad88 | 2022-06-09 17:13:31 -0300 | [diff] [blame] | 166 | if (IS_ENABLED(CONFIG_FSL_CAAM)) { |
| 167 | ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(caam_jr), &dev); |
| 168 | if (ret) |
| 169 | printf("Failed to initialize %s: %d\n", dev->name, ret); |
| 170 | } |
| 171 | |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 172 | puts("Normal Boot\n"); |
| 173 | |
| 174 | ret = uclass_get_device_by_name(UCLASS_CLK, |
| 175 | "clock-controller@30380000", |
| 176 | &dev); |
| 177 | if (ret < 0) |
| 178 | printf("Failed to find clock node. Check device tree\n"); |
| 179 | } |
| 180 | |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 181 | static int power_init_board(void) |
| 182 | { |
| 183 | struct udevice *dev; |
| 184 | int ret = pmic_get("pmic@25", &dev); |
| 185 | |
| 186 | if (ret == -ENODEV) |
| 187 | puts("No pmic found\n"); |
| 188 | |
| 189 | if (ret) |
| 190 | return ret; |
| 191 | |
| 192 | /* BUCKxOUT_DVS0/1 control BUCK123 output, clear PRESET_EN */ |
| 193 | pmic_reg_write(dev, PCA9450_BUCK123_DVS, 0x29); |
| 194 | |
| 195 | /* increase VDD_DRAM to 0.95V for 1.5GHz DDR */ |
| 196 | pmic_reg_write(dev, PCA9450_BUCK3OUT_DVS0, 0x1c); |
| 197 | |
| 198 | /* set VDD_SNVS_0V8 from default 0.85V to 0.8V */ |
| 199 | pmic_reg_write(dev, PCA9450_LDO2CTRL, 0xC0); |
| 200 | |
| 201 | /* set WDOG_B_CFG to cold reset */ |
| 202 | pmic_reg_write(dev, PCA9450_RESET_CTRL, 0xA1); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | void board_init_f(ulong dummy) |
| 208 | { |
| 209 | int ret; |
| 210 | |
| 211 | arch_cpu_init(); |
| 212 | |
| 213 | init_uart_clk(2); |
| 214 | |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 215 | timer_init(); |
| 216 | |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 217 | /* Clear the BSS. */ |
| 218 | memset(__bss_start, 0, __bss_end - __bss_start); |
| 219 | |
| 220 | ret = spl_init(); |
| 221 | if (ret) { |
| 222 | debug("spl_init() failed: %d\n", ret); |
| 223 | hang(); |
| 224 | } |
| 225 | |
Peng Fan | 8f52578 | 2022-06-11 20:21:00 +0800 | [diff] [blame] | 226 | preloader_console_init(); |
| 227 | |
Frieder Schrempf | 199dfd9 | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 228 | enable_tzc380(); |
| 229 | |
| 230 | /* PMIC initialization */ |
| 231 | power_init_board(); |
| 232 | |
| 233 | /* DDR initialization */ |
| 234 | spl_dram_init(); |
| 235 | |
| 236 | /* Detect the board type */ |
| 237 | do_board_detect(); |
| 238 | |
| 239 | board_init_r(NULL, 0); |
| 240 | } |
| 241 | |
| 242 | void board_boot_order(u32 *spl_boot_list) |
| 243 | { |
| 244 | u32 bootdev = spl_boot_device(); |
| 245 | |
| 246 | /* |
| 247 | * The default boot fuse settings use the SD card (MMC2) as primary |
| 248 | * boot device, but allow SPI NOR as a fallback boot device. |
| 249 | * We can't detect the fallback case and spl_boot_device() will return |
| 250 | * BOOT_DEVICE_MMC2 despite the actual boot device being SPI NOR. |
| 251 | * Therefore we try to load U-Boot proper vom SPI NOR after loading |
| 252 | * from MMC has failed. |
| 253 | */ |
| 254 | spl_boot_list[0] = bootdev; |
| 255 | |
| 256 | switch (bootdev) { |
| 257 | case BOOT_DEVICE_MMC1: |
| 258 | case BOOT_DEVICE_MMC2: |
| 259 | spl_boot_list[1] = BOOT_DEVICE_SPI; |
| 260 | break; |
| 261 | } |
| 262 | } |