1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net>
4 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
6 * (C) Copyright 2007-2011
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * Tom Cubie <tangliang@allwinnertech.com>
10 * Some board init for the Allwinner A10-evb board.
13 #include <clock_legacy.h>
22 #include <generic-phy.h>
23 #include <phy-sun4i-usb.h>
24 #include <asm/arch/clock.h>
25 #include <asm/arch/cpu.h>
26 #include <asm/arch/display.h>
27 #include <asm/arch/dram.h>
28 #include <asm/arch/mmc.h>
29 #include <asm/arch/prcm.h>
30 #include <asm/arch/pmic_bus.h>
31 #include <asm/arch/spl.h>
32 #include <asm/arch/sys_proto.h>
33 #include <asm/global_data.h>
34 #include <linux/delay.h>
35 #include <linux/printk.h>
36 #include <linux/types.h>
38 #include <asm/armv7.h>
41 #include <sunxi_gpio.h>
43 #include <u-boot/crc.h>
44 #include <env_internal.h>
45 #include <linux/libfdt.h>
46 #include <fdt_support.h>
51 #include <asm/setup.h>
52 #include <status_led.h>
54 DECLARE_GLOBAL_DATA_PTR;
56 void i2c_init_board(void)
58 #ifdef CONFIG_I2C0_ENABLE
59 #if defined(CONFIG_MACH_SUN4I) || \
60 defined(CONFIG_MACH_SUN5I) || \
61 defined(CONFIG_MACH_SUN7I) || \
62 defined(CONFIG_MACH_SUN8I_R40)
63 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
64 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
65 clock_twi_onoff(0, 1);
66 #elif defined(CONFIG_MACH_SUN6I)
67 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
68 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
69 clock_twi_onoff(0, 1);
70 #elif defined(CONFIG_MACH_SUN8I_V3S)
71 sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
72 sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
73 clock_twi_onoff(0, 1);
74 #elif defined(CONFIG_MACH_SUN8I)
75 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
76 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
77 clock_twi_onoff(0, 1);
78 #elif defined(CONFIG_MACH_SUN50I)
79 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
80 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
81 clock_twi_onoff(0, 1);
85 #ifdef CONFIG_I2C1_ENABLE
86 #if defined(CONFIG_MACH_SUN4I) || \
87 defined(CONFIG_MACH_SUN7I) || \
88 defined(CONFIG_MACH_SUN8I_R40)
89 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
90 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
91 clock_twi_onoff(1, 1);
92 #elif defined(CONFIG_MACH_SUN5I)
93 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
94 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
95 clock_twi_onoff(1, 1);
96 #elif defined(CONFIG_MACH_SUN6I)
97 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
98 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
99 clock_twi_onoff(1, 1);
100 #elif defined(CONFIG_MACH_SUN8I)
101 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
102 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
103 clock_twi_onoff(1, 1);
104 #elif defined(CONFIG_MACH_SUN50I)
105 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
106 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
107 clock_twi_onoff(1, 1);
111 #ifdef CONFIG_R_I2C_ENABLE
112 #ifdef CONFIG_MACH_SUN50I
113 clock_twi_onoff(5, 1);
114 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
115 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
116 #elif CONFIG_MACH_SUN50I_H616
117 clock_twi_onoff(5, 1);
118 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN50I_H616_GPL_R_TWI);
119 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN50I_H616_GPL_R_TWI);
121 clock_twi_onoff(5, 1);
122 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
123 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
129 * Try to use the environment from the boot source first.
130 * For MMC, this means a FAT partition on the boot device (SD or eMMC).
131 * If the raw MMC environment is also enabled, this is tried next.
132 * When booting from NAND we try UBI first, then NAND directly.
133 * SPI flash falls back to FAT (on SD card).
135 enum env_location env_get_location(enum env_operation op, int prio)
140 /* NOWHERE is exclusive, no other option can be defined. */
141 if (IS_ENABLED(CONFIG_ENV_IS_NOWHERE))
144 switch (sunxi_get_boot_device()) {
145 case BOOT_DEVICE_MMC1:
146 case BOOT_DEVICE_MMC2:
147 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
149 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
152 case BOOT_DEVICE_NAND:
153 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
155 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
158 case BOOT_DEVICE_SPI:
159 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
160 return ENVL_SPI_FLASH;
161 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
164 case BOOT_DEVICE_BOARD:
171 * If we come here for the first time, we *must* return a valid
172 * environment location other than ENVL_UNKNOWN, or the setup sequence
173 * in board_f() will silently hang. This is arguably a bug in
174 * env_init(), but for now pick one environment for which we know for
175 * sure to have a driver for. For all defconfigs this is either FAT
176 * or UBI, or NOWHERE, which is already handled above.
179 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
181 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
188 /* called only from U-Boot proper */
191 __maybe_unused int id_pfr1, ret;
193 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
195 #if !defined(CONFIG_ARM64) && !defined(CONFIG_MACH_SUNIV)
196 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
197 debug("id_pfr1: 0x%08x\n", id_pfr1);
198 /* Generic Timer Extension available? */
199 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
202 debug("Setting CNTFRQ\n");
205 * CNTFRQ is a secure register, so we will crash if we try to
206 * write this from the non-secure world (read is OK, though).
207 * In case some bootcode has already set the correct value,
208 * we avoid the risk of writing to it.
210 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
211 if (freq != CONFIG_COUNTER_FREQUENCY) {
212 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
213 freq, CONFIG_COUNTER_FREQUENCY);
214 #ifdef CONFIG_NON_SECURE
215 printf("arch timer frequency is wrong, but cannot adjust it\n");
217 asm volatile("mcr p15, 0, %0, c14, c0, 0"
218 : : "r"(CONFIG_COUNTER_FREQUENCY));
222 #endif /* !CONFIG_ARM64 && !CONFIG_MACH_SUNIV */
224 ret = axp_gpio_init();
234 * On older SoCs the SPL is actually at address zero, so using NULL as
235 * an error value does not work.
237 #define INVALID_SPL_HEADER ((void *)~0UL)
239 static struct boot_file_head * get_spl_header(uint8_t req_version)
241 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
242 uint8_t spl_header_version = spl->spl_signature[3];
244 /* Is there really the SPL header (still) there? */
245 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
246 return INVALID_SPL_HEADER;
248 if (spl_header_version < req_version) {
249 printf("sunxi SPL version mismatch: expected %u, got %u\n",
250 req_version, spl_header_version);
251 return INVALID_SPL_HEADER;
257 static const char *get_spl_dt_name(void)
259 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
261 /* Check if there is a DT name stored in the SPL header. */
262 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
263 return (char *)spl + spl->dt_name_offset;
270 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
272 if (spl == INVALID_SPL_HEADER)
273 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
276 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
278 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
279 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
284 #if defined(CONFIG_NAND_SUNXI) && defined(CONFIG_SPL_BUILD)
285 static void nand_pinmux_setup(void)
289 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
290 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
292 #if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
293 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
294 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
296 /* sun4i / sun7i do have a PC23, but it is not used for nand,
297 * only sun7i has a PC24 */
298 #ifdef CONFIG_MACH_SUN7I
299 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
303 static void nand_clock_setup(void)
305 struct sunxi_ccm_reg *const ccm =
306 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
308 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
309 #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
310 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
311 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
313 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
316 void board_nand_init(void)
321 #endif /* CONFIG_NAND_SUNXI */
324 static void mmc_pinmux_setup(int sdc)
331 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
332 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
333 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
334 sunxi_gpio_set_drv(pin, 2);
339 #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
340 defined(CONFIG_MACH_SUN8I_R40)
341 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
342 /* SDC1: PH22-PH-27 */
343 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
344 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
345 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
346 sunxi_gpio_set_drv(pin, 2);
350 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
351 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
352 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
353 sunxi_gpio_set_drv(pin, 2);
356 #elif defined(CONFIG_MACH_SUN5I)
358 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
359 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
360 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
361 sunxi_gpio_set_drv(pin, 2);
363 #elif defined(CONFIG_MACH_SUN6I)
365 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
366 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
367 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
368 sunxi_gpio_set_drv(pin, 2);
370 #elif defined(CONFIG_MACH_SUN8I)
372 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
373 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
374 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
375 sunxi_gpio_set_drv(pin, 2);
381 #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
383 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
384 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
385 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
386 sunxi_gpio_set_drv(pin, 2);
388 #elif defined(CONFIG_MACH_SUN5I)
390 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
391 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
392 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
393 sunxi_gpio_set_drv(pin, 2);
395 #elif defined(CONFIG_MACH_SUN6I)
396 /* SDC2: PC6-PC15, PC24 */
397 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
398 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
399 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
400 sunxi_gpio_set_drv(pin, 2);
403 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
404 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
405 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
406 #elif defined(CONFIG_MACH_SUN8I_R40)
407 /* SDC2: PC6-PC15, PC24 */
408 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
409 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
410 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
411 sunxi_gpio_set_drv(pin, 2);
414 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
415 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
416 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
417 #elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
418 /* SDC2: PC5-PC6, PC8-PC16 */
419 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
420 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
421 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
422 sunxi_gpio_set_drv(pin, 2);
425 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
426 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
427 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
428 sunxi_gpio_set_drv(pin, 2);
430 #elif defined(CONFIG_MACH_SUN50I_H6)
432 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
433 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
434 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
435 sunxi_gpio_set_drv(pin, 2);
437 #elif defined(CONFIG_MACH_SUN50I_H616)
438 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
439 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
440 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
442 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
444 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
445 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
446 sunxi_gpio_set_drv(pin, 3);
448 #elif defined(CONFIG_MACH_SUN9I)
450 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
451 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
452 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
453 sunxi_gpio_set_drv(pin, 2);
455 #elif defined(CONFIG_MACH_SUN8I_R528)
457 for (pin = SUNXI_GPC(2); pin <= SUNXI_GPC(7); pin++) {
458 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
459 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
460 sunxi_gpio_set_drv(pin, 2);
463 puts("ERROR: No pinmux setup defined for MMC2!\n");
468 #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
469 defined(CONFIG_MACH_SUN8I_R40)
471 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
472 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
473 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
474 sunxi_gpio_set_drv(pin, 2);
476 #elif defined(CONFIG_MACH_SUN6I)
477 /* SDC3: PC6-PC15, PC24 */
478 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
479 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
480 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
481 sunxi_gpio_set_drv(pin, 2);
484 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
485 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
486 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
491 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
496 int board_mmc_init(struct bd_info *bis)
499 * The BROM always accesses MMC port 0 (typically an SD card), and
500 * most boards seem to have such a slot. The others haven't reported
501 * any problem with unconditionally enabling this in the SPL.
503 if (!IS_ENABLED(CONFIG_UART0_PORT_F)) {
505 if (!sunxi_mmc_init(0))
509 if (CONFIG_MMC_SUNXI_SLOT_EXTRA != -1) {
510 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
511 if (!sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA))
518 #ifdef CONFIG_SYS_MMC_ENV_DEV
519 int mmc_get_env_dev(void)
521 switch (sunxi_get_boot_device()) {
522 case BOOT_DEVICE_MMC1:
524 case BOOT_DEVICE_MMC2:
527 return CONFIG_SYS_MMC_ENV_DEV;
531 #endif /* CONFIG_MMC */
533 #ifdef CONFIG_SPL_BUILD
535 static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
537 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
539 if (spl == INVALID_SPL_HEADER)
542 /* Promote the header version for U-Boot proper, if needed. */
543 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
544 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
546 spl->dram_size = dram_size >> 20;
549 void sunxi_board_init(void)
551 int power_failed = 0;
553 #ifdef CONFIG_LED_STATUS
554 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
558 #ifdef CONFIG_SY8106A_POWER
559 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
562 #if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
563 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
564 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER || \
565 defined CONFIG_AXP313_POWER || defined CONFIG_AXP717_POWER
566 power_failed = axp_init();
568 if (IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {
571 pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
572 if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
573 printf("Power on by plug-in, shutting down.\n");
574 pmic_bus_write(0x32, BIT(7));
578 #ifdef CONFIG_AXP_DCDC1_VOLT
579 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
580 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
582 #ifdef CONFIG_AXP_DCDC2_VOLT
583 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
584 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
586 #ifdef CONFIG_AXP_DCDC4_VOLT
587 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
590 #ifdef CONFIG_AXP_ALDO1_VOLT
591 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
593 #ifdef CONFIG_AXP_ALDO2_VOLT
594 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
596 #ifdef CONFIG_AXP_ALDO3_VOLT
597 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
599 #ifdef CONFIG_AXP_ALDO4_VOLT
600 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
603 #ifdef CONFIG_AXP_DLDO1_VOLT
604 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
605 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
607 #ifdef CONFIG_AXP_DLDO3_VOLT
608 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
609 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
611 #ifdef CONFIG_AXP_ELDO1_VOLT
612 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
613 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
614 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
617 #ifdef CONFIG_AXP_FLDO1_VOLT
618 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
619 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
620 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
623 #if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
624 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
626 #endif /* CONFIG_AXPxxx_POWER */
628 gd->ram_size = sunxi_dram_init();
629 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
633 sunxi_spl_store_dram_size(gd->ram_size);
636 * Only clock up the CPU to full speed if we are reasonably
637 * assured it's being powered with suitable core voltage
640 clock_set_pll1(get_board_sys_clk());
642 printf("Failed to set core voltage! Can't set CPU frequency\n");
644 #endif /* CONFIG_SPL_BUILD */
646 #ifdef CONFIG_USB_GADGET
647 int g_dnl_board_usb_cable_connected(void)
653 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
655 pr_err("%s: Cannot find USB device\n", __func__);
659 ret = generic_phy_get_by_name(dev, "usb", &phy);
661 pr_err("failed to get %s USB PHY\n", dev->name);
665 ret = generic_phy_init(&phy);
667 pr_debug("failed to init %s USB PHY\n", dev->name);
671 return sun4i_usb_phy_vbus_detect(&phy);
673 #endif /* CONFIG_USB_GADGET */
675 #ifdef CONFIG_SERIAL_TAG
676 void get_board_serial(struct tag_serialnr *serialnr)
679 unsigned long long serial;
681 serial_string = env_get("serial#");
684 serial = simple_strtoull(serial_string, NULL, 16);
686 serialnr->high = (unsigned int) (serial >> 32);
687 serialnr->low = (unsigned int) (serial & 0xffffffff);
696 * Check the SPL header for the "sunxi" variant. If found: parse values
697 * that might have been passed by the loader ("fel" utility), and update
698 * the environment accordingly.
700 static void parse_spl_header(const uint32_t spl_addr)
702 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
704 if (spl == INVALID_SPL_HEADER)
707 if (!spl->fel_script_address)
710 if (spl->fel_uEnv_length != 0) {
712 * data is expected in uEnv.txt compatible format, so "env
713 * import -t" the string(s) at fel_script_address right away.
715 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
716 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
719 /* otherwise assume .scr format (mkimage-type script) */
720 env_set_hex("fel_scriptaddr", spl->fel_script_address);
723 static bool get_unique_sid(unsigned int *sid)
725 if (sunxi_get_sid(sid) != 0)
732 * The single words 1 - 3 of the SID have quite a few bits
733 * which are the same on many models, so we take a crc32
734 * of all 3 words, to get a more unique value.
736 * Note we only do this on newer SoCs as we cannot change
737 * the algorithm on older SoCs since those have been using
738 * fixed mac-addresses based on only using word 3 for a
739 * long time and changing a fixed mac-address with an
740 * u-boot update is not good.
742 #if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
743 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
744 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
745 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
748 /* Ensure the NIC specific bytes of the mac are not all 0 */
749 if ((sid[3] & 0xffffff) == 0)
756 * Note this function gets called multiple times.
757 * It must not make any changes to env variables which already exist.
759 static void setup_environment(const void *fdt)
761 char serial_string[17] = { 0 };
767 if (!get_unique_sid(sid))
770 for (i = 0; i < 4; i++) {
771 sprintf(ethaddr, "ethernet%d", i);
772 if (!fdt_get_alias(fdt, ethaddr))
776 strcpy(ethaddr, "ethaddr");
778 sprintf(ethaddr, "eth%daddr", i);
780 if (env_get(ethaddr))
783 /* Non OUI / registered MAC address */
784 mac_addr[0] = (i << 4) | 0x02;
785 mac_addr[1] = (sid[0] >> 0) & 0xff;
786 mac_addr[2] = (sid[3] >> 24) & 0xff;
787 mac_addr[3] = (sid[3] >> 16) & 0xff;
788 mac_addr[4] = (sid[3] >> 8) & 0xff;
789 mac_addr[5] = (sid[3] >> 0) & 0xff;
791 eth_env_set_enetaddr(ethaddr, mac_addr);
794 if (!env_get("serial#")) {
795 snprintf(serial_string, sizeof(serial_string),
796 "%08x%08x", sid[0], sid[3]);
798 env_set("serial#", serial_string);
802 int misc_init_r(void)
804 const char *spl_dt_name;
807 env_set("fel_booted", NULL);
808 env_set("fel_scriptaddr", NULL);
809 env_set("mmc_bootdev", NULL);
811 boot = sunxi_get_boot_device();
812 /* determine if we are running in FEL mode */
813 if (boot == BOOT_DEVICE_BOARD) {
814 env_set("fel_booted", "1");
815 parse_spl_header(SPL_ADDR);
816 /* or if we booted from MMC, and which one */
817 } else if (boot == BOOT_DEVICE_MMC1) {
818 env_set("mmc_bootdev", "0");
819 } else if (boot == BOOT_DEVICE_MMC2) {
820 env_set("mmc_bootdev", "1");
823 /* Set fdtfile to match the FIT configuration chosen in SPL. */
824 spl_dt_name = get_spl_dt_name();
826 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
829 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
830 env_set("fdtfile", str);
833 setup_environment(gd->fdt_blob);
838 int board_late_init(void)
840 #ifdef CONFIG_USB_ETHER
847 static void bluetooth_dt_fixup(void *blob)
849 /* Some devices ship with a Bluetooth controller default address.
850 * Set a valid address through the device tree.
852 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
856 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
859 if (eth_env_get_enetaddr("bdaddr", tmp)) {
860 /* Convert between the binary formats of the corresponding stacks */
861 for (i = 0; i < ETH_ALEN; ++i)
862 bdaddr[i] = tmp[ETH_ALEN - i - 1];
864 if (!get_unique_sid(sid))
867 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
868 bdaddr[1] = (sid[3] >> 8) & 0xff;
869 bdaddr[2] = (sid[3] >> 16) & 0xff;
870 bdaddr[3] = (sid[3] >> 24) & 0xff;
871 bdaddr[4] = (sid[0] >> 0) & 0xff;
875 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
876 "local-bd-address", bdaddr, ETH_ALEN, 1);
879 int ft_board_setup(void *blob, struct bd_info *bd)
881 int __maybe_unused r;
884 * Call setup_environment and fdt_fixup_ethernet again
885 * in case the boot fdt has ethernet aliases the u-boot
886 * copy does not have.
888 setup_environment(blob);
889 fdt_fixup_ethernet(blob);
891 bluetooth_dt_fixup(blob);
893 #ifdef CONFIG_VIDEO_DT_SIMPLEFB
894 r = sunxi_simplefb_setup(blob);
901 #ifdef CONFIG_SPL_LOAD_FIT
902 static void set_spl_dt_name(const char *name)
904 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
906 if (spl == INVALID_SPL_HEADER)
909 /* Promote the header version for U-Boot proper, if needed. */
910 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
911 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
913 strcpy((char *)&spl->string_pool, name);
914 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
917 int board_fit_config_name_match(const char *name)
919 const char *best_dt_name = get_spl_dt_name();
922 #ifdef CONFIG_DEFAULT_DEVICE_TREE
923 if (best_dt_name == NULL)
924 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
927 if (best_dt_name == NULL) {
928 /* No DT name was provided, so accept the first config. */
931 #ifdef CONFIG_PINE64_DT_SELECTION
932 if (strstr(best_dt_name, "-pine64-plus")) {
933 /* Differentiate the Pine A64 boards by their DRAM size. */
934 if (gd->ram_size == SZ_512M)
935 best_dt_name = "sun50i-a64-pine64";
938 #ifdef CONFIG_PINEPHONE_DT_SELECTION
939 if (strstr(best_dt_name, "-pinephone")) {
940 /* Differentiate the PinePhone revisions by GPIO inputs. */
941 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
942 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
943 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
946 /* PL6 is pulled low by the modem on v1.2. */
947 if (gpio_get_value(SUNXI_GPL(6)) == 0)
948 best_dt_name = "sun50i-a64-pinephone-1.2";
950 best_dt_name = "sun50i-a64-pinephone-1.1";
952 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
953 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
954 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
958 ret = strcmp(name, best_dt_name);
961 * If one of the FIT configurations matches the most accurate DT name,
962 * update the SPL header to provide that DT name to U-Boot proper.
965 set_spl_dt_name(best_dt_name);
969 #endif /* CONFIG_SPL_LOAD_FIT */