blob: 6fa5cf4d27d773c18fa3b038096d0ee3248cee42 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Fabio Estevam1d97a592015-04-20 14:48:57 -03002/*
Josua Mayer101237a2022-05-19 12:31:59 +03003 * Copyright (C) 2022 Josua Mayer <josua@solid-run.com>
4 *
Fabio Estevam1d97a592015-04-20 14:48:57 -03005 * Copyright (C) 2015 Freescale Semiconductor, Inc.
6 *
7 * Author: Fabio Estevam <fabio.estevam@freescale.com>
8 *
9 * Copyright (C) 2013 Jon Nettleton <jon.nettleton@gmail.com>
10 *
11 * Based on SPL code from Solidrun tree, which is:
12 * Author: Tungyi Lin <tungyilin1127@gmail.com>
13 *
14 * Derived from EDM_CF_IMX6 code by TechNexion,Inc
15 * Ported to SolidRun microSOM by Rabeeh Khoury <rabeeh@solid-run.com>
Fabio Estevam1d97a592015-04-20 14:48:57 -030016 */
17
Simon Glass1e268642020-05-10 11:39:55 -060018#include <common.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060019#include <image.h>
Simon Glassa7b51302019-11-14 12:57:46 -070020#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060021#include <log.h>
Fabio Estevam1d97a592015-04-20 14:48:57 -030022#include <asm/arch/clock.h>
23#include <asm/arch/imx-regs.h>
24#include <asm/arch/iomux.h>
25#include <asm/arch/mx6-pins.h>
Fabio Estevam239fd312015-04-29 22:28:09 -030026#include <asm/arch/mxc_hdmi.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060027#include <env.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060028#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060029#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090030#include <linux/errno.h>
Fabio Estevam1d97a592015-04-20 14:48:57 -030031#include <asm/gpio.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020032#include <asm/mach-imx/iomux-v3.h>
33#include <asm/mach-imx/sata.h>
34#include <asm/mach-imx/video.h>
Fabio Estevam1d97a592015-04-20 14:48:57 -030035#include <mmc.h>
Yangbo Lu73340382019-06-21 11:42:28 +080036#include <fsl_esdhc_imx.h>
Fabio Estevam444f0012015-05-04 11:22:55 -030037#include <malloc.h>
Fabio Estevam1d97a592015-04-20 14:48:57 -030038#include <asm/arch/crm_regs.h>
39#include <asm/io.h>
40#include <asm/arch/sys_proto.h>
Fabio Estevam1d97a592015-04-20 14:48:57 -030041#include <spl.h>
Fabio Estevam729bbb82015-04-29 22:28:10 -030042#include <usb.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020043#include <usb/ehci-ci.h>
Josua Mayer101237a2022-05-19 12:31:59 +030044#include <netdev.h>
45#include <phy.h>
Fabio Estevam1d97a592015-04-20 14:48:57 -030046
47DECLARE_GLOBAL_DATA_PTR;
48
49#define UART_PAD_CTRL (PAD_CTL_PUS_100K_UP | \
50 PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm | \
51 PAD_CTL_SRE_FAST | PAD_CTL_HYS)
52
53#define USDHC_PAD_CTRL (PAD_CTL_PUS_47K_UP | \
54 PAD_CTL_SPEED_LOW | PAD_CTL_DSE_80ohm | \
55 PAD_CTL_SRE_FAST | PAD_CTL_HYS)
56
Fabio Estevam729bbb82015-04-29 22:28:10 -030057#define USB_H1_VBUS IMX_GPIO_NR(1, 0)
Fabio Estevam1d97a592015-04-20 14:48:57 -030058
Jon Nettleton30cba092018-06-07 16:17:36 +030059enum board_type {
60 CUBOXI = 0x00,
61 HUMMINGBOARD = 0x01,
62 HUMMINGBOARD2 = 0x02,
63 UNKNOWN = 0x03,
64};
65
Baruch Siach6d3f68c2019-11-10 14:38:07 +020066static struct gpio_desc board_detect_desc[5];
67
Jon Nettletondfe7fab2018-06-07 16:17:37 +030068#define MEM_STRIDE 0x4000000
69static u32 get_ram_size_stride_test(u32 *base, u32 maxsize)
70{
71 volatile u32 *addr;
72 u32 save[64];
73 u32 cnt;
74 u32 size;
75 int i = 0;
76
77 /* First save the data */
78 for (cnt = 0; cnt < maxsize; cnt += MEM_STRIDE) {
79 addr = (volatile u32 *)((u32)base + cnt); /* pointer arith! */
80 sync ();
81 save[i++] = *addr;
82 sync ();
83 }
84
85 /* First write a signature */
86 * (volatile u32 *)base = 0x12345678;
87 for (size = MEM_STRIDE; size < maxsize; size += MEM_STRIDE) {
88 * (volatile u32 *)((u32)base + size) = size;
89 sync ();
90 if (* (volatile u32 *)((u32)base) == size) { /* We reached the overlapping address */
91 break;
92 }
93 }
94
95 /* Restore the data */
96 for (cnt = (maxsize - MEM_STRIDE); i > 0; cnt -= MEM_STRIDE) {
97 addr = (volatile u32 *)((u32)base + cnt); /* pointer arith! */
98 sync ();
99 *addr = save[i--];
100 sync ();
101 }
102
103 return (size);
104}
105
Fabio Estevam1d97a592015-04-20 14:48:57 -0300106int dram_init(void)
107{
Jon Nettletondfe7fab2018-06-07 16:17:37 +0300108 u32 max_size = imx_ddr_size();
109
Tom Rinibb4dd962022-11-16 13:10:37 -0500110 gd->ram_size = get_ram_size_stride_test((u32 *) CFG_SYS_SDRAM_BASE,
Jon Nettletondfe7fab2018-06-07 16:17:37 +0300111 (u32)max_size);
112
Fabio Estevam1d97a592015-04-20 14:48:57 -0300113 return 0;
114}
115
116static iomux_v3_cfg_t const uart1_pads[] = {
Fabio Estevam5f402c42015-04-25 18:47:17 -0300117 IOMUX_PADS(PAD_CSI0_DAT10__UART1_TX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL)),
118 IOMUX_PADS(PAD_CSI0_DAT11__UART1_RX_DATA | MUX_PAD_CTRL(UART_PAD_CTRL)),
Fabio Estevam1d97a592015-04-20 14:48:57 -0300119};
120
121static iomux_v3_cfg_t const usdhc2_pads[] = {
Fabio Estevam5f402c42015-04-25 18:47:17 -0300122 IOMUX_PADS(PAD_SD2_CLK__SD2_CLK | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
123 IOMUX_PADS(PAD_SD2_CMD__SD2_CMD | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
124 IOMUX_PADS(PAD_SD2_DAT0__SD2_DATA0 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
125 IOMUX_PADS(PAD_SD2_DAT1__SD2_DATA1 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
126 IOMUX_PADS(PAD_SD2_DAT2__SD2_DATA2 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
127 IOMUX_PADS(PAD_SD2_DAT3__SD2_DATA3 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
Fabio Estevam1d97a592015-04-20 14:48:57 -0300128};
129
Jon Nettleton08d38cb2018-06-11 15:26:20 +0300130static iomux_v3_cfg_t const usdhc3_pads[] = {
131 IOMUX_PADS(PAD_SD3_CLK__SD3_CLK | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
132 IOMUX_PADS(PAD_SD3_CMD__SD3_CMD | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
133 IOMUX_PADS(PAD_SD3_DAT0__SD3_DATA0 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
134 IOMUX_PADS(PAD_SD3_DAT1__SD3_DATA1 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
135 IOMUX_PADS(PAD_SD3_DAT2__SD3_DATA2 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
136 IOMUX_PADS(PAD_SD3_DAT3__SD3_DATA3 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
137 IOMUX_PADS(PAD_SD3_DAT4__SD3_DATA4 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
138 IOMUX_PADS(PAD_SD3_DAT5__SD3_DATA5 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
139 IOMUX_PADS(PAD_SD3_DAT6__SD3_DATA6 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
140 IOMUX_PADS(PAD_SD3_DAT7__SD3_DATA7 | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
141 IOMUX_PADS(PAD_SD3_RST__SD3_RESET | MUX_PAD_CTRL(USDHC_PAD_CTRL)),
142};
143
Jon Nettleton30cba092018-06-07 16:17:36 +0300144static iomux_v3_cfg_t const board_detect[] = {
Fabio Estevam7a2f9cd2015-04-25 18:47:19 -0300145 /* These pins are for sensing if it is a CuBox-i or a HummingBoard */
146 IOMUX_PADS(PAD_KEY_ROW1__GPIO4_IO09 | MUX_PAD_CTRL(UART_PAD_CTRL)),
147 IOMUX_PADS(PAD_EIM_DA4__GPIO3_IO04 | MUX_PAD_CTRL(UART_PAD_CTRL)),
Jon Nettleton30cba092018-06-07 16:17:36 +0300148 IOMUX_PADS(PAD_SD4_DAT0__GPIO2_IO08 | MUX_PAD_CTRL(UART_PAD_CTRL)),
149};
150
151static iomux_v3_cfg_t const som_rev_detect[] = {
152 /* These pins are for sensing if it is a CuBox-i or a HummingBoard */
153 IOMUX_PADS(PAD_CSI0_DAT14__GPIO6_IO00 | MUX_PAD_CTRL(UART_PAD_CTRL)),
154 IOMUX_PADS(PAD_CSI0_DAT18__GPIO6_IO04 | MUX_PAD_CTRL(UART_PAD_CTRL)),
Fabio Estevam7a2f9cd2015-04-25 18:47:19 -0300155};
156
Fabio Estevam1d97a592015-04-20 14:48:57 -0300157static void setup_iomux_uart(void)
158{
Fabio Estevam5f402c42015-04-25 18:47:17 -0300159 SETUP_IOMUX_PADS(uart1_pads);
Fabio Estevam1d97a592015-04-20 14:48:57 -0300160}
161
Jon Nettleton08d38cb2018-06-11 15:26:20 +0300162int board_mmc_get_env_dev(int devno)
163{
Baruch Siach6d3f68c2019-11-10 14:38:07 +0200164 return devno;
Jon Nettleton08d38cb2018-06-11 15:26:20 +0300165}
166
Fabio Estevam239fd312015-04-29 22:28:09 -0300167#ifdef CONFIG_VIDEO_IPUV3
168static void do_enable_hdmi(struct display_info_t const *dev)
169{
170 imx_enable_hdmi_phy();
171}
172
173struct display_info_t const displays[] = {
174 {
175 .bus = -1,
176 .addr = 0,
177 .pixfmt = IPU_PIX_FMT_RGB24,
178 .detect = detect_hdmi,
179 .enable = do_enable_hdmi,
180 .mode = {
181 .name = "HDMI",
182 /* 1024x768@60Hz (VESA)*/
183 .refresh = 60,
184 .xres = 1024,
185 .yres = 768,
186 .pixclock = 15384,
187 .left_margin = 160,
188 .right_margin = 24,
189 .upper_margin = 29,
190 .lower_margin = 3,
191 .hsync_len = 136,
192 .vsync_len = 6,
193 .sync = FB_SYNC_EXT,
194 .vmode = FB_VMODE_NONINTERLACED
195 }
196 }
197};
198
199size_t display_count = ARRAY_SIZE(displays);
200
201static int setup_display(void)
202{
203 struct mxc_ccm_reg *ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
204 int reg;
205 int timeout = 100000;
206
207 enable_ipu_clock();
208 imx_setup_hdmi();
209
210 /* set video pll to 455MHz (24MHz * (37+11/12) / 2) */
211 setbits_le32(&ccm->analog_pll_video, BM_ANADIG_PLL_VIDEO_POWERDOWN);
212
213 reg = readl(&ccm->analog_pll_video);
214 reg &= ~BM_ANADIG_PLL_VIDEO_DIV_SELECT;
215 reg |= BF_ANADIG_PLL_VIDEO_DIV_SELECT(37);
216 reg &= ~BM_ANADIG_PLL_VIDEO_POST_DIV_SELECT;
217 reg |= BF_ANADIG_PLL_VIDEO_POST_DIV_SELECT(1);
218 writel(reg, &ccm->analog_pll_video);
219
220 writel(BF_ANADIG_PLL_VIDEO_NUM_A(11), &ccm->analog_pll_video_num);
221 writel(BF_ANADIG_PLL_VIDEO_DENOM_B(12), &ccm->analog_pll_video_denom);
222
223 reg &= ~BM_ANADIG_PLL_VIDEO_POWERDOWN;
224 writel(reg, &ccm->analog_pll_video);
225
226 while (timeout--)
227 if (readl(&ccm->analog_pll_video) & BM_ANADIG_PLL_VIDEO_LOCK)
228 break;
229 if (timeout < 0) {
230 printf("Warning: video pll lock timeout!\n");
231 return -ETIMEDOUT;
232 }
233
234 reg = readl(&ccm->analog_pll_video);
235 reg |= BM_ANADIG_PLL_VIDEO_ENABLE;
236 reg &= ~BM_ANADIG_PLL_VIDEO_BYPASS;
237 writel(reg, &ccm->analog_pll_video);
238
239 /* gate ipu1_di0_clk */
240 clrbits_le32(&ccm->CCGR3, MXC_CCM_CCGR3_LDB_DI0_MASK);
241
242 /* select video_pll clock / 7 for ipu1_di0_clk -> 65MHz pixclock */
243 reg = readl(&ccm->chsccdr);
244 reg &= ~(MXC_CCM_CHSCCDR_IPU1_DI0_PRE_CLK_SEL_MASK |
245 MXC_CCM_CHSCCDR_IPU1_DI0_PODF_MASK |
246 MXC_CCM_CHSCCDR_IPU1_DI0_CLK_SEL_MASK);
247 reg |= (2 << MXC_CCM_CHSCCDR_IPU1_DI0_PRE_CLK_SEL_OFFSET) |
248 (6 << MXC_CCM_CHSCCDR_IPU1_DI0_PODF_OFFSET) |
249 (0 << MXC_CCM_CHSCCDR_IPU1_DI0_CLK_SEL_OFFSET);
250 writel(reg, &ccm->chsccdr);
251
252 /* enable ipu1_di0_clk */
253 setbits_le32(&ccm->CCGR3, MXC_CCM_CCGR3_LDB_DI0_MASK);
254
255 return 0;
256}
257#endif /* CONFIG_VIDEO_IPUV3 */
258
Fabio Estevam4bc9de52020-06-18 20:21:20 -0300259static int setup_fec(void)
260{
261 struct iomuxc *const iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
262 int ret;
263
264 ret = enable_fec_anatop_clock(0, ENET_25MHZ);
265 if (ret)
266 return ret;
267
268 /* set gpr1[ENET_CLK_SEL] */
269 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_ENET_CLK_SEL_MASK);
270
271 return 0;
272}
273
Fabio Estevam1d97a592015-04-20 14:48:57 -0300274int board_early_init_f(void)
275{
276 setup_iomux_uart();
Fabio Estevam239fd312015-04-29 22:28:09 -0300277
Troy Kiskyd64485e2023-03-13 14:31:40 -0700278 if (CONFIG_IS_ENABLED(SATA))
279 setup_sata();
Fabio Estevam4bc9de52020-06-18 20:21:20 -0300280 setup_fec();
281
Fabio Estevamf82f20c2017-09-22 23:45:31 -0300282 return 0;
Fabio Estevam1d97a592015-04-20 14:48:57 -0300283}
284
285int board_init(void)
286{
Fabio Estevamf82f20c2017-09-22 23:45:31 -0300287 int ret = 0;
288
Fabio Estevam1d97a592015-04-20 14:48:57 -0300289 /* address of boot parameters */
Tom Rinibb4dd962022-11-16 13:10:37 -0500290 gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100;
Fabio Estevam1d97a592015-04-20 14:48:57 -0300291
Fabio Estevamf82f20c2017-09-22 23:45:31 -0300292#ifdef CONFIG_VIDEO_IPUV3
293 ret = setup_display();
294#endif
295
296 return ret;
Fabio Estevam1d97a592015-04-20 14:48:57 -0300297}
298
Baruch Siach6d3f68c2019-11-10 14:38:07 +0200299static int request_detect_gpios(void)
300{
301 int node;
302 int ret;
303
304 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0,
305 "solidrun,hummingboard-detect");
306 if (node < 0)
307 return -ENODEV;
308
309 ret = gpio_request_list_by_name_nodev(offset_to_ofnode(node),
310 "detect-gpios", board_detect_desc,
311 ARRAY_SIZE(board_detect_desc), GPIOD_IS_IN);
312
313 return ret;
314}
315
316static int free_detect_gpios(void)
317{
318 return gpio_free_list_nodev(board_detect_desc,
319 ARRAY_SIZE(board_detect_desc));
320}
321
Jon Nettleton30cba092018-06-07 16:17:36 +0300322static enum board_type board_type(void)
Fabio Estevam7a2f9cd2015-04-25 18:47:19 -0300323{
Jon Nettleton30cba092018-06-07 16:17:36 +0300324 int val1, val2, val3;
Fabio Estevam7a2f9cd2015-04-25 18:47:19 -0300325
Jon Nettleton30cba092018-06-07 16:17:36 +0300326 SETUP_IOMUX_PADS(board_detect);
Fabio Estevam7a2f9cd2015-04-25 18:47:19 -0300327
328 /*
329 * Machine selection -
Jon Nettleton30cba092018-06-07 16:17:36 +0300330 * Machine val1, val2, val3
331 * ----------------------------
332 * HB2 x x 0
333 * HB rev 3.x x 0 x
334 * CBi 0 1 x
335 * HB 1 1 x
Fabio Estevam7a2f9cd2015-04-25 18:47:19 -0300336 */
337
Jon Nettleton30cba092018-06-07 16:17:36 +0300338 gpio_direction_input(IMX_GPIO_NR(2, 8));
339 val3 = gpio_get_value(IMX_GPIO_NR(2, 8));
Fabio Estevam7a2f9cd2015-04-25 18:47:19 -0300340
Jon Nettleton30cba092018-06-07 16:17:36 +0300341 if (val3 == 0)
342 return HUMMINGBOARD2;
Dennis Gilmore52e61ba2017-08-24 10:49:43 -0500343
Jon Nettleton30cba092018-06-07 16:17:36 +0300344 gpio_direction_input(IMX_GPIO_NR(3, 4));
345 val2 = gpio_get_value(IMX_GPIO_NR(3, 4));
Dennis Gilmore52e61ba2017-08-24 10:49:43 -0500346
Jon Nettleton30cba092018-06-07 16:17:36 +0300347 if (val2 == 0)
348 return HUMMINGBOARD;
Dennis Gilmore52e61ba2017-08-24 10:49:43 -0500349
Jon Nettleton30cba092018-06-07 16:17:36 +0300350 gpio_direction_input(IMX_GPIO_NR(4, 9));
351 val1 = gpio_get_value(IMX_GPIO_NR(4, 9));
Dennis Gilmore52e61ba2017-08-24 10:49:43 -0500352
Jon Nettleton30cba092018-06-07 16:17:36 +0300353 if (val1 == 0) {
354 return CUBOXI;
355 } else {
356 return HUMMINGBOARD;
357 }
358}
359
360static bool is_rev_15_som(void)
361{
362 int val1, val2;
363 SETUP_IOMUX_PADS(som_rev_detect);
Dennis Gilmore52e61ba2017-08-24 10:49:43 -0500364
Jon Nettleton30cba092018-06-07 16:17:36 +0300365 val1 = gpio_get_value(IMX_GPIO_NR(6, 0));
366 val2 = gpio_get_value(IMX_GPIO_NR(6, 4));
367
368 if (val1 == 1 && val2 == 0)
Dennis Gilmore52e61ba2017-08-24 10:49:43 -0500369 return true;
Jon Nettleton30cba092018-06-07 16:17:36 +0300370
371 return false;
Dennis Gilmore52e61ba2017-08-24 10:49:43 -0500372}
373
Jon Nettleton51182112018-06-11 15:26:22 +0300374static bool has_emmc(void)
375{
376 struct mmc *mmc;
Baruch Siach6d3f68c2019-11-10 14:38:07 +0200377 mmc = find_mmc_device(2);
Jon Nettleton51182112018-06-11 15:26:22 +0300378 if (!mmc)
379 return 0;
Pali Rohár7c639622021-07-14 16:37:29 +0200380 return (mmc_get_op_cond(mmc, true) < 0) ? 0 : 1;
Jon Nettleton51182112018-06-11 15:26:22 +0300381}
382
Fabio Estevam1d97a592015-04-20 14:48:57 -0300383int checkboard(void)
384{
Baruch Siach6d3f68c2019-11-10 14:38:07 +0200385 request_detect_gpios();
386
Jon Nettleton30cba092018-06-07 16:17:36 +0300387 switch (board_type()) {
388 case CUBOXI:
389 puts("Board: MX6 Cubox-i");
390 break;
391 case HUMMINGBOARD:
392 puts("Board: MX6 HummingBoard");
393 break;
394 case HUMMINGBOARD2:
395 puts("Board: MX6 HummingBoard2");
396 break;
397 case UNKNOWN:
398 default:
399 puts("Board: Unknown\n");
400 goto out;
401 }
402
403 if (is_rev_15_som())
404 puts(" (som rev 1.5)\n");
Fabio Estevam7a2f9cd2015-04-25 18:47:19 -0300405 else
Jon Nettleton30cba092018-06-07 16:17:36 +0300406 puts("\n");
Fabio Estevam7a2f9cd2015-04-25 18:47:19 -0300407
Baruch Siach6d3f68c2019-11-10 14:38:07 +0200408 free_detect_gpios();
Jon Nettleton30cba092018-06-07 16:17:36 +0300409out:
Fabio Estevam1d97a592015-04-20 14:48:57 -0300410 return 0;
411}
412
Josua Mayer101237a2022-05-19 12:31:59 +0300413static int find_ethernet_phy(void)
414{
415 struct mii_dev *bus = NULL;
416 struct phy_device *phydev = NULL;
417 int phy_addr = -ENOENT;
418
419#ifdef CONFIG_FEC_MXC
420 bus = fec_get_miibus(ENET_BASE_ADDR, -1);
421 if (!bus)
422 return -ENOENT;
423
424 // scan address 0, 1, 4
425 phydev = phy_find_by_mask(bus, 0b00010011);
426 if (!phydev) {
427 free(bus);
428 return -ENOENT;
429 }
430 pr_debug("%s: detected ethernet phy at address %d\n", __func__, phydev->addr);
431 phy_addr = phydev->addr;
432
433 free(phydev);
434#endif
435
436 return phy_addr;
437}
438
439#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
440/*
441 * Configure the correct ethernet PHYs nodes in device-tree:
442 * - AR8035 at addresses 0 or 4: Cubox
443 * - AR8035 at address 0: HummingBoard, HummingBoard 2
444 * - ADIN1300 at address 1: since SoM rev 1.9
445 */
446int ft_board_setup(void *fdt, struct bd_info *bd)
447{
448 int node_phy0, node_phy1, node_phy4;
449 int ret, phy;
450 bool enable_phy0 = false, enable_phy1 = false, enable_phy4 = false;
Josua Mayer5674e4c2022-06-16 11:40:15 +0300451 enum board_type board;
452
453 // detect device
454 request_detect_gpios();
455 board = board_type();
456 free_detect_gpios();
Josua Mayer101237a2022-05-19 12:31:59 +0300457
458 // detect phy
459 phy = find_ethernet_phy();
460 if (phy == 0 || phy == 4) {
461 enable_phy0 = true;
Josua Mayer5674e4c2022-06-16 11:40:15 +0300462 switch (board) {
463 case HUMMINGBOARD:
464 case HUMMINGBOARD2:
465 /* atheros phy may appear only at address 0 */
466 break;
Josua Mayer101237a2022-05-19 12:31:59 +0300467 case CUBOXI:
468 case UNKNOWN:
469 default:
Josua Mayer5674e4c2022-06-16 11:40:15 +0300470 /* atheros phy may appear at either address 0 or 4 */
Josua Mayer101237a2022-05-19 12:31:59 +0300471 enable_phy4 = true;
472 }
473 } else if (phy == 1) {
474 enable_phy1 = true;
475 } else {
476 pr_err("%s: couldn't detect ethernet phy, not patching dtb!\n", __func__);
477 return 0;
478 }
479
480 // update all phy nodes status
481 node_phy0 = fdt_path_offset(fdt, "/soc/bus@2100000/ethernet@2188000/mdio/ethernet-phy@0");
482 ret = fdt_setprop_string(fdt, node_phy0, "status", enable_phy0 ? "okay" : "disabled");
483 if (ret < 0 && enable_phy0)
484 pr_err("%s: failed to enable ethernet phy at address 0 in dtb!\n", __func__);
485 node_phy1 = fdt_path_offset(fdt, "/soc/bus@2100000/ethernet@2188000/mdio/ethernet-phy@1");
486 ret = fdt_setprop_string(fdt, node_phy1, "status", enable_phy1 ? "okay" : "disabled");
487 if (ret < 0 && enable_phy1)
488 pr_err("%s: failed to enable ethernet phy at address 1 in dtb!\n", __func__);
489 node_phy4 = fdt_path_offset(fdt, "/soc/bus@2100000/ethernet@2188000/mdio/ethernet-phy@4");
490 ret = fdt_setprop_string(fdt, node_phy4, "status", enable_phy4 ? "okay" : "disabled");
491 if (ret < 0 && enable_phy4)
492 pr_err("%s: failed to enable ethernet phy at address 4 in dtb!\n", __func__);
493
494 return 0;
495}
496#endif
497
Baruch Siach6d3f68c2019-11-10 14:38:07 +0200498/* Override the default implementation, DT model is not accurate */
499int show_board_info(void)
500{
501 return checkboard();
502}
503
Fabio Estevam9887c1a2015-04-25 18:47:21 -0300504int board_late_init(void)
505{
506#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
Baruch Siach6d3f68c2019-11-10 14:38:07 +0200507 request_detect_gpios();
508
Jon Nettleton30cba092018-06-07 16:17:36 +0300509 switch (board_type()) {
510 case CUBOXI:
511 env_set("board_name", "CUBOXI");
512 break;
513 case HUMMINGBOARD:
Simon Glass6a38e412017-08-03 12:22:09 -0600514 env_set("board_name", "HUMMINGBOARD");
Jon Nettleton30cba092018-06-07 16:17:36 +0300515 break;
516 case HUMMINGBOARD2:
517 env_set("board_name", "HUMMINGBOARD2");
518 break;
519 case UNKNOWN:
520 default:
Simon Glass6a38e412017-08-03 12:22:09 -0600521 env_set("board_name", "CUBOXI");
Jon Nettleton30cba092018-06-07 16:17:36 +0300522 }
Fabio Estevam9887c1a2015-04-25 18:47:21 -0300523
Breno Limaba776122016-07-22 09:11:30 -0300524 if (is_mx6dq())
Simon Glass6a38e412017-08-03 12:22:09 -0600525 env_set("board_rev", "MX6Q");
Fabio Estevam9887c1a2015-04-25 18:47:21 -0300526 else
Simon Glass6a38e412017-08-03 12:22:09 -0600527 env_set("board_rev", "MX6DL");
Jon Nettleton30cba092018-06-07 16:17:36 +0300528
529 if (is_rev_15_som())
530 env_set("som_rev", "V15");
Jon Nettleton51182112018-06-11 15:26:22 +0300531
532 if (has_emmc())
533 env_set("has_emmc", "yes");
534
Baruch Siach6d3f68c2019-11-10 14:38:07 +0200535 free_detect_gpios();
Fabio Estevam9887c1a2015-04-25 18:47:21 -0300536#endif
537
538 return 0;
539}
540
Baruch Siach6d3f68c2019-11-10 14:38:07 +0200541/*
542 * This is not a perfect match. Avoid dependency on the DM GPIO driver needed
543 * for accurate board detection. Hummingboard2 DT is good enough for U-Boot on
544 * all Hummingboard/Cubox-i platforms.
545 */
546int board_fit_config_name_match(const char *name)
547{
548 char tmp_name[36];
549
550 snprintf(tmp_name, sizeof(tmp_name), "%s-hummingboard2-emmc-som-v15",
551 is_mx6dq() ? "imx6q" : "imx6dl");
552
553 return strcmp(name, tmp_name);
554}
555
Walter Lozanof74f7e82020-05-19 15:24:22 -0300556void board_boot_order(u32 *spl_boot_list)
557{
558 struct src *psrc = (struct src *)SRC_BASE_ADDR;
559 unsigned int reg = readl(&psrc->sbmr1) >> 11;
560 u32 boot_mode = imx6_src_get_boot_mode() & IMX6_BMODE_MASK;
561 unsigned int bmode = readl(&src_base->sbmr2);
562
563 /* If bmode is serial or USB phy is active, return serial */
564 if (((bmode >> 24) & 0x03) == 0x01 || is_usbotg_phy_active()) {
565 spl_boot_list[0] = BOOT_DEVICE_BOARD;
566 return;
567 }
568
569 switch (boot_mode >> IMX6_BMODE_SHIFT) {
570 case IMX6_BMODE_SD:
571 case IMX6_BMODE_ESD:
572 case IMX6_BMODE_MMC:
573 case IMX6_BMODE_EMMC:
574 /*
575 * Upon reading BOOT_CFG register the following map is done:
576 * Bit 11 and 12 of BOOT_CFG register can determine the current
577 * mmc port
578 * 0x1 SD2
579 * 0x2 SD3
580 */
581
582 reg &= 0x3; /* Only care about bottom 2 bits */
583 switch (reg) {
584 case 1:
585 SETUP_IOMUX_PADS(usdhc2_pads);
586 spl_boot_list[0] = BOOT_DEVICE_MMC1;
587 break;
588 case 2:
589 SETUP_IOMUX_PADS(usdhc3_pads);
590 spl_boot_list[0] = BOOT_DEVICE_MMC2;
591 break;
592 }
593 break;
594 default:
595 /* By default use USB downloader */
596 spl_boot_list[0] = BOOT_DEVICE_BOARD;
597 break;
598 }
599
600 /* As a last resort, use serial downloader */
601 spl_boot_list[1] = BOOT_DEVICE_BOARD;
602}
603
Fabio Estevam1d97a592015-04-20 14:48:57 -0300604#ifdef CONFIG_SPL_BUILD
Fabio Estevam5f402c42015-04-25 18:47:17 -0300605#include <asm/arch/mx6-ddr.h>
Fabio Estevamcb601912015-04-25 18:47:18 -0300606static const struct mx6dq_iomux_ddr_regs mx6q_ddr_ioregs = {
Fabio Estevam1d97a592015-04-20 14:48:57 -0300607 .dram_sdclk_0 = 0x00020030,
608 .dram_sdclk_1 = 0x00020030,
609 .dram_cas = 0x00020030,
610 .dram_ras = 0x00020030,
Jon Nettletoncd2020d2018-04-10 17:05:35 -0300611 .dram_reset = 0x000c0030,
Fabio Estevam1d97a592015-04-20 14:48:57 -0300612 .dram_sdcke0 = 0x00003000,
613 .dram_sdcke1 = 0x00003000,
614 .dram_sdba2 = 0x00000000,
615 .dram_sdodt0 = 0x00003030,
616 .dram_sdodt1 = 0x00003030,
617 .dram_sdqs0 = 0x00000030,
618 .dram_sdqs1 = 0x00000030,
619 .dram_sdqs2 = 0x00000030,
620 .dram_sdqs3 = 0x00000030,
621 .dram_sdqs4 = 0x00000030,
622 .dram_sdqs5 = 0x00000030,
623 .dram_sdqs6 = 0x00000030,
624 .dram_sdqs7 = 0x00000030,
625 .dram_dqm0 = 0x00020030,
626 .dram_dqm1 = 0x00020030,
627 .dram_dqm2 = 0x00020030,
628 .dram_dqm3 = 0x00020030,
629 .dram_dqm4 = 0x00020030,
630 .dram_dqm5 = 0x00020030,
631 .dram_dqm6 = 0x00020030,
632 .dram_dqm7 = 0x00020030,
633};
634
Fabio Estevamcb601912015-04-25 18:47:18 -0300635static const struct mx6sdl_iomux_ddr_regs mx6dl_ddr_ioregs = {
636 .dram_sdclk_0 = 0x00000028,
637 .dram_sdclk_1 = 0x00000028,
638 .dram_cas = 0x00000028,
639 .dram_ras = 0x00000028,
640 .dram_reset = 0x000c0028,
641 .dram_sdcke0 = 0x00003000,
642 .dram_sdcke1 = 0x00003000,
643 .dram_sdba2 = 0x00000000,
644 .dram_sdodt0 = 0x00003030,
645 .dram_sdodt1 = 0x00003030,
646 .dram_sdqs0 = 0x00000028,
647 .dram_sdqs1 = 0x00000028,
648 .dram_sdqs2 = 0x00000028,
649 .dram_sdqs3 = 0x00000028,
650 .dram_sdqs4 = 0x00000028,
651 .dram_sdqs5 = 0x00000028,
652 .dram_sdqs6 = 0x00000028,
653 .dram_sdqs7 = 0x00000028,
654 .dram_dqm0 = 0x00000028,
655 .dram_dqm1 = 0x00000028,
656 .dram_dqm2 = 0x00000028,
657 .dram_dqm3 = 0x00000028,
658 .dram_dqm4 = 0x00000028,
659 .dram_dqm5 = 0x00000028,
660 .dram_dqm6 = 0x00000028,
661 .dram_dqm7 = 0x00000028,
662};
663
664static const struct mx6dq_iomux_grp_regs mx6q_grp_ioregs = {
Fabio Estevam1d97a592015-04-20 14:48:57 -0300665 .grp_ddr_type = 0x000C0000,
666 .grp_ddrmode_ctl = 0x00020000,
667 .grp_ddrpke = 0x00000000,
668 .grp_addds = 0x00000030,
669 .grp_ctlds = 0x00000030,
670 .grp_ddrmode = 0x00020000,
671 .grp_b0ds = 0x00000030,
672 .grp_b1ds = 0x00000030,
673 .grp_b2ds = 0x00000030,
674 .grp_b3ds = 0x00000030,
675 .grp_b4ds = 0x00000030,
676 .grp_b5ds = 0x00000030,
677 .grp_b6ds = 0x00000030,
678 .grp_b7ds = 0x00000030,
679};
680
Fabio Estevamcb601912015-04-25 18:47:18 -0300681static const struct mx6sdl_iomux_grp_regs mx6sdl_grp_ioregs = {
682 .grp_ddr_type = 0x000c0000,
683 .grp_ddrmode_ctl = 0x00020000,
684 .grp_ddrpke = 0x00000000,
685 .grp_addds = 0x00000028,
686 .grp_ctlds = 0x00000028,
687 .grp_ddrmode = 0x00020000,
688 .grp_b0ds = 0x00000028,
689 .grp_b1ds = 0x00000028,
690 .grp_b2ds = 0x00000028,
691 .grp_b3ds = 0x00000028,
692 .grp_b4ds = 0x00000028,
693 .grp_b5ds = 0x00000028,
694 .grp_b6ds = 0x00000028,
695 .grp_b7ds = 0x00000028,
696};
697
698/* microSOM with Dual processor and 1GB memory */
699static const struct mx6_mmdc_calibration mx6q_1g_mmcd_calib = {
700 .p0_mpwldectrl0 = 0x00000000,
701 .p0_mpwldectrl1 = 0x00000000,
702 .p1_mpwldectrl0 = 0x00000000,
703 .p1_mpwldectrl1 = 0x00000000,
704 .p0_mpdgctrl0 = 0x0314031c,
705 .p0_mpdgctrl1 = 0x023e0304,
706 .p1_mpdgctrl0 = 0x03240330,
707 .p1_mpdgctrl1 = 0x03180260,
708 .p0_mprddlctl = 0x3630323c,
709 .p1_mprddlctl = 0x3436283a,
710 .p0_mpwrdlctl = 0x36344038,
711 .p1_mpwrdlctl = 0x422a423c,
712};
713
714/* microSOM with Quad processor and 2GB memory */
715static const struct mx6_mmdc_calibration mx6q_2g_mmcd_calib = {
Fabio Estevam1d97a592015-04-20 14:48:57 -0300716 .p0_mpwldectrl0 = 0x00000000,
717 .p0_mpwldectrl1 = 0x00000000,
718 .p1_mpwldectrl0 = 0x00000000,
719 .p1_mpwldectrl1 = 0x00000000,
720 .p0_mpdgctrl0 = 0x0314031c,
721 .p0_mpdgctrl1 = 0x023e0304,
722 .p1_mpdgctrl0 = 0x03240330,
723 .p1_mpdgctrl1 = 0x03180260,
724 .p0_mprddlctl = 0x3630323c,
725 .p1_mprddlctl = 0x3436283a,
726 .p0_mpwrdlctl = 0x36344038,
727 .p1_mpwrdlctl = 0x422a423c,
728};
729
Fabio Estevamcb601912015-04-25 18:47:18 -0300730/* microSOM with Solo processor and 512MB memory */
731static const struct mx6_mmdc_calibration mx6dl_512m_mmcd_calib = {
732 .p0_mpwldectrl0 = 0x0045004D,
733 .p0_mpwldectrl1 = 0x003A0047,
734 .p0_mpdgctrl0 = 0x023C0224,
735 .p0_mpdgctrl1 = 0x02000220,
736 .p0_mprddlctl = 0x44444846,
737 .p0_mpwrdlctl = 0x32343032,
738};
739
740/* microSOM with Dual lite processor and 1GB memory */
741static const struct mx6_mmdc_calibration mx6dl_1g_mmcd_calib = {
742 .p0_mpwldectrl0 = 0x0045004D,
743 .p0_mpwldectrl1 = 0x003A0047,
744 .p1_mpwldectrl0 = 0x001F001F,
745 .p1_mpwldectrl1 = 0x00210035,
746 .p0_mpdgctrl0 = 0x023C0224,
747 .p0_mpdgctrl1 = 0x02000220,
748 .p1_mpdgctrl0 = 0x02200220,
Fabio Estevam4461e1e2015-05-29 13:00:36 -0300749 .p1_mpdgctrl1 = 0x02040208,
Fabio Estevamcb601912015-04-25 18:47:18 -0300750 .p0_mprddlctl = 0x44444846,
751 .p1_mprddlctl = 0x4042463C,
752 .p0_mpwrdlctl = 0x32343032,
753 .p1_mpwrdlctl = 0x36363430,
754};
755
756static struct mx6_ddr3_cfg mem_ddr_2g = {
Fabio Estevam1d97a592015-04-20 14:48:57 -0300757 .mem_speed = 1600,
758 .density = 2,
759 .width = 16,
760 .banks = 8,
761 .rowaddr = 14,
762 .coladdr = 10,
763 .pagesz = 2,
764 .trcd = 1375,
765 .trcmin = 4875,
766 .trasmin = 3500,
Fabio Estevam1d97a592015-04-20 14:48:57 -0300767};
768
Fabio Estevamcb601912015-04-25 18:47:18 -0300769static struct mx6_ddr3_cfg mem_ddr_4g = {
770 .mem_speed = 1600,
771 .density = 4,
772 .width = 16,
773 .banks = 8,
Jon Nettletondfe7fab2018-06-07 16:17:37 +0300774 .rowaddr = 16,
Fabio Estevamcb601912015-04-25 18:47:18 -0300775 .coladdr = 10,
776 .pagesz = 2,
777 .trcd = 1375,
778 .trcmin = 4875,
779 .trasmin = 3500,
780};
781
Fabio Estevam1d97a592015-04-20 14:48:57 -0300782static void ccgr_init(void)
783{
784 struct mxc_ccm_reg *ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
785
786 writel(0x00C03F3F, &ccm->CCGR0);
787 writel(0x0030FC03, &ccm->CCGR1);
788 writel(0x0FFFC000, &ccm->CCGR2);
789 writel(0x3FF00000, &ccm->CCGR3);
790 writel(0x00FFF300, &ccm->CCGR4);
791 writel(0x0F0000C3, &ccm->CCGR5);
792 writel(0x000003FF, &ccm->CCGR6);
793}
794
Fabio Estevamcb601912015-04-25 18:47:18 -0300795static void spl_dram_init(int width)
Fabio Estevam1d97a592015-04-20 14:48:57 -0300796{
797 struct mx6_ddr_sysinfo sysinfo = {
798 /* width of data bus: 0=16, 1=32, 2=64 */
Fabio Estevamcb601912015-04-25 18:47:18 -0300799 .dsize = width / 32,
Fabio Estevam1d97a592015-04-20 14:48:57 -0300800 /* config for full 4GB range so that get_mem_size() works */
801 .cs_density = 32, /* 32Gb per CS */
802 .ncs = 1, /* single chip select */
803 .cs1_mirror = 0,
804 .rtt_wr = 1 /*DDR3_RTT_60_OHM*/, /* RTT_Wr = RZQ/4 */
805 .rtt_nom = 1 /*DDR3_RTT_60_OHM*/, /* RTT_Nom = RZQ/4 */
806 .walat = 1, /* Write additional latency */
807 .ralat = 5, /* Read additional latency */
808 .mif3_mode = 3, /* Command prediction working mode */
809 .bi_on = 1, /* Bank interleaving enabled */
810 .sde_to_rst = 0x10, /* 14 cycles, 200us (JEDEC default) */
811 .rst_to_cke = 0x23, /* 33 cycles, 500us (JEDEC default) */
Peng Fan77e86952015-08-17 16:11:03 +0800812 .ddr_type = DDR_TYPE_DDR3,
Fabio Estevamcb3c1212016-08-29 20:37:15 -0300813 .refsel = 1, /* Refresh cycles at 32KHz */
814 .refr = 7, /* 8 refresh commands per refresh cycle */
Fabio Estevam1d97a592015-04-20 14:48:57 -0300815 };
816
Breno Limaba776122016-07-22 09:11:30 -0300817 if (is_mx6dq())
Fabio Estevamcb601912015-04-25 18:47:18 -0300818 mx6dq_dram_iocfg(width, &mx6q_ddr_ioregs, &mx6q_grp_ioregs);
819 else
820 mx6sdl_dram_iocfg(width, &mx6dl_ddr_ioregs, &mx6sdl_grp_ioregs);
821
822 if (is_cpu_type(MXC_CPU_MX6D))
823 mx6_dram_cfg(&sysinfo, &mx6q_1g_mmcd_calib, &mem_ddr_2g);
824 else if (is_cpu_type(MXC_CPU_MX6Q))
825 mx6_dram_cfg(&sysinfo, &mx6q_2g_mmcd_calib, &mem_ddr_4g);
826 else if (is_cpu_type(MXC_CPU_MX6DL))
Fabio Estevam4461e1e2015-05-29 13:00:36 -0300827 mx6_dram_cfg(&sysinfo, &mx6dl_1g_mmcd_calib, &mem_ddr_2g);
Fabio Estevamcb601912015-04-25 18:47:18 -0300828 else if (is_cpu_type(MXC_CPU_MX6SOLO))
829 mx6_dram_cfg(&sysinfo, &mx6dl_512m_mmcd_calib, &mem_ddr_2g);
Fabio Estevam1d97a592015-04-20 14:48:57 -0300830}
831
832void board_init_f(ulong dummy)
833{
834 /* setup AIPS and disable watchdog */
835 arch_cpu_init();
836
837 ccgr_init();
838 gpr_init();
839
840 /* iomux and setup of i2c */
841 board_early_init_f();
842
843 /* setup GP timer */
844 timer_init();
845
Baruch Siachad5944d2022-11-03 18:03:38 +0200846 /* Enable device tree and early DM support*/
847 spl_early_init();
848
Fabio Estevam1d97a592015-04-20 14:48:57 -0300849 /* UART clocks enabled and gd valid - init serial console */
850 preloader_console_init();
851
852 /* DDR initialization */
Fabio Estevamcb601912015-04-25 18:47:18 -0300853 if (is_cpu_type(MXC_CPU_MX6SOLO))
854 spl_dram_init(32);
855 else
856 spl_dram_init(64);
Fabio Estevam1d97a592015-04-20 14:48:57 -0300857
858 /* Clear the BSS. */
859 memset(__bss_start, 0, __bss_end - __bss_start);
860
861 /* load/boot image from boot device */
862 board_init_r(NULL, 0);
863}
864#endif