blob: 1588f17821b7afc3360d9ef135a915acdee1c4e4 [file] [log] [blame]
Marek Vasutb52fb0b2020-04-29 20:09:08 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018 NXP
4 */
5
6#include <common.h>
7#include <hang.h>
8#include <asm/arch/clock.h>
9#include <asm/arch/ddr.h>
10#include <asm/arch/imx8mq_pins.h>
11#include <asm/arch/sys_proto.h>
12#include <asm/io.h>
13#include <asm/mach-imx/gpio.h>
14#include <asm/mach-imx/iomux-v3.h>
15#include <asm/mach-imx/mxc_i2c.h>
16#include <errno.h>
17#include <fsl_esdhc_imx.h>
18#include <mmc.h>
19#include <spl.h>
20
21#include "lpddr4_timing.h"
22
23DECLARE_GLOBAL_DATA_PTR;
24
25#define DDR_DET_1 IMX_GPIO_NR(3, 11)
26#define DDR_DET_2 IMX_GPIO_NR(3, 12)
27#define DDR_DET_3 IMX_GPIO_NR(3, 13)
28
29static iomux_v3_cfg_t const verdet_pads[] = {
30 IMX8MQ_PAD_NAND_DATA01__GPIO3_IO7 | MUX_PAD_CTRL(NO_PAD_CTRL),
31 IMX8MQ_PAD_NAND_DATA02__GPIO3_IO8 | MUX_PAD_CTRL(NO_PAD_CTRL),
32 IMX8MQ_PAD_NAND_DATA03__GPIO3_IO9 | MUX_PAD_CTRL(NO_PAD_CTRL),
33 IMX8MQ_PAD_NAND_DATA04__GPIO3_IO10 | MUX_PAD_CTRL(NO_PAD_CTRL),
34 IMX8MQ_PAD_NAND_DATA05__GPIO3_IO11 | MUX_PAD_CTRL(NO_PAD_CTRL),
35 IMX8MQ_PAD_NAND_DATA06__GPIO3_IO12 | MUX_PAD_CTRL(NO_PAD_CTRL),
36 IMX8MQ_PAD_NAND_DATA07__GPIO3_IO13 | MUX_PAD_CTRL(NO_PAD_CTRL),
37};
38
39/*
40 * DDR_DET_1 DDR_DET_2 DDR_DET_3
41 * 0 0 1 4G LPDDR4
42 * 1 1 1 3G LPDDR4
43 * 1 1 0 2G LPDDR4
44 * 1 0 1 1G LPDDR4
45 */
46static void spl_dram_init(void)
47{
48 struct dram_timing_info *dram_timing;
49 u8 ddr = 0, size;
50
51 imx_iomux_v3_setup_multiple_pads(verdet_pads, ARRAY_SIZE(verdet_pads));
52
53 gpio_request(DDR_DET_1, "ddr_det_1");
54 gpio_direction_input(DDR_DET_1);
55 gpio_request(DDR_DET_2, "ddr_det_2");
56 gpio_direction_input(DDR_DET_2);
57 gpio_request(DDR_DET_3, "ddr_det_3");
58 gpio_direction_input(DDR_DET_3);
59
60 ddr |= !!gpio_get_value(DDR_DET_3) << 0;
61 ddr |= !!gpio_get_value(DDR_DET_2) << 1;
62 ddr |= !!gpio_get_value(DDR_DET_1) << 2;
63
64 switch (ddr) {
65 case 0x1:
66 size = 4;
67 dram_timing = &dram_timing_4gb;
68 break;
69 case 0x7:
70 size = 3;
71 dram_timing = &dram_timing_3gb;
72 break;
73 case 0x6:
74 size = 2;
75 dram_timing = &dram_timing_2gb;
76 break;
77 case 0x5:
78 size = 1;
79 dram_timing = &dram_timing_1gb;
80 break;
81 default:
82 puts("Unknown DDR type!!!\n");
83 return;
84 }
85
86 printf("%s: LPDDR4 %d GiB\n", __func__, size);
87 ddr_init(dram_timing);
88 writel(size, M4_BOOTROM_BASE_ADDR);
89}
90
91#define USDHC2_CD_GPIO IMX_GPIO_NR(2, 12)
92#define USDHC1_PWR_GPIO IMX_GPIO_NR(2, 10)
93#define USDHC2_PWR_GPIO IMX_GPIO_NR(2, 19)
94
95int board_mmc_getcd(struct mmc *mmc)
96{
97 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
98 int ret = 0;
99
100 switch (cfg->esdhc_base) {
101 case USDHC1_BASE_ADDR:
102 ret = 1;
103 break;
104 case USDHC2_BASE_ADDR:
105 ret = !gpio_get_value(USDHC2_CD_GPIO);
106 return ret;
107 }
108
109 return 1;
110}
111
112#define USDHC_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_HYS | PAD_CTL_PUE | \
113 PAD_CTL_FSEL2)
114#define USDHC_GPIO_PAD_CTRL (PAD_CTL_PUE | PAD_CTL_DSE1)
115
116static iomux_v3_cfg_t const usdhc1_pads[] = {
117 IMX8MQ_PAD_SD1_CLK__USDHC1_CLK | MUX_PAD_CTRL(USDHC_PAD_CTRL),
118 IMX8MQ_PAD_SD1_CMD__USDHC1_CMD | MUX_PAD_CTRL(USDHC_PAD_CTRL),
119 IMX8MQ_PAD_SD1_DATA0__USDHC1_DATA0 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
120 IMX8MQ_PAD_SD1_DATA1__USDHC1_DATA1 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
121 IMX8MQ_PAD_SD1_DATA2__USDHC1_DATA2 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
122 IMX8MQ_PAD_SD1_DATA3__USDHC1_DATA3 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
123 IMX8MQ_PAD_SD1_DATA4__USDHC1_DATA4 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
124 IMX8MQ_PAD_SD1_DATA5__USDHC1_DATA5 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
125 IMX8MQ_PAD_SD1_DATA6__USDHC1_DATA6 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
126 IMX8MQ_PAD_SD1_DATA7__USDHC1_DATA7 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
127 IMX8MQ_PAD_SD1_RESET_B__GPIO2_IO10 | MUX_PAD_CTRL(NO_PAD_CTRL),
128};
129
130static iomux_v3_cfg_t const usdhc2_pads[] = {
131 IMX8MQ_PAD_SD2_CLK__USDHC2_CLK | MUX_PAD_CTRL(USDHC_PAD_CTRL),
132 IMX8MQ_PAD_SD2_CMD__USDHC2_CMD | MUX_PAD_CTRL(USDHC_PAD_CTRL),
133 IMX8MQ_PAD_SD2_DATA0__USDHC2_DATA0 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
134 IMX8MQ_PAD_SD2_DATA1__USDHC2_DATA1 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
135 IMX8MQ_PAD_SD2_DATA2__USDHC2_DATA2 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
136 IMX8MQ_PAD_SD2_DATA3__USDHC2_DATA3 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
137 IMX8MQ_PAD_SD2_CD_B__GPIO2_IO12 | MUX_PAD_CTRL(USDHC_GPIO_PAD_CTRL),
138 IMX8MQ_PAD_SD2_RESET_B__GPIO2_IO19 | MUX_PAD_CTRL(USDHC_GPIO_PAD_CTRL),
139};
140
141static struct fsl_esdhc_cfg usdhc_cfg[2] = {
142 {USDHC1_BASE_ADDR, 0, 8},
143 {USDHC2_BASE_ADDR, 0, 4},
144};
145
146int board_mmc_init(bd_t *bis)
147{
148 int ret;
149 /*
150 * According to the board_mmc_init() the following map is done:
151 * (U-Boot device node) (Physical Port)
152 * mmc0 USDHC1
153 * mmc1 USDHC2
154 */
155 init_clk_usdhc(0);
156 usdhc_cfg[0].sdhc_clk = mxc_get_clock(USDHC1_CLK_ROOT);
157 imx_iomux_v3_setup_multiple_pads(usdhc1_pads, ARRAY_SIZE(usdhc1_pads));
158 gpio_request(USDHC1_PWR_GPIO, "usdhc1_reset");
159 gpio_direction_output(USDHC1_PWR_GPIO, 0);
160 udelay(500);
161 gpio_direction_output(USDHC1_PWR_GPIO, 1);
162 ret = fsl_esdhc_initialize(bis, &usdhc_cfg[0]);
163 if (ret)
164 return ret;
165
166 init_clk_usdhc(1);
167 usdhc_cfg[1].sdhc_clk = mxc_get_clock(USDHC2_CLK_ROOT);
168 imx_iomux_v3_setup_multiple_pads(usdhc2_pads, ARRAY_SIZE(usdhc2_pads));
169 gpio_request(USDHC2_PWR_GPIO, "usdhc2_reset");
170 gpio_direction_output(USDHC2_PWR_GPIO, 0);
171 udelay(500);
172 gpio_direction_output(USDHC2_PWR_GPIO, 1);
173 return fsl_esdhc_initialize(bis, &usdhc_cfg[1]);
174}
175
176void spl_board_init(void)
177{
178 puts("Normal Boot\n");
179}
180
181#ifdef CONFIG_SPL_LOAD_FIT
182int board_fit_config_name_match(const char *name)
183{
184 /* Just empty function now - can't decide what to choose */
185 debug("%s: %s\n", __func__, name);
186
187 return 0;
188}
189#endif
190
191void board_init_f(ulong dummy)
192{
193 int ret;
194
195 /* Clear global data */
196 memset((void *)gd, 0, sizeof(gd_t));
197
198 arch_cpu_init();
199
200 init_uart_clk(0);
201
202 board_early_init_f();
203
204 timer_init();
205
206 preloader_console_init();
207
208 /* Clear the BSS. */
209 memset(__bss_start, 0, __bss_end - __bss_start);
210
211 ret = spl_init();
212 if (ret) {
213 debug("spl_init() failed: %d\n", ret);
214 hang();
215 }
216
217 enable_tzc380();
218
219 /* DDR initialization */
220 spl_dram_init();
221
222 board_init_r(NULL, 0);
223}