Frieder Schrempf | 9cab87f | 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> |
Shiji Yang | 506df9d | 2023-08-03 09:47:16 +0800 | [diff] [blame] | 15 | #include <asm/sections.h> |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 16 | #include <dm/uclass.h> |
Fabio Estevam | b502325 | 2022-06-09 17:13:31 -0300 | [diff] [blame] | 17 | #include <dm/device.h> |
| 18 | #include <dm/uclass-internal.h> |
| 19 | #include <dm/device-internal.h> |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 20 | #include <hang.h> |
| 21 | #include <i2c.h> |
| 22 | #include <init.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <power/pca9450.h> |
| 26 | #include <power/pmic.h> |
| 27 | #include <spl.h> |
| 28 | |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
| 31 | enum { |
| 32 | BOARD_TYPE_KTN_N801X, |
Frieder Schrempf | ba30cc2 | 2022-08-24 15:59:19 +0200 | [diff] [blame] | 33 | BOARD_TYPE_KTN_N802X, |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 34 | BOARD_TYPE_MAX |
| 35 | }; |
| 36 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 37 | #define I2C_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_HYS | PAD_CTL_PUE) |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 38 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 39 | static 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 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 44 | int spl_board_boot_device(enum boot_device boot_dev_spl) |
| 45 | { |
| 46 | switch (boot_dev_spl) { |
| 47 | case USB_BOOT: |
| 48 | return BOOT_DEVICE_BOARD; |
| 49 | case SPI_NOR_BOOT: |
| 50 | return BOOT_DEVICE_SPI; |
| 51 | case SD1_BOOT: |
| 52 | case MMC1_BOOT: |
| 53 | return BOOT_DEVICE_MMC1; |
| 54 | case SD2_BOOT: |
| 55 | case MMC2_BOOT: |
| 56 | return BOOT_DEVICE_MMC2; |
| 57 | default: |
| 58 | return BOOT_DEVICE_NONE; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool check_ram_available(long size) |
| 63 | { |
| 64 | long sz = get_ram_size((long *)PHYS_SDRAM, size); |
| 65 | |
| 66 | if (sz == size) |
| 67 | return true; |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | static void spl_dram_init(void) |
| 73 | { |
| 74 | u32 size = 0; |
| 75 | |
| 76 | /* |
| 77 | * Try the default DDR settings in lpddr4_timing.c to |
| 78 | * comply with the Micron 4GB DDR. |
| 79 | */ |
| 80 | if (!ddr_init(&dram_timing) && check_ram_available(SZ_4G)) { |
| 81 | size = 4; |
| 82 | } else { |
| 83 | /* |
| 84 | * Overwrite some values to comply with the Micron 1GB/2GB DDRs. |
| 85 | */ |
| 86 | dram_timing.ddrc_cfg[2].val = 0xa1080020; |
| 87 | dram_timing.ddrc_cfg[37].val = 0x1f; |
| 88 | |
Frieder Schrempf | 3a683ae | 2022-08-24 15:59:13 +0200 | [diff] [blame] | 89 | dram_timing.fsp_msg[0].fsp_cfg[8].val = 0x110; |
| 90 | dram_timing.fsp_msg[0].fsp_cfg[20].val = 0x1; |
| 91 | dram_timing.fsp_msg[1].fsp_cfg[9].val = 0x110; |
| 92 | dram_timing.fsp_msg[1].fsp_cfg[21].val = 0x1; |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 93 | dram_timing.fsp_msg[2].fsp_cfg[10].val = 0x110; |
| 94 | dram_timing.fsp_msg[2].fsp_cfg[22].val = 0x1; |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 95 | |
| 96 | if (!ddr_init(&dram_timing)) { |
| 97 | if (check_ram_available(SZ_2G)) |
| 98 | size = 2; |
| 99 | else if (check_ram_available(SZ_1G)) |
| 100 | size = 1; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (size == 0) { |
| 105 | printf("Failed to initialize DDR RAM!\n"); |
| 106 | size = 1; |
| 107 | } |
| 108 | |
Frieder Schrempf | c25c906 | 2022-08-24 15:59:18 +0200 | [diff] [blame] | 109 | gd->ram_size = size; |
Peng Fan | 9395eb0 | 2023-06-15 18:09:18 +0800 | [diff] [blame] | 110 | writel(size, MCU_BOOTROM_BASE_ADDR); |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 113 | int do_board_detect(void) |
| 114 | { |
Frieder Schrempf | ba30cc2 | 2022-08-24 15:59:19 +0200 | [diff] [blame] | 115 | struct udevice *udev; |
| 116 | |
| 117 | /* |
| 118 | * Check for the RTC on the OSM module. |
| 119 | */ |
| 120 | imx_iomux_v3_setup_multiple_pads(i2c1_pads, ARRAY_SIZE(i2c1_pads)); |
| 121 | |
| 122 | if (i2c_get_chip_for_busnum(0, 0x52, 0, &udev) == 0) { |
| 123 | gd->board_type = BOARD_TYPE_KTN_N802X; |
| 124 | printf("Kontron OSM-S i.MX8MM (N802X) module, %u GB RAM detected\n", |
| 125 | (unsigned int)gd->ram_size); |
| 126 | } else { |
| 127 | gd->board_type = BOARD_TYPE_KTN_N801X; |
| 128 | printf("Kontron SL i.MX8MM (N801X) module, %u GB RAM detected\n", |
| 129 | (unsigned int)gd->ram_size); |
| 130 | } |
Frieder Schrempf | c25c906 | 2022-08-24 15:59:18 +0200 | [diff] [blame] | 131 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 132 | /* |
| 133 | * Check the I2C PMIC to detect the deprecated SoM with DA9063. |
| 134 | */ |
| 135 | imx_iomux_v3_setup_multiple_pads(i2c1_pads, ARRAY_SIZE(i2c1_pads)); |
| 136 | |
Frieder Schrempf | c25c906 | 2022-08-24 15:59:18 +0200 | [diff] [blame] | 137 | if (i2c_get_chip_for_busnum(0, 0x58, 0, &udev) == 0) { |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 138 | printf("### ATTENTION: DEPRECATED SOM REVISION (N8010 Rev0) DETECTED! ###\n"); |
Frieder Schrempf | a06c6b2 | 2022-08-24 15:52:23 +0200 | [diff] [blame] | 139 | printf("### THIS HW IS NOT SUPPORTED AND BOOTING WILL PROBABLY FAIL ###\n"); |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 140 | printf("### PLEASE UPGRADE TO LATEST MODULE ###\n"); |
| 141 | } |
| 142 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int board_fit_config_name_match(const char *name) |
| 147 | { |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 148 | if (gd->board_type == BOARD_TYPE_KTN_N801X && is_imx8mm() && |
Frieder Schrempf | dfbdc69 | 2022-08-24 15:59:15 +0200 | [diff] [blame] | 149 | (!strcmp(name, "imx8mm-kontron-n801x-s") || |
| 150 | !strcmp(name, "imx8mm-kontron-bl"))) |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 151 | return 0; |
| 152 | |
Frieder Schrempf | ba30cc2 | 2022-08-24 15:59:19 +0200 | [diff] [blame] | 153 | if (gd->board_type == BOARD_TYPE_KTN_N802X && is_imx8mm() && |
| 154 | (!strcmp(name, "imx8mm-kontron-n802x-s") || |
| 155 | !strcmp(name, "imx8mm-kontron-bl-osm-s"))) |
| 156 | return 0; |
| 157 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | void spl_board_init(void) |
| 162 | { |
| 163 | struct udevice *dev; |
| 164 | int ret; |
| 165 | |
Marek Vasut | 1f908b1 | 2022-09-19 21:41:15 +0200 | [diff] [blame] | 166 | arch_misc_init(); |
Fabio Estevam | b502325 | 2022-06-09 17:13:31 -0300 | [diff] [blame] | 167 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 168 | puts("Normal Boot\n"); |
| 169 | |
| 170 | ret = uclass_get_device_by_name(UCLASS_CLK, |
| 171 | "clock-controller@30380000", |
| 172 | &dev); |
| 173 | if (ret < 0) |
| 174 | printf("Failed to find clock node. Check device tree\n"); |
| 175 | } |
| 176 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 177 | static int power_init_board(void) |
| 178 | { |
| 179 | struct udevice *dev; |
| 180 | int ret = pmic_get("pmic@25", &dev); |
| 181 | |
| 182 | if (ret == -ENODEV) |
| 183 | puts("No pmic found\n"); |
| 184 | |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | |
| 188 | /* BUCKxOUT_DVS0/1 control BUCK123 output, clear PRESET_EN */ |
| 189 | pmic_reg_write(dev, PCA9450_BUCK123_DVS, 0x29); |
| 190 | |
| 191 | /* increase VDD_DRAM to 0.95V for 1.5GHz DDR */ |
| 192 | pmic_reg_write(dev, PCA9450_BUCK3OUT_DVS0, 0x1c); |
| 193 | |
| 194 | /* set VDD_SNVS_0V8 from default 0.85V to 0.8V */ |
| 195 | pmic_reg_write(dev, PCA9450_LDO2CTRL, 0xC0); |
| 196 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | void board_init_f(ulong dummy) |
| 201 | { |
| 202 | int ret; |
| 203 | |
| 204 | arch_cpu_init(); |
| 205 | |
| 206 | init_uart_clk(2); |
| 207 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 208 | timer_init(); |
| 209 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 210 | /* Clear the BSS. */ |
| 211 | memset(__bss_start, 0, __bss_end - __bss_start); |
| 212 | |
| 213 | ret = spl_init(); |
| 214 | if (ret) { |
| 215 | debug("spl_init() failed: %d\n", ret); |
| 216 | hang(); |
| 217 | } |
| 218 | |
Peng Fan | 0a16da8 | 2022-06-11 20:21:00 +0800 | [diff] [blame] | 219 | preloader_console_init(); |
| 220 | |
Frieder Schrempf | 9cab87f | 2021-09-29 16:42:42 +0200 | [diff] [blame] | 221 | enable_tzc380(); |
| 222 | |
| 223 | /* PMIC initialization */ |
| 224 | power_init_board(); |
| 225 | |
| 226 | /* DDR initialization */ |
| 227 | spl_dram_init(); |
| 228 | |
| 229 | /* Detect the board type */ |
| 230 | do_board_detect(); |
| 231 | |
| 232 | board_init_r(NULL, 0); |
| 233 | } |