blob: dc7fc2020490435be9512be6dd9e091554913973 [file] [log] [blame]
Fabio Estevam95736c32024-09-13 21:56:05 -03001// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright (C) 2017 Stefano Babic <sbabic@denx.de>
4// Copyright (C) 2024 Fabio Estevam <festevam@denx.de>
5
6#include <asm/io.h>
7#include <asm/arch/clock.h>
8#include <asm/arch/imx-regs.h>
9#include <asm/arch/crm_regs.h>
10#include <asm/arch/mx6-ddr.h>
11#include <asm/arch/iomux.h>
12#include <asm/arch/mx6-pins.h>
13#include <asm/mach-imx/iomux-v3.h>
14#include <asm/mach-imx/boot_mode.h>
15
16#include <asm/mach-imx/spi.h>
17#include <linux/errno.h>
18#include <asm/gpio.h>
19#include <nand.h>
20#include <miiphy.h>
21#include <netdev.h>
22#include <asm/arch/sys_proto.h>
23#include <asm/sections.h>
24#include <linux/delay.h>
25
26#include <image.h>
27#include <init.h>
28#include <serial.h>
29#include <spl.h>
30#include <linux/sizes.h>
31#include <mmc.h>
32#include <fsl_esdhc_imx.h>
33
34DECLARE_GLOBAL_DATA_PTR;
35
36#define UART_PAD_CTRL (PAD_CTL_PUS_100K_UP | \
37 PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm | \
38 PAD_CTL_SRE_FAST | PAD_CTL_HYS)
39
40#define SPI_PAD_CTRL (PAD_CTL_HYS | PAD_CTL_SPEED_MED | \
41 PAD_CTL_DSE_40ohm | PAD_CTL_SRE_FAST)
42
43#define NAND_PAD_CTRL (PAD_CTL_PUS_100K_UP | PAD_CTL_SPEED_MED | \
44 PAD_CTL_DSE_40ohm | PAD_CTL_SRE_FAST)
45
46int dram_init(void)
47{
48 gd->ram_size = imx_ddr_size();
49 return 0;
50}
51
52static const iomux_v3_cfg_t uart4_pads[] = {
53 MX6_PAD_KEY_COL0__UART4_TX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL),
54 MX6_PAD_KEY_ROW0__UART4_RX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL),
55};
56
57static void setup_iomux_uart(void)
58{
59 imx_iomux_v3_setup_multiple_pads(uart4_pads, ARRAY_SIZE(uart4_pads));
60}
61
62static void setup_gpmi_nand(void)
63{
64 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
65
66 /* gate ENFC_CLK_ROOT clock first,before clk source switch */
67 clrbits_le32(&mxc_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
68
69 /* config gpmi and bch clock to 100 MHz */
70 clrsetbits_le32(&mxc_ccm->cs2cdr,
71 MXC_CCM_CS2CDR_ENFC_CLK_PODF_MASK |
72 MXC_CCM_CS2CDR_ENFC_CLK_PRED_MASK |
73 MXC_CCM_CS2CDR_ENFC_CLK_SEL_MASK,
74 MXC_CCM_CS2CDR_ENFC_CLK_PODF(0) |
75 MXC_CCM_CS2CDR_ENFC_CLK_PRED(3) |
76 MXC_CCM_CS2CDR_ENFC_CLK_SEL(3));
77
78 /* enable ENFC_CLK_ROOT clock */
79 setbits_le32(&mxc_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
80
81 /* enable gpmi and bch clock gating */
82 setbits_le32(&mxc_ccm->CCGR4,
83 MXC_CCM_CCGR4_RAWNAND_U_BCH_INPUT_APB_MASK |
84 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_BCH_MASK |
85 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK |
86 MXC_CCM_CCGR4_RAWNAND_U_GPMI_INPUT_APB_MASK |
87 MXC_CCM_CCGR4_PL301_MX6QPER1_BCH_OFFSET);
88
89 /* enable apbh clock gating */
90 setbits_le32(&mxc_ccm->CCGR0, MXC_CCM_CCGR0_APBHDMA_MASK);
91}
92
93int board_spi_cs_gpio(unsigned int bus, unsigned int cs)
94{
95 return IMX_GPIO_NR(4, 24);
96}
97
98int board_early_init_f(void)
99{
100 setup_iomux_uart();
101
102 return 0;
103}
104
105int board_init(void)
106{
107 /* address of boot parameters */
108 gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
109
110 setup_gpmi_nand();
111
112 return 0;
113}
114
115/*
116 * BOOT_CFG1, BOOT_CFG2, BOOT_CFG3, BOOT_CFG4
117 * see Table 8-11 and Table 5-9
118 * BOOT_CFG1[7] = 1 (boot from NAND)
119 * BOOT_CFG1[5] = 0 - raw NAND
120 * BOOT_CFG1[4] = 0 - default pad settings
121 * BOOT_CFG1[3:2] = 00 - devices = 1
122 * BOOT_CFG1[1:0] = 00 - Row Address Cycles = 3
123 * BOOT_CFG2[4:3] = 00 - Boot Search Count = 2
124 * BOOT_CFG2[2:1] = 01 - Pages In Block = 64
125 * BOOT_CFG2[0] = 0 - Reset time 12ms
126 */
127static const struct boot_mode board_boot_modes[] = {
128 /* NAND: 64pages per block, 3 row addr cycles, 2 copies of FCB/DBBT */
129 {"nand", MAKE_CFGVAL(0x80, 0x02, 0x00, 0x00)},
130 {"mmc0", MAKE_CFGVAL(0x40, 0x20, 0x00, 0x00)},
131 {NULL, 0},
132};
133
134int board_late_init(void)
135{
136 add_board_boot_modes(board_boot_modes);
137
138 return 0;
139}
140
Simon Glass49c24a82024-09-29 19:49:47 -0600141#ifdef CONFIG_XPL_BUILD
Fabio Estevam95736c32024-09-13 21:56:05 -0300142#include <spl.h>
143
144#define MX6_PHYFLEX_ERR006282 IMX_GPIO_NR(2, 11)
145static void phyflex_err006282_workaround(void)
146{
147 /*
148 * Boards beginning with 1362.2 have the SD4_DAT3 pin connected
149 * to the CMIC. If this pin isn't toggled within 10s the boards
150 * reset. The pin is unconnected on older boards, so we do not
151 * need a check for older boards before applying this fixup.
152 */
153
154 gpio_request(MX6_PHYFLEX_ERR006282, "errata_gpio");
155 gpio_direction_output(MX6_PHYFLEX_ERR006282, 0);
156 mdelay(2);
157 gpio_direction_output(MX6_PHYFLEX_ERR006282, 1);
158 mdelay(2);
159 gpio_set_value(MX6_PHYFLEX_ERR006282, 0);
160
161 imx_iomux_v3_setup_pad(MX6_PAD_SD4_DAT3__GPIO2_IO11);
162
163 gpio_direction_input(MX6_PHYFLEX_ERR006282);
164}
165
166static const iomux_v3_cfg_t gpios_pads[] = {
167 MX6_PAD_SD4_DAT3__GPIO2_IO11 | MUX_PAD_CTRL(NO_PAD_CTRL),
168 MX6_PAD_SD4_DAT4__GPIO2_IO12 | MUX_PAD_CTRL(NO_PAD_CTRL),
169 MX6_PAD_SD4_DAT5__GPIO2_IO13 | MUX_PAD_CTRL(NO_PAD_CTRL),
170 MX6_PAD_SD4_DAT6__GPIO2_IO14 | MUX_PAD_CTRL(NO_PAD_CTRL),
171 MX6_PAD_SD4_DAT7__GPIO2_IO15 | MUX_PAD_CTRL(NO_PAD_CTRL),
172 MX6_PAD_EIM_EB3__GPIO2_IO31 | MUX_PAD_CTRL(NO_PAD_CTRL),
173 MX6_PAD_ENET_TXD0__GPIO1_IO30 | MUX_PAD_CTRL(NO_PAD_CTRL),
174};
175
176static void setup_gpios(void)
177{
178 imx_iomux_v3_setup_multiple_pads(gpios_pads, ARRAY_SIZE(gpios_pads));
179}
180
181static const struct mx6dq_iomux_ddr_regs mx6_ddr_ioregs = {
182 .dram_sdclk_0 = 0x00000030,
183 .dram_sdclk_1 = 0x00000030,
184 .dram_cas = 0x00000030,
185 .dram_ras = 0x00000030,
186 .dram_reset = 0x00000030,
187 .dram_sdcke0 = 0x00003000,
188 .dram_sdcke1 = 0x00003000,
189 .dram_sdba2 = 0x00000030,
190 .dram_sdodt0 = 0x00000030,
191 .dram_sdodt1 = 0x00000030,
192
193 .dram_sdqs0 = 0x00000028,
194 .dram_sdqs1 = 0x00000028,
195 .dram_sdqs2 = 0x00000028,
196 .dram_sdqs3 = 0x00000028,
197 .dram_sdqs4 = 0x00000028,
198 .dram_sdqs5 = 0x00000028,
199 .dram_sdqs6 = 0x00000028,
200 .dram_sdqs7 = 0x00000028,
201 .dram_dqm0 = 0x00000028,
202 .dram_dqm1 = 0x00000028,
203 .dram_dqm2 = 0x00000028,
204 .dram_dqm3 = 0x00000028,
205 .dram_dqm4 = 0x00000028,
206 .dram_dqm5 = 0x00000028,
207 .dram_dqm6 = 0x00000028,
208 .dram_dqm7 = 0x00000028,
209};
210
211static const struct mx6dq_iomux_grp_regs mx6_grp_ioregs = {
212 .grp_ddr_type = 0x000C0000,
213 .grp_ddrmode_ctl = 0x00020000,
214 .grp_ddrpke = 0x00000000,
215 .grp_addds = 0x30,
216 .grp_ctlds = 0x30,
217 .grp_ddrmode = 0x00020000,
218 .grp_b0ds = 0x00000028,
219 .grp_b1ds = 0x00000028,
220 .grp_b2ds = 0x00000028,
221 .grp_b3ds = 0x00000028,
222 .grp_b4ds = 0x00000028,
223 .grp_b5ds = 0x00000028,
224 .grp_b6ds = 0x00000028,
225 .grp_b7ds = 0x00000028,
226};
227
228static const struct mx6_mmdc_calibration mx6_mmcd_calib = {
229 .p0_mpwldectrl0 = 0x00170018,
230 .p0_mpwldectrl1 = 0x003B0039,
231 .p1_mpwldectrl0 = 0x00350048,
232 .p1_mpwldectrl1 = 0x00410052,
233 .p0_mpdgctrl0 = 0x03600374,
234 .p0_mpdgctrl1 = 0x03680360,
235 .p1_mpdgctrl0 = 0x0370037C,
236 .p1_mpdgctrl1 = 0x03700350,
237 .p0_mprddlctl = 0x3A363234,
238 .p1_mprddlctl = 0x3634363C,
239 .p0_mpwrdlctl = 0x38383E3C,
240 .p1_mpwrdlctl = 0x422A483C,
241};
242
243/* MT41K64M16JT-125 (1Gb density) */
244static struct mx6_ddr3_cfg mem_ddr = {
245 .mem_speed = 1600,
246 .density = 1,
247 .width = 16,
248 .banks = 8,
249 .rowaddr = 13,
250 .coladdr = 10,
251 .pagesz = 2,
252 .trcd = 1375,
253 .trcmin = 4875,
254 .trasmin = 3500,
255 .SRT = 1,
256};
257
258static void ccgr_init(void)
259{
260 struct mxc_ccm_reg *ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
261
262 writel(0x00C03F3F, &ccm->CCGR0);
263 writel(0x0030FC03, &ccm->CCGR1);
264 writel(0x0FFFC000, &ccm->CCGR2);
265 writel(0x3FF00000, &ccm->CCGR3);
266 writel(0x00FFF300, &ccm->CCGR4);
267 writel(0x0F0000C3, &ccm->CCGR5);
268 writel(0x000003FF, &ccm->CCGR6);
269}
270
271static void spl_dram_init(void)
272{
273 struct mx6_ddr_sysinfo sysinfo = {
274 .dsize = 2,
275 .cs_density = 6,
276 .ncs = 2,
277 .cs1_mirror = 1,
278 .rtt_wr = 1,
279 .rtt_nom = 1,
280 .walat = 1,
281 .ralat = 5,
282 .mif3_mode = 3,
283 .bi_on = 1,
284 .sde_to_rst = 0x10,
285 .rst_to_cke = 0x23,
286 .ddr_type = DDR_TYPE_DDR3,
287 .refsel = 1,
288 .refr = 7,
289 };
290
291 mx6dq_dram_iocfg(64, &mx6_ddr_ioregs, &mx6_grp_ioregs);
292 mx6_dram_cfg(&sysinfo, &mx6_mmcd_calib, &mem_ddr);
293}
294
295#define USDHC_PAD_CTRL (PAD_CTL_PUS_47K_UP | \
296 PAD_CTL_SPEED_LOW | PAD_CTL_DSE_80ohm | \
297 PAD_CTL_SRE_FAST | PAD_CTL_HYS)
298
299struct fsl_esdhc_cfg usdhc_cfg[1] = {
300 {USDHC3_BASE_ADDR},
301};
302
303static const iomux_v3_cfg_t usdhc3_pads[] = {
304 MX6_PAD_SD3_CLK__SD3_CLK | MUX_PAD_CTRL(USDHC_PAD_CTRL),
305 MX6_PAD_SD3_CMD__SD3_CMD | MUX_PAD_CTRL(USDHC_PAD_CTRL),
306 MX6_PAD_SD3_DAT0__SD3_DATA0 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
307 MX6_PAD_SD3_DAT1__SD3_DATA1 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
308 MX6_PAD_SD3_DAT2__SD3_DATA2 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
309 MX6_PAD_SD3_DAT3__SD3_DATA3 | MUX_PAD_CTRL(USDHC_PAD_CTRL),
310};
311
312int board_mmc_init(struct bd_info *bis)
313{
314 imx_iomux_v3_setup_multiple_pads(usdhc3_pads, ARRAY_SIZE(usdhc3_pads));
315 usdhc_cfg[0].esdhc_base = USDHC3_BASE_ADDR;
316 usdhc_cfg[0].sdhc_clk = mxc_get_clock(MXC_ESDHC3_CLK);
317 usdhc_cfg[0].max_bus_width = 4;
318 gd->arch.sdhc_clk = usdhc_cfg[0].sdhc_clk;
319
320 return fsl_esdhc_initialize(bis, &usdhc_cfg[0]);
321}
322
323void board_boot_order(u32 *spl_boot_list)
324{
325 spl_boot_list[0] = spl_boot_device();
326
327 switch (spl_boot_list[0]) {
328 case BOOT_DEVICE_SPI:
329 spl_boot_list[1] = BOOT_DEVICE_UART;
330 break;
331 case BOOT_DEVICE_MMC1:
332 spl_boot_list[1] = BOOT_DEVICE_SPI;
333 spl_boot_list[2] = BOOT_DEVICE_UART;
334 break;
335 default:
336 printf("Boot device %x\n", spl_boot_list[0]);
337 }
338}
339
340static const iomux_v3_cfg_t ecspi3_pads[] = {
341 MX6_PAD_DISP0_DAT0__ECSPI3_SCLK | MUX_PAD_CTRL(SPI_PAD_CTRL),
342 MX6_PAD_DISP0_DAT1__ECSPI3_MOSI | MUX_PAD_CTRL(SPI_PAD_CTRL),
343 MX6_PAD_DISP0_DAT2__ECSPI3_MISO | MUX_PAD_CTRL(SPI_PAD_CTRL),
344 MX6_PAD_DISP0_DAT3__GPIO4_IO24 | MUX_PAD_CTRL(NO_PAD_CTRL),
345};
346
347static void setup_spi(void)
348{
349 imx_iomux_v3_setup_multiple_pads(ecspi3_pads, ARRAY_SIZE(ecspi3_pads));
350
351 enable_spi_clk(true, 2);
352}
353
354void board_init_f(ulong dummy)
355{
356 /* setup clock gating */
357 ccgr_init();
358
359 /* setup AIPS and disable watchdog */
360 arch_cpu_init();
361
362 /* setup AXI */
363 gpr_init();
364
365 board_early_init_f();
366
367 /* setup GP timer */
368 timer_init();
369
370 /* UART clocks enabled and gd valid - init serial console */
371 preloader_console_init();
372
373 setup_spi();
374
375 setup_gpios();
376
377 /* DDR initialization */
378 spl_dram_init();
379
380 /* Clear the BSS. */
381 memset(__bss_start, 0, __bss_end - __bss_start);
382
383 phyflex_err006282_workaround();
384
385 /* load/boot image from boot device */
386 board_init_r(NULL, 0);
387}
388#endif