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