blob: 404c4fa4963883c1ffd75ab57b76ca5da5f64dd8 [file] [log] [blame]
Tim Harvey03bf8432021-03-02 14:00:21 -08001// 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 Harvey03bf8432021-03-02 14:00:21 -080010#include <init.h>
Tim Harvey03bf8432021-03-02 14:00:21 -080011#include <spl.h>
Tim Harvey03bf8432021-03-02 14:00:21 -080012#include <asm/mach-imx/gpio.h>
Tim Harvey03bf8432021-03-02 14:00:21 -080013#include <asm/arch/clock.h>
14#include <asm/arch/imx8mm_pins.h>
Tim Harvey2cb156e2022-02-11 10:48:56 -080015#include <asm/arch/imx8mn_pins.h>
Tim Harvey23956252022-04-13 11:31:09 -070016#include <asm/arch/imx8mp_pins.h>
Tim Harvey03bf8432021-03-02 14:00:21 -080017#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 Harvey03bf8432021-03-02 14:00:21 -080021#include <dm/uclass.h>
22#include <dm/device.h>
Tim Harveyfb9ec332022-04-13 08:56:40 -070023#include <linux/delay.h>
Tim Harveyc9f7ef32021-06-30 16:50:02 -070024#include <power/bd71837.h>
Tim Harvey03bf8432021-03-02 14:00:21 -080025#include <power/mp5416.h>
Tim Harvey23956252022-04-13 11:31:09 -070026#include <power/pca9450.h>
Tim Harvey03bf8432021-03-02 14:00:21 -080027
Tim Harveyfb9ec332022-04-13 08:56:40 -070028#include "eeprom.h"
Tim Harvey03bf8432021-03-02 14:00:21 -080029#include "lpddr4_timing.h"
30
31#define PCIE_RSTN IMX_GPIO_NR(4, 6)
32
Tim Harvey03bf8432021-03-02 14:00:21 -080033static void spl_dram_init(int size)
34{
35 struct dram_timing_info *dram_timing;
36
37 switch (size) {
Tim Harvey2cb156e2022-02-11 10:48:56 -080038#ifdef CONFIG_IMX8MM
Tim Harveya1c71102022-02-18 15:19:33 -080039 case 512:
40 dram_timing = &dram_timing_512mb;
41 break;
42 case 1024:
Tim Harvey03bf8432021-03-02 14:00:21 -080043 dram_timing = &dram_timing_1gb;
44 break;
Tim Harveya1c71102022-02-18 15:19:33 -080045 case 2048:
Tim Harveya8a72c32021-07-27 15:19:41 -070046 dram_timing = &dram_timing_2gb;
47 break;
Tim Harveya1c71102022-02-18 15:19:33 -080048 case 4096:
Tim Harvey03bf8432021-03-02 14:00:21 -080049 dram_timing = &dram_timing_4gb;
50 break;
51 default:
Tim Harveya1c71102022-02-18 15:19:33 -080052 printf("Unknown DDR configuration: %d MiB\n", size);
Tim Harvey03bf8432021-03-02 14:00:21 -080053 dram_timing = &dram_timing_1gb;
Tim Harveya1c71102022-02-18 15:19:33 -080054 size = 1024;
Tim Harvey23956252022-04-13 11:31:09 -070055#elif CONFIG_IMX8MN
Tim Harveya1c71102022-02-18 15:19:33 -080056 case 1024:
Tim Harvey2cb156e2022-02-11 10:48:56 -080057 dram_timing = &dram_timing_1gb_single_die;
58 break;
Tim Harveya1c71102022-02-18 15:19:33 -080059 case 2048:
Tim Harveyfb9ec332022-04-13 08:56:40 -070060 if (!strcmp(eeprom_get_model(), "GW7902-SP466-A") ||
61 !strcmp(eeprom_get_model(), "GW7902-SP466-B")) {
Tim Harvey2cb156e2022-02-11 10:48:56 -080062 dram_timing = &dram_timing_2gb_dual_die;
63 } else {
64 dram_timing = &dram_timing_2gb_single_die;
65 }
66 break;
67 default:
Tim Harveya1c71102022-02-18 15:19:33 -080068 printf("Unknown DDR configuration: %d MiB\n", size);
Tim Harvey2cb156e2022-02-11 10:48:56 -080069 dram_timing = &dram_timing_2gb_dual_die;
Tim Harveya1c71102022-02-18 15:19:33 -080070 size = 2048;
Tim Harvey23956252022-04-13 11:31:09 -070071#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 Harvey2cb156e2022-02-11 10:48:56 -080079#endif
Tim Harvey03bf8432021-03-02 14:00:21 -080080 }
81
Tim Harveya1c71102022-02-18 15:19:33 -080082 printf("DRAM : LPDDR4 ");
83 if (size > 512)
84 printf("%d GiB\n", size / 1024);
85 else
86 printf("%d MiB\n", size);
Tim Harvey03bf8432021-03-02 14:00:21 -080087 ddr_init(dram_timing);
Tim Harvey03bf8432021-03-02 14:00:21 -080088}
89
Tim Harvey03bf8432021-03-02 14:00:21 -080090/*
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 Harveyc9f7ef32021-06-30 16:50:02 -070096 * 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 Harvey03bf8432021-03-02 14:00:21 -080099 */
Tim Harveyc9f7ef32021-06-30 16:50:02 -0700100static 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 Harvey03bf8432021-03-02 14:00:21 -0800113static int power_init_board(void)
114{
Tim Harveyfb9ec332022-04-13 08:56:40 -0700115 const char *model = eeprom_get_model();
Tim Harvey03bf8432021-03-02 14:00:21 -0800116 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 Harvey67c6d032021-07-27 15:19:38 -0700123 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
Tim Harvey03bf8432021-03-02 14:00:21 -0800124 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 Harvey23956252022-04-13 11:31:09 -0700140 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
159 /* Set DVS1 to 0.8v for suspend */
160 dm_i2c_reg_write(dev, PCA9450_BUCK1OUT_DVS1, 0x10);
161
162 /* increase VDD_DRAM to 0.95v for 3Ghz DDR */
163 dm_i2c_reg_write(dev, PCA9450_BUCK3OUT_DVS0, 0x1C);
164
165 /* VDD_DRAM off in suspend: B1_ENMODE=10 */
166 dm_i2c_reg_write(dev, PCA9450_BUCK3CTRL, 0x4a);
167
168 /* set VDD_SNVS_0V8 from default 0.85V */
169 dm_i2c_reg_write(dev, PCA9450_LDO2CTRL, 0xC0);
170
171 /* set WDOG_B_CFG to cold reset */
172 dm_i2c_reg_write(dev, PCA9450_RESET_CTRL, 0xA1);
173 }
174
Tim Harveya8a72c32021-07-27 15:19:41 -0700175 else if ((!strncmp(model, "GW7901", 6)) ||
176 (!strncmp(model, "GW7902", 6))) {
177 if (!strncmp(model, "GW7901", 6))
178 ret = uclass_get_device_by_seq(UCLASS_I2C, 1, &bus);
179 else
180 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
Tim Harveyc9f7ef32021-06-30 16:50:02 -0700181 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 Harvey03bf8432021-03-02 14:00:21 -0800216 return 0;
217}
218
219void 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 Harvey03bf8432021-03-02 14:00:21 -0800229 timer_init();
230
Tim Harvey03bf8432021-03-02 14:00:21 -0800231 /* 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 Harvey6fc63942022-04-29 12:36:25 -0700240 preloader_console_init();
241
Tim Harvey03bf8432021-03-02 14:00:21 -0800242 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 Harveyfb9ec332022-04-13 08:56:40 -0700248 /*
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 Harvey37d5bf42022-08-11 12:04:01 -0700271 dram_sz = venice_eeprom_init(0);
Tim Harvey03bf8432021-03-02 14:00:21 -0800272
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 */
283void board_boot_order(u32 *spl_boot_list)
284{
Tim Harvey23956252022-04-13 11:31:09 -0700285 int i = 0;
286
Tim Harvey03bf8432021-03-02 14:00:21 -0800287 /*
288 * If the SPL was loaded via serial loader, we try to get
289 * U-Boot proper via USB SDP.
290 */
Tim Harvey23956252022-04-13 11:31:09 -0700291 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 Harvey03bf8432021-03-02 14:00:21 -0800298
299 /* we have only eMMC in default venice dt */
Tim Harvey23956252022-04-13 11:31:09 -0700300 spl_boot_list[i++] = BOOT_DEVICE_MMC1;
Tim Harvey03bf8432021-03-02 14:00:21 -0800301}
302
303/* return boot device based on where the SPL was loaded from */
304int 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 Harvey25565812022-03-08 10:45:39 -0800321
322const 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}