blob: b56e1b607d586306124ab0a3f92d147d618fd57d [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>
10#include <image.h>
11#include <init.h>
12#include <log.h>
13#include <spl.h>
14#include <asm/io.h>
15#include <asm/mach-imx/gpio.h>
16#include <asm/mach-imx/iomux-v3.h>
17#include <asm/arch/clock.h>
18#include <asm/arch/imx8mm_pins.h>
Tim Harvey1a50e742022-02-11 10:48:56 -080019#include <asm/arch/imx8mn_pins.h>
Tim Harvey256dba02021-03-02 14:00:21 -080020#include <asm/arch/sys_proto.h>
21#include <asm/mach-imx/boot_mode.h>
22#include <asm/arch/ddr.h>
23#include <asm-generic/gpio.h>
24
25#include <dm/uclass.h>
26#include <dm/device.h>
27#include <dm/uclass-internal.h>
28#include <dm/device-internal.h>
29
Tim Harvey1b7fbf62021-06-30 16:50:02 -070030#include <power/bd71837.h>
Tim Harvey256dba02021-03-02 14:00:21 -080031#include <power/mp5416.h>
32
33#include "gsc.h"
34#include "lpddr4_timing.h"
35
36#define PCIE_RSTN IMX_GPIO_NR(4, 6)
37
38DECLARE_GLOBAL_DATA_PTR;
39
40static void spl_dram_init(int size)
41{
42 struct dram_timing_info *dram_timing;
43
44 switch (size) {
Tim Harvey1a50e742022-02-11 10:48:56 -080045#ifdef CONFIG_IMX8MM
Tim Harvey5cc5e192022-02-18 15:19:33 -080046 case 512:
47 dram_timing = &dram_timing_512mb;
48 break;
49 case 1024:
Tim Harvey256dba02021-03-02 14:00:21 -080050 dram_timing = &dram_timing_1gb;
51 break;
Tim Harvey5cc5e192022-02-18 15:19:33 -080052 case 2048:
Tim Harvey6603b5e2021-07-27 15:19:41 -070053 dram_timing = &dram_timing_2gb;
54 break;
Tim Harvey5cc5e192022-02-18 15:19:33 -080055 case 4096:
Tim Harvey256dba02021-03-02 14:00:21 -080056 dram_timing = &dram_timing_4gb;
57 break;
58 default:
Tim Harvey5cc5e192022-02-18 15:19:33 -080059 printf("Unknown DDR configuration: %d MiB\n", size);
Tim Harvey256dba02021-03-02 14:00:21 -080060 dram_timing = &dram_timing_1gb;
Tim Harvey5cc5e192022-02-18 15:19:33 -080061 size = 1024;
Tim Harvey1a50e742022-02-11 10:48:56 -080062#endif
63#ifdef CONFIG_IMX8MN
Tim Harvey5cc5e192022-02-18 15:19:33 -080064 case 1024:
Tim Harvey1a50e742022-02-11 10:48:56 -080065 dram_timing = &dram_timing_1gb_single_die;
66 break;
Tim Harvey5cc5e192022-02-18 15:19:33 -080067 case 2048:
Tim Harvey1a50e742022-02-11 10:48:56 -080068 if (!strcmp(gsc_get_model(), "GW7902-SP466-A") ||
69 !strcmp(gsc_get_model(), "GW7902-SP466-B")) {
70 dram_timing = &dram_timing_2gb_dual_die;
71 } else {
72 dram_timing = &dram_timing_2gb_single_die;
73 }
74 break;
75 default:
Tim Harvey5cc5e192022-02-18 15:19:33 -080076 printf("Unknown DDR configuration: %d MiB\n", size);
Tim Harvey1a50e742022-02-11 10:48:56 -080077 dram_timing = &dram_timing_2gb_dual_die;
Tim Harvey5cc5e192022-02-18 15:19:33 -080078 size = 2048;
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 Harvey1a50e742022-02-11 10:48:56 -0800102#endif
103#ifdef CONFIG_IMX8MN
104static const iomux_v3_cfg_t uart_pads[] = {
105 IMX8MN_PAD_UART2_RXD__UART2_DCE_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
106 IMX8MN_PAD_UART2_TXD__UART2_DCE_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
107};
108
109static const iomux_v3_cfg_t wdog_pads[] = {
110 IMX8MN_PAD_GPIO1_IO02__WDOG1_WDOG_B | MUX_PAD_CTRL(WDOG_PAD_CTRL),
111};
112#endif
Tim Harvey256dba02021-03-02 14:00:21 -0800113
114int board_early_init_f(void)
115{
116 struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR;
117
118 imx_iomux_v3_setup_multiple_pads(wdog_pads, ARRAY_SIZE(wdog_pads));
119
120 set_wdog_reset(wdog);
121
122 imx_iomux_v3_setup_multiple_pads(uart_pads, ARRAY_SIZE(uart_pads));
123
124 return 0;
125}
126
127/*
128 * Model specific PMIC adjustments necessary prior to DRAM init
129 *
130 * Note that we can not use pmic dm drivers here as we have a generic
131 * venice dt that does not have board-specific pmic's defined.
132 *
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700133 * Instead we must use dm_i2c so we a helpers to give us
134 * clrsetbit functions we would otherwise have if we could use PMIC dm
135 * drivers.
Tim Harvey256dba02021-03-02 14:00:21 -0800136 */
Tim Harvey1b7fbf62021-06-30 16:50:02 -0700137static int dm_i2c_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
138{
139 int ret;
140 u8 val;
141
142 ret = dm_i2c_read(dev, reg, &val, 1);
143 if (ret)
144 return ret;
145 val = (val & ~clr) | set;
146
147 return dm_i2c_write(dev, reg, &val, 1);
148}
149
Tim Harvey256dba02021-03-02 14:00:21 -0800150static int power_init_board(void)
151{
152 const char *model = gsc_get_model();
153 struct udevice *bus;
154 struct udevice *dev;
155 int ret;
156
157 if ((!strncmp(model, "GW71", 4)) ||
158 (!strncmp(model, "GW72", 4)) ||
159 (!strncmp(model, "GW73", 4))) {
Tim Harveyd5419272021-07-27 15:19:38 -0700160 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &bus);
Tim Harvey256dba02021-03-02 14:00:21 -0800161 if (ret) {
162 printf("PMIC : failed I2C1 probe: %d\n", ret);
163 return ret;
164 }
165 ret = dm_i2c_probe(bus, 0x69, 0, &dev);
166 if (ret) {
167 printf("PMIC : failed probe: %d\n", ret);
168 return ret;
169 }
170 puts("PMIC : MP5416\n");
171
172 /* set VDD_ARM SW3 to 0.92V for 1.6GHz */
173 dm_i2c_reg_write(dev, MP5416_VSET_SW3,
174 BIT(7) | MP5416_VSET_SW3_SVAL(920000));
175 }
176
Tim Harvey6603b5e2021-07-27 15:19:41 -0700177 else if ((!strncmp(model, "GW7901", 6)) ||
178 (!strncmp(model, "GW7902", 6))) {
179 if (!strncmp(model, "GW7901", 6))
180 ret = uclass_get_device_by_seq(UCLASS_I2C, 1, &bus);
181 else
182 ret = uclass_get_device_by_seq(UCLASS_I2C, 0, &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{
223 struct udevice *dev;
224 int ret;
225 int dram_sz;
226
227 arch_cpu_init();
228
229 init_uart_clk(1);
230
231 board_early_init_f();
232
233 timer_init();
234
235 preloader_console_init();
236
237 /* Clear the BSS. */
238 memset(__bss_start, 0, __bss_end - __bss_start);
239
240 ret = spl_early_init();
241 if (ret) {
242 debug("spl_early_init() failed: %d\n", ret);
243 hang();
244 }
245
246 ret = uclass_get_device_by_name(UCLASS_CLK,
247 "clock-controller@30380000",
248 &dev);
249 if (ret < 0) {
250 printf("Failed to find clock node. Check device tree\n");
251 hang();
252 }
253
254 enable_tzc380();
255
256 /* need to hold PCIe switch in reset otherwise it can lock i2c bus EEPROM is on */
257 gpio_request(PCIE_RSTN, "perst#");
258 gpio_direction_output(PCIE_RSTN, 0);
259
260 /* GSC */
261 dram_sz = gsc_init(0);
262
263 /* PMIC */
264 power_init_board();
265
266 /* DDR initialization */
267 spl_dram_init(dram_sz);
268
269 board_init_r(NULL, 0);
270}
271
272/* determine prioritized order of boot devices to load U-Boot from */
273void board_boot_order(u32 *spl_boot_list)
274{
275 /*
276 * If the SPL was loaded via serial loader, we try to get
277 * U-Boot proper via USB SDP.
278 */
279 if (spl_boot_device() == BOOT_DEVICE_BOARD)
280 spl_boot_list[0] = BOOT_DEVICE_BOARD;
281
282 /* we have only eMMC in default venice dt */
283 spl_boot_list[0] = BOOT_DEVICE_MMC1;
284}
285
286/* return boot device based on where the SPL was loaded from */
287int spl_board_boot_device(enum boot_device boot_dev_spl)
288{
289 switch (boot_dev_spl) {
290 case USB_BOOT:
291 return BOOT_DEVICE_BOARD;
292 /* SDHC2 */
293 case SD2_BOOT:
294 case MMC2_BOOT:
295 return BOOT_DEVICE_MMC1;
296 /* SDHC3 */
297 case SD3_BOOT:
298 case MMC3_BOOT:
299 return BOOT_DEVICE_MMC2;
300 default:
301 return BOOT_DEVICE_NONE;
302 }
303}
Tim Harvey724d10a2022-03-08 10:45:39 -0800304
305const char *spl_board_loader_name(u32 boot_device)
306{
307 switch (boot_device) {
308 /* SDHC2 */
309 case BOOT_DEVICE_MMC1:
310 return "eMMC";
311 /* SDHC3 */
312 case BOOT_DEVICE_MMC2:
313 return "SD card";
314 default:
315 return NULL;
316 }
317}