Marek Vasut | 7894311 | 2022-12-11 21:17:14 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2022 Marek Vasut <marex@denx.de> |
| 4 | */ |
| 5 | |
Marek Vasut | 7894311 | 2022-12-11 21:17:14 +0100 | [diff] [blame] | 6 | #include <asm-generic/gpio.h> |
| 7 | #include <asm-generic/sections.h> |
| 8 | #include <asm/arch/clock.h> |
| 9 | #include <asm/arch/ddr.h> |
| 10 | #include <asm/arch/sys_proto.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <asm/mach-imx/boot_mode.h> |
| 13 | #include <asm/mach-imx/iomux-v3.h> |
Marek Vasut | 7894311 | 2022-12-11 21:17:14 +0100 | [diff] [blame] | 14 | #include <dm/uclass.h> |
| 15 | #include <hang.h> |
| 16 | #include <i2c_eeprom.h> |
| 17 | #include <image.h> |
| 18 | #include <init.h> |
| 19 | #include <net.h> |
| 20 | #include <spl.h> |
| 21 | |
| 22 | #include <dm/uclass.h> |
| 23 | #include <dm/device.h> |
| 24 | #include <dm/uclass-internal.h> |
| 25 | #include <dm/device-internal.h> |
| 26 | |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
| 29 | #define WDOG_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_ODE | PAD_CTL_PUE | PAD_CTL_PE) |
| 30 | |
Marek Vasut | b7ad770 | 2023-12-07 18:50:32 +0100 | [diff] [blame] | 31 | #define DDRC_ECCCFG0_ECC_MODE_MASK 0x7 |
| 32 | |
Marek Vasut | 7894311 | 2022-12-11 21:17:14 +0100 | [diff] [blame] | 33 | u8 dmo_get_memcfg(void) |
| 34 | { |
| 35 | struct gpio_desc gpio[4]; |
| 36 | u8 memcfg = 0; |
| 37 | ofnode node; |
| 38 | int i, ret; |
| 39 | |
| 40 | node = ofnode_path("/config"); |
| 41 | if (!ofnode_valid(node)) { |
| 42 | printf("%s: no /config node?\n", __func__); |
| 43 | return BIT(2) | BIT(0); |
| 44 | } |
| 45 | |
| 46 | ret = gpio_request_list_by_name_nodev(node, |
| 47 | "dmo,ram-coding-gpios", |
| 48 | gpio, ARRAY_SIZE(gpio), |
| 49 | GPIOD_IS_IN); |
| 50 | for (i = 0; i < ret; i++) |
| 51 | memcfg |= !!dm_gpio_get_value(&(gpio[i])) << i; |
| 52 | |
| 53 | gpio_free_list_nodev(gpio, ret); |
| 54 | |
| 55 | return memcfg; |
| 56 | } |
| 57 | |
| 58 | int board_phys_sdram_size(phys_size_t *size) |
| 59 | { |
| 60 | u8 memcfg = dmo_get_memcfg(); |
Marek Vasut | b7ad770 | 2023-12-07 18:50:32 +0100 | [diff] [blame] | 61 | u8 ecc = 0; |
| 62 | |
| 63 | *size = 4ULL >> ((memcfg >> 1) & 0x3); |
| 64 | |
| 65 | if (IS_ENABLED(CONFIG_IMX8M_DRAM_INLINE_ECC)) { |
| 66 | /* 896 MiB, i.e. 1 GiB without 12.5% reserved for in-band ECC */ |
| 67 | ecc = readl(DDRC_ECCCFG0(0)) & DDRC_ECCCFG0_ECC_MODE_MASK; |
| 68 | } |
Marek Vasut | 7894311 | 2022-12-11 21:17:14 +0100 | [diff] [blame] | 69 | |
Marek Vasut | b7ad770 | 2023-12-07 18:50:32 +0100 | [diff] [blame] | 70 | *size *= SZ_1G - (ecc ? (SZ_1G / 8) : 0); |
Marek Vasut | 7894311 | 2022-12-11 21:17:14 +0100 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Simon Glass | 49c24a8 | 2024-09-29 19:49:47 -0600 | [diff] [blame] | 75 | #ifdef CONFIG_XPL_BUILD |
Marek Vasut | 7894311 | 2022-12-11 21:17:14 +0100 | [diff] [blame] | 76 | static void data_modul_imx_edm_sbc_early_init_f(const iomux_v3_cfg_t wdog_pad) |
| 77 | { |
| 78 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR; |
| 79 | |
| 80 | imx_iomux_v3_setup_pad(wdog_pad | MUX_PAD_CTRL(WDOG_PAD_CTRL)); |
| 81 | |
| 82 | set_wdog_reset(wdog); |
| 83 | } |
| 84 | |
| 85 | __weak int data_modul_imx_edm_sbc_board_power_init(void) |
| 86 | { |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static void spl_dram_init(struct dram_timing_info *dram_timing_info[8]) |
| 91 | { |
| 92 | u8 memcfg = dmo_get_memcfg(); |
| 93 | int i; |
| 94 | |
| 95 | printf("DDR: %d GiB x%d [0x%x]\n", |
| 96 | /* 0..4 GiB, 1..2 GiB, 0..1 GiB */ |
| 97 | 4 >> ((memcfg >> 1) & 0x3), |
| 98 | /* 0..x32, 1..x16 */ |
| 99 | 32 >> (memcfg & BIT(0)), |
| 100 | memcfg); |
| 101 | |
| 102 | if (!dram_timing_info[memcfg]) { |
| 103 | printf("Unsupported DRAM strapping, trying lowest supported. MEMCFG=0x%x\n", |
| 104 | memcfg); |
| 105 | for (i = 7; i >= 0; i--) |
| 106 | if (dram_timing_info[i]) /* Configuration found */ |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | ddr_init(dram_timing_info[memcfg]); |
Marek Vasut | b7ad770 | 2023-12-07 18:50:32 +0100 | [diff] [blame] | 111 | |
| 112 | if (IS_ENABLED(CONFIG_IMX8M_DRAM_INLINE_ECC)) { |
| 113 | printf("DDR: Inline ECC %sabled\n", |
| 114 | (readl(DDRC_ECCCFG0(0)) & DDRC_ECCCFG0_ECC_MODE_MASK) ? |
| 115 | "en" : "dis"); |
| 116 | } |
Marek Vasut | 7894311 | 2022-12-11 21:17:14 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void dmo_board_init_f(const iomux_v3_cfg_t wdog_pad, |
| 120 | struct dram_timing_info *dram_timing_info[8]) |
| 121 | { |
| 122 | struct udevice *dev; |
| 123 | int ret; |
| 124 | |
| 125 | icache_enable(); |
| 126 | |
| 127 | arch_cpu_init(); |
| 128 | |
| 129 | init_uart_clk(2); |
| 130 | |
| 131 | data_modul_imx_edm_sbc_early_init_f(wdog_pad); |
| 132 | |
| 133 | /* Clear the BSS. */ |
| 134 | memset(__bss_start, 0, __bss_end - __bss_start); |
| 135 | |
| 136 | ret = spl_early_init(); |
| 137 | if (ret) { |
| 138 | debug("spl_early_init() failed: %d\n", ret); |
| 139 | hang(); |
| 140 | } |
| 141 | |
| 142 | preloader_console_init(); |
| 143 | |
| 144 | ret = uclass_get_device_by_name(UCLASS_CLK, |
| 145 | "clock-controller@30380000", |
| 146 | &dev); |
| 147 | if (ret < 0) { |
| 148 | printf("Failed to find clock node. Check device tree\n"); |
| 149 | hang(); |
| 150 | } |
| 151 | |
| 152 | enable_tzc380(); |
| 153 | |
| 154 | data_modul_imx_edm_sbc_board_power_init(); |
| 155 | |
| 156 | /* DDR initialization */ |
| 157 | spl_dram_init(dram_timing_info); |
| 158 | |
| 159 | board_init_r(NULL, 0); |
| 160 | } |
| 161 | #else |
| 162 | void dmo_setup_boot_device(void) |
| 163 | { |
| 164 | int boot_device = get_boot_device(); |
| 165 | char *devnum; |
| 166 | |
| 167 | devnum = env_get("devnum"); |
| 168 | if (devnum) /* devnum is already set */ |
| 169 | return; |
| 170 | |
| 171 | if (boot_device == MMC3_BOOT) /* eMMC */ |
| 172 | env_set_ulong("devnum", 0); |
| 173 | else |
| 174 | env_set_ulong("devnum", 1); |
| 175 | } |
| 176 | |
| 177 | void dmo_setup_mac_address(void) |
| 178 | { |
| 179 | unsigned char enetaddr[6]; |
| 180 | struct udevice *dev; |
| 181 | int off, ret; |
| 182 | |
| 183 | ret = eth_env_get_enetaddr("ethaddr", enetaddr); |
| 184 | if (ret) /* ethaddr is already set */ |
| 185 | return; |
| 186 | |
| 187 | off = fdt_path_offset(gd->fdt_blob, "eeprom0"); |
| 188 | if (off < 0) { |
| 189 | printf("%s: No eeprom0 path offset\n", __func__); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev); |
| 194 | if (ret) { |
| 195 | printf("Cannot find EEPROM!\n"); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | ret = i2c_eeprom_read(dev, 0xb0, enetaddr, 0x6); |
| 200 | if (ret) { |
| 201 | printf("Error reading configuration EEPROM!\n"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (is_valid_ethaddr(enetaddr)) |
| 206 | eth_env_set_enetaddr("ethaddr", enetaddr); |
| 207 | } |
| 208 | #endif |