blob: c37c5e0af6d9db06ad4ec052dba312310f028b9e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Agner41f75bb2016-07-20 21:27:49 -07002/*
Stefan Agnercbd59fe2018-08-06 09:19:19 +02003 * Copyright (C) 2016-2018 Toradex AG
Stefan Agner41f75bb2016-07-20 21:27:49 -07004 */
5
Simon Glass8e16b1e2019-12-28 10:45:05 -07006#include <common.h>
Simon Glassafb02152019-12-28 10:45:01 -07007#include <cpu_func.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -07008#include <init.h>
Simon Glass274e0b02020-05-10 11:39:56 -06009#include <net.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070010#include <asm/arch/clock.h>
11#include <asm/arch/crm_regs.h>
12#include <asm/arch/imx-regs.h>
13#include <asm/arch/mx7-pins.h>
14#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070016#include <asm/gpio.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020017#include <asm/mach-imx/iomux-v3.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070018#include <asm/io.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070019#include <dm.h>
20#include <dm/platform_data/serial_mxc.h>
Stefan Agner6a667482017-03-09 17:17:54 -080021#include <fdt_support.h>
Yangbo Lu73340382019-06-21 11:42:28 +080022#include <fsl_esdhc_imx.h>
Stefan Agner6a667482017-03-09 17:17:54 -080023#include <jffs2/load_kernel.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070025#include <linux/sizes.h>
26#include <mmc.h>
27#include <miiphy.h>
Stefan Agner6a667482017-03-09 17:17:54 -080028#include <mtd_node.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070029#include <netdev.h>
Stefan Agnere65377a2016-10-05 15:27:11 -070030#include <power/pmic.h>
31#include <power/rn5t567_pmic.h>
Stefan Agner443166e2017-03-09 17:17:52 -080032#include <usb.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070033#include <usb/ehci-ci.h>
Stefan Agner98ffd0f2016-11-30 13:41:53 -080034#include "../common/tdx-common.h"
Stefan Agner41f75bb2016-07-20 21:27:49 -070035
36DECLARE_GLOBAL_DATA_PTR;
37
38#define UART_PAD_CTRL (PAD_CTL_DSE_3P3V_49OHM | \
39 PAD_CTL_PUS_PU100KOHM | PAD_CTL_HYS)
40
Stefan Agner41f75bb2016-07-20 21:27:49 -070041#define ENET_PAD_CTRL (PAD_CTL_PUS_PU100KOHM | PAD_CTL_DSE_3P3V_49OHM)
42#define ENET_PAD_CTRL_MII (PAD_CTL_DSE_3P3V_32OHM)
43
44#define ENET_RX_PAD_CTRL (PAD_CTL_PUS_PU100KOHM | PAD_CTL_DSE_3P3V_49OHM)
45
Stefan Agner41f75bb2016-07-20 21:27:49 -070046#define LCD_PAD_CTRL (PAD_CTL_HYS | PAD_CTL_PUS_PU100KOHM | \
47 PAD_CTL_DSE_3P3V_49OHM)
48
49#define NAND_PAD_CTRL (PAD_CTL_DSE_3P3V_49OHM | PAD_CTL_SRE_SLOW | PAD_CTL_HYS)
50
51#define NAND_PAD_READY0_CTRL (PAD_CTL_DSE_3P3V_49OHM | PAD_CTL_PUS_PU5KOHM)
52
Stefan Agner443166e2017-03-09 17:17:52 -080053#define USB_CDET_GPIO IMX_GPIO_NR(7, 14)
54
Hiago De Francoe7438bd2023-10-02 08:57:49 -030055#define FLASH_DETECTION_CTRL (PAD_CTL_HYS | PAD_CTL_PUE)
56#define FLASH_DET_GPIO IMX_GPIO_NR(6, 11)
57
58static bool is_emmc;
59
Stefan Agner41f75bb2016-07-20 21:27:49 -070060int dram_init(void)
61{
Fabio Estevamf8774732018-09-19 13:01:56 +020062 gd->ram_size = get_ram_size((void *)PHYS_SDRAM, imx_ddr_size());
Stefan Agner41f75bb2016-07-20 21:27:49 -070063
64 return 0;
65}
66
Hiago De Francoe7438bd2023-10-02 08:57:49 -030067static iomux_v3_cfg_t const flash_detection_pads[] = {
Marcel Ziswiler41ff9602023-12-12 08:28:15 -030068 MX7D_PAD_SD3_RESET_B__GPIO6_IO11 | MUX_PAD_CTRL(FLASH_DETECTION_CTRL) | MUX_MODE_SION,
Hiago De Francoe7438bd2023-10-02 08:57:49 -030069};
70
Stefan Agner41f75bb2016-07-20 21:27:49 -070071static iomux_v3_cfg_t const uart1_pads[] = {
72 MX7D_PAD_UART1_RX_DATA__UART1_DTE_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
73 MX7D_PAD_UART1_TX_DATA__UART1_DTE_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
74 MX7D_PAD_SAI2_TX_BCLK__UART1_DTE_CTS | MUX_PAD_CTRL(UART_PAD_CTRL),
75 MX7D_PAD_SAI2_TX_SYNC__UART1_DTE_RTS | MUX_PAD_CTRL(UART_PAD_CTRL),
76};
77
Stefan Agner443166e2017-03-09 17:17:52 -080078#ifdef CONFIG_USB_EHCI_MX7
79static iomux_v3_cfg_t const usb_cdet_pads[] = {
80 MX7D_PAD_ENET1_CRS__GPIO7_IO14 | MUX_PAD_CTRL(NO_PAD_CTRL),
81};
82#endif
Stefan Agner41f75bb2016-07-20 21:27:49 -070083
Stefan Agnercbd59fe2018-08-06 09:19:19 +020084#ifdef CONFIG_TARGET_COLIBRI_IMX7_NAND
Stefan Agner41f75bb2016-07-20 21:27:49 -070085static iomux_v3_cfg_t const gpmi_pads[] = {
86 MX7D_PAD_SD3_DATA0__NAND_DATA00 | MUX_PAD_CTRL(NAND_PAD_CTRL),
87 MX7D_PAD_SD3_DATA1__NAND_DATA01 | MUX_PAD_CTRL(NAND_PAD_CTRL),
88 MX7D_PAD_SD3_DATA2__NAND_DATA02 | MUX_PAD_CTRL(NAND_PAD_CTRL),
89 MX7D_PAD_SD3_DATA3__NAND_DATA03 | MUX_PAD_CTRL(NAND_PAD_CTRL),
90 MX7D_PAD_SD3_DATA4__NAND_DATA04 | MUX_PAD_CTRL(NAND_PAD_CTRL),
91 MX7D_PAD_SD3_DATA5__NAND_DATA05 | MUX_PAD_CTRL(NAND_PAD_CTRL),
92 MX7D_PAD_SD3_DATA6__NAND_DATA06 | MUX_PAD_CTRL(NAND_PAD_CTRL),
93 MX7D_PAD_SD3_DATA7__NAND_DATA07 | MUX_PAD_CTRL(NAND_PAD_CTRL),
94 MX7D_PAD_SD3_CLK__NAND_CLE | MUX_PAD_CTRL(NAND_PAD_CTRL),
95 MX7D_PAD_SD3_CMD__NAND_ALE | MUX_PAD_CTRL(NAND_PAD_CTRL),
96 MX7D_PAD_SD3_STROBE__NAND_RE_B | MUX_PAD_CTRL(NAND_PAD_CTRL),
97 MX7D_PAD_SD3_RESET_B__NAND_WE_B | MUX_PAD_CTRL(NAND_PAD_CTRL),
98 MX7D_PAD_SAI1_RX_DATA__NAND_CE1_B | MUX_PAD_CTRL(NAND_PAD_CTRL),
99 MX7D_PAD_SAI1_TX_BCLK__NAND_CE0_B | MUX_PAD_CTRL(NAND_PAD_CTRL),
100 MX7D_PAD_SAI1_TX_DATA__NAND_READY_B | MUX_PAD_CTRL(NAND_PAD_READY0_CTRL),
101};
102
103static void setup_gpmi_nand(void)
104{
105 imx_iomux_v3_setup_multiple_pads(gpmi_pads, ARRAY_SIZE(gpmi_pads));
106
107 /* NAND_USDHC_BUS_CLK is set in rom */
108 set_clk_nand();
109}
110#endif
111
Simon Glass52cb5042022-10-18 07:46:31 -0600112#ifdef CONFIG_VIDEO
Stefan Agner41f75bb2016-07-20 21:27:49 -0700113static iomux_v3_cfg_t const backlight_pads[] = {
114 /* Backlight On */
115 MX7D_PAD_SD1_WP__GPIO5_IO1 | MUX_PAD_CTRL(NO_PAD_CTRL),
116 /* Backlight PWM<A> (multiplexed pin) */
117 MX7D_PAD_GPIO1_IO08__GPIO1_IO8 | MUX_PAD_CTRL(NO_PAD_CTRL),
118 MX7D_PAD_ECSPI2_MOSI__GPIO4_IO21 | MUX_PAD_CTRL(NO_PAD_CTRL),
119};
120
121#define GPIO_BL_ON IMX_GPIO_NR(5, 1)
122#define GPIO_PWM_A IMX_GPIO_NR(1, 8)
123
124static int setup_lcd(void)
125{
Stefan Agner41f75bb2016-07-20 21:27:49 -0700126 imx_iomux_v3_setup_multiple_pads(backlight_pads, ARRAY_SIZE(backlight_pads));
127
128 /* Set BL_ON */
129 gpio_request(GPIO_BL_ON, "BL_ON");
130 gpio_direction_output(GPIO_BL_ON, 1);
131
132 /* Set PWM<A> to full brightness (assuming inversed polarity) */
133 gpio_request(GPIO_PWM_A, "PWM<A>");
134 gpio_direction_output(GPIO_PWM_A, 0);
135
136 return 0;
137}
138#endif
139
Gerard Salvatella108d7392018-11-19 15:54:10 +0100140/*
141 * Backlight off before OS handover
142 */
143void board_preboot_os(void)
144{
Simon Glass52cb5042022-10-18 07:46:31 -0600145#ifdef CONFIG_VIDEO
Gerard Salvatella108d7392018-11-19 15:54:10 +0100146 gpio_direction_output(GPIO_PWM_A, 1);
147 gpio_direction_output(GPIO_BL_ON, 0);
Igor Opaniukefe398f2020-09-14 11:01:07 +0300148#endif
Gerard Salvatella108d7392018-11-19 15:54:10 +0100149}
150
Stefan Agner41f75bb2016-07-20 21:27:49 -0700151static void setup_iomux_uart(void)
152{
153 imx_iomux_v3_setup_multiple_pads(uart1_pads, ARRAY_SIZE(uart1_pads));
154}
155
Stefan Agner41f75bb2016-07-20 21:27:49 -0700156#ifdef CONFIG_FEC_MXC
Stefan Agner41f75bb2016-07-20 21:27:49 -0700157static int setup_fec(void)
158{
159 struct iomuxc_gpr_base_regs *const iomuxc_gpr_regs
160 = (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
161
162#ifndef CONFIG_COLIBRI_IMX7_EXT_PHYCLK
163 /*
164 * Use 50M anatop REF_CLK1 for ENET1, clear gpr1[13], set gpr1[17]
165 * and output it on the pin
166 */
167 clrsetbits_le32(&iomuxc_gpr_regs->gpr[1],
168 IOMUXC_GPR_GPR1_GPR_ENET1_TX_CLK_SEL_MASK,
169 IOMUXC_GPR_GPR1_GPR_ENET1_CLK_DIR_MASK);
170#else
171 /* Use 50M external CLK for ENET1, set gpr1[13], clear gpr1[17] */
172 clrsetbits_le32(&iomuxc_gpr_regs->gpr[1],
173 IOMUXC_GPR_GPR1_GPR_ENET1_CLK_DIR_MASK,
174 IOMUXC_GPR_GPR1_GPR_ENET1_TX_CLK_SEL_MASK);
175#endif
176
Eric Nelsoneadd7322017-08-31 08:34:23 -0700177 return set_clk_enet(ENET_50MHZ);
Stefan Agner41f75bb2016-07-20 21:27:49 -0700178}
179
Stefan Agner41f75bb2016-07-20 21:27:49 -0700180#endif
181
182int board_early_init_f(void)
183{
184 setup_iomux_uart();
185
Stefan Agner41f75bb2016-07-20 21:27:49 -0700186 return 0;
187}
188
189int board_init(void)
190{
191 /* address of boot parameters */
192 gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
193
Hiago De Francoe7438bd2023-10-02 08:57:49 -0300194 /*
Marcel Ziswiler41ff9602023-12-12 08:28:15 -0300195 * Enable GPIO SION on NAND_WE_B/eMMC_RST with 100k pull-down. eMMC_RST
Hiago De Francoe7438bd2023-10-02 08:57:49 -0300196 * is pulled high with 4.7k for eMMC devices. This allows to reliably
Marcel Ziswiler41ff9602023-12-12 08:28:15 -0300197 * detect eMMC vs NAND flash.
Hiago De Francoe7438bd2023-10-02 08:57:49 -0300198 */
199 imx_iomux_v3_setup_multiple_pads(flash_detection_pads, ARRAY_SIZE(flash_detection_pads));
200 gpio_request(FLASH_DET_GPIO, "flash-detection-gpio");
201 is_emmc = gpio_get_value(FLASH_DET_GPIO);
202 gpio_free(FLASH_DET_GPIO);
203
Stefan Agner41f75bb2016-07-20 21:27:49 -0700204#ifdef CONFIG_FEC_MXC
205 setup_fec();
206#endif
207
Stefan Agnercbd59fe2018-08-06 09:19:19 +0200208#ifdef CONFIG_TARGET_COLIBRI_IMX7_NAND
Stefan Agner41f75bb2016-07-20 21:27:49 -0700209 setup_gpmi_nand();
210#endif
211
Stefan Agner443166e2017-03-09 17:17:52 -0800212#ifdef CONFIG_USB_EHCI_MX7
213 imx_iomux_v3_setup_multiple_pads(usb_cdet_pads, ARRAY_SIZE(usb_cdet_pads));
214 gpio_request(USB_CDET_GPIO, "usb-cdet-gpio");
215#endif
216
Stefan Agner41f75bb2016-07-20 21:27:49 -0700217 return 0;
218}
219
Stefan Agnere65377a2016-10-05 15:27:11 -0700220#ifdef CONFIG_DM_PMIC
221int power_init_board(void)
222{
223 struct udevice *dev;
224 int reg, ver;
225 int ret;
226
227
Marcel Ziswiler23b65be2022-07-21 15:27:35 +0200228 ret = pmic_get("pmic@33", &dev);
Stefan Agnere65377a2016-10-05 15:27:11 -0700229 if (ret)
230 return ret;
231 ver = pmic_reg_read(dev, RN5T567_LSIVER);
232 reg = pmic_reg_read(dev, RN5T567_OTPVER);
233
234 printf("PMIC: RN5T567 LSIVER=0x%02x OTPVER=0x%02x\n", ver, reg);
235
236 /* set judge and press timer of N_OE to minimal values */
237 pmic_clrsetbits(dev, RN5T567_NOETIMSETCNT, 0x7, 0);
238
Stefan Agner0f2c5ad2017-03-09 17:17:53 -0800239 /* configure sleep slot for 3.3V Ethernet */
240 reg = pmic_reg_read(dev, RN5T567_LDO1_SLOT);
241 reg = (reg & 0xf0) | reg >> 4;
242 pmic_reg_write(dev, RN5T567_LDO1_SLOT, reg);
243
244 /* disable DCDC2 discharge to avoid backfeeding through VFB2 */
245 pmic_clrsetbits(dev, RN5T567_DC2CTL, 0x2, 0);
246
247 /* configure sleep slot for ARM rail */
248 reg = pmic_reg_read(dev, RN5T567_DC2_SLOT);
249 reg = (reg & 0xf0) | reg >> 4;
250 pmic_reg_write(dev, RN5T567_DC2_SLOT, reg);
251
252 /* disable LDO2 discharge to avoid backfeeding from +V3.3_SD */
253 pmic_clrsetbits(dev, RN5T567_LDODIS1, 0x2, 0);
254
Stefan Agnere65377a2016-10-05 15:27:11 -0700255 return 0;
256}
257
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100258void reset_cpu(void)
Stefan Agnere65377a2016-10-05 15:27:11 -0700259{
260 struct udevice *dev;
261
Marcel Ziswiler23b65be2022-07-21 15:27:35 +0200262 pmic_get("pmic@33", &dev);
Stefan Agnere65377a2016-10-05 15:27:11 -0700263
264 /* Use PMIC to reset, set REPWRTIM to 0 and REPWRON to 1 */
265 pmic_reg_write(dev, RN5T567_REPCNT, 0x1);
266 pmic_reg_write(dev, RN5T567_SLPCNT, 0x1);
267
268 /*
269 * Re-power factor detection on PMIC side is not instant. 1ms
270 * proved to be enough time until reset takes effect.
271 */
272 mdelay(1);
273}
274#endif
275
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800276#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900277int ft_board_setup(void *blob, struct bd_info *bd)
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800278{
Igor Opaniukcbee9452019-12-03 14:04:47 +0200279#if defined(CONFIG_IMX_BOOTAUX) && defined(CONFIG_ARCH_FIXUP_FDT_MEMORY)
280 int up;
281
282 up = arch_auxiliary_core_check_up(0);
283 if (up) {
284 int ret;
285 int areas = 1;
286 u64 start[2], size[2];
287
288 /*
289 * Reserve 1MB of memory for M4 (1MiB is also the minimum
290 * alignment for Linux due to MMU section size restrictions).
291 */
292 start[0] = gd->bd->bi_dram[0].start;
293 size[0] = SZ_256M - SZ_1M;
294
295 /* If needed, create a second entry for memory beyond 256M */
296 if (gd->bd->bi_dram[0].size > SZ_256M) {
297 start[1] = gd->bd->bi_dram[0].start + SZ_256M;
298 size[1] = gd->bd->bi_dram[0].size - SZ_256M;
299 areas = 2;
300 }
301
302 ret = fdt_set_usable_memory(blob, start, size, areas);
303 if (ret) {
304 eprintf("Cannot set usable memory\n");
305 return ret;
306 }
307 } else {
308 int off;
309
310 off = fdt_node_offset_by_compatible(blob, -1,
311 "fsl,imx7d-rpmsg");
312 if (off > 0)
313 fdt_status_disabled(blob, off);
314 }
315#endif
Stefan Agner6a667482017-03-09 17:17:54 -0800316
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800317 return ft_common_board_setup(blob, bd);
318}
319#endif
320
Stefan Agner41f75bb2016-07-20 21:27:49 -0700321#ifdef CONFIG_USB_EHCI_MX7
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200322int board_fix_fdt(void *rw_fdt_blob)
Stefan Agner41f75bb2016-07-20 21:27:49 -0700323{
Fabio Estevamde951e22023-07-04 14:09:45 -0300324 int ret;
325
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200326 /* i.MX 7Solo has only one single USB OTG1 but no USB host port */
327 if (is_cpu_type(MXC_CPU_MX7S)) {
328 int offset = fdt_path_offset(rw_fdt_blob, "/soc/bus@30800000/usb@30b20000");
Stefan Agner41f75bb2016-07-20 21:27:49 -0700329
Fabio Estevamde951e22023-07-04 14:09:45 -0300330 /*
331 * We're changing from status = "okay" to status = "disabled".
332 * In this case we'll need more space, so increase the size
333 * a little bit.
334 */
335 ret = fdt_increase_size(rw_fdt_blob, 32);
336 if (ret < 0) {
337 printf("Cannot increase FDT size: %d\n", ret);
338 return ret;
339 }
340
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200341 return fdt_status_disabled(rw_fdt_blob, offset);
Stefan Agner41f75bb2016-07-20 21:27:49 -0700342 }
Stefan Agner443166e2017-03-09 17:17:52 -0800343
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200344 return 0;
Stefan Agner443166e2017-03-09 17:17:52 -0800345}
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300346
Stefan Agner8fa31872021-07-23 09:39:45 +0300347#if defined(CONFIG_BOARD_LATE_INIT)
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300348int board_late_init(void)
349{
Simon Glass52cb5042022-10-18 07:46:31 -0600350#if defined(CONFIG_VIDEO)
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300351 setup_lcd();
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300352#endif
Stefan Agner8fa31872021-07-23 09:39:45 +0300353
Hiago De Francoc3615822023-11-09 13:24:01 -0300354 if (IS_ENABLED(CONFIG_USB) && is_boot_from_usb()) {
Stefan Agner8fa31872021-07-23 09:39:45 +0300355 env_set("bootdelay", "0");
Hiago De Francoc3615822023-11-09 13:24:01 -0300356 if (IS_ENABLED(CONFIG_CMD_USB_SDP)) {
357 printf("Serial Downloader recovery mode, using sdp command\n");
358 env_set("bootcmd", "sdp 0");
359 } else if (IS_ENABLED(CONFIG_CMD_FASTBOOT)) {
360 printf("Fastboot recovery mode, using fastboot command\n");
361 env_set("bootcmd", "fastboot usb 0");
362 }
Stefan Agner8fa31872021-07-23 09:39:45 +0300363 }
Hiago De Francoc3615822023-11-09 13:24:01 -0300364
Hiago De Francoe7438bd2023-10-02 08:57:49 -0300365 if (is_emmc)
366 env_set("variant", "-emmc");
367 else
368 env_set("variant", "");
369
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300370 return 0;
371}
Stefan Agner8fa31872021-07-23 09:39:45 +0300372#endif /* CONFIG_BOARD_LATE_INIT */
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300373
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200374#endif /* CONFIG_USB_EHCI_MX7 */