blob: 50056da3ee0fd5076b72e6ae37510240bfdcef7a [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)
86 printf("%d GiB\n", size / 1024);
87 else
88 printf("%d MiB\n", size);
Tim Harvey256dba02021-03-02 14:00:21 -080089 ddr_init(dram_timing);
Tim Harvey256dba02021-03-02 14:00:21 -080090}
91
Tim Harvey256dba02021-03-02 14:00:21 -080092/*
93 * Model specific PMIC adjustments necessary prior to DRAM init
94 *
95 * Note that we can not use pmic dm drivers here as we have a generic
96 * venice dt that does not have board-specific pmic's defined.
97 *
Tim Harvey1b7fbf62021-06-30 16:50:02 -070098 * Instead we must use dm_i2c so we a helpers to give us
99 * clrsetbit functions we would otherwise have if we could use PMIC dm
100 * drivers.
Tim Harvey256dba02021-03-02 14:00:21 -0800101 */
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700102static int dm_i2c_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
103{
104 int ret;
105 u8 val;
106
107 ret = dm_i2c_read(dev, reg, &val, 1);
108 if (ret)
109 return ret;
110 val = (val & ~clr) | set;
111
112 return dm_i2c_write(dev, reg, &val, 1);
113}
114
Tim Harvey256dba02021-03-02 14:00:21 -0800115static int power_init_board(void)
116{
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700117 const char *model = eeprom_get_model();
Tim Harvey256dba02021-03-02 14:00:21 -0800118 struct udevice *bus;
119 struct udevice *dev;
120 int ret;
121
122 if ((!strncmp(model, "GW71", 4)) ||
123 (!strncmp(model, "GW72", 4)) ||
124 (!strncmp(model, "GW73", 4))) {
Tim Harveyd5419272021-07-27 15:19:38 -0700125 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
Tim Harvey256dba02021-03-02 14:00:21 -0800126 if (ret) {
127 printf("PMIC : failed I2C1 probe: %d\n", ret);
128 return ret;
129 }
130 ret = dm_i2c_probe(bus, 0x69, 0, &dev);
131 if (ret) {
132 printf("PMIC : failed probe: %d\n", ret);
133 return ret;
134 }
135 puts("PMIC : MP5416\n");
136
137 /* set VDD_ARM SW3 to 0.92V for 1.6GHz */
138 dm_i2c_reg_write(dev, MP5416_VSET_SW3,
139 BIT(7) | MP5416_VSET_SW3_SVAL(920000));
140 }
141
Tim Harvey0f5717f2022-04-13 11:31:09 -0700142 else if (!strncmp(model, "GW74", 4)) {
143 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
144 if (ret) {
145 printf("PMIC : failed I2C1 probe: %d\n", ret);
146 return ret;
147 }
148 ret = dm_i2c_probe(bus, 0x25, 0, &dev);
149 if (ret) {
150 printf("PMIC : failed probe: %d\n", ret);
151 return ret;
152 }
153 puts("PMIC : PCA9450\n");
154
155 /* BUCKxOUT_DVS0/1 control BUCK123 output */
156 dm_i2c_reg_write(dev, PCA9450_BUCK123_DVS, 0x29);
157
158 /* Buck 1 DVS control through PMIC_STBY_REQ */
159 dm_i2c_reg_write(dev, PCA9450_BUCK1CTRL, 0x59);
160
Tim Harvey4897fc22022-09-08 14:41:09 -0700161 /* Set DVS1 to 0.85v for suspend */
162 dm_i2c_reg_write(dev, PCA9450_BUCK1OUT_DVS1, 0x14);
Tim Harvey0f5717f2022-04-13 11:31:09 -0700163
Tim Harvey4897fc22022-09-08 14:41:09 -0700164 /* increase VDD_SOC to 0.95V before first DRAM access */
165 dm_i2c_reg_write(dev, PCA9450_BUCK1OUT_DVS0, 0x1C);
Tim Harvey0f5717f2022-04-13 11:31:09 -0700166
Tim Harvey4897fc22022-09-08 14:41:09 -0700167 /* Kernel uses OD/OD freq for SOC */
168 /* To avoid timing risk from SOC to ARM, increase VDD_ARM to OD voltage 0.95v */
169 dm_i2c_reg_write(dev, PCA9450_BUCK2OUT_DVS0, 0x1C);
Tim Harvey0f5717f2022-04-13 11:31:09 -0700170 }
171
Tim Harvey6603b5e2021-07-27 15:19:41 -0700172 else if ((!strncmp(model, "GW7901", 6)) ||
Tim Harvey83ffc472022-08-11 11:57:04 -0700173 (!strncmp(model, "GW7902", 6)) ||
Tim Harveyb4531572022-09-14 09:02:19 -0700174 (!strncmp(model, "GW7903", 6)) ||
175 (!strncmp(model, "GW7904", 6))) {
Tim Harvey83ffc472022-08-11 11:57:04 -0700176 if (!strncmp(model, "GW7902", 6))
Tim Harvey6603b5e2021-07-27 15:19:41 -0700177 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
Tim Harvey83ffc472022-08-11 11:57:04 -0700178 else
179 ret = uclass_get_device_by_seq(UCLASS_I2C, 1, &bus);
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700180 if (ret) {
181 printf("PMIC : failed I2C2 probe: %d\n", ret);
182 return ret;
183 }
184 ret = dm_i2c_probe(bus, 0x4b, 0, &dev);
185 if (ret) {
186 printf("PMIC : failed probe: %d\n", ret);
187 return ret;
188 }
189 puts("PMIC : BD71847\n");
190
191 /* unlock the PMIC regs */
192 dm_i2c_reg_write(dev, BD718XX_REGLOCK, 0x1);
193
194 /* set switchers to forced PWM mode */
195 dm_i2c_clrsetbits(dev, BD718XX_BUCK1_CTRL, 0, 0x8);
196 dm_i2c_clrsetbits(dev, BD718XX_BUCK2_CTRL, 0, 0x8);
197 dm_i2c_clrsetbits(dev, BD718XX_1ST_NODVS_BUCK_CTRL, 0, 0x8);
198 dm_i2c_clrsetbits(dev, BD718XX_2ND_NODVS_BUCK_CTRL, 0, 0x8);
199 dm_i2c_clrsetbits(dev, BD718XX_3RD_NODVS_BUCK_CTRL, 0, 0x8);
200 dm_i2c_clrsetbits(dev, BD718XX_4TH_NODVS_BUCK_CTRL, 0, 0x8);
201
202 /* increase VDD_0P95 (VDD_GPU/VPU/DRAM) to 0.975v for 1.5Ghz DDR */
203 dm_i2c_reg_write(dev, BD718XX_1ST_NODVS_BUCK_VOLT, 0x83);
204
205 /* increase VDD_SOC to 0.85v before first DRAM access */
206 dm_i2c_reg_write(dev, BD718XX_BUCK1_VOLT_RUN, 0x0f);
207
208 /* increase VDD_ARM to 0.92v for 800 and 1600Mhz */
209 dm_i2c_reg_write(dev, BD718XX_BUCK2_VOLT_RUN, 0x16);
210
211 /* Lock the PMIC regs */
212 dm_i2c_reg_write(dev, BD718XX_REGLOCK, 0x11);
213 }
214
Tim Harvey256dba02021-03-02 14:00:21 -0800215 return 0;
216}
217
218void board_init_f(ulong dummy)
219{
Tim Harvey2ccf28d2022-11-11 08:03:07 -0800220 struct udevice *bus, *dev;
221 int i, ret;
Tim Harvey256dba02021-03-02 14:00:21 -0800222 int dram_sz;
223
224 arch_cpu_init();
225
226 init_uart_clk(1);
227
Tim Harvey256dba02021-03-02 14:00:21 -0800228 timer_init();
229
Tim Harvey256dba02021-03-02 14:00:21 -0800230 /* Clear the BSS. */
231 memset(__bss_start, 0, __bss_end - __bss_start);
232
233 ret = spl_early_init();
234 if (ret) {
235 debug("spl_early_init() failed: %d\n", ret);
236 hang();
237 }
238
Tim Harvey91db7932022-04-29 12:36:25 -0700239 preloader_console_init();
240
Tim Harvey256dba02021-03-02 14:00:21 -0800241 enable_tzc380();
242
243 /* need to hold PCIe switch in reset otherwise it can lock i2c bus EEPROM is on */
244 gpio_request(PCIE_RSTN, "perst#");
245 gpio_direction_output(PCIE_RSTN, 0);
246
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700247 /*
248 * probe GSC device
249 *
250 * On a board with a missing/depleted backup battery for GSC, the
251 * board may be ready to probe the GSC before its firmware is
Tim Harvey2ccf28d2022-11-11 08:03:07 -0800252 * running. Wait here for 50ms for the GSC firmware to let go of
253 * the SCL/SDA lines to avoid the i2c driver spamming
254 * 'Arbitration lost' I2C errors
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700255 */
Tim Harvey2ccf28d2022-11-11 08:03:07 -0800256 if (!uclass_get_device_by_seq(UCLASS_I2C, 0, &bus)) {
257 if (!pinctrl_select_state(bus, "gpio")) {
258 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
259 struct gpio_desc *scl_gpio = &i2c_bus->scl_gpio;
260 struct gpio_desc *sda_gpio = &i2c_bus->sda_gpio;
261
262 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
263 dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
264 for (i = 0; i < 5; i++) {
265 if (dm_gpio_get_value(scl_gpio) &&
266 dm_gpio_get_value(sda_gpio))
267 break;
268 mdelay(10);
269 }
270 pinctrl_select_state(bus, "default");
271 }
272 }
273 /* Wait indefiniately until the GSC probes */
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700274 while (1) {
275 if (!uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(gsc), &dev))
276 break;
277 mdelay(1);
278 }
Tim Harvey1fec1822022-08-11 12:04:01 -0700279 dram_sz = venice_eeprom_init(0);
Tim Harvey256dba02021-03-02 14:00:21 -0800280
281 /* PMIC */
282 power_init_board();
283
284 /* DDR initialization */
285 spl_dram_init(dram_sz);
286
287 board_init_r(NULL, 0);
288}
289
290/* determine prioritized order of boot devices to load U-Boot from */
291void board_boot_order(u32 *spl_boot_list)
292{
Tim Harvey0f5717f2022-04-13 11:31:09 -0700293 int i = 0;
294
Tim Harvey256dba02021-03-02 14:00:21 -0800295 /*
296 * If the SPL was loaded via serial loader, we try to get
297 * U-Boot proper via USB SDP.
298 */
Tim Harvey0f5717f2022-04-13 11:31:09 -0700299 if (spl_boot_device() == BOOT_DEVICE_BOARD) {
300#ifdef CONFIG_IMX8MM
301 spl_boot_list[i++] = BOOT_DEVICE_BOARD;
302#else
303 spl_boot_list[i++] = BOOT_DEVICE_BOOTROM;
304#endif
305 }
Tim Harvey256dba02021-03-02 14:00:21 -0800306
307 /* we have only eMMC in default venice dt */
Tim Harvey0f5717f2022-04-13 11:31:09 -0700308 spl_boot_list[i++] = BOOT_DEVICE_MMC1;
Tim Harvey256dba02021-03-02 14:00:21 -0800309}
310
311/* return boot device based on where the SPL was loaded from */
312int spl_board_boot_device(enum boot_device boot_dev_spl)
313{
314 switch (boot_dev_spl) {
315 case USB_BOOT:
316 return BOOT_DEVICE_BOARD;
317 /* SDHC2 */
318 case SD2_BOOT:
319 case MMC2_BOOT:
320 return BOOT_DEVICE_MMC1;
321 /* SDHC3 */
322 case SD3_BOOT:
323 case MMC3_BOOT:
324 return BOOT_DEVICE_MMC2;
325 default:
326 return BOOT_DEVICE_NONE;
327 }
328}
Tim Harvey724d10a2022-03-08 10:45:39 -0800329
Tim Harveya42793c2023-05-02 17:05:53 -0700330unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc, unsigned long raw_sect)
331{
332 if (!IS_SD(mmc)) {
333 switch (EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config)) {
334 case 1:
335 case 2:
336 if (IS_ENABLED(CONFIG_IMX8MN) || IS_ENABLED(CONFIG_IMX8MP))
337 raw_sect -= 32 * 2;
338 break;
339 }
340 }
341
342 return raw_sect;
343}
344
Tim Harvey724d10a2022-03-08 10:45:39 -0800345const char *spl_board_loader_name(u32 boot_device)
346{
347 switch (boot_device) {
348 /* SDHC2 */
349 case BOOT_DEVICE_MMC1:
350 return "eMMC";
351 /* SDHC3 */
352 case BOOT_DEVICE_MMC2:
353 return "SD card";
354 default:
355 return NULL;
356 }
357}