blob: 1313b01dcea555a9818eeceaf2c8b95497f8cc85 [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>
Tom Rini8c70baa2021-12-14 13:36:40 -050014#include <clock_legacy.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053015#include <dm.h>
Simon Glass313112a2019-08-01 09:46:46 -060016#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070017#include <hang.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060018#include <image.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070019#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060020#include <log.h>
Hans de Goede63deaa82014-10-02 21:13:54 +020021#include <mmc.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020022#include <axp_pmic.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053023#include <generic-phy.h>
24#include <phy-sun4i-usb.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010025#include <asm/arch/clock.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020026#include <asm/arch/cpu.h>
Luc Verhaegen4869a8c2014-08-13 07:55:07 +020027#include <asm/arch/display.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010028#include <asm/arch/dram.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010029#include <asm/arch/mmc.h>
Samuel Holland9c7cefc2020-10-24 10:21:52 -050030#include <asm/arch/prcm.h>
Chris Morgan2ff2a1d2022-01-21 13:37:32 +000031#include <asm/arch/pmic_bus.h>
Hans de Goedea146c502016-07-09 09:56:56 +020032#include <asm/arch/spl.h>
Andre Przywara1823c232022-03-15 00:00:53 +000033#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060034#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060035#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060036#include <linux/printk.h>
Tom Riniaf73cfb2023-07-17 15:29:20 -040037#include <linux/types.h>
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020038#ifndef CONFIG_ARM64
39#include <asm/armv7.h>
40#endif
Hans de Goeded9d05652015-04-23 23:23:50 +020041#include <asm/gpio.h>
Andre Przywaraf944a612022-09-06 10:36:38 +010042#include <sunxi_gpio.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020043#include <asm/io.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010044#include <u-boot/crc.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060045#include <env_internal.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090046#include <linux/libfdt.h>
Andre Heiderbf8c8102021-10-01 19:29:00 +010047#include <fdt_support.h>
Hans de Goede5ed52f62015-08-15 11:55:26 +020048#include <nand.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020049#include <net.h>
Maxime Ripardae56d972017-08-23 10:08:29 +020050#include <spl.h>
Jelle van der Waa3f3a3092016-02-23 18:47:19 +010051#include <sy8106a.h>
Simon Glassd9a766f2017-05-17 08:23:00 -060052#include <asm/setup.h>
Arnaud Ferraris61485e92021-09-08 21:14:19 +020053#include <status_led.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010054
55DECLARE_GLOBAL_DATA_PTR;
56
Jernej Skrabec07da8802017-04-27 00:03:35 +020057void i2c_init_board(void)
58{
59#ifdef CONFIG_I2C0_ENABLE
60#if defined(CONFIG_MACH_SUN4I) || \
61 defined(CONFIG_MACH_SUN5I) || \
62 defined(CONFIG_MACH_SUN7I) || \
63 defined(CONFIG_MACH_SUN8I_R40)
64 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
65 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
66 clock_twi_onoff(0, 1);
67#elif defined(CONFIG_MACH_SUN6I)
68 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
69 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
70 clock_twi_onoff(0, 1);
Icenowy Zheng365951a2020-10-26 22:19:34 +080071#elif defined(CONFIG_MACH_SUN8I_V3S)
72 sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
73 sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
74 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +020075#elif defined(CONFIG_MACH_SUN8I)
76 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
77 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
78 clock_twi_onoff(0, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +020079#elif defined(CONFIG_MACH_SUN50I)
80 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
81 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
82 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +020083#endif
84#endif
85
86#ifdef CONFIG_I2C1_ENABLE
87#if defined(CONFIG_MACH_SUN4I) || \
88 defined(CONFIG_MACH_SUN7I) || \
89 defined(CONFIG_MACH_SUN8I_R40)
90 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
91 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
92 clock_twi_onoff(1, 1);
93#elif defined(CONFIG_MACH_SUN5I)
94 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
95 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
96 clock_twi_onoff(1, 1);
97#elif defined(CONFIG_MACH_SUN6I)
98 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
99 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
100 clock_twi_onoff(1, 1);
101#elif defined(CONFIG_MACH_SUN8I)
102 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
103 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
104 clock_twi_onoff(1, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200105#elif defined(CONFIG_MACH_SUN50I)
106 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
107 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
108 clock_twi_onoff(1, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200109#endif
110#endif
111
Jernej Skrabec07da8802017-04-27 00:03:35 +0200112#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800113#ifdef CONFIG_MACH_SUN50I
114 clock_twi_onoff(5, 1);
115 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
116 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
Jernej Skrabec7de8eb02021-01-11 21:11:42 +0100117#elif CONFIG_MACH_SUN50I_H616
118 clock_twi_onoff(5, 1);
119 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN50I_H616_GPL_R_TWI);
120 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN50I_H616_GPL_R_TWI);
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800121#else
Jernej Skrabec07da8802017-04-27 00:03:35 +0200122 clock_twi_onoff(5, 1);
123 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
124 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
125#endif
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800126#endif
Jernej Skrabec07da8802017-04-27 00:03:35 +0200127}
128
Andre Przywarab176bf32022-01-11 12:46:04 +0000129/*
130 * Try to use the environment from the boot source first.
131 * For MMC, this means a FAT partition on the boot device (SD or eMMC).
132 * If the raw MMC environment is also enabled, this is tried next.
Samuel Hollandf7135742022-04-20 23:15:39 +0100133 * When booting from NAND we try UBI first, then NAND directly.
Andre Przywarab176bf32022-01-11 12:46:04 +0000134 * SPI flash falls back to FAT (on SD card).
135 */
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100136enum env_location env_get_location(enum env_operation op, int prio)
137{
Samuel Hollandf7135742022-04-20 23:15:39 +0100138 if (prio > 1)
139 return ENVL_UNKNOWN;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100140
Samuel Hollandf7135742022-04-20 23:15:39 +0100141 /* NOWHERE is exclusive, no other option can be defined. */
142 if (IS_ENABLED(CONFIG_ENV_IS_NOWHERE))
143 return ENVL_NOWHERE;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100144
Andre Przywarab176bf32022-01-11 12:46:04 +0000145 switch (sunxi_get_boot_device()) {
146 case BOOT_DEVICE_MMC1:
147 case BOOT_DEVICE_MMC2:
Samuel Hollandf7135742022-04-20 23:15:39 +0100148 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
149 return ENVL_FAT;
150 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
151 return ENVL_MMC;
Andre Przywarab176bf32022-01-11 12:46:04 +0000152 break;
153 case BOOT_DEVICE_NAND:
Samuel Hollandf7135742022-04-20 23:15:39 +0100154 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
155 return ENVL_UBI;
Andre Przywarab176bf32022-01-11 12:46:04 +0000156 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
Samuel Hollandf7135742022-04-20 23:15:39 +0100157 return ENVL_NAND;
Andre Przywarab176bf32022-01-11 12:46:04 +0000158 break;
159 case BOOT_DEVICE_SPI:
Samuel Hollandf7135742022-04-20 23:15:39 +0100160 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
161 return ENVL_SPI_FLASH;
162 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
163 return ENVL_FAT;
Andre Przywarab176bf32022-01-11 12:46:04 +0000164 break;
165 case BOOT_DEVICE_BOARD:
166 break;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100167 default:
Andre Przywarab176bf32022-01-11 12:46:04 +0000168 break;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100169 }
Andre Przywarab176bf32022-01-11 12:46:04 +0000170
Samuel Hollandf7135742022-04-20 23:15:39 +0100171 /*
172 * If we come here for the first time, we *must* return a valid
173 * environment location other than ENVL_UNKNOWN, or the setup sequence
174 * in board_f() will silently hang. This is arguably a bug in
175 * env_init(), but for now pick one environment for which we know for
176 * sure to have a driver for. For all defconfigs this is either FAT
177 * or UBI, or NOWHERE, which is already handled above.
178 */
179 if (prio == 0) {
180 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
Andre Przywarab176bf32022-01-11 12:46:04 +0000181 return ENVL_FAT;
Samuel Hollandf7135742022-04-20 23:15:39 +0100182 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
183 return ENVL_UBI;
Andre Przywarab176bf32022-01-11 12:46:04 +0000184 }
185
186 return ENVL_UNKNOWN;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100187}
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100188
Andre Przywara2721ea52024-01-02 15:02:51 +0000189/* called only from U-Boot proper */
Ian Campbell6efe3692014-05-05 11:52:26 +0100190int board_init(void)
191{
Andre Przywara493e8ba2022-06-08 14:56:56 +0100192 __maybe_unused int id_pfr1, ret;
Ian Campbell6efe3692014-05-05 11:52:26 +0100193
194 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
195
Icenowy Zheng3a3b7342022-01-29 10:23:05 -0500196#if !defined(CONFIG_ARM64) && !defined(CONFIG_MACH_SUNIV)
Ian Campbell6efe3692014-05-05 11:52:26 +0100197 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
198 debug("id_pfr1: 0x%08x\n", id_pfr1);
199 /* Generic Timer Extension available? */
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200200 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
201 uint32_t freq;
202
Ian Campbell6efe3692014-05-05 11:52:26 +0100203 debug("Setting CNTFRQ\n");
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200204
205 /*
206 * CNTFRQ is a secure register, so we will crash if we try to
207 * write this from the non-secure world (read is OK, though).
208 * In case some bootcode has already set the correct value,
209 * we avoid the risk of writing to it.
210 */
211 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Peng Fane7c59392022-04-13 17:47:22 +0800212 if (freq != CONFIG_COUNTER_FREQUENCY) {
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200213 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Peng Fane7c59392022-04-13 17:47:22 +0800214 freq, CONFIG_COUNTER_FREQUENCY);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200215#ifdef CONFIG_NON_SECURE
216 printf("arch timer frequency is wrong, but cannot adjust it\n");
217#else
218 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Peng Fane7c59392022-04-13 17:47:22 +0800219 : : "r"(CONFIG_COUNTER_FREQUENCY));
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200220#endif
221 }
Ian Campbell6efe3692014-05-05 11:52:26 +0100222 }
Icenowy Zheng3a3b7342022-01-29 10:23:05 -0500223#endif /* !CONFIG_ARM64 && !CONFIG_MACH_SUNIV */
Ian Campbell6efe3692014-05-05 11:52:26 +0100224
Hans de Goede3ae1d132015-04-25 17:25:14 +0200225 ret = axp_gpio_init();
226 if (ret)
227 return ret;
228
Andre Przywara1823c232022-03-15 00:00:53 +0000229 eth_init_board();
230
Samuel Holland75fe0f42021-10-08 00:17:24 -0500231 return 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100232}
233
Andre Przywara14a25392018-10-25 17:23:04 +0800234/*
235 * On older SoCs the SPL is actually at address zero, so using NULL as
236 * an error value does not work.
237 */
238#define INVALID_SPL_HEADER ((void *)~0UL)
239
240static struct boot_file_head * get_spl_header(uint8_t req_version)
241{
242 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
243 uint8_t spl_header_version = spl->spl_signature[3];
244
245 /* Is there really the SPL header (still) there? */
246 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
247 return INVALID_SPL_HEADER;
248
249 if (spl_header_version < req_version) {
250 printf("sunxi SPL version mismatch: expected %u, got %u\n",
251 req_version, spl_header_version);
252 return INVALID_SPL_HEADER;
253 }
254
255 return spl;
256}
257
Samuel Hollandba44e942020-10-24 10:21:50 -0500258static const char *get_spl_dt_name(void)
259{
260 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
261
262 /* Check if there is a DT name stored in the SPL header. */
263 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
264 return (char *)spl + spl->dt_name_offset;
265
266 return NULL;
267}
Samuel Hollandba44e942020-10-24 10:21:50 -0500268
Ian Campbell6efe3692014-05-05 11:52:26 +0100269int dram_init(void)
270{
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800271 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
272
273 if (spl == INVALID_SPL_HEADER)
274 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
275 PHYS_SDRAM_0_SIZE);
276 else
277 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
278
279 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
280 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbell6efe3692014-05-05 11:52:26 +0100281
282 return 0;
283}
284
Samuel Holland890f7de2023-01-22 16:06:35 -0600285#if defined(CONFIG_NAND_SUNXI) && defined(CONFIG_SPL_BUILD)
Karol Gugala7bea8932015-07-23 14:33:01 +0200286static void nand_pinmux_setup(void)
287{
288 unsigned int pin;
Karol Gugala7bea8932015-07-23 14:33:01 +0200289
Hans de Goeded2236782015-08-15 13:17:49 +0200290 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugala7bea8932015-07-23 14:33:01 +0200291 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
292
Hans de Goeded2236782015-08-15 13:17:49 +0200293#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
294 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
295 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
296#endif
297 /* sun4i / sun7i do have a PC23, but it is not used for nand,
298 * only sun7i has a PC24 */
299#ifdef CONFIG_MACH_SUN7I
Karol Gugala7bea8932015-07-23 14:33:01 +0200300 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goeded2236782015-08-15 13:17:49 +0200301#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200302}
303
304static void nand_clock_setup(void)
305{
306 struct sunxi_ccm_reg *const ccm =
307 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedee5561a82015-08-15 11:58:03 +0200308
Karol Gugala7bea8932015-07-23 14:33:01 +0200309 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalebeeb802018-02-28 20:51:53 +0100310#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
311 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
312 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
313#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200314 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
315}
Hans de Goede5ed52f62015-08-15 11:55:26 +0200316
317void board_nand_init(void)
318{
319 nand_pinmux_setup();
320 nand_clock_setup();
321}
Andre Przywaraa9aab242022-11-28 00:02:56 +0000322#endif /* CONFIG_NAND_SUNXI */
Karol Gugala7bea8932015-07-23 14:33:01 +0200323
Masahiro Yamada0a780172017-05-09 20:31:39 +0900324#ifdef CONFIG_MMC
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100325static void mmc_pinmux_setup(int sdc)
326{
327 unsigned int pin;
328
329 switch (sdc) {
330 case 0:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100331 /* SDC0: PF0-PF5 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100332 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100333 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100334 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
335 sunxi_gpio_set_drv(pin, 2);
336 }
337 break;
338
339 case 1:
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800340#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
341 defined(CONFIG_MACH_SUN8I_R40)
Samuel Holland51951052021-09-12 10:28:35 -0500342 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100343 /* SDC1: PH22-PH-27 */
344 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
345 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
346 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
347 sunxi_gpio_set_drv(pin, 2);
348 }
349 } else {
350 /* SDC1: PG0-PG5 */
351 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
352 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
353 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
354 sunxi_gpio_set_drv(pin, 2);
355 }
356 }
357#elif defined(CONFIG_MACH_SUN5I)
358 /* SDC1: PG3-PG8 */
Hans de Goede4dccfd42014-10-03 16:44:57 +0200359 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100360 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100361 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
362 sunxi_gpio_set_drv(pin, 2);
363 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100364#elif defined(CONFIG_MACH_SUN6I)
365 /* SDC1: PG0-PG5 */
366 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
367 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
368 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
369 sunxi_gpio_set_drv(pin, 2);
370 }
371#elif defined(CONFIG_MACH_SUN8I)
Samuel Holland51951052021-09-12 10:28:35 -0500372 /* SDC1: PG0-PG5 */
373 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
374 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
375 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
376 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100377 }
378#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100379 break;
380
381 case 2:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100382#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
383 /* SDC2: PC6-PC11 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100384 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100385 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
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 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100389#elif defined(CONFIG_MACH_SUN5I)
Samuel Holland51951052021-09-12 10:28:35 -0500390 /* SDC2: PC6-PC15 */
391 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
392 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
393 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
394 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100395 }
396#elif defined(CONFIG_MACH_SUN6I)
Samuel Holland51951052021-09-12 10:28:35 -0500397 /* SDC2: PC6-PC15, PC24 */
398 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
399 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
400 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
401 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100402 }
Samuel Holland51951052021-09-12 10:28:35 -0500403
404 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
405 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
406 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800407#elif defined(CONFIG_MACH_SUN8I_R40)
408 /* SDC2: PC6-PC15, PC24 */
409 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
410 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
411 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
412 sunxi_gpio_set_drv(pin, 2);
413 }
414
415 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
416 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
417 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200418#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100419 /* SDC2: PC5-PC6, PC8-PC16 */
420 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
421 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
422 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
423 sunxi_gpio_set_drv(pin, 2);
424 }
425
426 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
427 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
428 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
429 sunxi_gpio_set_drv(pin, 2);
430 }
Icenowy Zhenga838a152018-07-21 16:20:29 +0800431#elif defined(CONFIG_MACH_SUN50I_H6)
432 /* SDC2: PC4-PC14 */
433 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
434 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
435 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
436 sunxi_gpio_set_drv(pin, 2);
437 }
Andre Przywara96f55642021-04-26 00:38:04 +0100438#elif defined(CONFIG_MACH_SUN50I_H616)
439 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
440 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
441 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
442 continue;
443 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
444 continue;
445 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
446 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
447 sunxi_gpio_set_drv(pin, 3);
448 }
Philipp Tomsicha0c7c712016-10-28 18:21:33 +0800449#elif defined(CONFIG_MACH_SUN9I)
450 /* SDC2: PC6-PC16 */
451 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
452 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
453 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
454 sunxi_gpio_set_drv(pin, 2);
455 }
Okhunjon Sobirjonovbff083a2023-09-25 06:43:28 +0300456#elif defined(CONFIG_MACH_SUN8I_R528)
457 /* SDC2: PC2-PC7 */
458 for (pin = SUNXI_GPC(2); pin <= SUNXI_GPC(7); pin++) {
459 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
460 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
461 sunxi_gpio_set_drv(pin, 2);
462 }
Andre Przywara96f55642021-04-26 00:38:04 +0100463#else
464 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100465#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100466 break;
467
468 case 3:
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800469#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
470 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100471 /* SDC3: PI4-PI9 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100472 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100473 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100474 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
475 sunxi_gpio_set_drv(pin, 2);
476 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100477#elif defined(CONFIG_MACH_SUN6I)
Samuel Holland51951052021-09-12 10:28:35 -0500478 /* SDC3: PC6-PC15, PC24 */
479 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
480 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
481 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
482 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100483 }
Samuel Holland51951052021-09-12 10:28:35 -0500484
485 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
486 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
487 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100488#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100489 break;
490
491 default:
492 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
493 break;
494 }
495}
496
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900497int board_mmc_init(struct bd_info *bis)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100498{
Andre Przywaraff32afe2022-11-28 00:03:53 +0000499 /*
500 * The BROM always accesses MMC port 0 (typically an SD card), and
501 * most boards seem to have such a slot. The others haven't reported
502 * any problem with unconditionally enabling this in the SPL.
503 */
Samuel Holland35663cf2022-04-10 00:13:33 -0500504 if (!IS_ENABLED(CONFIG_UART0_PORT_F)) {
Andre Przywaraff32afe2022-11-28 00:03:53 +0000505 mmc_pinmux_setup(0);
506 if (!sunxi_mmc_init(0))
Samuel Holland35663cf2022-04-10 00:13:33 -0500507 return -1;
508 }
Hans de Goede63deaa82014-10-02 21:13:54 +0200509
Samuel Holland35663cf2022-04-10 00:13:33 -0500510 if (CONFIG_MMC_SUNXI_SLOT_EXTRA != -1) {
511 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
512 if (!sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA))
513 return -1;
514 }
Hans de Goede63deaa82014-10-02 21:13:54 +0200515
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100516 return 0;
517}
Samuel Hollandbc42abb2021-04-18 22:16:21 -0500518
519#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
520int mmc_get_env_dev(void)
521{
522 switch (sunxi_get_boot_device()) {
523 case BOOT_DEVICE_MMC1:
524 return 0;
525 case BOOT_DEVICE_MMC2:
526 return 1;
527 default:
528 return CONFIG_SYS_MMC_ENV_DEV;
529 }
530}
531#endif
Andre Przywaraa9aab242022-11-28 00:02:56 +0000532#endif /* CONFIG_MMC */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100533
Ian Campbell6efe3692014-05-05 11:52:26 +0100534#ifdef CONFIG_SPL_BUILD
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800535
536static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
537{
538 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
539
540 if (spl == INVALID_SPL_HEADER)
541 return;
542
543 /* Promote the header version for U-Boot proper, if needed. */
544 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
545 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
546
547 spl->dram_size = dram_size >> 20;
548}
549
Ian Campbell6efe3692014-05-05 11:52:26 +0100550void sunxi_board_init(void)
551{
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200552 int power_failed = 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100553
Arnaud Ferraris61485e92021-09-08 21:14:19 +0200554#ifdef CONFIG_LED_STATUS
555 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
556 status_led_init();
557#endif
558
Jelle van der Waa3f3a3092016-02-23 18:47:19 +0100559#ifdef CONFIG_SY8106A_POWER
560 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
561#endif
562
vishnupatekar1895dfd2015-11-29 01:07:22 +0800563#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100564 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
Andre Przywara107d1ae2023-07-30 01:11:01 +0100565 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER || \
566 defined CONFIG_AXP313_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200567 power_failed = axp_init();
568
Chris Morgan2ff2a1d2022-01-21 13:37:32 +0000569 if (IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {
570 u8 boot_reason;
571
572 pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
573 if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
574 printf("Power on by plug-in, shutting down.\n");
575 pmic_bus_write(0x32, BIT(7));
576 }
577 }
578
Andre Przywara2e370a32021-06-27 01:13:09 +0100579#ifdef CONFIG_AXP_DCDC1_VOLT
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200580 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Andre Przywara2e370a32021-06-27 01:13:09 +0100581 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Hans de Goede1f247362014-06-13 22:55:51 +0200582#endif
Andre Przywara2e370a32021-06-27 01:13:09 +0100583#ifdef CONFIG_AXP_DCDC2_VOLT
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200584 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
585 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100586#endif
Andre Przywara2e370a32021-06-27 01:13:09 +0100587#ifdef CONFIG_AXP_DCDC4_VOLT
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200588 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200589#endif
590
Andre Przywara2e370a32021-06-27 01:13:09 +0100591#ifdef CONFIG_AXP_ALDO1_VOLT
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200592 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
593#endif
Andre Przywara2e370a32021-06-27 01:13:09 +0100594#ifdef CONFIG_AXP_ALDO2_VOLT
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200595 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100596#endif
Andre Przywara2e370a32021-06-27 01:13:09 +0100597#ifdef CONFIG_AXP_ALDO3_VOLT
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200598 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
599#endif
Andre Przywara2e370a32021-06-27 01:13:09 +0100600#ifdef CONFIG_AXP_ALDO4_VOLT
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200601 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
602#endif
603
Andre Przywara2e370a32021-06-27 01:13:09 +0100604#ifdef CONFIG_AXP_DLDO1_VOLT
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800605 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
606 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Andre Przywara2e370a32021-06-27 01:13:09 +0100607#endif
608#ifdef CONFIG_AXP_DLDO3_VOLT
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800609 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
610 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800611#endif
Andre Przywara2e370a32021-06-27 01:13:09 +0100612#ifdef CONFIG_AXP_ELDO1_VOLT
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200613 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
614 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
615 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
616#endif
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800617
Andre Przywara2e370a32021-06-27 01:13:09 +0100618#ifdef CONFIG_AXP_FLDO1_VOLT
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800619 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
620 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
621 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800622#endif
623
624#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai0e3efd32016-05-02 10:28:12 +0800625 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800626#endif
Andre Przywara2e370a32021-06-27 01:13:09 +0100627#endif /* CONFIG_AXPxxx_POWER */
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000628 printf("DRAM:");
629 gd->ram_size = sunxi_dram_init();
630 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
631 if (!gd->ram_size)
632 hang();
633
634 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800635
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200636 /*
637 * Only clock up the CPU to full speed if we are reasonably
638 * assured it's being powered with suitable core voltage
639 */
640 if (!power_failed)
Tom Rini8c70baa2021-12-14 13:36:40 -0500641 clock_set_pll1(get_board_sys_clk());
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200642 else
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000643 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbell6efe3692014-05-05 11:52:26 +0100644}
Andre Przywaraa9aab242022-11-28 00:02:56 +0000645#endif /* CONFIG_SPL_BUILD */
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200646
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100647#ifdef CONFIG_USB_GADGET
648int g_dnl_board_usb_cable_connected(void)
649{
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530650 struct udevice *dev;
651 struct phy phy;
652 int ret;
653
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100654 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530655 if (ret) {
656 pr_err("%s: Cannot find USB device\n", __func__);
657 return ret;
658 }
659
660 ret = generic_phy_get_by_name(dev, "usb", &phy);
661 if (ret) {
662 pr_err("failed to get %s USB PHY\n", dev->name);
663 return ret;
664 }
665
666 ret = generic_phy_init(&phy);
667 if (ret) {
Patrick Delaunay287e33c2020-07-03 17:36:41 +0200668 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530669 return ret;
670 }
671
Andre Przywarae79ee612021-11-02 19:45:47 +0000672 return sun4i_usb_phy_vbus_detect(&phy);
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100673}
Andre Przywaraa9aab242022-11-28 00:02:56 +0000674#endif /* CONFIG_USB_GADGET */
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100675
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100676#ifdef CONFIG_SERIAL_TAG
677void get_board_serial(struct tag_serialnr *serialnr)
678{
679 char *serial_string;
680 unsigned long long serial;
681
Simon Glass64b723f2017-08-03 12:22:12 -0600682 serial_string = env_get("serial#");
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100683
684 if (serial_string) {
685 serial = simple_strtoull(serial_string, NULL, 16);
686
687 serialnr->high = (unsigned int) (serial >> 32);
688 serialnr->low = (unsigned int) (serial & 0xffffffff);
689 } else {
690 serialnr->high = 0;
691 serialnr->low = 0;
692 }
693}
694#endif
695
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200696/*
697 * Check the SPL header for the "sunxi" variant. If found: parse values
698 * that might have been passed by the loader ("fel" utility), and update
699 * the environment accordingly.
700 */
701static void parse_spl_header(const uint32_t spl_addr)
702{
Andre Przywara14a25392018-10-25 17:23:04 +0800703 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200704
Andre Przywara14a25392018-10-25 17:23:04 +0800705 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200706 return;
Andre Przywara14a25392018-10-25 17:23:04 +0800707
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200708 if (!spl->fel_script_address)
709 return;
710
711 if (spl->fel_uEnv_length != 0) {
712 /*
713 * data is expected in uEnv.txt compatible format, so "env
714 * import -t" the string(s) at fel_script_address right away.
715 */
Andre Przywaraac4e6732016-09-05 01:32:41 +0100716 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200717 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
718 return;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200719 }
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200720 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass4d949a22017-08-03 12:22:10 -0600721 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200722}
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200723
Andre Heiderebdc3d42021-10-01 19:29:00 +0100724static bool get_unique_sid(unsigned int *sid)
725{
726 if (sunxi_get_sid(sid) != 0)
727 return false;
728
729 if (!sid[0])
730 return false;
731
732 /*
733 * The single words 1 - 3 of the SID have quite a few bits
734 * which are the same on many models, so we take a crc32
735 * of all 3 words, to get a more unique value.
736 *
737 * Note we only do this on newer SoCs as we cannot change
738 * the algorithm on older SoCs since those have been using
739 * fixed mac-addresses based on only using word 3 for a
740 * long time and changing a fixed mac-address with an
741 * u-boot update is not good.
742 */
743#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
744 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
745 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
746 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
747#endif
748
749 /* Ensure the NIC specific bytes of the mac are not all 0 */
750 if ((sid[3] & 0xffffff) == 0)
751 sid[3] |= 0x800000;
752
753 return true;
754}
755
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200756/*
757 * Note this function gets called multiple times.
758 * It must not make any changes to env variables which already exist.
759 */
760static void setup_environment(const void *fdt)
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200761{
Paul Kocialkowski92935942015-03-28 18:35:35 +0100762 char serial_string[17] = { 0 };
Hans de Goede11d70982014-11-26 00:04:24 +0100763 unsigned int sid[4];
Paul Kocialkowski92935942015-03-28 18:35:35 +0100764 uint8_t mac_addr[6];
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200765 char ethaddr[16];
Andre Heiderebdc3d42021-10-01 19:29:00 +0100766 int i;
Hans de Goedee5fe5482016-07-29 11:47:03 +0200767
Andre Heiderebdc3d42021-10-01 19:29:00 +0100768 if (!get_unique_sid(sid))
769 return;
Hans de Goedeabca8432016-07-27 17:58:06 +0200770
Andre Heiderebdc3d42021-10-01 19:29:00 +0100771 for (i = 0; i < 4; i++) {
772 sprintf(ethaddr, "ethernet%d", i);
773 if (!fdt_get_alias(fdt, ethaddr))
774 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200775
Andre Heiderebdc3d42021-10-01 19:29:00 +0100776 if (i == 0)
777 strcpy(ethaddr, "ethaddr");
778 else
779 sprintf(ethaddr, "eth%daddr", i);
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200780
Andre Heiderebdc3d42021-10-01 19:29:00 +0100781 if (env_get(ethaddr))
782 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200783
Andre Heiderebdc3d42021-10-01 19:29:00 +0100784 /* Non OUI / registered MAC address */
785 mac_addr[0] = (i << 4) | 0x02;
786 mac_addr[1] = (sid[0] >> 0) & 0xff;
787 mac_addr[2] = (sid[3] >> 24) & 0xff;
788 mac_addr[3] = (sid[3] >> 16) & 0xff;
789 mac_addr[4] = (sid[3] >> 8) & 0xff;
790 mac_addr[5] = (sid[3] >> 0) & 0xff;
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200791
Andre Heiderebdc3d42021-10-01 19:29:00 +0100792 eth_env_set_enetaddr(ethaddr, mac_addr);
793 }
Paul Kocialkowski92935942015-03-28 18:35:35 +0100794
Andre Heiderebdc3d42021-10-01 19:29:00 +0100795 if (!env_get("serial#")) {
796 snprintf(serial_string, sizeof(serial_string),
797 "%08x%08x", sid[0], sid[3]);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200798
Andre Heiderebdc3d42021-10-01 19:29:00 +0100799 env_set("serial#", serial_string);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200800 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200801}
802
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200803int misc_init_r(void)
804{
Samuel Holland87f940a2020-10-24 10:21:54 -0500805 const char *spl_dt_name;
Maxime Ripardae56d972017-08-23 10:08:29 +0200806 uint boot;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200807
Simon Glass6a38e412017-08-03 12:22:09 -0600808 env_set("fel_booted", NULL);
809 env_set("fel_scriptaddr", NULL);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200810 env_set("mmc_bootdev", NULL);
Maxime Ripardae56d972017-08-23 10:08:29 +0200811
812 boot = sunxi_get_boot_device();
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200813 /* determine if we are running in FEL mode */
Maxime Ripardae56d972017-08-23 10:08:29 +0200814 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass6a38e412017-08-03 12:22:09 -0600815 env_set("fel_booted", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200816 parse_spl_header(SPL_ADDR);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200817 /* or if we booted from MMC, and which one */
818 } else if (boot == BOOT_DEVICE_MMC1) {
819 env_set("mmc_bootdev", "0");
820 } else if (boot == BOOT_DEVICE_MMC2) {
821 env_set("mmc_bootdev", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200822 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200823
Samuel Holland87f940a2020-10-24 10:21:54 -0500824 /* Set fdtfile to match the FIT configuration chosen in SPL. */
825 spl_dt_name = get_spl_dt_name();
826 if (spl_dt_name) {
827 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
828 char str[64];
829
830 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
831 env_set("fdtfile", str);
832 }
833
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200834 setup_environment(gd->fdt_blob);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200835
Andy Shevchenko1facc0f2020-12-08 17:45:31 +0200836 return 0;
837}
838
839int board_late_init(void)
840{
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800841#ifdef CONFIG_USB_ETHER
Maxime Ripardf54aba32017-09-06 22:25:03 +0200842 usb_ether_init();
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800843#endif
Maxime Ripardf54aba32017-09-06 22:25:03 +0200844
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200845 return 0;
846}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200847
Andre Heiderbf8c8102021-10-01 19:29:00 +0100848static void bluetooth_dt_fixup(void *blob)
849{
850 /* Some devices ship with a Bluetooth controller default address.
851 * Set a valid address through the device tree.
852 */
853 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
854 unsigned int sid[4];
855 int i;
856
857 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
858 return;
859
860 if (eth_env_get_enetaddr("bdaddr", tmp)) {
861 /* Convert between the binary formats of the corresponding stacks */
862 for (i = 0; i < ETH_ALEN; ++i)
863 bdaddr[i] = tmp[ETH_ALEN - i - 1];
864 } else {
865 if (!get_unique_sid(sid))
866 return;
867
868 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
869 bdaddr[1] = (sid[3] >> 8) & 0xff;
870 bdaddr[2] = (sid[3] >> 16) & 0xff;
871 bdaddr[3] = (sid[3] >> 24) & 0xff;
872 bdaddr[4] = (sid[0] >> 0) & 0xff;
873 bdaddr[5] = 0x02;
874 }
875
876 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
877 "local-bd-address", bdaddr, ETH_ALEN, 1);
878}
879
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900880int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200881{
Hans de Goede48a234a2016-03-22 22:51:52 +0100882 int __maybe_unused r;
883
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200884 /*
Icenowy Zheng5a1456b2021-09-11 19:39:16 +0200885 * Call setup_environment and fdt_fixup_ethernet again
886 * in case the boot fdt has ethernet aliases the u-boot
887 * copy does not have.
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200888 */
889 setup_environment(blob);
Icenowy Zheng5a1456b2021-09-11 19:39:16 +0200890 fdt_fixup_ethernet(blob);
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200891
Andre Heiderbf8c8102021-10-01 19:29:00 +0100892 bluetooth_dt_fixup(blob);
893
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200894#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goede48a234a2016-03-22 22:51:52 +0100895 r = sunxi_simplefb_setup(blob);
896 if (r)
897 return r;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200898#endif
Hans de Goede48a234a2016-03-22 22:51:52 +0100899 return 0;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200900}
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100901
902#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland64933e92020-10-24 10:21:53 -0500903static void set_spl_dt_name(const char *name)
904{
905 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
906
907 if (spl == INVALID_SPL_HEADER)
908 return;
909
910 /* Promote the header version for U-Boot proper, if needed. */
911 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
912 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
913
914 strcpy((char *)&spl->string_pool, name);
915 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
916}
917
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100918int board_fit_config_name_match(const char *name)
919{
Samuel Hollandba44e942020-10-24 10:21:50 -0500920 const char *best_dt_name = get_spl_dt_name();
Samuel Holland64933e92020-10-24 10:21:53 -0500921 int ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100922
923#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Hollandba44e942020-10-24 10:21:50 -0500924 if (best_dt_name == NULL)
Samuel Holland37b86202020-10-24 10:21:49 -0500925 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100926#endif
927
Samuel Hollandba44e942020-10-24 10:21:50 -0500928 if (best_dt_name == NULL) {
929 /* No DT name was provided, so accept the first config. */
930 return 0;
931 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800932#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Hollandf2352dd2020-10-24 10:21:51 -0500933 if (strstr(best_dt_name, "-pine64-plus")) {
934 /* Differentiate the Pine A64 boards by their DRAM size. */
Tom Riniaf73cfb2023-07-17 15:29:20 -0400935 if (gd->ram_size == SZ_512M)
Samuel Hollandf2352dd2020-10-24 10:21:51 -0500936 best_dt_name = "sun50i-a64-pine64";
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100937 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800938#endif
Samuel Holland9c7cefc2020-10-24 10:21:52 -0500939#ifdef CONFIG_PINEPHONE_DT_SELECTION
940 if (strstr(best_dt_name, "-pinephone")) {
941 /* Differentiate the PinePhone revisions by GPIO inputs. */
942 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
943 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
944 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
945 udelay(100);
946
947 /* PL6 is pulled low by the modem on v1.2. */
948 if (gpio_get_value(SUNXI_GPL(6)) == 0)
949 best_dt_name = "sun50i-a64-pinephone-1.2";
950 else
951 best_dt_name = "sun50i-a64-pinephone-1.1";
952
953 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
954 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
955 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
956 }
957#endif
958
Samuel Holland64933e92020-10-24 10:21:53 -0500959 ret = strcmp(name, best_dt_name);
960
961 /*
962 * If one of the FIT configurations matches the most accurate DT name,
963 * update the SPL header to provide that DT name to U-Boot proper.
964 */
965 if (ret == 0)
966 set_spl_dt_name(best_dt_name);
967
968 return ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100969}
Andre Przywaraa9aab242022-11-28 00:02:56 +0000970#endif /* CONFIG_SPL_LOAD_FIT */