blob: f498fdf8070d05a18f648fe2a12b113c0d94b6f4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefano Babic99d51c52017-07-26 18:23:05 +02002/*
3 * Copyright (C) 2017 Stefano Babic <sbabic@denx.de>
Stefano Babic99d51c52017-07-26 18:23:05 +02004 */
5
6#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07007#include <cpu_func.h>
Stefano Babic99d51c52017-07-26 18:23:05 +02008#include <asm/io.h>
9#include <asm/arch/clock.h>
10#include <asm/arch/imx-regs.h>
11#include <asm/arch/iomux.h>
12#include <asm/arch/crm_regs.h>
13#include <asm/arch/iomux.h>
14#include <asm/arch/mx6-pins.h>
15#include <asm/mach-imx/iomux-v3.h>
16#include <asm/mach-imx/boot_mode.h>
17#include <asm/mach-imx/mxc_i2c.h>
18#include <asm/mach-imx/spi.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060019#include <env.h>
Stefano Babic99d51c52017-07-26 18:23:05 +020020#include <linux/errno.h>
21#include <asm/gpio.h>
22#include <mmc.h>
23#include <i2c.h>
Yangbo Lu73340382019-06-21 11:42:28 +080024#include <fsl_esdhc_imx.h>
Stefano Babic99d51c52017-07-26 18:23:05 +020025#include <nand.h>
26#include <miiphy.h>
27#include <netdev.h>
28#include <asm/arch/sys_proto.h>
29#include <asm/sections.h>
30
31DECLARE_GLOBAL_DATA_PTR;
32
33#define UART_PAD_CTRL (PAD_CTL_PUS_100K_UP | \
34 PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm | \
35 PAD_CTL_SRE_FAST | PAD_CTL_HYS)
36
37#define USDHC_PAD_CTRL (PAD_CTL_PUS_47K_UP | \
38 PAD_CTL_SPEED_LOW | PAD_CTL_DSE_80ohm | \
39 PAD_CTL_SRE_FAST | PAD_CTL_HYS)
40
41#define ENET_PAD_CTRL (PAD_CTL_PUS_100K_UP | \
42 PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm | PAD_CTL_HYS)
43
44#define SPI_PAD_CTRL (PAD_CTL_HYS | PAD_CTL_SPEED_MED | \
45 PAD_CTL_DSE_40ohm | PAD_CTL_SRE_FAST)
46
47#define I2C_PAD_CTRL (PAD_CTL_PUS_100K_UP | \
48 PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm | PAD_CTL_HYS | \
49 PAD_CTL_ODE | PAD_CTL_SRE_FAST)
50
51#define I2C_PAD MUX_PAD_CTRL(I2C_PAD_CTRL)
52
53#define ASRC_PAD_CTRL (PAD_CTL_HYS | PAD_CTL_PUS_100K_UP | \
54 PAD_CTL_DSE_40ohm | PAD_CTL_SRE_FAST)
55
56#define NAND_PAD_CTRL (PAD_CTL_PUS_100K_UP | PAD_CTL_SPEED_MED | \
57 PAD_CTL_DSE_40ohm | PAD_CTL_SRE_FAST)
58
59#define ENET_PHY_RESET_GPIO IMX_GPIO_NR(1, 14)
60#define USDHC2_CD_GPIO IMX_GPIO_NR(1, 4)
61#define GREEN_LED IMX_GPIO_NR(2, 31)
62#define RED_LED IMX_GPIO_NR(1, 30)
63#define IMX6Q_DRIVE_STRENGTH 0x30
64
65int dram_init(void)
66{
67 gd->ram_size = imx_ddr_size();
68 return 0;
69}
70
71static iomux_v3_cfg_t const uart4_pads[] = {
72 IOMUX_PADS(PAD_KEY_COL0__UART4_TX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL)),
73 IOMUX_PADS(PAD_KEY_ROW0__UART4_RX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL)),
74};
75
76static iomux_v3_cfg_t const enet_pads[] = {
77 IOMUX_PADS(PAD_ENET_MDIO__ENET_MDIO | MUX_PAD_CTRL(ENET_PAD_CTRL)),
78 IOMUX_PADS(PAD_ENET_MDC__ENET_MDC | MUX_PAD_CTRL(ENET_PAD_CTRL)),
79 IOMUX_PADS(PAD_RGMII_TXC__RGMII_TXC | MUX_PAD_CTRL(ENET_PAD_CTRL)),
80 IOMUX_PADS(PAD_RGMII_TD0__RGMII_TD0 | MUX_PAD_CTRL(ENET_PAD_CTRL)),
81 IOMUX_PADS(PAD_RGMII_TD1__RGMII_TD1 | MUX_PAD_CTRL(ENET_PAD_CTRL)),
82 IOMUX_PADS(PAD_RGMII_TD2__RGMII_TD2 | MUX_PAD_CTRL(ENET_PAD_CTRL)),
83 IOMUX_PADS(PAD_RGMII_TD3__RGMII_TD3 | MUX_PAD_CTRL(ENET_PAD_CTRL)),
84 IOMUX_PADS(PAD_RGMII_TX_CTL__RGMII_TX_CTL |
85 MUX_PAD_CTRL(ENET_PAD_CTRL)),
86 IOMUX_PADS(PAD_ENET_REF_CLK__ENET_TX_CLK |
87 MUX_PAD_CTRL(ENET_PAD_CTRL)),
88 IOMUX_PADS(PAD_RGMII_RXC__RGMII_RXC | MUX_PAD_CTRL(ENET_PAD_CTRL)),
89 IOMUX_PADS(PAD_RGMII_RD0__RGMII_RD0 | MUX_PAD_CTRL(ENET_PAD_CTRL)),
90 IOMUX_PADS(PAD_RGMII_RD1__RGMII_RD1 | MUX_PAD_CTRL(ENET_PAD_CTRL)),
91 IOMUX_PADS(PAD_RGMII_RD2__RGMII_RD2 | MUX_PAD_CTRL(ENET_PAD_CTRL)),
92 IOMUX_PADS(PAD_RGMII_RD3__RGMII_RD3 | MUX_PAD_CTRL(ENET_PAD_CTRL)),
93 IOMUX_PADS(PAD_RGMII_RX_CTL__RGMII_RX_CTL |
94 MUX_PAD_CTRL(ENET_PAD_CTRL)),
95 IOMUX_PADS(PAD_SD2_DAT1__GPIO1_IO14 | MUX_PAD_CTRL(NO_PAD_CTRL)),
96};
97
98static iomux_v3_cfg_t const ecspi3_pads[] = {
99 IOMUX_PADS(PAD_DISP0_DAT0__ECSPI3_SCLK | MUX_PAD_CTRL(SPI_PAD_CTRL)),
100 IOMUX_PADS(PAD_DISP0_DAT1__ECSPI3_MOSI | MUX_PAD_CTRL(SPI_PAD_CTRL)),
101 IOMUX_PADS(PAD_DISP0_DAT2__ECSPI3_MISO | MUX_PAD_CTRL(SPI_PAD_CTRL)),
102 IOMUX_PADS(PAD_DISP0_DAT3__GPIO4_IO24 | MUX_PAD_CTRL(NO_PAD_CTRL)),
103};
104
105static iomux_v3_cfg_t const gpios_pads[] = {
106 IOMUX_PADS(PAD_SD4_DAT3__GPIO2_IO11 | MUX_PAD_CTRL(NO_PAD_CTRL)),
107 IOMUX_PADS(PAD_SD4_DAT4__GPIO2_IO12 | MUX_PAD_CTRL(NO_PAD_CTRL)),
108 IOMUX_PADS(PAD_SD4_DAT5__GPIO2_IO13 | MUX_PAD_CTRL(NO_PAD_CTRL)),
109 IOMUX_PADS(PAD_SD4_DAT6__GPIO2_IO14 | MUX_PAD_CTRL(NO_PAD_CTRL)),
110 IOMUX_PADS(PAD_SD4_DAT7__GPIO2_IO15 | MUX_PAD_CTRL(NO_PAD_CTRL)),
111 IOMUX_PADS(PAD_EIM_EB3__GPIO2_IO31 | MUX_PAD_CTRL(NO_PAD_CTRL)),
112 IOMUX_PADS(PAD_ENET_TXD0__GPIO1_IO30 | MUX_PAD_CTRL(NO_PAD_CTRL)),
113 IOMUX_PADS(PAD_SD4_DAT3__GPIO2_IO11 | MUX_PAD_CTRL(NO_PAD_CTRL)),
114};
115
116#ifdef CONFIG_CMD_NAND
117/* NAND */
118static iomux_v3_cfg_t const nfc_pads[] = {
119 IOMUX_PADS(PAD_NANDF_CLE__NAND_CLE | MUX_PAD_CTRL(NAND_PAD_CTRL)),
120 IOMUX_PADS(PAD_NANDF_ALE__NAND_ALE | MUX_PAD_CTRL(NAND_PAD_CTRL)),
121 IOMUX_PADS(PAD_NANDF_WP_B__NAND_WP_B | MUX_PAD_CTRL(NAND_PAD_CTRL)),
122 IOMUX_PADS(PAD_NANDF_RB0__NAND_READY_B | MUX_PAD_CTRL(NAND_PAD_CTRL)),
123 IOMUX_PADS(PAD_NANDF_CS0__NAND_CE0_B | MUX_PAD_CTRL(NAND_PAD_CTRL)),
124 IOMUX_PADS(PAD_NANDF_CS1__NAND_CE1_B | MUX_PAD_CTRL(NAND_PAD_CTRL)),
125 IOMUX_PADS(PAD_NANDF_CS2__NAND_CE2_B | MUX_PAD_CTRL(NAND_PAD_CTRL)),
126 IOMUX_PADS(PAD_NANDF_CS3__NAND_CE3_B | MUX_PAD_CTRL(NAND_PAD_CTRL)),
127 IOMUX_PADS(PAD_SD4_CMD__NAND_RE_B | MUX_PAD_CTRL(NAND_PAD_CTRL)),
128 IOMUX_PADS(PAD_SD4_CLK__NAND_WE_B | MUX_PAD_CTRL(NAND_PAD_CTRL)),
129 IOMUX_PADS(PAD_NANDF_D0__NAND_DATA00 | MUX_PAD_CTRL(NAND_PAD_CTRL)),
130 IOMUX_PADS(PAD_NANDF_D1__NAND_DATA01 | MUX_PAD_CTRL(NAND_PAD_CTRL)),
131 IOMUX_PADS(PAD_NANDF_D2__NAND_DATA02 | MUX_PAD_CTRL(NAND_PAD_CTRL)),
132 IOMUX_PADS(PAD_NANDF_D3__NAND_DATA03 | MUX_PAD_CTRL(NAND_PAD_CTRL)),
133 IOMUX_PADS(PAD_NANDF_D4__NAND_DATA04 | MUX_PAD_CTRL(NAND_PAD_CTRL)),
134 IOMUX_PADS(PAD_NANDF_D5__NAND_DATA05 | MUX_PAD_CTRL(NAND_PAD_CTRL)),
135 IOMUX_PADS(PAD_NANDF_D6__NAND_DATA06 | MUX_PAD_CTRL(NAND_PAD_CTRL)),
136 IOMUX_PADS(PAD_NANDF_D7__NAND_DATA07 | MUX_PAD_CTRL(NAND_PAD_CTRL)),
137 IOMUX_PADS(PAD_SD4_DAT0__NAND_DQS | MUX_PAD_CTRL(NAND_PAD_CTRL)),
138};
139#endif
140
141static struct i2c_pads_info i2c_pad_info = {
142 .scl = {
143 .i2c_mode = MX6Q_PAD_EIM_D21__I2C1_SCL | I2C_PAD,
144 .gpio_mode = MX6Q_PAD_EIM_D21__GPIO3_IO21 | I2C_PAD,
145 .gp = IMX_GPIO_NR(3, 21)
146 },
147 .sda = {
148 .i2c_mode = MX6Q_PAD_EIM_D28__I2C1_SDA | I2C_PAD,
149 .gpio_mode = MX6Q_PAD_EIM_D28__GPIO3_IO28 | I2C_PAD,
150 .gp = IMX_GPIO_NR(3, 28)
151 }
152};
153
154static struct fsl_esdhc_cfg usdhc_cfg[] = {
155 {USDHC3_BASE_ADDR,
156 .max_bus_width = 4},
157 {.esdhc_base = USDHC2_BASE_ADDR,
158 .max_bus_width = 4},
159};
160
161#if !defined(CONFIG_SPL_BUILD)
162static iomux_v3_cfg_t const usdhc2_pads[] = {
163 IOMUX_PADS(PAD_SD2_CLK__SD2_CLK | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
164 IOMUX_PADS(PAD_SD2_CMD__SD2_CMD | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
165 IOMUX_PADS(PAD_SD2_DAT0__SD2_DATA0 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
166 IOMUX_PADS(PAD_SD2_DAT1__SD2_DATA1 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
167 IOMUX_PADS(PAD_SD2_DAT2__SD2_DATA2 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
168 IOMUX_PADS(PAD_SD2_DAT3__SD2_DATA3 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
169 IOMUX_PADS(PAD_EIM_BCLK__GPIO6_IO31 | MUX_PAD_CTRL(NO_PAD_CTRL)),
170 IOMUX_PADS(PAD_GPIO_4__GPIO1_IO04 | MUX_PAD_CTRL(NO_PAD_CTRL)),
171};
172#endif
173
174static iomux_v3_cfg_t const usdhc3_pads[] = {
175 IOMUX_PADS(PAD_SD3_CLK__SD3_CLK | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
176 IOMUX_PADS(PAD_SD3_CMD__SD3_CMD | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
177 IOMUX_PADS(PAD_SD3_DAT0__SD3_DATA0 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
178 IOMUX_PADS(PAD_SD3_DAT1__SD3_DATA1 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
179 IOMUX_PADS(PAD_SD3_DAT2__SD3_DATA2 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
180 IOMUX_PADS(PAD_SD3_DAT3__SD3_DATA3 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
181 IOMUX_PADS(PAD_SD3_DAT4__SD3_DATA4 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
182 IOMUX_PADS(PAD_SD3_DAT5__SD3_DATA5 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
183 IOMUX_PADS(PAD_SD3_DAT6__SD3_DATA6 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
184 IOMUX_PADS(PAD_SD3_DAT7__SD3_DATA7 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
185};
186
187int board_mmc_get_env_dev(int devno)
188{
189 return devno - 1;
190}
191
192int board_mmc_getcd(struct mmc *mmc)
193{
194 struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
195 int ret = 0;
196
197 switch (cfg->esdhc_base) {
198 case USDHC2_BASE_ADDR:
199 ret = !gpio_get_value(USDHC2_CD_GPIO);
200 ret = 1;
201 break;
202 case USDHC3_BASE_ADDR:
203 ret = 1;
204 break;
205 }
206
207 return ret;
208}
209
210#ifndef CONFIG_SPL_BUILD
211int board_mmc_init(bd_t *bis)
212{
213 int ret;
214 int i;
215
216 for (i = 0; i < CONFIG_SYS_FSL_USDHC_NUM; i++) {
217 switch (i) {
218 case 0:
219 SETUP_IOMUX_PADS(usdhc3_pads);
220 usdhc_cfg[0].sdhc_clk = mxc_get_clock(MXC_ESDHC3_CLK);
221 break;
222 case 1:
223 SETUP_IOMUX_PADS(usdhc2_pads);
224 gpio_direction_input(USDHC2_CD_GPIO);
225 usdhc_cfg[1].sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
226 break;
227 default:
228 printf("Warning: you configured more USDHC controllers"
229 "(%d) then supported by the board (%d)\n",
230 i + 1, CONFIG_SYS_FSL_USDHC_NUM);
231 return -EINVAL;
232 }
233
234 ret = fsl_esdhc_initialize(bis, &usdhc_cfg[i]);
235 if (ret)
236 return ret;
237 }
238
239 return 0;
240}
241#endif
242
243static void setup_iomux_uart(void)
244{
245 SETUP_IOMUX_PADS(uart4_pads);
246}
247
248static void setup_iomux_enet(void)
249{
250 SETUP_IOMUX_PADS(enet_pads);
251
252 gpio_direction_output(ENET_PHY_RESET_GPIO, 0);
253 mdelay(10);
254 gpio_set_value(ENET_PHY_RESET_GPIO, 1);
255 mdelay(30);
256}
257
258static void setup_spi(void)
259{
260 gpio_request(IMX_GPIO_NR(4, 24), "spi_cs0");
261 gpio_direction_output(IMX_GPIO_NR(4, 24), 1);
262
263 SETUP_IOMUX_PADS(ecspi3_pads);
264
265 enable_spi_clk(true, 2);
266}
267
268static void setup_gpios(void)
269{
270 SETUP_IOMUX_PADS(gpios_pads);
271}
272
273#ifdef CONFIG_CMD_NAND
274static void setup_gpmi_nand(void)
275{
276 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
277
278 /* config gpmi nand iomux */
279 SETUP_IOMUX_PADS(nfc_pads);
280
281 /* gate ENFC_CLK_ROOT clock first,before clk source switch */
282 clrbits_le32(&mxc_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
283
284 /* config gpmi and bch clock to 100 MHz */
285 clrsetbits_le32(&mxc_ccm->cs2cdr,
286 MXC_CCM_CS2CDR_ENFC_CLK_PODF_MASK |
287 MXC_CCM_CS2CDR_ENFC_CLK_PRED_MASK |
288 MXC_CCM_CS2CDR_ENFC_CLK_SEL_MASK,
289 MXC_CCM_CS2CDR_ENFC_CLK_PODF(0) |
290 MXC_CCM_CS2CDR_ENFC_CLK_PRED(3) |
291 MXC_CCM_CS2CDR_ENFC_CLK_SEL(3));
292
293 /* enable ENFC_CLK_ROOT clock */
294 setbits_le32(&mxc_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
295
296 /* enable gpmi and bch clock gating */
297 setbits_le32(&mxc_ccm->CCGR4,
298 MXC_CCM_CCGR4_RAWNAND_U_BCH_INPUT_APB_MASK |
299 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_BCH_MASK |
300 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK |
301 MXC_CCM_CCGR4_RAWNAND_U_GPMI_INPUT_APB_MASK |
302 MXC_CCM_CCGR4_PL301_MX6QPER1_BCH_OFFSET);
303
304 /* enable apbh clock gating */
305 setbits_le32(&mxc_ccm->CCGR0, MXC_CCM_CCGR0_APBHDMA_MASK);
306}
307#endif
308
309/*
310 * Board revision is coded in 4 GPIOs
311 */
312u32 get_board_rev(void)
313{
314 u32 rev;
315 int i;
316
317 for (i = 0, rev = 0; i < 4; i++)
318 rev |= (gpio_get_value(IMX_GPIO_NR(2, 12 + i)) << i);
319
320 return 16 - rev;
321}
322
323int board_spi_cs_gpio(unsigned bus, unsigned cs)
324{
325 if (bus != 2 || (cs != 0))
326 return -EINVAL;
327
328 return IMX_GPIO_NR(4, 24);
329}
330
331int board_eth_init(bd_t *bis)
332{
333 setup_iomux_enet();
334
335 return cpu_eth_init(bis);
336}
337
338int board_early_init_f(void)
339{
340 setup_iomux_uart();
341
342 return 0;
343}
344
345int board_init(void)
346{
347 /* address of boot parameters */
348 gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
349
350#ifdef CONFIG_SYS_I2C_MXC
351 setup_i2c(0, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info);
352#endif
353
354#ifdef CONFIG_MXC_SPI
355 setup_spi();
356#endif
357
358 setup_gpios();
359
360#ifdef CONFIG_CMD_NAND
361 setup_gpmi_nand();
362#endif
363 return 0;
364}
365
366
367#ifdef CONFIG_CMD_BMODE
368/*
369 * BOOT_CFG1, BOOT_CFG2, BOOT_CFG3, BOOT_CFG4
370 * see Table 8-11 and Table 5-9
371 * BOOT_CFG1[7] = 1 (boot from NAND)
372 * BOOT_CFG1[5] = 0 - raw NAND
373 * BOOT_CFG1[4] = 0 - default pad settings
374 * BOOT_CFG1[3:2] = 00 - devices = 1
375 * BOOT_CFG1[1:0] = 00 - Row Address Cycles = 3
376 * BOOT_CFG2[4:3] = 00 - Boot Search Count = 2
377 * BOOT_CFG2[2:1] = 01 - Pages In Block = 64
378 * BOOT_CFG2[0] = 0 - Reset time 12ms
379 */
380static const struct boot_mode board_boot_modes[] = {
381 /* NAND: 64pages per block, 3 row addr cycles, 2 copies of FCB/DBBT */
382 {"nand", MAKE_CFGVAL(0x80, 0x02, 0x00, 0x00)},
383 {"mmc0", MAKE_CFGVAL(0x40, 0x20, 0x00, 0x00)},
384 {NULL, 0},
385};
386#endif
387
388int board_late_init(void)
389{
390 char buf[10];
391#ifdef CONFIG_CMD_BMODE
392 add_board_boot_modes(board_boot_modes);
393#endif
394
395 snprintf(buf, sizeof(buf), "%d", get_board_rev());
Tom Rini962a75a2017-08-16 18:07:20 -0400396 env_set("board_rev", buf);
Stefano Babic99d51c52017-07-26 18:23:05 +0200397
398 return 0;
399}
400
401#ifdef CONFIG_SPL_BUILD
402#include <asm/arch/mx6-ddr.h>
403#include <spl.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +0900404#include <linux/libfdt.h>
Stefano Babic99d51c52017-07-26 18:23:05 +0200405
406#define MX6_PHYFLEX_ERR006282 IMX_GPIO_NR(2, 11)
407static void phyflex_err006282_workaround(void)
408{
409 /*
410 * Boards beginning with 1362.2 have the SD4_DAT3 pin connected
411 * to the CMIC. If this pin isn't toggled within 10s the boards
412 * reset. The pin is unconnected on older boards, so we do not
413 * need a check for older boards before applying this fixup.
414 */
415
416 gpio_direction_output(MX6_PHYFLEX_ERR006282, 0);
417 mdelay(2);
418 gpio_direction_output(MX6_PHYFLEX_ERR006282, 1);
419 mdelay(2);
420 gpio_set_value(MX6_PHYFLEX_ERR006282, 0);
421
422 gpio_direction_input(MX6_PHYFLEX_ERR006282);
423}
424
425static const struct mx6dq_iomux_ddr_regs mx6_ddr_ioregs = {
426 .dram_sdclk_0 = 0x00000030,
427 .dram_sdclk_1 = 0x00000030,
428 .dram_cas = 0x00000030,
429 .dram_ras = 0x00000030,
430 .dram_reset = 0x00000030,
431 .dram_sdcke0 = 0x00003000,
432 .dram_sdcke1 = 0x00003000,
433 .dram_sdba2 = 0x00000030,
434 .dram_sdodt0 = 0x00000030,
435 .dram_sdodt1 = 0x00000030,
436
437 .dram_sdqs0 = 0x00000028,
438 .dram_sdqs1 = 0x00000028,
439 .dram_sdqs2 = 0x00000028,
440 .dram_sdqs3 = 0x00000028,
441 .dram_sdqs4 = 0x00000028,
442 .dram_sdqs5 = 0x00000028,
443 .dram_sdqs6 = 0x00000028,
444 .dram_sdqs7 = 0x00000028,
445 .dram_dqm0 = 0x00000028,
446 .dram_dqm1 = 0x00000028,
447 .dram_dqm2 = 0x00000028,
448 .dram_dqm3 = 0x00000028,
449 .dram_dqm4 = 0x00000028,
450 .dram_dqm5 = 0x00000028,
451 .dram_dqm6 = 0x00000028,
452 .dram_dqm7 = 0x00000028,
453};
454
455static const struct mx6dq_iomux_grp_regs mx6_grp_ioregs = {
456 .grp_ddr_type = 0x000C0000,
457 .grp_ddrmode_ctl = 0x00020000,
458 .grp_ddrpke = 0x00000000,
459 .grp_addds = IMX6Q_DRIVE_STRENGTH,
460 .grp_ctlds = IMX6Q_DRIVE_STRENGTH,
461 .grp_ddrmode = 0x00020000,
462 .grp_b0ds = 0x00000028,
463 .grp_b1ds = 0x00000028,
464 .grp_b2ds = 0x00000028,
465 .grp_b3ds = 0x00000028,
466 .grp_b4ds = 0x00000028,
467 .grp_b5ds = 0x00000028,
468 .grp_b6ds = 0x00000028,
469 .grp_b7ds = 0x00000028,
470};
471
472static const struct mx6_mmdc_calibration mx6_mmcd_calib = {
473 .p0_mpwldectrl0 = 0x00110011,
474 .p0_mpwldectrl1 = 0x00240024,
475 .p1_mpwldectrl0 = 0x00260038,
476 .p1_mpwldectrl1 = 0x002C0038,
477 .p0_mpdgctrl0 = 0x03400350,
478 .p0_mpdgctrl1 = 0x03440340,
479 .p1_mpdgctrl0 = 0x034C0354,
480 .p1_mpdgctrl1 = 0x035C033C,
481 .p0_mprddlctl = 0x322A2A2A,
482 .p1_mprddlctl = 0x302C2834,
483 .p0_mpwrdlctl = 0x34303834,
484 .p1_mpwrdlctl = 0x422A3E36,
485};
486
487/* Index in RAM Chip array */
488enum {
Stefano Babic004cbb62017-08-25 13:02:53 +0200489 RAM_MT64K,
490 RAM_MT128K,
491 RAM_MT256K
Stefano Babic99d51c52017-07-26 18:23:05 +0200492};
493
494static struct mx6_ddr3_cfg mt41k_xx[] = {
495/* MT41K64M16JT-125 (1Gb density) */
496 {
497 .mem_speed = 1600,
498 .density = 1,
499 .width = 16,
500 .banks = 8,
501 .rowaddr = 13,
502 .coladdr = 10,
503 .pagesz = 2,
504 .trcd = 1375,
505 .trcmin = 4875,
506 .trasmin = 3500,
507 .SRT = 1,
508 },
509
510/* MT41K256M16JT-125 (2Gb density) */
511 {
512 .mem_speed = 1600,
513 .density = 2,
514 .width = 16,
515 .banks = 8,
516 .rowaddr = 14,
517 .coladdr = 10,
518 .pagesz = 2,
519 .trcd = 1375,
520 .trcmin = 4875,
521 .trasmin = 3500,
522 .SRT = 1,
523 },
524
525/* MT41K256M16JT-125 (4Gb density) */
526 {
527 .mem_speed = 1600,
528 .density = 4,
529 .width = 16,
530 .banks = 8,
531 .rowaddr = 15,
532 .coladdr = 10,
533 .pagesz = 2,
534 .trcd = 1375,
535 .trcmin = 4875,
536 .trasmin = 3500,
537 .SRT = 1,
538 }
539};
540
541static void ccgr_init(void)
542{
543 struct mxc_ccm_reg *ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
544
545 writel(0x00C03F3F, &ccm->CCGR0);
546 writel(0x0030FC03, &ccm->CCGR1);
547 writel(0x0FFFC000, &ccm->CCGR2);
548 writel(0x3FF00000, &ccm->CCGR3);
549 writel(0x00FFF300, &ccm->CCGR4);
550 writel(0x0F0000C3, &ccm->CCGR5);
551 writel(0x000003FF, &ccm->CCGR6);
552}
553
Stefano Babic004cbb62017-08-25 13:02:53 +0200554static void spl_dram_init(struct mx6_ddr_sysinfo *sysinfo,
555 struct mx6_ddr3_cfg *mem_ddr)
Stefano Babic99d51c52017-07-26 18:23:05 +0200556{
Stefano Babic99d51c52017-07-26 18:23:05 +0200557 mx6dq_dram_iocfg(64, &mx6_ddr_ioregs, &mx6_grp_ioregs);
Stefano Babic004cbb62017-08-25 13:02:53 +0200558 mx6_dram_cfg(sysinfo, &mx6_mmcd_calib, mem_ddr);
Stefano Babic99d51c52017-07-26 18:23:05 +0200559}
560
561int board_mmc_init(bd_t *bis)
562{
563 if (spl_boot_device() == BOOT_DEVICE_SPI)
564 printf("MMC SEtup, Boot SPI");
565
566 SETUP_IOMUX_PADS(usdhc3_pads);
567 usdhc_cfg[0].esdhc_base = USDHC3_BASE_ADDR;
568 usdhc_cfg[0].sdhc_clk = mxc_get_clock(MXC_ESDHC3_CLK);
569 usdhc_cfg[0].max_bus_width = 4;
570 gd->arch.sdhc_clk = usdhc_cfg[0].sdhc_clk;
571
572 return fsl_esdhc_initialize(bis, &usdhc_cfg[0]);
573}
574
575
576void board_boot_order(u32 *spl_boot_list)
577{
578 spl_boot_list[0] = spl_boot_device();
579 printf("Boot device %x\n", spl_boot_list[0]);
580 switch (spl_boot_list[0]) {
581 case BOOT_DEVICE_SPI:
582 spl_boot_list[1] = BOOT_DEVICE_UART;
583 break;
584 case BOOT_DEVICE_MMC1:
585 spl_boot_list[1] = BOOT_DEVICE_SPI;
586 spl_boot_list[2] = BOOT_DEVICE_UART;
587 break;
588 default:
589 printf("Boot device %x\n", spl_boot_list[0]);
590 }
591}
592
593/*
594 * This is used because get_ram_size() does not
595 * take care of cache, resulting a wrong size
596 * pfla02 has just 1, 2 or 4 GB option
597 * Function checks for mirrors in the first CS
598 */
599#define RAM_TEST_PATTERN 0xaa5555aa
Stefano Babic004cbb62017-08-25 13:02:53 +0200600#define MIN_BANK_SIZE (512 * 1024 * 1024)
601
602static unsigned int pfla02_detect_chiptype(void)
Stefano Babic99d51c52017-07-26 18:23:05 +0200603{
604 u32 *p, *p1;
Stefano Babic004cbb62017-08-25 13:02:53 +0200605 unsigned int offset = MIN_BANK_SIZE;
Stefano Babic99d51c52017-07-26 18:23:05 +0200606 int i;
607
608 for (i = 0; i < 2; i++) {
609 p = (u32 *)PHYS_SDRAM;
610 p1 = (u32 *)(PHYS_SDRAM + (i + 1) * offset);
611
612 *p1 = 0;
613 *p = RAM_TEST_PATTERN;
614
615 /*
616 * This is required to detect mirroring
617 * else we read back values from cache
618 */
619 flush_dcache_all();
620
621 if (*p == *p1)
622 return i;
623 }
Stefano Babic004cbb62017-08-25 13:02:53 +0200624 return RAM_MT256K;
Stefano Babic99d51c52017-07-26 18:23:05 +0200625}
626
627void board_init_f(ulong dummy)
628{
629 unsigned int ramchip;
Stefano Babic004cbb62017-08-25 13:02:53 +0200630
631 struct mx6_ddr_sysinfo sysinfo = {
632 /* width of data bus:0=16,1=32,2=64 */
633 .dsize = 2,
634 /* config for full 4GB range so that get_mem_size() works */
635 .cs_density = 32, /* 512 MB */
636 /* single chip select */
637#if IS_ENABLED(CONFIG_SPL_DRAM_1_BANK)
638 .ncs = 1,
639#else
640 .ncs = 2,
641#endif
642 .cs1_mirror = 1,
643 .rtt_wr = 1 /*DDR3_RTT_60_OHM*/, /* RTT_Wr = RZQ/4 */
644 .rtt_nom = 1 /*DDR3_RTT_60_OHM*/, /* RTT_Nom = RZQ/4 */
645 .walat = 1, /* Write additional latency */
646 .ralat = 5, /* Read additional latency */
647 .mif3_mode = 3, /* Command prediction working mode */
648 .bi_on = 1, /* Bank interleaving enabled */
649 .sde_to_rst = 0x10, /* 14 cycles, 200us (JEDEC default) */
650 .rst_to_cke = 0x23, /* 33 cycles, 500us (JEDEC default) */
651 .ddr_type = DDR_TYPE_DDR3,
652 .refsel = 1, /* Refresh cycles at 32KHz */
653 .refr = 7, /* 8 refresh commands per refresh cycle */
654 };
655
Stefano Babic99d51c52017-07-26 18:23:05 +0200656#ifdef CONFIG_CMD_NAND
657 /* Enable NAND */
658 setup_gpmi_nand();
659#endif
660
661 /* setup clock gating */
662 ccgr_init();
663
664 /* setup AIPS and disable watchdog */
665 arch_cpu_init();
666
667 /* setup AXI */
668 gpr_init();
669
670 board_early_init_f();
671
672 /* setup GP timer */
673 timer_init();
674
675 /* UART clocks enabled and gd valid - init serial console */
676 preloader_console_init();
677
678 setup_spi();
679
680 setup_gpios();
681
682 /* DDR initialization */
Stefano Babic004cbb62017-08-25 13:02:53 +0200683 spl_dram_init(&sysinfo, &mt41k_xx[RAM_MT256K]);
684 ramchip = pfla02_detect_chiptype();
685 debug("Detected chip %d\n", ramchip);
686#if !IS_ENABLED(CONFIG_SPL_DRAM_1_BANK)
687 switch (ramchip) {
688 case RAM_MT64K:
689 sysinfo.cs_density = 6;
690 break;
691 case RAM_MT128K:
692 sysinfo.cs_density = 10;
693 break;
694 case RAM_MT256K:
695 sysinfo.cs_density = 18;
696 break;
697 }
698#endif
699 spl_dram_init(&sysinfo, &mt41k_xx[ramchip]);
Stefano Babic99d51c52017-07-26 18:23:05 +0200700
701 /* Clear the BSS. */
702 memset(__bss_start, 0, __bss_end - __bss_start);
703
704 phyflex_err006282_workaround();
705
706 /* load/boot image from boot device */
707 board_init_r(NULL, 0);
708}
709#endif