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