blob: 223f22d3463c3fc1c1cbae1253827d1c5022fdf3 [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>
19#include <asm/arch/ddr.h>
20#include <asm-generic/gpio.h>
Tim Harvey256dba02021-03-02 14:00:21 -080021#include <dm/uclass.h>
22#include <dm/device.h>
Tim Harveyd4daeaa2022-04-13 08:56:40 -070023#include <linux/delay.h>
Tim Harvey1b7fbf62021-06-30 16:50:02 -070024#include <power/bd71837.h>
Tim Harvey256dba02021-03-02 14:00:21 -080025#include <power/mp5416.h>
Tim Harvey0f5717f2022-04-13 11:31:09 -070026#include <power/pca9450.h>
Tim Harvey256dba02021-03-02 14:00:21 -080027
Tim Harveyd4daeaa2022-04-13 08:56:40 -070028#include "eeprom.h"
Tim Harvey256dba02021-03-02 14:00:21 -080029#include "lpddr4_timing.h"
30
31#define PCIE_RSTN IMX_GPIO_NR(4, 6)
32
Tim Harvey256dba02021-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 Harvey1a50e742022-02-11 10:48:56 -080038#ifdef CONFIG_IMX8MM
Tim Harvey5cc5e192022-02-18 15:19:33 -080039 case 512:
40 dram_timing = &dram_timing_512mb;
41 break;
42 case 1024:
Tim Harvey256dba02021-03-02 14:00:21 -080043 dram_timing = &dram_timing_1gb;
44 break;
Tim Harvey5cc5e192022-02-18 15:19:33 -080045 case 2048:
Tim Harvey6603b5e2021-07-27 15:19:41 -070046 dram_timing = &dram_timing_2gb;
47 break;
Tim Harvey5cc5e192022-02-18 15:19:33 -080048 case 4096:
Tim Harvey256dba02021-03-02 14:00:21 -080049 dram_timing = &dram_timing_4gb;
50 break;
51 default:
Tim Harvey5cc5e192022-02-18 15:19:33 -080052 printf("Unknown DDR configuration: %d MiB\n", size);
Tim Harvey256dba02021-03-02 14:00:21 -080053 dram_timing = &dram_timing_1gb;
Tim Harvey5cc5e192022-02-18 15:19:33 -080054 size = 1024;
Tim Harvey0f5717f2022-04-13 11:31:09 -070055#elif CONFIG_IMX8MN
Tim Harvey5cc5e192022-02-18 15:19:33 -080056 case 1024:
Tim Harvey1a50e742022-02-11 10:48:56 -080057 dram_timing = &dram_timing_1gb_single_die;
58 break;
Tim Harvey5cc5e192022-02-18 15:19:33 -080059 case 2048:
Tim Harveyd4daeaa2022-04-13 08:56:40 -070060 if (!strcmp(eeprom_get_model(), "GW7902-SP466-A") ||
61 !strcmp(eeprom_get_model(), "GW7902-SP466-B")) {
Tim Harvey1a50e742022-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 Harvey5cc5e192022-02-18 15:19:33 -080068 printf("Unknown DDR configuration: %d MiB\n", size);
Tim Harvey1a50e742022-02-11 10:48:56 -080069 dram_timing = &dram_timing_2gb_dual_die;
Tim Harvey5cc5e192022-02-18 15:19:33 -080070 size = 2048;
Tim Harvey0f5717f2022-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 Harvey1a50e742022-02-11 10:48:56 -080079#endif
Tim Harvey256dba02021-03-02 14:00:21 -080080 }
81
Tim Harvey5cc5e192022-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 Harvey256dba02021-03-02 14:00:21 -080087 ddr_init(dram_timing);
Tim Harvey256dba02021-03-02 14:00:21 -080088}
89
90#define UART_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_FSEL1)
91#define WDOG_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_ODE | PAD_CTL_PUE | PAD_CTL_PE)
92
Tim Harvey1a50e742022-02-11 10:48:56 -080093#ifdef CONFIG_IMX8MM
Tim Harvey256dba02021-03-02 14:00:21 -080094static iomux_v3_cfg_t const uart_pads[] = {
95 IMX8MM_PAD_UART2_RXD_UART2_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
96 IMX8MM_PAD_UART2_TXD_UART2_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
97};
98
99static iomux_v3_cfg_t const wdog_pads[] = {
100 IMX8MM_PAD_GPIO1_IO02_WDOG1_WDOG_B | MUX_PAD_CTRL(WDOG_PAD_CTRL),
101};
Tim Harvey0f5717f2022-04-13 11:31:09 -0700102#elif CONFIG_IMX8MN
Tim Harvey1a50e742022-02-11 10:48:56 -0800103static const iomux_v3_cfg_t uart_pads[] = {
104 IMX8MN_PAD_UART2_RXD__UART2_DCE_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
105 IMX8MN_PAD_UART2_TXD__UART2_DCE_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
106};
107
108static const iomux_v3_cfg_t wdog_pads[] = {
109 IMX8MN_PAD_GPIO1_IO02__WDOG1_WDOG_B | MUX_PAD_CTRL(WDOG_PAD_CTRL),
110};
Tim Harvey0f5717f2022-04-13 11:31:09 -0700111#elif CONFIG_IMX8MP
112static const iomux_v3_cfg_t uart_pads[] = {
113 MX8MP_PAD_UART2_RXD__UART2_DCE_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
114 MX8MP_PAD_UART2_TXD__UART2_DCE_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
115};
116
117static const iomux_v3_cfg_t wdog_pads[] = {
118 MX8MP_PAD_GPIO1_IO02__WDOG1_WDOG_B | MUX_PAD_CTRL(WDOG_PAD_CTRL),
119};
120
Tim Harvey1a50e742022-02-11 10:48:56 -0800121#endif
Tim Harvey256dba02021-03-02 14:00:21 -0800122
123int board_early_init_f(void)
124{
125 struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR;
126
127 imx_iomux_v3_setup_multiple_pads(wdog_pads, ARRAY_SIZE(wdog_pads));
128
129 set_wdog_reset(wdog);
130
131 imx_iomux_v3_setup_multiple_pads(uart_pads, ARRAY_SIZE(uart_pads));
132
133 return 0;
134}
135
136/*
137 * Model specific PMIC adjustments necessary prior to DRAM init
138 *
139 * Note that we can not use pmic dm drivers here as we have a generic
140 * venice dt that does not have board-specific pmic's defined.
141 *
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700142 * Instead we must use dm_i2c so we a helpers to give us
143 * clrsetbit functions we would otherwise have if we could use PMIC dm
144 * drivers.
Tim Harvey256dba02021-03-02 14:00:21 -0800145 */
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700146static int dm_i2c_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
147{
148 int ret;
149 u8 val;
150
151 ret = dm_i2c_read(dev, reg, &val, 1);
152 if (ret)
153 return ret;
154 val = (val & ~clr) | set;
155
156 return dm_i2c_write(dev, reg, &val, 1);
157}
158
Tim Harvey256dba02021-03-02 14:00:21 -0800159static int power_init_board(void)
160{
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700161 const char *model = eeprom_get_model();
Tim Harvey256dba02021-03-02 14:00:21 -0800162 struct udevice *bus;
163 struct udevice *dev;
164 int ret;
165
166 if ((!strncmp(model, "GW71", 4)) ||
167 (!strncmp(model, "GW72", 4)) ||
168 (!strncmp(model, "GW73", 4))) {
Tim Harveyd5419272021-07-27 15:19:38 -0700169 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
Tim Harvey256dba02021-03-02 14:00:21 -0800170 if (ret) {
171 printf("PMIC : failed I2C1 probe: %d\n", ret);
172 return ret;
173 }
174 ret = dm_i2c_probe(bus, 0x69, 0, &dev);
175 if (ret) {
176 printf("PMIC : failed probe: %d\n", ret);
177 return ret;
178 }
179 puts("PMIC : MP5416\n");
180
181 /* set VDD_ARM SW3 to 0.92V for 1.6GHz */
182 dm_i2c_reg_write(dev, MP5416_VSET_SW3,
183 BIT(7) | MP5416_VSET_SW3_SVAL(920000));
184 }
185
Tim Harvey0f5717f2022-04-13 11:31:09 -0700186 else if (!strncmp(model, "GW74", 4)) {
187 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
188 if (ret) {
189 printf("PMIC : failed I2C1 probe: %d\n", ret);
190 return ret;
191 }
192 ret = dm_i2c_probe(bus, 0x25, 0, &dev);
193 if (ret) {
194 printf("PMIC : failed probe: %d\n", ret);
195 return ret;
196 }
197 puts("PMIC : PCA9450\n");
198
199 /* BUCKxOUT_DVS0/1 control BUCK123 output */
200 dm_i2c_reg_write(dev, PCA9450_BUCK123_DVS, 0x29);
201
202 /* Buck 1 DVS control through PMIC_STBY_REQ */
203 dm_i2c_reg_write(dev, PCA9450_BUCK1CTRL, 0x59);
204
205 /* Set DVS1 to 0.8v for suspend */
206 dm_i2c_reg_write(dev, PCA9450_BUCK1OUT_DVS1, 0x10);
207
208 /* increase VDD_DRAM to 0.95v for 3Ghz DDR */
209 dm_i2c_reg_write(dev, PCA9450_BUCK3OUT_DVS0, 0x1C);
210
211 /* VDD_DRAM off in suspend: B1_ENMODE=10 */
212 dm_i2c_reg_write(dev, PCA9450_BUCK3CTRL, 0x4a);
213
214 /* set VDD_SNVS_0V8 from default 0.85V */
215 dm_i2c_reg_write(dev, PCA9450_LDO2CTRL, 0xC0);
216
217 /* set WDOG_B_CFG to cold reset */
218 dm_i2c_reg_write(dev, PCA9450_RESET_CTRL, 0xA1);
219 }
220
Tim Harvey6603b5e2021-07-27 15:19:41 -0700221 else if ((!strncmp(model, "GW7901", 6)) ||
222 (!strncmp(model, "GW7902", 6))) {
223 if (!strncmp(model, "GW7901", 6))
224 ret = uclass_get_device_by_seq(UCLASS_I2C, 1, &bus);
225 else
226 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700227 if (ret) {
228 printf("PMIC : failed I2C2 probe: %d\n", ret);
229 return ret;
230 }
231 ret = dm_i2c_probe(bus, 0x4b, 0, &dev);
232 if (ret) {
233 printf("PMIC : failed probe: %d\n", ret);
234 return ret;
235 }
236 puts("PMIC : BD71847\n");
237
238 /* unlock the PMIC regs */
239 dm_i2c_reg_write(dev, BD718XX_REGLOCK, 0x1);
240
241 /* set switchers to forced PWM mode */
242 dm_i2c_clrsetbits(dev, BD718XX_BUCK1_CTRL, 0, 0x8);
243 dm_i2c_clrsetbits(dev, BD718XX_BUCK2_CTRL, 0, 0x8);
244 dm_i2c_clrsetbits(dev, BD718XX_1ST_NODVS_BUCK_CTRL, 0, 0x8);
245 dm_i2c_clrsetbits(dev, BD718XX_2ND_NODVS_BUCK_CTRL, 0, 0x8);
246 dm_i2c_clrsetbits(dev, BD718XX_3RD_NODVS_BUCK_CTRL, 0, 0x8);
247 dm_i2c_clrsetbits(dev, BD718XX_4TH_NODVS_BUCK_CTRL, 0, 0x8);
248
249 /* increase VDD_0P95 (VDD_GPU/VPU/DRAM) to 0.975v for 1.5Ghz DDR */
250 dm_i2c_reg_write(dev, BD718XX_1ST_NODVS_BUCK_VOLT, 0x83);
251
252 /* increase VDD_SOC to 0.85v before first DRAM access */
253 dm_i2c_reg_write(dev, BD718XX_BUCK1_VOLT_RUN, 0x0f);
254
255 /* increase VDD_ARM to 0.92v for 800 and 1600Mhz */
256 dm_i2c_reg_write(dev, BD718XX_BUCK2_VOLT_RUN, 0x16);
257
258 /* Lock the PMIC regs */
259 dm_i2c_reg_write(dev, BD718XX_REGLOCK, 0x11);
260 }
261
Tim Harvey256dba02021-03-02 14:00:21 -0800262 return 0;
263}
264
265void board_init_f(ulong dummy)
266{
267 struct udevice *dev;
268 int ret;
269 int dram_sz;
270
271 arch_cpu_init();
272
273 init_uart_clk(1);
274
275 board_early_init_f();
276
277 timer_init();
278
279 preloader_console_init();
280
281 /* Clear the BSS. */
282 memset(__bss_start, 0, __bss_end - __bss_start);
283
284 ret = spl_early_init();
285 if (ret) {
286 debug("spl_early_init() failed: %d\n", ret);
287 hang();
288 }
289
Tim Harvey256dba02021-03-02 14:00:21 -0800290 enable_tzc380();
291
292 /* need to hold PCIe switch in reset otherwise it can lock i2c bus EEPROM is on */
293 gpio_request(PCIE_RSTN, "perst#");
294 gpio_direction_output(PCIE_RSTN, 0);
295
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700296 /*
297 * probe GSC device
298 *
299 * On a board with a missing/depleted backup battery for GSC, the
300 * board may be ready to probe the GSC before its firmware is
301 * running. We will wait here indefinately for the GSC EEPROM.
302 */
303#ifdef CONFIG_IMX8MN
304 /*
305 * IMX8MN boots quicker than IMX8MM and exposes issue
306 * where because GSC I2C state machine isn't running and its
307 * SCL/SDA are driven low the I2C driver spams 'Arbitration lost'
308 * I2C errors.
309 *
310 * TODO: Put a loop here that somehow waits for I2C CLK/DAT to be high
311 */
312 mdelay(50);
313#endif
314 while (1) {
315 if (!uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(gsc), &dev))
316 break;
317 mdelay(1);
318 }
319 dram_sz = eeprom_init(0);
Tim Harvey256dba02021-03-02 14:00:21 -0800320
321 /* PMIC */
322 power_init_board();
323
324 /* DDR initialization */
325 spl_dram_init(dram_sz);
326
327 board_init_r(NULL, 0);
328}
329
330/* determine prioritized order of boot devices to load U-Boot from */
331void board_boot_order(u32 *spl_boot_list)
332{
Tim Harvey0f5717f2022-04-13 11:31:09 -0700333 int i = 0;
334
Tim Harvey256dba02021-03-02 14:00:21 -0800335 /*
336 * If the SPL was loaded via serial loader, we try to get
337 * U-Boot proper via USB SDP.
338 */
Tim Harvey0f5717f2022-04-13 11:31:09 -0700339 if (spl_boot_device() == BOOT_DEVICE_BOARD) {
340#ifdef CONFIG_IMX8MM
341 spl_boot_list[i++] = BOOT_DEVICE_BOARD;
342#else
343 spl_boot_list[i++] = BOOT_DEVICE_BOOTROM;
344#endif
345 }
Tim Harvey256dba02021-03-02 14:00:21 -0800346
347 /* we have only eMMC in default venice dt */
Tim Harvey0f5717f2022-04-13 11:31:09 -0700348 spl_boot_list[i++] = BOOT_DEVICE_MMC1;
Tim Harvey256dba02021-03-02 14:00:21 -0800349}
350
351/* return boot device based on where the SPL was loaded from */
352int spl_board_boot_device(enum boot_device boot_dev_spl)
353{
354 switch (boot_dev_spl) {
355 case USB_BOOT:
356 return BOOT_DEVICE_BOARD;
357 /* SDHC2 */
358 case SD2_BOOT:
359 case MMC2_BOOT:
360 return BOOT_DEVICE_MMC1;
361 /* SDHC3 */
362 case SD3_BOOT:
363 case MMC3_BOOT:
364 return BOOT_DEVICE_MMC2;
365 default:
366 return BOOT_DEVICE_NONE;
367 }
368}
Tim Harvey724d10a2022-03-08 10:45:39 -0800369
370const char *spl_board_loader_name(u32 boot_device)
371{
372 switch (boot_device) {
373 /* SDHC2 */
374 case BOOT_DEVICE_MMC1:
375 return "eMMC";
376 /* SDHC3 */
377 case BOOT_DEVICE_MMC2:
378 return "SD card";
379 default:
380 return NULL;
381 }
382}