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