blob: c81498e830d63a3608b4b40280852609ba998dcb [file] [log] [blame]
Tim Harvey256dba02021-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 Harvey256dba02021-03-02 14:00:21 -080010#include <init.h>
Tim Harvey256dba02021-03-02 14:00:21 -080011#include <spl.h>
Tim Harvey256dba02021-03-02 14:00:21 -080012#include <asm/mach-imx/gpio.h>
Tim Harvey256dba02021-03-02 14:00:21 -080013#include <asm/arch/clock.h>
14#include <asm/arch/imx8mm_pins.h>
Tim Harvey1a50e742022-02-11 10:48:56 -080015#include <asm/arch/imx8mn_pins.h>
Tim Harvey0f5717f2022-04-13 11:31:09 -070016#include <asm/arch/imx8mp_pins.h>
Tim Harvey256dba02021-03-02 14:00:21 -080017#include <asm/arch/sys_proto.h>
18#include <asm/mach-imx/boot_mode.h>
Tim Harvey2ccf28d2022-11-11 08:03:07 -080019#include <asm/mach-imx/mxc_i2c.h>
Tim Harvey256dba02021-03-02 14:00:21 -080020#include <asm/arch/ddr.h>
21#include <asm-generic/gpio.h>
Tim Harvey256dba02021-03-02 14:00:21 -080022#include <dm/uclass.h>
23#include <dm/device.h>
Tim Harvey2ccf28d2022-11-11 08:03:07 -080024#include <dm/pinctrl.h>
Tim Harveyd4daeaa2022-04-13 08:56:40 -070025#include <linux/delay.h>
Tim Harvey1b7fbf62021-06-30 16:50:02 -070026#include <power/bd71837.h>
Tim Harvey256dba02021-03-02 14:00:21 -080027#include <power/mp5416.h>
Tim Harvey0f5717f2022-04-13 11:31:09 -070028#include <power/pca9450.h>
Tim Harvey256dba02021-03-02 14:00:21 -080029
Tim Harveyd4daeaa2022-04-13 08:56:40 -070030#include "eeprom.h"
Tim Harvey256dba02021-03-02 14:00:21 -080031#include "lpddr4_timing.h"
32
33#define PCIE_RSTN IMX_GPIO_NR(4, 6)
34
Tim Harvey256dba02021-03-02 14:00:21 -080035static void spl_dram_init(int size)
36{
37 struct dram_timing_info *dram_timing;
38
39 switch (size) {
Tim Harvey1a50e742022-02-11 10:48:56 -080040#ifdef CONFIG_IMX8MM
Tim Harvey5cc5e192022-02-18 15:19:33 -080041 case 512:
42 dram_timing = &dram_timing_512mb;
43 break;
44 case 1024:
Tim Harvey256dba02021-03-02 14:00:21 -080045 dram_timing = &dram_timing_1gb;
46 break;
Tim Harvey5cc5e192022-02-18 15:19:33 -080047 case 2048:
Tim Harvey6603b5e2021-07-27 15:19:41 -070048 dram_timing = &dram_timing_2gb;
49 break;
Tim Harvey5cc5e192022-02-18 15:19:33 -080050 case 4096:
Tim Harvey256dba02021-03-02 14:00:21 -080051 dram_timing = &dram_timing_4gb;
52 break;
53 default:
Tim Harvey5cc5e192022-02-18 15:19:33 -080054 printf("Unknown DDR configuration: %d MiB\n", size);
Tim Harvey256dba02021-03-02 14:00:21 -080055 dram_timing = &dram_timing_1gb;
Tim Harvey5cc5e192022-02-18 15:19:33 -080056 size = 1024;
Tim Harvey0f5717f2022-04-13 11:31:09 -070057#elif CONFIG_IMX8MN
Tim Harvey5cc5e192022-02-18 15:19:33 -080058 case 1024:
Tim Harvey1a50e742022-02-11 10:48:56 -080059 dram_timing = &dram_timing_1gb_single_die;
60 break;
Tim Harvey5cc5e192022-02-18 15:19:33 -080061 case 2048:
Tim Harveyd4daeaa2022-04-13 08:56:40 -070062 if (!strcmp(eeprom_get_model(), "GW7902-SP466-A") ||
63 !strcmp(eeprom_get_model(), "GW7902-SP466-B")) {
Tim Harvey1a50e742022-02-11 10:48:56 -080064 dram_timing = &dram_timing_2gb_dual_die;
65 } else {
66 dram_timing = &dram_timing_2gb_single_die;
67 }
68 break;
69 default:
Tim Harvey5cc5e192022-02-18 15:19:33 -080070 printf("Unknown DDR configuration: %d MiB\n", size);
Tim Harvey1a50e742022-02-11 10:48:56 -080071 dram_timing = &dram_timing_2gb_dual_die;
Tim Harvey5cc5e192022-02-18 15:19:33 -080072 size = 2048;
Tim Harvey0f5717f2022-04-13 11:31:09 -070073#elif CONFIG_IMX8MP
74 case 4096:
75 dram_timing = &dram_timing_4gb_dual_die;
76 break;
77 default:
78 printf("Unknown DDR configuration: %d GiB\n", size);
79 dram_timing = &dram_timing_4gb_dual_die;
80 size = 4096;
Tim Harvey1a50e742022-02-11 10:48:56 -080081#endif
Tim Harvey256dba02021-03-02 14:00:21 -080082 }
83
Tim Harvey5cc5e192022-02-18 15:19:33 -080084 printf("DRAM : LPDDR4 ");
85 if (size > 512)
Tim Harvey03280f32023-06-09 09:54:01 -070086 printf("%d GiB", size / 1024);
Tim Harvey5cc5e192022-02-18 15:19:33 -080087 else
Tim Harvey03280f32023-06-09 09:54:01 -070088 printf("%d MiB", size);
89 printf(" %dMT/s %dMHz\n",
90 dram_timing->fsp_msg[0].drate,
91 dram_timing->fsp_msg[0].drate / 2);
Tim Harvey256dba02021-03-02 14:00:21 -080092 ddr_init(dram_timing);
Tim Harvey256dba02021-03-02 14:00:21 -080093}
94
Tim Harvey256dba02021-03-02 14:00:21 -080095/*
96 * Model specific PMIC adjustments necessary prior to DRAM init
97 *
98 * Note that we can not use pmic dm drivers here as we have a generic
99 * venice dt that does not have board-specific pmic's defined.
100 *
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700101 * Instead we must use dm_i2c so we a helpers to give us
102 * clrsetbit functions we would otherwise have if we could use PMIC dm
103 * drivers.
Tim Harvey256dba02021-03-02 14:00:21 -0800104 */
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700105static int dm_i2c_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
106{
107 int ret;
108 u8 val;
109
110 ret = dm_i2c_read(dev, reg, &val, 1);
111 if (ret)
112 return ret;
113 val = (val & ~clr) | set;
114
115 return dm_i2c_write(dev, reg, &val, 1);
116}
117
Tim Harvey256dba02021-03-02 14:00:21 -0800118static int power_init_board(void)
119{
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700120 const char *model = eeprom_get_model();
Tim Harvey256dba02021-03-02 14:00:21 -0800121 struct udevice *bus;
122 struct udevice *dev;
123 int ret;
124
125 if ((!strncmp(model, "GW71", 4)) ||
126 (!strncmp(model, "GW72", 4)) ||
127 (!strncmp(model, "GW73", 4))) {
Tim Harveyd5419272021-07-27 15:19:38 -0700128 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
Tim Harvey256dba02021-03-02 14:00:21 -0800129 if (ret) {
130 printf("PMIC : failed I2C1 probe: %d\n", ret);
131 return ret;
132 }
133 ret = dm_i2c_probe(bus, 0x69, 0, &dev);
134 if (ret) {
135 printf("PMIC : failed probe: %d\n", ret);
136 return ret;
137 }
138 puts("PMIC : MP5416\n");
139
140 /* set VDD_ARM SW3 to 0.92V for 1.6GHz */
141 dm_i2c_reg_write(dev, MP5416_VSET_SW3,
142 BIT(7) | MP5416_VSET_SW3_SVAL(920000));
143 }
144
Tim Harvey0f5717f2022-04-13 11:31:09 -0700145 else if (!strncmp(model, "GW74", 4)) {
146 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
147 if (ret) {
148 printf("PMIC : failed I2C1 probe: %d\n", ret);
149 return ret;
150 }
151 ret = dm_i2c_probe(bus, 0x25, 0, &dev);
152 if (ret) {
153 printf("PMIC : failed probe: %d\n", ret);
154 return ret;
155 }
156 puts("PMIC : PCA9450\n");
157
158 /* BUCKxOUT_DVS0/1 control BUCK123 output */
159 dm_i2c_reg_write(dev, PCA9450_BUCK123_DVS, 0x29);
160
161 /* Buck 1 DVS control through PMIC_STBY_REQ */
162 dm_i2c_reg_write(dev, PCA9450_BUCK1CTRL, 0x59);
163
Tim Harvey4897fc22022-09-08 14:41:09 -0700164 /* Set DVS1 to 0.85v for suspend */
165 dm_i2c_reg_write(dev, PCA9450_BUCK1OUT_DVS1, 0x14);
Tim Harvey0f5717f2022-04-13 11:31:09 -0700166
Tim Harvey4897fc22022-09-08 14:41:09 -0700167 /* increase VDD_SOC to 0.95V before first DRAM access */
168 dm_i2c_reg_write(dev, PCA9450_BUCK1OUT_DVS0, 0x1C);
Tim Harvey0f5717f2022-04-13 11:31:09 -0700169
Tim Harvey4897fc22022-09-08 14:41:09 -0700170 /* Kernel uses OD/OD freq for SOC */
171 /* To avoid timing risk from SOC to ARM, increase VDD_ARM to OD voltage 0.95v */
172 dm_i2c_reg_write(dev, PCA9450_BUCK2OUT_DVS0, 0x1C);
Tim Harvey0f5717f2022-04-13 11:31:09 -0700173 }
174
Tim Harvey6603b5e2021-07-27 15:19:41 -0700175 else if ((!strncmp(model, "GW7901", 6)) ||
Tim Harvey83ffc472022-08-11 11:57:04 -0700176 (!strncmp(model, "GW7902", 6)) ||
Tim Harveyb4531572022-09-14 09:02:19 -0700177 (!strncmp(model, "GW7903", 6)) ||
178 (!strncmp(model, "GW7904", 6))) {
Tim Harvey83ffc472022-08-11 11:57:04 -0700179 if (!strncmp(model, "GW7902", 6))
Tim Harvey6603b5e2021-07-27 15:19:41 -0700180 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
Tim Harvey83ffc472022-08-11 11:57:04 -0700181 else
182 ret = uclass_get_device_by_seq(UCLASS_I2C, 1, &bus);
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700183 if (ret) {
184 printf("PMIC : failed I2C2 probe: %d\n", ret);
185 return ret;
186 }
187 ret = dm_i2c_probe(bus, 0x4b, 0, &dev);
188 if (ret) {
189 printf("PMIC : failed probe: %d\n", ret);
190 return ret;
191 }
192 puts("PMIC : BD71847\n");
193
194 /* unlock the PMIC regs */
195 dm_i2c_reg_write(dev, BD718XX_REGLOCK, 0x1);
196
197 /* set switchers to forced PWM mode */
198 dm_i2c_clrsetbits(dev, BD718XX_BUCK1_CTRL, 0, 0x8);
199 dm_i2c_clrsetbits(dev, BD718XX_BUCK2_CTRL, 0, 0x8);
200 dm_i2c_clrsetbits(dev, BD718XX_1ST_NODVS_BUCK_CTRL, 0, 0x8);
201 dm_i2c_clrsetbits(dev, BD718XX_2ND_NODVS_BUCK_CTRL, 0, 0x8);
202 dm_i2c_clrsetbits(dev, BD718XX_3RD_NODVS_BUCK_CTRL, 0, 0x8);
203 dm_i2c_clrsetbits(dev, BD718XX_4TH_NODVS_BUCK_CTRL, 0, 0x8);
204
205 /* increase VDD_0P95 (VDD_GPU/VPU/DRAM) to 0.975v for 1.5Ghz DDR */
206 dm_i2c_reg_write(dev, BD718XX_1ST_NODVS_BUCK_VOLT, 0x83);
207
208 /* increase VDD_SOC to 0.85v before first DRAM access */
209 dm_i2c_reg_write(dev, BD718XX_BUCK1_VOLT_RUN, 0x0f);
210
211 /* increase VDD_ARM to 0.92v for 800 and 1600Mhz */
212 dm_i2c_reg_write(dev, BD718XX_BUCK2_VOLT_RUN, 0x16);
213
214 /* Lock the PMIC regs */
215 dm_i2c_reg_write(dev, BD718XX_REGLOCK, 0x11);
216 }
217
Tim Harvey256dba02021-03-02 14:00:21 -0800218 return 0;
219}
220
221void board_init_f(ulong dummy)
222{
Tim Harvey2ccf28d2022-11-11 08:03:07 -0800223 struct udevice *bus, *dev;
224 int i, ret;
Tim Harvey256dba02021-03-02 14:00:21 -0800225 int dram_sz;
226
227 arch_cpu_init();
228
229 init_uart_clk(1);
230
Tim Harvey256dba02021-03-02 14:00:21 -0800231 timer_init();
232
Tim Harvey256dba02021-03-02 14:00:21 -0800233 /* Clear the BSS. */
234 memset(__bss_start, 0, __bss_end - __bss_start);
235
236 ret = spl_early_init();
237 if (ret) {
238 debug("spl_early_init() failed: %d\n", ret);
239 hang();
240 }
241
Tim Harvey91db7932022-04-29 12:36:25 -0700242 preloader_console_init();
243
Tim Harvey256dba02021-03-02 14:00:21 -0800244 enable_tzc380();
245
246 /* need to hold PCIe switch in reset otherwise it can lock i2c bus EEPROM is on */
247 gpio_request(PCIE_RSTN, "perst#");
248 gpio_direction_output(PCIE_RSTN, 0);
249
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700250 /*
251 * probe GSC device
252 *
253 * On a board with a missing/depleted backup battery for GSC, the
254 * board may be ready to probe the GSC before its firmware is
Tim Harvey2ccf28d2022-11-11 08:03:07 -0800255 * running. Wait here for 50ms for the GSC firmware to let go of
256 * the SCL/SDA lines to avoid the i2c driver spamming
257 * 'Arbitration lost' I2C errors
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700258 */
Tim Harvey2ccf28d2022-11-11 08:03:07 -0800259 if (!uclass_get_device_by_seq(UCLASS_I2C, 0, &bus)) {
260 if (!pinctrl_select_state(bus, "gpio")) {
261 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
262 struct gpio_desc *scl_gpio = &i2c_bus->scl_gpio;
263 struct gpio_desc *sda_gpio = &i2c_bus->sda_gpio;
264
265 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
266 dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
267 for (i = 0; i < 5; i++) {
268 if (dm_gpio_get_value(scl_gpio) &&
269 dm_gpio_get_value(sda_gpio))
270 break;
271 mdelay(10);
272 }
273 pinctrl_select_state(bus, "default");
274 }
275 }
276 /* Wait indefiniately until the GSC probes */
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700277 while (1) {
278 if (!uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(gsc), &dev))
279 break;
280 mdelay(1);
281 }
Tim Harvey1fec1822022-08-11 12:04:01 -0700282 dram_sz = venice_eeprom_init(0);
Tim Harvey256dba02021-03-02 14:00:21 -0800283
284 /* PMIC */
285 power_init_board();
286
287 /* DDR initialization */
288 spl_dram_init(dram_sz);
289
290 board_init_r(NULL, 0);
291}
292
293/* determine prioritized order of boot devices to load U-Boot from */
294void board_boot_order(u32 *spl_boot_list)
295{
Tim Harvey0f5717f2022-04-13 11:31:09 -0700296 int i = 0;
297
Tim Harvey256dba02021-03-02 14:00:21 -0800298 /*
299 * If the SPL was loaded via serial loader, we try to get
300 * U-Boot proper via USB SDP.
301 */
Tim Harvey0f5717f2022-04-13 11:31:09 -0700302 if (spl_boot_device() == BOOT_DEVICE_BOARD) {
303#ifdef CONFIG_IMX8MM
304 spl_boot_list[i++] = BOOT_DEVICE_BOARD;
305#else
306 spl_boot_list[i++] = BOOT_DEVICE_BOOTROM;
307#endif
308 }
Tim Harvey256dba02021-03-02 14:00:21 -0800309
310 /* we have only eMMC in default venice dt */
Tim Harvey0f5717f2022-04-13 11:31:09 -0700311 spl_boot_list[i++] = BOOT_DEVICE_MMC1;
Tim Harvey256dba02021-03-02 14:00:21 -0800312}
313
314/* return boot device based on where the SPL was loaded from */
315int spl_board_boot_device(enum boot_device boot_dev_spl)
316{
317 switch (boot_dev_spl) {
318 case USB_BOOT:
319 return BOOT_DEVICE_BOARD;
320 /* SDHC2 */
321 case SD2_BOOT:
322 case MMC2_BOOT:
323 return BOOT_DEVICE_MMC1;
324 /* SDHC3 */
325 case SD3_BOOT:
326 case MMC3_BOOT:
327 return BOOT_DEVICE_MMC2;
328 default:
329 return BOOT_DEVICE_NONE;
330 }
331}
Tim Harvey724d10a2022-03-08 10:45:39 -0800332
Tim Harveya42793c2023-05-02 17:05:53 -0700333unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc, unsigned long raw_sect)
334{
335 if (!IS_SD(mmc)) {
336 switch (EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config)) {
337 case 1:
338 case 2:
339 if (IS_ENABLED(CONFIG_IMX8MN) || IS_ENABLED(CONFIG_IMX8MP))
340 raw_sect -= 32 * 2;
341 break;
342 }
343 }
344
345 return raw_sect;
346}
347
Tim Harvey724d10a2022-03-08 10:45:39 -0800348const char *spl_board_loader_name(u32 boot_device)
349{
350 switch (boot_device) {
351 /* SDHC2 */
352 case BOOT_DEVICE_MMC1:
353 return "eMMC";
354 /* SDHC3 */
355 case BOOT_DEVICE_MMC2:
356 return "SD card";
357 default:
358 return NULL;
359 }
360}