blob: b9450a0e36b4e769c1313954dedddaa017b6f1a8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbell6efe3692014-05-05 11:52:26 +01002/*
3 * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net>
4 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 *
6 * (C) Copyright 2007-2011
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * Tom Cubie <tangliang@allwinnertech.com>
9 *
10 * Some board init for the Allwinner A10-evb board.
Ian Campbell6efe3692014-05-05 11:52:26 +010011 */
12
13#include <common.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053014#include <dm.h>
Simon Glass313112a2019-08-01 09:46:46 -060015#include <env.h>
Hans de Goede63deaa82014-10-02 21:13:54 +020016#include <mmc.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020017#include <axp_pmic.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053018#include <generic-phy.h>
19#include <phy-sun4i-usb.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010020#include <asm/arch/clock.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020021#include <asm/arch/cpu.h>
Luc Verhaegen4869a8c2014-08-13 07:55:07 +020022#include <asm/arch/display.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010023#include <asm/arch/dram.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010024#include <asm/arch/gpio.h>
25#include <asm/arch/mmc.h>
Hans de Goedea146c502016-07-09 09:56:56 +020026#include <asm/arch/spl.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070027#include <u-boot/crc.h>
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020028#ifndef CONFIG_ARM64
29#include <asm/armv7.h>
30#endif
Hans de Goeded9d05652015-04-23 23:23:50 +020031#include <asm/gpio.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020032#include <asm/io.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010033#include <u-boot/crc.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060034#include <env_internal.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090035#include <linux/libfdt.h>
Hans de Goede5ed52f62015-08-15 11:55:26 +020036#include <nand.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020037#include <net.h>
Maxime Ripardae56d972017-08-23 10:08:29 +020038#include <spl.h>
Jelle van der Waa3f3a3092016-02-23 18:47:19 +010039#include <sy8106a.h>
Simon Glassd9a766f2017-05-17 08:23:00 -060040#include <asm/setup.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010041
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010042#if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD)
43/* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */
44int soft_i2c_gpio_sda;
45int soft_i2c_gpio_scl;
Hans de Goeded9d05652015-04-23 23:23:50 +020046
47static int soft_i2c_board_init(void)
48{
49 int ret;
50
51 soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA);
52 if (soft_i2c_gpio_sda < 0) {
53 printf("Error invalid soft i2c sda pin: '%s', err %d\n",
54 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda);
55 return soft_i2c_gpio_sda;
56 }
57 ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda");
58 if (ret) {
59 printf("Error requesting soft i2c sda pin: '%s', err %d\n",
60 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret);
61 return ret;
62 }
63
64 soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL);
65 if (soft_i2c_gpio_scl < 0) {
66 printf("Error invalid soft i2c scl pin: '%s', err %d\n",
67 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl);
68 return soft_i2c_gpio_scl;
69 }
70 ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl");
71 if (ret) {
72 printf("Error requesting soft i2c scl pin: '%s', err %d\n",
73 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret);
74 return ret;
75 }
76
77 return 0;
78}
79#else
80static int soft_i2c_board_init(void) { return 0; }
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010081#endif
82
Ian Campbell6efe3692014-05-05 11:52:26 +010083DECLARE_GLOBAL_DATA_PTR;
84
Jernej Skrabec07da8802017-04-27 00:03:35 +020085void i2c_init_board(void)
86{
87#ifdef CONFIG_I2C0_ENABLE
88#if defined(CONFIG_MACH_SUN4I) || \
89 defined(CONFIG_MACH_SUN5I) || \
90 defined(CONFIG_MACH_SUN7I) || \
91 defined(CONFIG_MACH_SUN8I_R40)
92 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
93 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
94 clock_twi_onoff(0, 1);
95#elif defined(CONFIG_MACH_SUN6I)
96 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
97 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
98 clock_twi_onoff(0, 1);
99#elif defined(CONFIG_MACH_SUN8I)
100 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
101 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
102 clock_twi_onoff(0, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200103#elif defined(CONFIG_MACH_SUN50I)
104 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
105 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
106 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200107#endif
108#endif
109
110#ifdef CONFIG_I2C1_ENABLE
111#if defined(CONFIG_MACH_SUN4I) || \
112 defined(CONFIG_MACH_SUN7I) || \
113 defined(CONFIG_MACH_SUN8I_R40)
114 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
115 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
116 clock_twi_onoff(1, 1);
117#elif defined(CONFIG_MACH_SUN5I)
118 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
119 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
120 clock_twi_onoff(1, 1);
121#elif defined(CONFIG_MACH_SUN6I)
122 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
123 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
124 clock_twi_onoff(1, 1);
125#elif defined(CONFIG_MACH_SUN8I)
126 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
127 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
128 clock_twi_onoff(1, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200129#elif defined(CONFIG_MACH_SUN50I)
130 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
131 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
132 clock_twi_onoff(1, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200133#endif
134#endif
135
136#ifdef CONFIG_I2C2_ENABLE
137#if defined(CONFIG_MACH_SUN4I) || \
138 defined(CONFIG_MACH_SUN7I) || \
139 defined(CONFIG_MACH_SUN8I_R40)
140 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
141 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
142 clock_twi_onoff(2, 1);
143#elif defined(CONFIG_MACH_SUN5I)
144 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
145 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
146 clock_twi_onoff(2, 1);
147#elif defined(CONFIG_MACH_SUN6I)
148 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
149 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
150 clock_twi_onoff(2, 1);
151#elif defined(CONFIG_MACH_SUN8I)
152 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
153 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
154 clock_twi_onoff(2, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200155#elif defined(CONFIG_MACH_SUN50I)
156 sunxi_gpio_set_cfgpin(SUNXI_GPE(14), SUN50I_GPE_TWI2);
157 sunxi_gpio_set_cfgpin(SUNXI_GPE(15), SUN50I_GPE_TWI2);
158 clock_twi_onoff(2, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200159#endif
160#endif
161
162#ifdef CONFIG_I2C3_ENABLE
163#if defined(CONFIG_MACH_SUN6I)
164 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
165 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
166 clock_twi_onoff(3, 1);
167#elif defined(CONFIG_MACH_SUN7I) || \
168 defined(CONFIG_MACH_SUN8I_R40)
169 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
170 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
171 clock_twi_onoff(3, 1);
172#endif
173#endif
174
175#ifdef CONFIG_I2C4_ENABLE
176#if defined(CONFIG_MACH_SUN7I) || \
177 defined(CONFIG_MACH_SUN8I_R40)
178 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
179 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
180 clock_twi_onoff(4, 1);
181#endif
182#endif
183
184#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800185#ifdef CONFIG_MACH_SUN50I
186 clock_twi_onoff(5, 1);
187 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
188 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
189#else
Jernej Skrabec07da8802017-04-27 00:03:35 +0200190 clock_twi_onoff(5, 1);
191 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
192 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
193#endif
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800194#endif
Jernej Skrabec07da8802017-04-27 00:03:35 +0200195}
196
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100197#if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT)
198enum env_location env_get_location(enum env_operation op, int prio)
199{
200 switch (prio) {
201 case 0:
202 return ENVL_FAT;
203
204 case 1:
205 return ENVL_MMC;
206
207 default:
208 return ENVL_UNKNOWN;
209 }
210}
211#endif
212
Andre Przywarad7cea362019-01-29 15:54:14 +0000213#ifdef CONFIG_DM_MMC
214static void mmc_pinmux_setup(int sdc);
215#endif
216
Ian Campbell6efe3692014-05-05 11:52:26 +0100217/* add board specific code here */
218int board_init(void)
219{
Mylène Josserand147c6062017-04-02 12:59:10 +0200220 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbell6efe3692014-05-05 11:52:26 +0100221
222 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
223
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200224#ifndef CONFIG_ARM64
Ian Campbell6efe3692014-05-05 11:52:26 +0100225 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
226 debug("id_pfr1: 0x%08x\n", id_pfr1);
227 /* Generic Timer Extension available? */
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200228 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
229 uint32_t freq;
230
Ian Campbell6efe3692014-05-05 11:52:26 +0100231 debug("Setting CNTFRQ\n");
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200232
233 /*
234 * CNTFRQ is a secure register, so we will crash if we try to
235 * write this from the non-secure world (read is OK, though).
236 * In case some bootcode has already set the correct value,
237 * we avoid the risk of writing to it.
238 */
239 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Andre Przywara70c78932017-02-16 01:20:19 +0000240 if (freq != COUNTER_FREQUENCY) {
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200241 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Andre Przywara70c78932017-02-16 01:20:19 +0000242 freq, COUNTER_FREQUENCY);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200243#ifdef CONFIG_NON_SECURE
244 printf("arch timer frequency is wrong, but cannot adjust it\n");
245#else
246 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Andre Przywara70c78932017-02-16 01:20:19 +0000247 : : "r"(COUNTER_FREQUENCY));
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200248#endif
249 }
Ian Campbell6efe3692014-05-05 11:52:26 +0100250 }
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200251#endif /* !CONFIG_ARM64 */
Ian Campbell6efe3692014-05-05 11:52:26 +0100252
Hans de Goede3ae1d132015-04-25 17:25:14 +0200253 ret = axp_gpio_init();
254 if (ret)
255 return ret;
256
Hans de Goede9c34c3e2016-03-22 20:10:30 +0100257#ifdef CONFIG_SATAPWR
Mylène Josserand628426a2017-04-02 12:59:09 +0200258 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
259 gpio_request(satapwr_pin, "satapwr");
260 gpio_direction_output(satapwr_pin, 1);
Werner Böllmanne58f8302017-11-10 19:14:20 +0530261 /* Give attached sata device time to power-up to avoid link timeouts */
262 mdelay(500);
Hans de Goede9c34c3e2016-03-22 20:10:30 +0100263#endif
Hans de Goede42cbbe32016-03-17 13:53:03 +0100264#ifdef CONFIG_MACPWR
Mylène Josserand147c6062017-04-02 12:59:10 +0200265 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
266 gpio_request(macpwr_pin, "macpwr");
267 gpio_direction_output(macpwr_pin, 1);
Hans de Goede42cbbe32016-03-17 13:53:03 +0100268#endif
269
Jernej Skrabec9220d502017-04-27 00:03:36 +0200270#ifdef CONFIG_DM_I2C
271 /*
272 * Temporary workaround for enabling I2C clocks until proper sunxi DM
273 * clk, reset and pinctrl drivers land.
274 */
275 i2c_init_board();
276#endif
Andre Przywarad7cea362019-01-29 15:54:14 +0000277
278#ifdef CONFIG_DM_MMC
279 /*
280 * Temporary workaround for enabling MMC clocks until a sunxi DM
281 * pinctrl driver lands.
282 */
283 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
284#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
285 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
286#endif
287#endif /* CONFIG_DM_MMC */
Jernej Skrabec9220d502017-04-27 00:03:36 +0200288
Hans de Goeded9d05652015-04-23 23:23:50 +0200289 /* Uses dm gpio code so do this here and not in i2c_init_board() */
290 return soft_i2c_board_init();
Ian Campbell6efe3692014-05-05 11:52:26 +0100291}
292
Andre Przywara14a25392018-10-25 17:23:04 +0800293/*
294 * On older SoCs the SPL is actually at address zero, so using NULL as
295 * an error value does not work.
296 */
297#define INVALID_SPL_HEADER ((void *)~0UL)
298
299static struct boot_file_head * get_spl_header(uint8_t req_version)
300{
301 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
302 uint8_t spl_header_version = spl->spl_signature[3];
303
304 /* Is there really the SPL header (still) there? */
305 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
306 return INVALID_SPL_HEADER;
307
308 if (spl_header_version < req_version) {
309 printf("sunxi SPL version mismatch: expected %u, got %u\n",
310 req_version, spl_header_version);
311 return INVALID_SPL_HEADER;
312 }
313
314 return spl;
315}
316
Ian Campbell6efe3692014-05-05 11:52:26 +0100317int dram_init(void)
318{
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800319 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
320
321 if (spl == INVALID_SPL_HEADER)
322 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
323 PHYS_SDRAM_0_SIZE);
324 else
325 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
326
327 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
328 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbell6efe3692014-05-05 11:52:26 +0100329
330 return 0;
331}
332
Boris Brezillon57f20382016-06-15 21:09:23 +0200333#if defined(CONFIG_NAND_SUNXI)
Karol Gugala7bea8932015-07-23 14:33:01 +0200334static void nand_pinmux_setup(void)
335{
336 unsigned int pin;
Karol Gugala7bea8932015-07-23 14:33:01 +0200337
Hans de Goeded2236782015-08-15 13:17:49 +0200338 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugala7bea8932015-07-23 14:33:01 +0200339 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
340
Hans de Goeded2236782015-08-15 13:17:49 +0200341#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
342 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
343 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
344#endif
345 /* sun4i / sun7i do have a PC23, but it is not used for nand,
346 * only sun7i has a PC24 */
347#ifdef CONFIG_MACH_SUN7I
Karol Gugala7bea8932015-07-23 14:33:01 +0200348 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goeded2236782015-08-15 13:17:49 +0200349#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200350}
351
352static void nand_clock_setup(void)
353{
354 struct sunxi_ccm_reg *const ccm =
355 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedee5561a82015-08-15 11:58:03 +0200356
Karol Gugala7bea8932015-07-23 14:33:01 +0200357 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalebeeb802018-02-28 20:51:53 +0100358#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
359 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
360 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
361#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200362 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
363}
Hans de Goede5ed52f62015-08-15 11:55:26 +0200364
365void board_nand_init(void)
366{
367 nand_pinmux_setup();
368 nand_clock_setup();
Boris Brezillon57f20382016-06-15 21:09:23 +0200369#ifndef CONFIG_SPL_BUILD
370 sunxi_nand_init();
371#endif
Hans de Goede5ed52f62015-08-15 11:55:26 +0200372}
Karol Gugala7bea8932015-07-23 14:33:01 +0200373#endif
374
Masahiro Yamada0a780172017-05-09 20:31:39 +0900375#ifdef CONFIG_MMC
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100376static void mmc_pinmux_setup(int sdc)
377{
378 unsigned int pin;
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100379 __maybe_unused int pins;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100380
381 switch (sdc) {
382 case 0:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100383 /* SDC0: PF0-PF5 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100384 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100385 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100386 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
387 sunxi_gpio_set_drv(pin, 2);
388 }
389 break;
390
391 case 1:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100392 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS);
393
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800394#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
395 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100396 if (pins == SUNXI_GPIO_H) {
397 /* SDC1: PH22-PH-27 */
398 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
399 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
400 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
401 sunxi_gpio_set_drv(pin, 2);
402 }
403 } else {
404 /* SDC1: PG0-PG5 */
405 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
406 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
407 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
408 sunxi_gpio_set_drv(pin, 2);
409 }
410 }
411#elif defined(CONFIG_MACH_SUN5I)
412 /* SDC1: PG3-PG8 */
Hans de Goede4dccfd42014-10-03 16:44:57 +0200413 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100414 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100415 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
416 sunxi_gpio_set_drv(pin, 2);
417 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100418#elif defined(CONFIG_MACH_SUN6I)
419 /* SDC1: PG0-PG5 */
420 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
421 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
422 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
423 sunxi_gpio_set_drv(pin, 2);
424 }
425#elif defined(CONFIG_MACH_SUN8I)
426 if (pins == SUNXI_GPIO_D) {
427 /* SDC1: PD2-PD7 */
428 for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) {
429 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1);
430 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
431 sunxi_gpio_set_drv(pin, 2);
432 }
433 } else {
434 /* SDC1: PG0-PG5 */
435 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
436 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
437 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
438 sunxi_gpio_set_drv(pin, 2);
439 }
440 }
441#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100442 break;
443
444 case 2:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100445 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS);
446
447#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
448 /* SDC2: PC6-PC11 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100449 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100450 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100451 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
452 sunxi_gpio_set_drv(pin, 2);
453 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100454#elif defined(CONFIG_MACH_SUN5I)
455 if (pins == SUNXI_GPIO_E) {
456 /* SDC2: PE4-PE9 */
457 for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) {
458 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2);
459 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
460 sunxi_gpio_set_drv(pin, 2);
461 }
462 } else {
463 /* SDC2: PC6-PC15 */
464 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
465 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
466 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
467 sunxi_gpio_set_drv(pin, 2);
468 }
469 }
470#elif defined(CONFIG_MACH_SUN6I)
471 if (pins == SUNXI_GPIO_A) {
472 /* SDC2: PA9-PA14 */
473 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
474 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2);
475 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
476 sunxi_gpio_set_drv(pin, 2);
477 }
478 } else {
479 /* SDC2: PC6-PC15, PC24 */
480 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
481 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
482 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
483 sunxi_gpio_set_drv(pin, 2);
484 }
485
486 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
487 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
488 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
489 }
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800490#elif defined(CONFIG_MACH_SUN8I_R40)
491 /* SDC2: PC6-PC15, PC24 */
492 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
493 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
494 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
495 sunxi_gpio_set_drv(pin, 2);
496 }
497
498 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
499 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
500 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200501#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100502 /* SDC2: PC5-PC6, PC8-PC16 */
503 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
504 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
505 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
506 sunxi_gpio_set_drv(pin, 2);
507 }
508
509 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
510 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
511 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
512 sunxi_gpio_set_drv(pin, 2);
513 }
Icenowy Zhenga838a152018-07-21 16:20:29 +0800514#elif defined(CONFIG_MACH_SUN50I_H6)
515 /* SDC2: PC4-PC14 */
516 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
517 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
518 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
519 sunxi_gpio_set_drv(pin, 2);
520 }
Philipp Tomsicha0c7c712016-10-28 18:21:33 +0800521#elif defined(CONFIG_MACH_SUN9I)
522 /* SDC2: PC6-PC16 */
523 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
524 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
525 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
526 sunxi_gpio_set_drv(pin, 2);
527 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100528#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100529 break;
530
531 case 3:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100532 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS);
533
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800534#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
535 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100536 /* SDC3: PI4-PI9 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100537 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100538 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100539 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
540 sunxi_gpio_set_drv(pin, 2);
541 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100542#elif defined(CONFIG_MACH_SUN6I)
543 if (pins == SUNXI_GPIO_A) {
544 /* SDC3: PA9-PA14 */
545 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
546 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3);
547 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
548 sunxi_gpio_set_drv(pin, 2);
549 }
550 } else {
551 /* SDC3: PC6-PC15, PC24 */
552 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
553 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
554 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
555 sunxi_gpio_set_drv(pin, 2);
556 }
557
558 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
559 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
560 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
561 }
562#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100563 break;
564
565 default:
566 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
567 break;
568 }
569}
570
571int board_mmc_init(bd_t *bis)
572{
Hans de Goede63deaa82014-10-02 21:13:54 +0200573 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goede63deaa82014-10-02 21:13:54 +0200574
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100575 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goede63deaa82014-10-02 21:13:54 +0200576 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
577 if (!mmc0)
578 return -1;
579
Hans de Goedeaf593e42014-10-02 20:43:50 +0200580#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100581 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goede63deaa82014-10-02 21:13:54 +0200582 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
583 if (!mmc1)
584 return -1;
585#endif
586
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100587 return 0;
588}
589#endif
590
Ian Campbell6efe3692014-05-05 11:52:26 +0100591#ifdef CONFIG_SPL_BUILD
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800592
593static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
594{
595 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
596
597 if (spl == INVALID_SPL_HEADER)
598 return;
599
600 /* Promote the header version for U-Boot proper, if needed. */
601 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
602 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
603
604 spl->dram_size = dram_size >> 20;
605}
606
Ian Campbell6efe3692014-05-05 11:52:26 +0100607void sunxi_board_init(void)
608{
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200609 int power_failed = 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100610
Jelle van der Waa3f3a3092016-02-23 18:47:19 +0100611#ifdef CONFIG_SY8106A_POWER
612 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
613#endif
614
vishnupatekar1895dfd2015-11-29 01:07:22 +0800615#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800616 defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
617 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200618 power_failed = axp_init();
619
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800620#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
621 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200622 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede1f247362014-06-13 22:55:51 +0200623#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200624 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
625 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
vishnupatekar1895dfd2015-11-29 01:07:22 +0800626#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200627 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200628#endif
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800629#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
630 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200631 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200632#endif
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200633
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800634#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
635 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200636 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
637#endif
638 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Chen-Yu Tsaic05aa392016-01-12 14:42:40 +0800639#if !defined(CONFIG_AXP152_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200640 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
641#endif
642#ifdef CONFIG_AXP209_POWER
643 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
644#endif
645
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800646#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
647 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800648 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
649 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800650#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800651 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
652 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800653#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200654 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
655 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
656 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
657#endif
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800658
659#ifdef CONFIG_AXP818_POWER
660 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
661 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
662 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800663#endif
664
665#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai0e3efd32016-05-02 10:28:12 +0800666 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800667#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200668#endif
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000669 printf("DRAM:");
670 gd->ram_size = sunxi_dram_init();
671 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
672 if (!gd->ram_size)
673 hang();
674
675 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800676
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200677 /*
678 * Only clock up the CPU to full speed if we are reasonably
679 * assured it's being powered with suitable core voltage
680 */
681 if (!power_failed)
Iain Paton630df142015-03-28 10:26:38 +0000682 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200683 else
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000684 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbell6efe3692014-05-05 11:52:26 +0100685}
686#endif
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200687
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100688#ifdef CONFIG_USB_GADGET
689int g_dnl_board_usb_cable_connected(void)
690{
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530691 struct udevice *dev;
692 struct phy phy;
693 int ret;
694
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100695 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530696 if (ret) {
697 pr_err("%s: Cannot find USB device\n", __func__);
698 return ret;
699 }
700
701 ret = generic_phy_get_by_name(dev, "usb", &phy);
702 if (ret) {
703 pr_err("failed to get %s USB PHY\n", dev->name);
704 return ret;
705 }
706
707 ret = generic_phy_init(&phy);
708 if (ret) {
709 pr_err("failed to init %s USB PHY\n", dev->name);
710 return ret;
711 }
712
713 ret = sun4i_usb_phy_vbus_detect(&phy);
714 if (ret == 1) {
715 pr_err("A charger is plugged into the OTG\n");
716 return -ENODEV;
717 }
718
719 return ret;
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100720}
721#endif
722
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100723#ifdef CONFIG_SERIAL_TAG
724void get_board_serial(struct tag_serialnr *serialnr)
725{
726 char *serial_string;
727 unsigned long long serial;
728
Simon Glass64b723f2017-08-03 12:22:12 -0600729 serial_string = env_get("serial#");
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100730
731 if (serial_string) {
732 serial = simple_strtoull(serial_string, NULL, 16);
733
734 serialnr->high = (unsigned int) (serial >> 32);
735 serialnr->low = (unsigned int) (serial & 0xffffffff);
736 } else {
737 serialnr->high = 0;
738 serialnr->low = 0;
739 }
740}
741#endif
742
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200743/*
744 * Check the SPL header for the "sunxi" variant. If found: parse values
745 * that might have been passed by the loader ("fel" utility), and update
746 * the environment accordingly.
747 */
748static void parse_spl_header(const uint32_t spl_addr)
749{
Andre Przywara14a25392018-10-25 17:23:04 +0800750 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200751
Andre Przywara14a25392018-10-25 17:23:04 +0800752 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200753 return;
Andre Przywara14a25392018-10-25 17:23:04 +0800754
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200755 if (!spl->fel_script_address)
756 return;
757
758 if (spl->fel_uEnv_length != 0) {
759 /*
760 * data is expected in uEnv.txt compatible format, so "env
761 * import -t" the string(s) at fel_script_address right away.
762 */
Andre Przywaraac4e6732016-09-05 01:32:41 +0100763 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200764 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
765 return;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200766 }
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200767 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass4d949a22017-08-03 12:22:10 -0600768 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200769}
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200770
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200771/*
772 * Note this function gets called multiple times.
773 * It must not make any changes to env variables which already exist.
774 */
775static void setup_environment(const void *fdt)
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200776{
Paul Kocialkowski92935942015-03-28 18:35:35 +0100777 char serial_string[17] = { 0 };
Hans de Goede11d70982014-11-26 00:04:24 +0100778 unsigned int sid[4];
Paul Kocialkowski92935942015-03-28 18:35:35 +0100779 uint8_t mac_addr[6];
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200780 char ethaddr[16];
781 int i, ret;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200782
Paul Kocialkowski92935942015-03-28 18:35:35 +0100783 ret = sunxi_get_sid(sid);
Hans de Goedee5fe5482016-07-29 11:47:03 +0200784 if (ret == 0 && sid[0] != 0) {
785 /*
786 * The single words 1 - 3 of the SID have quite a few bits
787 * which are the same on many models, so we take a crc32
788 * of all 3 words, to get a more unique value.
789 *
790 * Note we only do this on newer SoCs as we cannot change
791 * the algorithm on older SoCs since those have been using
792 * fixed mac-addresses based on only using word 3 for a
793 * long time and changing a fixed mac-address with an
794 * u-boot update is not good.
795 */
796#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
797 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
798 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
799 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
800#endif
801
Hans de Goedeabca8432016-07-27 17:58:06 +0200802 /* Ensure the NIC specific bytes of the mac are not all 0 */
803 if ((sid[3] & 0xffffff) == 0)
804 sid[3] |= 0x800000;
805
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200806 for (i = 0; i < 4; i++) {
807 sprintf(ethaddr, "ethernet%d", i);
808 if (!fdt_get_alias(fdt, ethaddr))
809 continue;
810
811 if (i == 0)
812 strcpy(ethaddr, "ethaddr");
813 else
814 sprintf(ethaddr, "eth%daddr", i);
815
Simon Glass64b723f2017-08-03 12:22:12 -0600816 if (env_get(ethaddr))
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200817 continue;
818
Paul Kocialkowski92935942015-03-28 18:35:35 +0100819 /* Non OUI / registered MAC address */
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200820 mac_addr[0] = (i << 4) | 0x02;
Paul Kocialkowski92935942015-03-28 18:35:35 +0100821 mac_addr[1] = (sid[0] >> 0) & 0xff;
822 mac_addr[2] = (sid[3] >> 24) & 0xff;
823 mac_addr[3] = (sid[3] >> 16) & 0xff;
824 mac_addr[4] = (sid[3] >> 8) & 0xff;
825 mac_addr[5] = (sid[3] >> 0) & 0xff;
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200826
Simon Glass8551d552017-08-03 12:22:11 -0600827 eth_env_set_enetaddr(ethaddr, mac_addr);
Paul Kocialkowski92935942015-03-28 18:35:35 +0100828 }
829
Simon Glass64b723f2017-08-03 12:22:12 -0600830 if (!env_get("serial#")) {
Paul Kocialkowski92935942015-03-28 18:35:35 +0100831 snprintf(serial_string, sizeof(serial_string),
832 "%08x%08x", sid[0], sid[3]);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200833
Simon Glass6a38e412017-08-03 12:22:09 -0600834 env_set("serial#", serial_string);
Paul Kocialkowski92935942015-03-28 18:35:35 +0100835 }
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200836 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200837}
838
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200839int misc_init_r(void)
840{
Maxime Ripardae56d972017-08-23 10:08:29 +0200841 uint boot;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200842
Simon Glass6a38e412017-08-03 12:22:09 -0600843 env_set("fel_booted", NULL);
844 env_set("fel_scriptaddr", NULL);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200845 env_set("mmc_bootdev", NULL);
Maxime Ripardae56d972017-08-23 10:08:29 +0200846
847 boot = sunxi_get_boot_device();
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200848 /* determine if we are running in FEL mode */
Maxime Ripardae56d972017-08-23 10:08:29 +0200849 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass6a38e412017-08-03 12:22:09 -0600850 env_set("fel_booted", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200851 parse_spl_header(SPL_ADDR);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200852 /* or if we booted from MMC, and which one */
853 } else if (boot == BOOT_DEVICE_MMC1) {
854 env_set("mmc_bootdev", "0");
855 } else if (boot == BOOT_DEVICE_MMC2) {
856 env_set("mmc_bootdev", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200857 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200858
859 setup_environment(gd->fdt_blob);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200860
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800861#ifdef CONFIG_USB_ETHER
Maxime Ripardf54aba32017-09-06 22:25:03 +0200862 usb_ether_init();
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800863#endif
Maxime Ripardf54aba32017-09-06 22:25:03 +0200864
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200865 return 0;
866}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200867
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200868int ft_board_setup(void *blob, bd_t *bd)
869{
Hans de Goede48a234a2016-03-22 22:51:52 +0100870 int __maybe_unused r;
871
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200872 /*
873 * Call setup_environment again in case the boot fdt has
874 * ethernet aliases the u-boot copy does not have.
875 */
876 setup_environment(blob);
877
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200878#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goede48a234a2016-03-22 22:51:52 +0100879 r = sunxi_simplefb_setup(blob);
880 if (r)
881 return r;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200882#endif
Hans de Goede48a234a2016-03-22 22:51:52 +0100883 return 0;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200884}
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100885
886#ifdef CONFIG_SPL_LOAD_FIT
887int board_fit_config_name_match(const char *name)
888{
Andre Przywara14a25392018-10-25 17:23:04 +0800889 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
890 const char *cmp_str = (const char *)spl;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100891
Andre Przywara4f99ea62017-04-26 01:32:50 +0100892 /* Check if there is a DT name stored in the SPL header and use that. */
Andre Przywara14a25392018-10-25 17:23:04 +0800893 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset) {
Andre Przywara4f99ea62017-04-26 01:32:50 +0100894 cmp_str += spl->dt_name_offset;
895 } else {
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100896#ifdef CONFIG_DEFAULT_DEVICE_TREE
Andre Przywara4f99ea62017-04-26 01:32:50 +0100897 cmp_str = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100898#else
Andre Przywara4f99ea62017-04-26 01:32:50 +0100899 return 0;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100900#endif
Andre Przywara4f99ea62017-04-26 01:32:50 +0100901 };
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100902
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800903#ifdef CONFIG_PINE64_DT_SELECTION
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100904/* Differentiate the two Pine64 board DTs by their DRAM size. */
905 if (strstr(name, "-pine64") && strstr(cmp_str, "-pine64")) {
906 if ((gd->ram_size > 512 * 1024 * 1024))
907 return !strstr(name, "plus");
908 else
909 return !!strstr(name, "plus");
910 } else {
911 return strcmp(name, cmp_str);
912 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800913#endif
914 return strcmp(name, cmp_str);
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100915}
916#endif