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