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