Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 Gateworks Corporation |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <cpu_func.h> |
| 8 | #include <hang.h> |
| 9 | #include <i2c.h> |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 10 | #include <init.h> |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 11 | #include <spl.h> |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 12 | #include <asm/mach-imx/gpio.h> |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 13 | #include <asm/arch/clock.h> |
| 14 | #include <asm/arch/imx8mm_pins.h> |
Tim Harvey | 1a50e74 | 2022-02-11 10:48:56 -0800 | [diff] [blame] | 15 | #include <asm/arch/imx8mn_pins.h> |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 16 | #include <asm/arch/imx8mp_pins.h> |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 17 | #include <asm/arch/sys_proto.h> |
| 18 | #include <asm/mach-imx/boot_mode.h> |
| 19 | #include <asm/arch/ddr.h> |
| 20 | #include <asm-generic/gpio.h> |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 21 | #include <dm/uclass.h> |
| 22 | #include <dm/device.h> |
Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 23 | #include <linux/delay.h> |
Tim Harvey | 1b7fbf6 | 2021-06-30 16:50:02 -0700 | [diff] [blame] | 24 | #include <power/bd71837.h> |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 25 | #include <power/mp5416.h> |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 26 | #include <power/pca9450.h> |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 27 | |
Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 28 | #include "eeprom.h" |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 29 | #include "lpddr4_timing.h" |
| 30 | |
| 31 | #define PCIE_RSTN IMX_GPIO_NR(4, 6) |
| 32 | |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 33 | static void spl_dram_init(int size) |
| 34 | { |
| 35 | struct dram_timing_info *dram_timing; |
| 36 | |
| 37 | switch (size) { |
Tim Harvey | 1a50e74 | 2022-02-11 10:48:56 -0800 | [diff] [blame] | 38 | #ifdef CONFIG_IMX8MM |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 39 | case 512: |
| 40 | dram_timing = &dram_timing_512mb; |
| 41 | break; |
| 42 | case 1024: |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 43 | dram_timing = &dram_timing_1gb; |
| 44 | break; |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 45 | case 2048: |
Tim Harvey | 6603b5e | 2021-07-27 15:19:41 -0700 | [diff] [blame] | 46 | dram_timing = &dram_timing_2gb; |
| 47 | break; |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 48 | case 4096: |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 49 | dram_timing = &dram_timing_4gb; |
| 50 | break; |
| 51 | default: |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 52 | printf("Unknown DDR configuration: %d MiB\n", size); |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 53 | dram_timing = &dram_timing_1gb; |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 54 | size = 1024; |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 55 | #elif CONFIG_IMX8MN |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 56 | case 1024: |
Tim Harvey | 1a50e74 | 2022-02-11 10:48:56 -0800 | [diff] [blame] | 57 | dram_timing = &dram_timing_1gb_single_die; |
| 58 | break; |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 59 | case 2048: |
Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 60 | if (!strcmp(eeprom_get_model(), "GW7902-SP466-A") || |
| 61 | !strcmp(eeprom_get_model(), "GW7902-SP466-B")) { |
Tim Harvey | 1a50e74 | 2022-02-11 10:48:56 -0800 | [diff] [blame] | 62 | dram_timing = &dram_timing_2gb_dual_die; |
| 63 | } else { |
| 64 | dram_timing = &dram_timing_2gb_single_die; |
| 65 | } |
| 66 | break; |
| 67 | default: |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 68 | printf("Unknown DDR configuration: %d MiB\n", size); |
Tim Harvey | 1a50e74 | 2022-02-11 10:48:56 -0800 | [diff] [blame] | 69 | dram_timing = &dram_timing_2gb_dual_die; |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 70 | size = 2048; |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 71 | #elif CONFIG_IMX8MP |
| 72 | case 4096: |
| 73 | dram_timing = &dram_timing_4gb_dual_die; |
| 74 | break; |
| 75 | default: |
| 76 | printf("Unknown DDR configuration: %d GiB\n", size); |
| 77 | dram_timing = &dram_timing_4gb_dual_die; |
| 78 | size = 4096; |
Tim Harvey | 1a50e74 | 2022-02-11 10:48:56 -0800 | [diff] [blame] | 79 | #endif |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Tim Harvey | 5cc5e19 | 2022-02-18 15:19:33 -0800 | [diff] [blame] | 82 | printf("DRAM : LPDDR4 "); |
| 83 | if (size > 512) |
| 84 | printf("%d GiB\n", size / 1024); |
| 85 | else |
| 86 | printf("%d MiB\n", size); |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 87 | ddr_init(dram_timing); |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 90 | /* |
| 91 | * Model specific PMIC adjustments necessary prior to DRAM init |
| 92 | * |
| 93 | * Note that we can not use pmic dm drivers here as we have a generic |
| 94 | * venice dt that does not have board-specific pmic's defined. |
| 95 | * |
Tim Harvey | 1b7fbf6 | 2021-06-30 16:50:02 -0700 | [diff] [blame] | 96 | * Instead we must use dm_i2c so we a helpers to give us |
| 97 | * clrsetbit functions we would otherwise have if we could use PMIC dm |
| 98 | * drivers. |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 99 | */ |
Tim Harvey | 1b7fbf6 | 2021-06-30 16:50:02 -0700 | [diff] [blame] | 100 | static int dm_i2c_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set) |
| 101 | { |
| 102 | int ret; |
| 103 | u8 val; |
| 104 | |
| 105 | ret = dm_i2c_read(dev, reg, &val, 1); |
| 106 | if (ret) |
| 107 | return ret; |
| 108 | val = (val & ~clr) | set; |
| 109 | |
| 110 | return dm_i2c_write(dev, reg, &val, 1); |
| 111 | } |
| 112 | |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 113 | static int power_init_board(void) |
| 114 | { |
Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 115 | const char *model = eeprom_get_model(); |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 116 | struct udevice *bus; |
| 117 | struct udevice *dev; |
| 118 | int ret; |
| 119 | |
| 120 | if ((!strncmp(model, "GW71", 4)) || |
| 121 | (!strncmp(model, "GW72", 4)) || |
| 122 | (!strncmp(model, "GW73", 4))) { |
Tim Harvey | d541927 | 2021-07-27 15:19:38 -0700 | [diff] [blame] | 123 | ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus); |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 124 | if (ret) { |
| 125 | printf("PMIC : failed I2C1 probe: %d\n", ret); |
| 126 | return ret; |
| 127 | } |
| 128 | ret = dm_i2c_probe(bus, 0x69, 0, &dev); |
| 129 | if (ret) { |
| 130 | printf("PMIC : failed probe: %d\n", ret); |
| 131 | return ret; |
| 132 | } |
| 133 | puts("PMIC : MP5416\n"); |
| 134 | |
| 135 | /* set VDD_ARM SW3 to 0.92V for 1.6GHz */ |
| 136 | dm_i2c_reg_write(dev, MP5416_VSET_SW3, |
| 137 | BIT(7) | MP5416_VSET_SW3_SVAL(920000)); |
| 138 | } |
| 139 | |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 140 | else if (!strncmp(model, "GW74", 4)) { |
| 141 | ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus); |
| 142 | if (ret) { |
| 143 | printf("PMIC : failed I2C1 probe: %d\n", ret); |
| 144 | return ret; |
| 145 | } |
| 146 | ret = dm_i2c_probe(bus, 0x25, 0, &dev); |
| 147 | if (ret) { |
| 148 | printf("PMIC : failed probe: %d\n", ret); |
| 149 | return ret; |
| 150 | } |
| 151 | puts("PMIC : PCA9450\n"); |
| 152 | |
| 153 | /* BUCKxOUT_DVS0/1 control BUCK123 output */ |
| 154 | dm_i2c_reg_write(dev, PCA9450_BUCK123_DVS, 0x29); |
| 155 | |
| 156 | /* Buck 1 DVS control through PMIC_STBY_REQ */ |
| 157 | dm_i2c_reg_write(dev, PCA9450_BUCK1CTRL, 0x59); |
| 158 | |
Tim Harvey | 4897fc2 | 2022-09-08 14:41:09 -0700 | [diff] [blame] | 159 | /* Set DVS1 to 0.85v for suspend */ |
| 160 | dm_i2c_reg_write(dev, PCA9450_BUCK1OUT_DVS1, 0x14); |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 161 | |
Tim Harvey | 4897fc2 | 2022-09-08 14:41:09 -0700 | [diff] [blame] | 162 | /* increase VDD_SOC to 0.95V before first DRAM access */ |
| 163 | dm_i2c_reg_write(dev, PCA9450_BUCK1OUT_DVS0, 0x1C); |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 164 | |
Tim Harvey | 4897fc2 | 2022-09-08 14:41:09 -0700 | [diff] [blame] | 165 | /* Kernel uses OD/OD freq for SOC */ |
| 166 | /* To avoid timing risk from SOC to ARM, increase VDD_ARM to OD voltage 0.95v */ |
| 167 | dm_i2c_reg_write(dev, PCA9450_BUCK2OUT_DVS0, 0x1C); |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 168 | |
| 169 | /* set WDOG_B_CFG to cold reset */ |
| 170 | dm_i2c_reg_write(dev, PCA9450_RESET_CTRL, 0xA1); |
| 171 | } |
| 172 | |
Tim Harvey | 6603b5e | 2021-07-27 15:19:41 -0700 | [diff] [blame] | 173 | else if ((!strncmp(model, "GW7901", 6)) || |
Tim Harvey | 83ffc47 | 2022-08-11 11:57:04 -0700 | [diff] [blame] | 174 | (!strncmp(model, "GW7902", 6)) || |
Tim Harvey | b453157 | 2022-09-14 09:02:19 -0700 | [diff] [blame] | 175 | (!strncmp(model, "GW7903", 6)) || |
| 176 | (!strncmp(model, "GW7904", 6))) { |
Tim Harvey | 83ffc47 | 2022-08-11 11:57:04 -0700 | [diff] [blame] | 177 | if (!strncmp(model, "GW7902", 6)) |
Tim Harvey | 6603b5e | 2021-07-27 15:19:41 -0700 | [diff] [blame] | 178 | ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus); |
Tim Harvey | 83ffc47 | 2022-08-11 11:57:04 -0700 | [diff] [blame] | 179 | else |
| 180 | ret = uclass_get_device_by_seq(UCLASS_I2C, 1, &bus); |
Tim Harvey | 1b7fbf6 | 2021-06-30 16:50:02 -0700 | [diff] [blame] | 181 | if (ret) { |
| 182 | printf("PMIC : failed I2C2 probe: %d\n", ret); |
| 183 | return ret; |
| 184 | } |
| 185 | ret = dm_i2c_probe(bus, 0x4b, 0, &dev); |
| 186 | if (ret) { |
| 187 | printf("PMIC : failed probe: %d\n", ret); |
| 188 | return ret; |
| 189 | } |
| 190 | puts("PMIC : BD71847\n"); |
| 191 | |
| 192 | /* unlock the PMIC regs */ |
| 193 | dm_i2c_reg_write(dev, BD718XX_REGLOCK, 0x1); |
| 194 | |
| 195 | /* set switchers to forced PWM mode */ |
| 196 | dm_i2c_clrsetbits(dev, BD718XX_BUCK1_CTRL, 0, 0x8); |
| 197 | dm_i2c_clrsetbits(dev, BD718XX_BUCK2_CTRL, 0, 0x8); |
| 198 | dm_i2c_clrsetbits(dev, BD718XX_1ST_NODVS_BUCK_CTRL, 0, 0x8); |
| 199 | dm_i2c_clrsetbits(dev, BD718XX_2ND_NODVS_BUCK_CTRL, 0, 0x8); |
| 200 | dm_i2c_clrsetbits(dev, BD718XX_3RD_NODVS_BUCK_CTRL, 0, 0x8); |
| 201 | dm_i2c_clrsetbits(dev, BD718XX_4TH_NODVS_BUCK_CTRL, 0, 0x8); |
| 202 | |
| 203 | /* increase VDD_0P95 (VDD_GPU/VPU/DRAM) to 0.975v for 1.5Ghz DDR */ |
| 204 | dm_i2c_reg_write(dev, BD718XX_1ST_NODVS_BUCK_VOLT, 0x83); |
| 205 | |
| 206 | /* increase VDD_SOC to 0.85v before first DRAM access */ |
| 207 | dm_i2c_reg_write(dev, BD718XX_BUCK1_VOLT_RUN, 0x0f); |
| 208 | |
| 209 | /* increase VDD_ARM to 0.92v for 800 and 1600Mhz */ |
| 210 | dm_i2c_reg_write(dev, BD718XX_BUCK2_VOLT_RUN, 0x16); |
| 211 | |
| 212 | /* Lock the PMIC regs */ |
| 213 | dm_i2c_reg_write(dev, BD718XX_REGLOCK, 0x11); |
| 214 | } |
| 215 | |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | void board_init_f(ulong dummy) |
| 220 | { |
| 221 | struct udevice *dev; |
| 222 | int ret; |
| 223 | int dram_sz; |
| 224 | |
| 225 | arch_cpu_init(); |
| 226 | |
| 227 | init_uart_clk(1); |
| 228 | |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 229 | timer_init(); |
| 230 | |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 231 | /* Clear the BSS. */ |
| 232 | memset(__bss_start, 0, __bss_end - __bss_start); |
| 233 | |
| 234 | ret = spl_early_init(); |
| 235 | if (ret) { |
| 236 | debug("spl_early_init() failed: %d\n", ret); |
| 237 | hang(); |
| 238 | } |
| 239 | |
Tim Harvey | 91db793 | 2022-04-29 12:36:25 -0700 | [diff] [blame] | 240 | preloader_console_init(); |
| 241 | |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 242 | enable_tzc380(); |
| 243 | |
| 244 | /* need to hold PCIe switch in reset otherwise it can lock i2c bus EEPROM is on */ |
| 245 | gpio_request(PCIE_RSTN, "perst#"); |
| 246 | gpio_direction_output(PCIE_RSTN, 0); |
| 247 | |
Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 248 | /* |
| 249 | * probe GSC device |
| 250 | * |
| 251 | * On a board with a missing/depleted backup battery for GSC, the |
| 252 | * board may be ready to probe the GSC before its firmware is |
| 253 | * running. We will wait here indefinately for the GSC EEPROM. |
| 254 | */ |
| 255 | #ifdef CONFIG_IMX8MN |
| 256 | /* |
| 257 | * IMX8MN boots quicker than IMX8MM and exposes issue |
| 258 | * where because GSC I2C state machine isn't running and its |
| 259 | * SCL/SDA are driven low the I2C driver spams 'Arbitration lost' |
| 260 | * I2C errors. |
| 261 | * |
| 262 | * TODO: Put a loop here that somehow waits for I2C CLK/DAT to be high |
| 263 | */ |
| 264 | mdelay(50); |
| 265 | #endif |
| 266 | while (1) { |
| 267 | if (!uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(gsc), &dev)) |
| 268 | break; |
| 269 | mdelay(1); |
| 270 | } |
Tim Harvey | 1fec182 | 2022-08-11 12:04:01 -0700 | [diff] [blame] | 271 | dram_sz = venice_eeprom_init(0); |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 272 | |
| 273 | /* PMIC */ |
| 274 | power_init_board(); |
| 275 | |
| 276 | /* DDR initialization */ |
| 277 | spl_dram_init(dram_sz); |
| 278 | |
| 279 | board_init_r(NULL, 0); |
| 280 | } |
| 281 | |
| 282 | /* determine prioritized order of boot devices to load U-Boot from */ |
| 283 | void board_boot_order(u32 *spl_boot_list) |
| 284 | { |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 285 | int i = 0; |
| 286 | |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 287 | /* |
| 288 | * If the SPL was loaded via serial loader, we try to get |
| 289 | * U-Boot proper via USB SDP. |
| 290 | */ |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 291 | if (spl_boot_device() == BOOT_DEVICE_BOARD) { |
| 292 | #ifdef CONFIG_IMX8MM |
| 293 | spl_boot_list[i++] = BOOT_DEVICE_BOARD; |
| 294 | #else |
| 295 | spl_boot_list[i++] = BOOT_DEVICE_BOOTROM; |
| 296 | #endif |
| 297 | } |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 298 | |
| 299 | /* we have only eMMC in default venice dt */ |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame] | 300 | spl_boot_list[i++] = BOOT_DEVICE_MMC1; |
Tim Harvey | 256dba0 | 2021-03-02 14:00:21 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* return boot device based on where the SPL was loaded from */ |
| 304 | int spl_board_boot_device(enum boot_device boot_dev_spl) |
| 305 | { |
| 306 | switch (boot_dev_spl) { |
| 307 | case USB_BOOT: |
| 308 | return BOOT_DEVICE_BOARD; |
| 309 | /* SDHC2 */ |
| 310 | case SD2_BOOT: |
| 311 | case MMC2_BOOT: |
| 312 | return BOOT_DEVICE_MMC1; |
| 313 | /* SDHC3 */ |
| 314 | case SD3_BOOT: |
| 315 | case MMC3_BOOT: |
| 316 | return BOOT_DEVICE_MMC2; |
| 317 | default: |
| 318 | return BOOT_DEVICE_NONE; |
| 319 | } |
| 320 | } |
Tim Harvey | 724d10a | 2022-03-08 10:45:39 -0800 | [diff] [blame] | 321 | |
| 322 | const char *spl_board_loader_name(u32 boot_device) |
| 323 | { |
| 324 | switch (boot_device) { |
| 325 | /* SDHC2 */ |
| 326 | case BOOT_DEVICE_MMC1: |
| 327 | return "eMMC"; |
| 328 | /* SDHC3 */ |
| 329 | case BOOT_DEVICE_MMC2: |
| 330 | return "SD card"; |
| 331 | default: |
| 332 | return NULL; |
| 333 | } |
| 334 | } |