1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * XEA iMX28 board
4  *
5  * Copyright (C) 2019 DENX Software Engineering
6  * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
7  *
8  * Copyright (C) 2018 DENX Software Engineering
9  * Måns Rullgård, DENX Software Engineering, mans@mansr.com
10  *
11  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
12  * on behalf of DENX Software Engineering GmbH
13  *
14  */
15 
16 #include <common.h>
17 #include <fdt_support.h>
18 #include <init.h>
19 #include <log.h>
20 #include <net.h>
21 #include <asm/global_data.h>
22 #include <asm/gpio.h>
23 #include <asm/io.h>
24 #include <asm/arch/imx-regs.h>
25 #include <asm/arch/iomux-mx28.h>
26 #include <asm/arch/clock.h>
27 #include <asm/arch/sys_proto.h>
28 #include <linux/delay.h>
29 #include <linux/mii.h>
30 #include <miiphy.h>
31 #include <netdev.h>
32 #include <errno.h>
33 #include <usb.h>
34 #include <serial.h>
35 
36 #ifdef CONFIG_SPL_BUILD
37 #include <spl.h>
38 #endif
39 
40 DECLARE_GLOBAL_DATA_PTR;
41 
42 /*
43  * Functions
44  */
45 
init_clocks(void)46 static void init_clocks(void)
47 {
48 	/* IO0 clock at 480MHz */
49 	mxs_set_ioclk(MXC_IOCLK0, 480000);
50 	/* IO1 clock at 480MHz */
51 	mxs_set_ioclk(MXC_IOCLK1, 480000);
52 
53 	/* SSP0 clock at 96MHz */
54 	mxs_set_sspclk(MXC_SSPCLK0, 96000, 0);
55 	/* SSP2 clock at 160MHz */
56 	mxs_set_sspclk(MXC_SSPCLK2, 160000, 0);
57 	/* SSP3 clock at 96MHz */
58 	mxs_set_sspclk(MXC_SSPCLK3, 96000, 0);
59 }
60 
61 #ifdef CONFIG_SPL_BUILD
board_init_f(ulong arg)62 void board_init_f(ulong arg)
63 {
64 	init_clocks();
65 	preloader_console_init();
66 }
67 
68 static int boot_tiva0, boot_tiva1;
69 
70 /* Check if TIVAs request booting via U-Boot proper */
spl_board_init(void)71 void spl_board_init(void)
72 {
73 	struct gpio_desc btiva0, btiva1, en_3_3v;
74 	int ret;
75 
76 	/*
77 	 * Setup GPIO0_0 (TIVA power enable pin) to be output high
78 	 * to allow TIVA startup.
79 	 */
80 	ret = dm_gpio_lookup_name("GPIO0_0", &en_3_3v);
81 	if (ret)
82 		printf("Cannot get GPIO0_0\n");
83 
84 	ret = dm_gpio_request(&en_3_3v, "pwr_3_3v");
85 	if (ret)
86 		printf("Cannot request GPIO0_0\n");
87 
88 	/* Set GPIO0_0 to HIGH */
89 	dm_gpio_set_dir_flags(&en_3_3v, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
90 
91 	ret = dm_gpio_lookup_name("GPIO0_23", &btiva0);
92 	if (ret)
93 		printf("Cannot get GPIO0_23\n");
94 
95 	ret = dm_gpio_lookup_name("GPIO0_25", &btiva1);
96 	if (ret)
97 		printf("Cannot get GPIO0_25\n");
98 
99 	ret = dm_gpio_request(&btiva0, "boot-tiva0");
100 	if (ret)
101 		printf("Cannot request GPIO0_23\n");
102 
103 	ret = dm_gpio_request(&btiva1, "boot-tiva1");
104 	if (ret)
105 		printf("Cannot request GPIO0_25\n");
106 
107 	dm_gpio_set_dir_flags(&btiva0, GPIOD_IS_IN);
108 	dm_gpio_set_dir_flags(&btiva1, GPIOD_IS_IN);
109 
110 	udelay(1000);
111 
112 	boot_tiva0 = dm_gpio_get_value(&btiva0);
113 	boot_tiva1 = dm_gpio_get_value(&btiva1);
114 }
115 
board_boot_order(u32 * spl_boot_list)116 void board_boot_order(u32 *spl_boot_list)
117 {
118 	spl_boot_list[0] = BOOT_DEVICE_MMC1;
119 	spl_boot_list[1] = BOOT_DEVICE_SPI;
120 	spl_boot_list[2] = BOOT_DEVICE_UART;
121 }
122 
spl_start_uboot(void)123 int spl_start_uboot(void)
124 {
125 	/* break into full u-boot on 'c' */
126 	if (serial_tstc() && serial_getc() == 'c')
127 		return 1;
128 
129 	debug("%s: btiva0: %d btiva1: %d\n", __func__, boot_tiva0, boot_tiva1);
130 	return !boot_tiva0 || !boot_tiva1;
131 }
132 #else
133 
board_early_init_f(void)134 int board_early_init_f(void)
135 {
136 	init_clocks();
137 
138 	return 0;
139 }
140 
board_init(void)141 int board_init(void)
142 {
143 	struct gpio_desc phy_rst;
144 	int ret;
145 
146 	/* Address of boot parameters */
147 	gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
148 
149 	cpu_eth_init(NULL);
150 
151 	/* PHY INT#/PWDN# */
152 	ret = dm_gpio_lookup_name("GPIO4_13", &phy_rst);
153 	if (ret) {
154 		printf("Cannot get GPIO4_13\n");
155 		return ret;
156 	}
157 
158 	ret = dm_gpio_request(&phy_rst, "phy-rst");
159 	if (ret) {
160 		printf("Cannot request GPIO4_13\n");
161 		return ret;
162 	}
163 
164 	dm_gpio_set_dir_flags(&phy_rst, GPIOD_IS_IN);
165 	udelay(1000);
166 
167 	return 0;
168 }
169 
dram_init(void)170 int dram_init(void)
171 {
172 	return mxs_dram_init();
173 }
174 
175 #ifdef CONFIG_OF_BOARD_SETUP
fdt_fixup_l2switch(void * blob)176 static int fdt_fixup_l2switch(void *blob)
177 {
178 	u8 ethaddr[6];
179 	int ret;
180 
181 	if (eth_env_get_enetaddr("ethaddr", ethaddr)) {
182 		ret = fdt_find_and_setprop(blob,
183 					   "/ahb@80080000/switch@800f0000",
184 					   "local-mac-address", ethaddr, 6, 1);
185 		if (ret < 0)
186 			printf("%s: can't find usbether@1 node: %d\n",
187 			       __func__, ret);
188 	}
189 
190 	return 0;
191 }
192 
ft_board_setup(void * blob,struct bd_info * bd)193 int ft_board_setup(void *blob, struct bd_info *bd)
194 {
195 	/*
196 	 * i.MX28 L2 switch needs manual update (fixup) of eth MAC address
197 	 * (in 'local-mac-address' property) as it uses "switch@800f0000"
198 	 * node, not set by default FIT image handling code in
199 	 * "ethernet@800f0000"
200 	 */
201 	fdt_fixup_l2switch(blob);
202 
203 	return 0;
204 }
205 #endif
206 
207 #endif	/* CONFIG_SPL_BUILD */
208