blob: 7de29e3abfb6393a6032c0b6999e886088919ad2 [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 Glassafb02152019-12-28 10:45:01 -07006#include <cpu_func.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -07007#include <init.h>
Simon Glass274e0b02020-05-10 11:39:56 -06008#include <net.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -07009#include <asm/arch/clock.h>
10#include <asm/arch/crm_regs.h>
11#include <asm/arch/imx-regs.h>
12#include <asm/arch/mx7-pins.h>
13#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070015#include <asm/gpio.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020016#include <asm/mach-imx/iomux-v3.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070017#include <asm/io.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070018#include <dm.h>
19#include <dm/platform_data/serial_mxc.h>
Stefan Agner6a667482017-03-09 17:17:54 -080020#include <fdt_support.h>
Yangbo Lu73340382019-06-21 11:42:28 +080021#include <fsl_esdhc_imx.h>
Stefan Agner6a667482017-03-09 17:17:54 -080022#include <jffs2/load_kernel.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070024#include <linux/sizes.h>
25#include <mmc.h>
26#include <miiphy.h>
Stefan Agner6a667482017-03-09 17:17:54 -080027#include <mtd_node.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070028#include <netdev.h>
Stefan Agnere65377a2016-10-05 15:27:11 -070029#include <power/pmic.h>
30#include <power/rn5t567_pmic.h>
Stefan Agner443166e2017-03-09 17:17:52 -080031#include <usb.h>
Stefan Agner41f75bb2016-07-20 21:27:49 -070032#include <usb/ehci-ci.h>
Stefan Agner98ffd0f2016-11-30 13:41:53 -080033#include "../common/tdx-common.h"
Stefan Agner41f75bb2016-07-20 21:27:49 -070034
35DECLARE_GLOBAL_DATA_PTR;
36
37#define UART_PAD_CTRL (PAD_CTL_DSE_3P3V_49OHM | \
38 PAD_CTL_PUS_PU100KOHM | PAD_CTL_HYS)
39
Stefan Agner41f75bb2016-07-20 21:27:49 -070040#define ENET_PAD_CTRL (PAD_CTL_PUS_PU100KOHM | PAD_CTL_DSE_3P3V_49OHM)
41#define ENET_PAD_CTRL_MII (PAD_CTL_DSE_3P3V_32OHM)
42
43#define ENET_RX_PAD_CTRL (PAD_CTL_PUS_PU100KOHM | PAD_CTL_DSE_3P3V_49OHM)
44
Stefan Agner41f75bb2016-07-20 21:27:49 -070045#define LCD_PAD_CTRL (PAD_CTL_HYS | PAD_CTL_PUS_PU100KOHM | \
46 PAD_CTL_DSE_3P3V_49OHM)
47
48#define NAND_PAD_CTRL (PAD_CTL_DSE_3P3V_49OHM | PAD_CTL_SRE_SLOW | PAD_CTL_HYS)
49
50#define NAND_PAD_READY0_CTRL (PAD_CTL_DSE_3P3V_49OHM | PAD_CTL_PUS_PU5KOHM)
51
Stefan Agner443166e2017-03-09 17:17:52 -080052#define USB_CDET_GPIO IMX_GPIO_NR(7, 14)
53
Hiago De Francoe7438bd2023-10-02 08:57:49 -030054#define FLASH_DETECTION_CTRL (PAD_CTL_HYS | PAD_CTL_PUE)
55#define FLASH_DET_GPIO IMX_GPIO_NR(6, 11)
56
57static bool is_emmc;
58
Stefan Agner41f75bb2016-07-20 21:27:49 -070059int dram_init(void)
60{
Fabio Estevamf8774732018-09-19 13:01:56 +020061 gd->ram_size = get_ram_size((void *)PHYS_SDRAM, imx_ddr_size());
Stefan Agner41f75bb2016-07-20 21:27:49 -070062
63 return 0;
64}
65
Hiago De Francoe7438bd2023-10-02 08:57:49 -030066static iomux_v3_cfg_t const flash_detection_pads[] = {
Marcel Ziswiler41ff9602023-12-12 08:28:15 -030067 MX7D_PAD_SD3_RESET_B__GPIO6_IO11 | MUX_PAD_CTRL(FLASH_DETECTION_CTRL) | MUX_MODE_SION,
Hiago De Francoe7438bd2023-10-02 08:57:49 -030068};
69
Stefan Agner41f75bb2016-07-20 21:27:49 -070070static iomux_v3_cfg_t const uart1_pads[] = {
71 MX7D_PAD_UART1_RX_DATA__UART1_DTE_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
72 MX7D_PAD_UART1_TX_DATA__UART1_DTE_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
73 MX7D_PAD_SAI2_TX_BCLK__UART1_DTE_CTS | MUX_PAD_CTRL(UART_PAD_CTRL),
74 MX7D_PAD_SAI2_TX_SYNC__UART1_DTE_RTS | MUX_PAD_CTRL(UART_PAD_CTRL),
75};
76
Stefan Agner443166e2017-03-09 17:17:52 -080077#ifdef CONFIG_USB_EHCI_MX7
78static iomux_v3_cfg_t const usb_cdet_pads[] = {
79 MX7D_PAD_ENET1_CRS__GPIO7_IO14 | MUX_PAD_CTRL(NO_PAD_CTRL),
80};
81#endif
Stefan Agner41f75bb2016-07-20 21:27:49 -070082
Stefan Agnercbd59fe2018-08-06 09:19:19 +020083#ifdef CONFIG_TARGET_COLIBRI_IMX7_NAND
Stefan Agner41f75bb2016-07-20 21:27:49 -070084static iomux_v3_cfg_t const gpmi_pads[] = {
85 MX7D_PAD_SD3_DATA0__NAND_DATA00 | MUX_PAD_CTRL(NAND_PAD_CTRL),
86 MX7D_PAD_SD3_DATA1__NAND_DATA01 | MUX_PAD_CTRL(NAND_PAD_CTRL),
87 MX7D_PAD_SD3_DATA2__NAND_DATA02 | MUX_PAD_CTRL(NAND_PAD_CTRL),
88 MX7D_PAD_SD3_DATA3__NAND_DATA03 | MUX_PAD_CTRL(NAND_PAD_CTRL),
89 MX7D_PAD_SD3_DATA4__NAND_DATA04 | MUX_PAD_CTRL(NAND_PAD_CTRL),
90 MX7D_PAD_SD3_DATA5__NAND_DATA05 | MUX_PAD_CTRL(NAND_PAD_CTRL),
91 MX7D_PAD_SD3_DATA6__NAND_DATA06 | MUX_PAD_CTRL(NAND_PAD_CTRL),
92 MX7D_PAD_SD3_DATA7__NAND_DATA07 | MUX_PAD_CTRL(NAND_PAD_CTRL),
93 MX7D_PAD_SD3_CLK__NAND_CLE | MUX_PAD_CTRL(NAND_PAD_CTRL),
94 MX7D_PAD_SD3_CMD__NAND_ALE | MUX_PAD_CTRL(NAND_PAD_CTRL),
95 MX7D_PAD_SD3_STROBE__NAND_RE_B | MUX_PAD_CTRL(NAND_PAD_CTRL),
96 MX7D_PAD_SD3_RESET_B__NAND_WE_B | MUX_PAD_CTRL(NAND_PAD_CTRL),
97 MX7D_PAD_SAI1_RX_DATA__NAND_CE1_B | MUX_PAD_CTRL(NAND_PAD_CTRL),
98 MX7D_PAD_SAI1_TX_BCLK__NAND_CE0_B | MUX_PAD_CTRL(NAND_PAD_CTRL),
99 MX7D_PAD_SAI1_TX_DATA__NAND_READY_B | MUX_PAD_CTRL(NAND_PAD_READY0_CTRL),
100};
101
102static void setup_gpmi_nand(void)
103{
104 imx_iomux_v3_setup_multiple_pads(gpmi_pads, ARRAY_SIZE(gpmi_pads));
105
106 /* NAND_USDHC_BUS_CLK is set in rom */
107 set_clk_nand();
108}
109#endif
110
Simon Glass52cb5042022-10-18 07:46:31 -0600111#ifdef CONFIG_VIDEO
Stefan Agner41f75bb2016-07-20 21:27:49 -0700112static iomux_v3_cfg_t const backlight_pads[] = {
113 /* Backlight On */
114 MX7D_PAD_SD1_WP__GPIO5_IO1 | MUX_PAD_CTRL(NO_PAD_CTRL),
115 /* Backlight PWM<A> (multiplexed pin) */
116 MX7D_PAD_GPIO1_IO08__GPIO1_IO8 | MUX_PAD_CTRL(NO_PAD_CTRL),
117 MX7D_PAD_ECSPI2_MOSI__GPIO4_IO21 | MUX_PAD_CTRL(NO_PAD_CTRL),
118};
119
120#define GPIO_BL_ON IMX_GPIO_NR(5, 1)
121#define GPIO_PWM_A IMX_GPIO_NR(1, 8)
122
123static int setup_lcd(void)
124{
Stefan Agner41f75bb2016-07-20 21:27:49 -0700125 imx_iomux_v3_setup_multiple_pads(backlight_pads, ARRAY_SIZE(backlight_pads));
126
127 /* Set BL_ON */
128 gpio_request(GPIO_BL_ON, "BL_ON");
129 gpio_direction_output(GPIO_BL_ON, 1);
130
131 /* Set PWM<A> to full brightness (assuming inversed polarity) */
132 gpio_request(GPIO_PWM_A, "PWM<A>");
133 gpio_direction_output(GPIO_PWM_A, 0);
134
135 return 0;
136}
137#endif
138
Gerard Salvatella108d7392018-11-19 15:54:10 +0100139/*
140 * Backlight off before OS handover
141 */
142void board_preboot_os(void)
143{
Simon Glass52cb5042022-10-18 07:46:31 -0600144#ifdef CONFIG_VIDEO
Gerard Salvatella108d7392018-11-19 15:54:10 +0100145 gpio_direction_output(GPIO_PWM_A, 1);
146 gpio_direction_output(GPIO_BL_ON, 0);
Igor Opaniukefe398f2020-09-14 11:01:07 +0300147#endif
Gerard Salvatella108d7392018-11-19 15:54:10 +0100148}
149
Stefan Agner41f75bb2016-07-20 21:27:49 -0700150static void setup_iomux_uart(void)
151{
152 imx_iomux_v3_setup_multiple_pads(uart1_pads, ARRAY_SIZE(uart1_pads));
153}
154
Stefan Agner41f75bb2016-07-20 21:27:49 -0700155#ifdef CONFIG_FEC_MXC
Stefan Agner41f75bb2016-07-20 21:27:49 -0700156static int setup_fec(void)
157{
158 struct iomuxc_gpr_base_regs *const iomuxc_gpr_regs
159 = (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
160
161#ifndef CONFIG_COLIBRI_IMX7_EXT_PHYCLK
162 /*
163 * Use 50M anatop REF_CLK1 for ENET1, clear gpr1[13], set gpr1[17]
164 * and output it on the pin
165 */
166 clrsetbits_le32(&iomuxc_gpr_regs->gpr[1],
167 IOMUXC_GPR_GPR1_GPR_ENET1_TX_CLK_SEL_MASK,
168 IOMUXC_GPR_GPR1_GPR_ENET1_CLK_DIR_MASK);
169#else
170 /* Use 50M external CLK for ENET1, set gpr1[13], clear gpr1[17] */
171 clrsetbits_le32(&iomuxc_gpr_regs->gpr[1],
172 IOMUXC_GPR_GPR1_GPR_ENET1_CLK_DIR_MASK,
173 IOMUXC_GPR_GPR1_GPR_ENET1_TX_CLK_SEL_MASK);
174#endif
175
Eric Nelsoneadd7322017-08-31 08:34:23 -0700176 return set_clk_enet(ENET_50MHZ);
Stefan Agner41f75bb2016-07-20 21:27:49 -0700177}
178
Stefan Agner41f75bb2016-07-20 21:27:49 -0700179#endif
180
181int board_early_init_f(void)
182{
183 setup_iomux_uart();
184
Stefan Agner41f75bb2016-07-20 21:27:49 -0700185 return 0;
186}
187
188int board_init(void)
189{
190 /* address of boot parameters */
191 gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
192
Hiago De Francoe7438bd2023-10-02 08:57:49 -0300193 /*
Marcel Ziswiler41ff9602023-12-12 08:28:15 -0300194 * Enable GPIO SION on NAND_WE_B/eMMC_RST with 100k pull-down. eMMC_RST
Hiago De Francoe7438bd2023-10-02 08:57:49 -0300195 * is pulled high with 4.7k for eMMC devices. This allows to reliably
Marcel Ziswiler41ff9602023-12-12 08:28:15 -0300196 * detect eMMC vs NAND flash.
Hiago De Francoe7438bd2023-10-02 08:57:49 -0300197 */
198 imx_iomux_v3_setup_multiple_pads(flash_detection_pads, ARRAY_SIZE(flash_detection_pads));
199 gpio_request(FLASH_DET_GPIO, "flash-detection-gpio");
200 is_emmc = gpio_get_value(FLASH_DET_GPIO);
201 gpio_free(FLASH_DET_GPIO);
202
Stefan Agner41f75bb2016-07-20 21:27:49 -0700203#ifdef CONFIG_FEC_MXC
204 setup_fec();
205#endif
206
Stefan Agnercbd59fe2018-08-06 09:19:19 +0200207#ifdef CONFIG_TARGET_COLIBRI_IMX7_NAND
Stefan Agner41f75bb2016-07-20 21:27:49 -0700208 setup_gpmi_nand();
209#endif
210
Stefan Agner443166e2017-03-09 17:17:52 -0800211#ifdef CONFIG_USB_EHCI_MX7
212 imx_iomux_v3_setup_multiple_pads(usb_cdet_pads, ARRAY_SIZE(usb_cdet_pads));
213 gpio_request(USB_CDET_GPIO, "usb-cdet-gpio");
214#endif
215
Stefan Agner41f75bb2016-07-20 21:27:49 -0700216 return 0;
217}
218
Stefan Agnere65377a2016-10-05 15:27:11 -0700219#ifdef CONFIG_DM_PMIC
220int power_init_board(void)
221{
222 struct udevice *dev;
223 int reg, ver;
224 int ret;
225
Marcel Ziswiler23b65be2022-07-21 15:27:35 +0200226 ret = pmic_get("pmic@33", &dev);
Stefan Agnere65377a2016-10-05 15:27:11 -0700227 if (ret)
228 return ret;
229 ver = pmic_reg_read(dev, RN5T567_LSIVER);
230 reg = pmic_reg_read(dev, RN5T567_OTPVER);
231
232 printf("PMIC: RN5T567 LSIVER=0x%02x OTPVER=0x%02x\n", ver, reg);
233
234 /* set judge and press timer of N_OE to minimal values */
235 pmic_clrsetbits(dev, RN5T567_NOETIMSETCNT, 0x7, 0);
236
Stefan Agner0f2c5ad2017-03-09 17:17:53 -0800237 /* configure sleep slot for 3.3V Ethernet */
238 reg = pmic_reg_read(dev, RN5T567_LDO1_SLOT);
239 reg = (reg & 0xf0) | reg >> 4;
240 pmic_reg_write(dev, RN5T567_LDO1_SLOT, reg);
241
242 /* disable DCDC2 discharge to avoid backfeeding through VFB2 */
243 pmic_clrsetbits(dev, RN5T567_DC2CTL, 0x2, 0);
244
245 /* configure sleep slot for ARM rail */
246 reg = pmic_reg_read(dev, RN5T567_DC2_SLOT);
247 reg = (reg & 0xf0) | reg >> 4;
248 pmic_reg_write(dev, RN5T567_DC2_SLOT, reg);
249
250 /* disable LDO2 discharge to avoid backfeeding from +V3.3_SD */
251 pmic_clrsetbits(dev, RN5T567_LDODIS1, 0x2, 0);
252
Stefan Agnere65377a2016-10-05 15:27:11 -0700253 return 0;
254}
255
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100256void reset_cpu(void)
Stefan Agnere65377a2016-10-05 15:27:11 -0700257{
258 struct udevice *dev;
259
Marcel Ziswiler23b65be2022-07-21 15:27:35 +0200260 pmic_get("pmic@33", &dev);
Stefan Agnere65377a2016-10-05 15:27:11 -0700261
262 /* Use PMIC to reset, set REPWRTIM to 0 and REPWRON to 1 */
263 pmic_reg_write(dev, RN5T567_REPCNT, 0x1);
264 pmic_reg_write(dev, RN5T567_SLPCNT, 0x1);
265
266 /*
267 * Re-power factor detection on PMIC side is not instant. 1ms
268 * proved to be enough time until reset takes effect.
269 */
270 mdelay(1);
271}
272#endif
273
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800274#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900275int ft_board_setup(void *blob, struct bd_info *bd)
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800276{
Igor Opaniukcbee9452019-12-03 14:04:47 +0200277#if defined(CONFIG_IMX_BOOTAUX) && defined(CONFIG_ARCH_FIXUP_FDT_MEMORY)
278 int up;
279
280 up = arch_auxiliary_core_check_up(0);
281 if (up) {
282 int ret;
283 int areas = 1;
284 u64 start[2], size[2];
285
286 /*
287 * Reserve 1MB of memory for M4 (1MiB is also the minimum
288 * alignment for Linux due to MMU section size restrictions).
289 */
290 start[0] = gd->bd->bi_dram[0].start;
291 size[0] = SZ_256M - SZ_1M;
292
293 /* If needed, create a second entry for memory beyond 256M */
294 if (gd->bd->bi_dram[0].size > SZ_256M) {
295 start[1] = gd->bd->bi_dram[0].start + SZ_256M;
296 size[1] = gd->bd->bi_dram[0].size - SZ_256M;
297 areas = 2;
298 }
299
300 ret = fdt_set_usable_memory(blob, start, size, areas);
301 if (ret) {
302 eprintf("Cannot set usable memory\n");
303 return ret;
304 }
305 } else {
306 int off;
307
308 off = fdt_node_offset_by_compatible(blob, -1,
309 "fsl,imx7d-rpmsg");
310 if (off > 0)
311 fdt_status_disabled(blob, off);
312 }
313#endif
Stefan Agner6a667482017-03-09 17:17:54 -0800314
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800315 return ft_common_board_setup(blob, bd);
316}
317#endif
318
Stefan Agner41f75bb2016-07-20 21:27:49 -0700319#ifdef CONFIG_USB_EHCI_MX7
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200320int board_fix_fdt(void *rw_fdt_blob)
Stefan Agner41f75bb2016-07-20 21:27:49 -0700321{
Fabio Estevamde951e22023-07-04 14:09:45 -0300322 int ret;
323
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200324 /* i.MX 7Solo has only one single USB OTG1 but no USB host port */
325 if (is_cpu_type(MXC_CPU_MX7S)) {
326 int offset = fdt_path_offset(rw_fdt_blob, "/soc/bus@30800000/usb@30b20000");
Stefan Agner41f75bb2016-07-20 21:27:49 -0700327
Fabio Estevamde951e22023-07-04 14:09:45 -0300328 /*
329 * We're changing from status = "okay" to status = "disabled".
330 * In this case we'll need more space, so increase the size
331 * a little bit.
332 */
333 ret = fdt_increase_size(rw_fdt_blob, 32);
334 if (ret < 0) {
335 printf("Cannot increase FDT size: %d\n", ret);
336 return ret;
337 }
338
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200339 return fdt_status_disabled(rw_fdt_blob, offset);
Stefan Agner41f75bb2016-07-20 21:27:49 -0700340 }
Stefan Agner443166e2017-03-09 17:17:52 -0800341
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200342 return 0;
Stefan Agner443166e2017-03-09 17:17:52 -0800343}
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300344
Stefan Agner8fa31872021-07-23 09:39:45 +0300345#if defined(CONFIG_BOARD_LATE_INIT)
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300346int board_late_init(void)
347{
Simon Glass52cb5042022-10-18 07:46:31 -0600348#if defined(CONFIG_VIDEO)
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300349 setup_lcd();
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300350#endif
Stefan Agner8fa31872021-07-23 09:39:45 +0300351
Hiago De Francoc3615822023-11-09 13:24:01 -0300352 if (IS_ENABLED(CONFIG_USB) && is_boot_from_usb()) {
Stefan Agner8fa31872021-07-23 09:39:45 +0300353 env_set("bootdelay", "0");
Hiago De Francoc3615822023-11-09 13:24:01 -0300354 if (IS_ENABLED(CONFIG_CMD_USB_SDP)) {
355 printf("Serial Downloader recovery mode, using sdp command\n");
356 env_set("bootcmd", "sdp 0");
357 } else if (IS_ENABLED(CONFIG_CMD_FASTBOOT)) {
358 printf("Fastboot recovery mode, using fastboot command\n");
359 env_set("bootcmd", "fastboot usb 0");
360 }
Stefan Agner8fa31872021-07-23 09:39:45 +0300361 }
Hiago De Francoc3615822023-11-09 13:24:01 -0300362
Hiago De Francoe7438bd2023-10-02 08:57:49 -0300363 if (is_emmc)
364 env_set("variant", "-emmc");
365 else
366 env_set("variant", "");
367
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300368 return 0;
369}
Stefan Agner8fa31872021-07-23 09:39:45 +0300370#endif /* CONFIG_BOARD_LATE_INIT */
Igor Opaniuk0d649c92020-07-15 13:31:05 +0300371
Marcel Ziswiler5ac1abf2022-04-13 11:33:33 +0200372#endif /* CONFIG_USB_EHCI_MX7 */