blob: 39ecbe988f7b4fa045a9164ee7ba68a438562221 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbellcba69ee2014-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 Campbellcba69ee2014-05-05 11:52:26 +010011 */
12
13#include <common.h>
Tom Rini2f8a6db2021-12-14 13:36:40 -050014#include <clock_legacy.h>
Jagan Teki237050f2018-05-07 13:03:36 +053015#include <dm.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060016#include <env.h>
Simon Glassdb41d652019-12-28 10:45:07 -070017#include <hang.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060018#include <image.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070019#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060020#include <log.h>
Hans de Goedee79c7c82014-10-02 21:13:54 +020021#include <mmc.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020022#include <axp_pmic.h>
Jagan Teki237050f2018-05-07 13:03:36 +053023#include <generic-phy.h>
24#include <phy-sun4i-usb.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010025#include <asm/arch/clock.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020026#include <asm/arch/cpu.h>
Luc Verhaegen2d7a0842014-08-13 07:55:07 +020027#include <asm/arch/display.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010028#include <asm/arch/dram.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010029#include <asm/arch/mmc.h>
Samuel Holland8a8b73b2020-10-24 10:21:52 -050030#include <asm/arch/prcm.h>
Chris Morgan52bcc4f2022-01-21 13:37:32 +000031#include <asm/arch/pmic_bus.h>
Hans de Goede4a8c7c12016-07-09 09:56:56 +020032#include <asm/arch/spl.h>
Andre Przywarae9437532022-03-15 00:00:53 +000033#include <asm/arch/sys_proto.h>
Simon Glass401d1c42020-10-30 21:38:53 -060034#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060035#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -060036#include <linux/printk.h>
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020037#ifndef CONFIG_ARM64
38#include <asm/armv7.h>
39#endif
Hans de Goede4f7e01c2015-04-23 23:23:50 +020040#include <asm/gpio.h>
Andre Przywara207ed0a2022-09-06 10:36:38 +010041#include <sunxi_gpio.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020042#include <asm/io.h>
Philipp Tomsicha740ee92018-11-25 19:22:18 +010043#include <u-boot/crc.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060044#include <env_internal.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090045#include <linux/libfdt.h>
Andre Heider9267ff82021-10-01 19:29:00 +010046#include <fdt_support.h>
Hans de Goedef62bfa52015-08-15 11:55:26 +020047#include <nand.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020048#include <net.h>
Maxime Ripardf4c35232017-08-23 10:08:29 +020049#include <spl.h>
Jelle van der Waa0d8382a2016-02-23 18:47:19 +010050#include <sy8106a.h>
Simon Glass5d982852017-05-17 08:23:00 -060051#include <asm/setup.h>
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +020052#include <status_led.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010053
54DECLARE_GLOBAL_DATA_PTR;
55
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020056void i2c_init_board(void)
57{
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);
Icenowy Zheng8c51c652020-10-26 22:19:34 +080070#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);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020074#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);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +020078#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);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020082#endif
83#endif
84
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);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +0200104#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);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200108#endif
109#endif
110
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200111#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800112#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);
Jernej Skrabecd0b07c12021-01-11 21:11:42 +0100116#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);
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800120#else
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200121 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);
124#endif
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800125#endif
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200126}
127
Andre Przywarae42dad42022-01-11 12:46:04 +0000128/*
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.
Samuel Hollande008e512022-04-20 23:15:39 +0100132 * When booting from NAND we try UBI first, then NAND directly.
Andre Przywarae42dad42022-01-11 12:46:04 +0000133 * SPI flash falls back to FAT (on SD card).
134 */
Maxime Ripardb39117c2018-01-23 21:17:03 +0100135enum env_location env_get_location(enum env_operation op, int prio)
136{
Samuel Hollande008e512022-04-20 23:15:39 +0100137 if (prio > 1)
138 return ENVL_UNKNOWN;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100139
Samuel Hollande008e512022-04-20 23:15:39 +0100140 /* NOWHERE is exclusive, no other option can be defined. */
141 if (IS_ENABLED(CONFIG_ENV_IS_NOWHERE))
142 return ENVL_NOWHERE;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100143
Andre Przywarae42dad42022-01-11 12:46:04 +0000144 switch (sunxi_get_boot_device()) {
145 case BOOT_DEVICE_MMC1:
146 case BOOT_DEVICE_MMC2:
Samuel Hollande008e512022-04-20 23:15:39 +0100147 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
148 return ENVL_FAT;
149 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
150 return ENVL_MMC;
Andre Przywarae42dad42022-01-11 12:46:04 +0000151 break;
152 case BOOT_DEVICE_NAND:
Samuel Hollande008e512022-04-20 23:15:39 +0100153 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
154 return ENVL_UBI;
Andre Przywarae42dad42022-01-11 12:46:04 +0000155 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
Samuel Hollande008e512022-04-20 23:15:39 +0100156 return ENVL_NAND;
Andre Przywarae42dad42022-01-11 12:46:04 +0000157 break;
158 case BOOT_DEVICE_SPI:
Samuel Hollande008e512022-04-20 23:15:39 +0100159 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))
162 return ENVL_FAT;
Andre Przywarae42dad42022-01-11 12:46:04 +0000163 break;
164 case BOOT_DEVICE_BOARD:
165 break;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100166 default:
Andre Przywarae42dad42022-01-11 12:46:04 +0000167 break;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100168 }
Andre Przywarae42dad42022-01-11 12:46:04 +0000169
Samuel Hollande008e512022-04-20 23:15:39 +0100170 /*
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.
177 */
178 if (prio == 0) {
179 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
Andre Przywarae42dad42022-01-11 12:46:04 +0000180 return ENVL_FAT;
Samuel Hollande008e512022-04-20 23:15:39 +0100181 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
182 return ENVL_UBI;
Andre Przywarae42dad42022-01-11 12:46:04 +0000183 }
184
185 return ENVL_UNKNOWN;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100186}
Maxime Ripardb39117c2018-01-23 21:17:03 +0100187
Ian Campbellcba69ee2014-05-05 11:52:26 +0100188/* add board specific code here */
189int board_init(void)
190{
Andre Przywara5ad98c52022-06-08 14:56:56 +0100191 __maybe_unused int id_pfr1, ret;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100192
193 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
194
Icenowy Zheng116e1ed2022-01-29 10:23:05 -0500195#if !defined(CONFIG_ARM64) && !defined(CONFIG_MACH_SUNIV)
Ian Campbellcba69ee2014-05-05 11:52:26 +0100196 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? */
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200199 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
200 uint32_t freq;
201
Ian Campbellcba69ee2014-05-05 11:52:26 +0100202 debug("Setting CNTFRQ\n");
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200203
204 /*
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.
209 */
210 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Peng Fan151a0302022-04-13 17:47:22 +0800211 if (freq != CONFIG_COUNTER_FREQUENCY) {
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200212 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Peng Fan151a0302022-04-13 17:47:22 +0800213 freq, CONFIG_COUNTER_FREQUENCY);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200214#ifdef CONFIG_NON_SECURE
215 printf("arch timer frequency is wrong, but cannot adjust it\n");
216#else
217 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Peng Fan151a0302022-04-13 17:47:22 +0800218 : : "r"(CONFIG_COUNTER_FREQUENCY));
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200219#endif
220 }
Ian Campbellcba69ee2014-05-05 11:52:26 +0100221 }
Icenowy Zheng116e1ed2022-01-29 10:23:05 -0500222#endif /* !CONFIG_ARM64 && !CONFIG_MACH_SUNIV */
Ian Campbellcba69ee2014-05-05 11:52:26 +0100223
Hans de Goede2fcf0332015-04-25 17:25:14 +0200224 ret = axp_gpio_init();
225 if (ret)
226 return ret;
227
Igor Opaniuk2147a162021-02-09 13:52:45 +0200228#if CONFIG_IS_ENABLED(DM_I2C)
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +0200229 /*
230 * Temporary workaround for enabling I2C clocks until proper sunxi DM
231 * clk, reset and pinctrl drivers land.
232 */
233 i2c_init_board();
234#endif
Andre Przywarae9437532022-03-15 00:00:53 +0000235 eth_init_board();
236
Samuel Holland24214972021-10-08 00:17:24 -0500237 return 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100238}
239
Andre Przywaracff5c132018-10-25 17:23:04 +0800240/*
241 * On older SoCs the SPL is actually at address zero, so using NULL as
242 * an error value does not work.
243 */
244#define INVALID_SPL_HEADER ((void *)~0UL)
245
246static struct boot_file_head * get_spl_header(uint8_t req_version)
247{
248 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
249 uint8_t spl_header_version = spl->spl_signature[3];
250
251 /* Is there really the SPL header (still) there? */
252 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
253 return INVALID_SPL_HEADER;
254
255 if (spl_header_version < req_version) {
256 printf("sunxi SPL version mismatch: expected %u, got %u\n",
257 req_version, spl_header_version);
258 return INVALID_SPL_HEADER;
259 }
260
261 return spl;
262}
263
Samuel Holland467b7e52020-10-24 10:21:50 -0500264static const char *get_spl_dt_name(void)
265{
266 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
267
268 /* Check if there is a DT name stored in the SPL header. */
269 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
270 return (char *)spl + spl->dt_name_offset;
271
272 return NULL;
273}
Samuel Holland467b7e52020-10-24 10:21:50 -0500274
Ian Campbellcba69ee2014-05-05 11:52:26 +0100275int dram_init(void)
276{
Andre Przywara57766102018-10-25 17:23:07 +0800277 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
278
279 if (spl == INVALID_SPL_HEADER)
280 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
281 PHYS_SDRAM_0_SIZE);
282 else
283 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
284
285 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
286 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100287
288 return 0;
289}
290
Samuel Holland21b790f2023-01-22 16:06:35 -0600291#if defined(CONFIG_NAND_SUNXI) && defined(CONFIG_SPL_BUILD)
Karol Gugalaad008292015-07-23 14:33:01 +0200292static void nand_pinmux_setup(void)
293{
294 unsigned int pin;
Hans de Goede022a99d2015-08-15 13:17:49 +0200295
296 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200297 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
298
Hans de Goede022a99d2015-08-15 13:17:49 +0200299#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
300 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200301 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200302#endif
303 /* sun4i / sun7i do have a PC23, but it is not used for nand,
304 * only sun7i has a PC24 */
305#ifdef CONFIG_MACH_SUN7I
Karol Gugalaad008292015-07-23 14:33:01 +0200306 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200307#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200308}
309
310static void nand_clock_setup(void)
311{
312 struct sunxi_ccm_reg *const ccm =
313 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede31c21472015-08-15 11:58:03 +0200314
Karol Gugalaad008292015-07-23 14:33:01 +0200315 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalba1c98b2018-02-28 20:51:53 +0100316#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
317 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
318 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
319#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200320 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
321}
Hans de Goedef62bfa52015-08-15 11:55:26 +0200322
323void board_nand_init(void)
324{
325 nand_pinmux_setup();
326 nand_clock_setup();
327}
Andre Przywara64531492022-11-28 00:02:56 +0000328#endif /* CONFIG_NAND_SUNXI */
Karol Gugalaad008292015-07-23 14:33:01 +0200329
Masahiro Yamada4aa2ba32017-05-09 20:31:39 +0900330#ifdef CONFIG_MMC
Ian Campbelle24ea552014-05-05 14:42:31 +0100331static void mmc_pinmux_setup(int sdc)
332{
333 unsigned int pin;
334
335 switch (sdc) {
336 case 0:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100337 /* SDC0: PF0-PF5 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100338 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100339 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100340 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
341 sunxi_gpio_set_drv(pin, 2);
342 }
343 break;
344
345 case 1:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800346#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
347 defined(CONFIG_MACH_SUN8I_R40)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500348 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100349 /* SDC1: PH22-PH-27 */
350 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
351 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
352 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
353 sunxi_gpio_set_drv(pin, 2);
354 }
355 } else {
356 /* SDC1: PG0-PG5 */
357 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
358 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
359 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
360 sunxi_gpio_set_drv(pin, 2);
361 }
362 }
363#elif defined(CONFIG_MACH_SUN5I)
364 /* SDC1: PG3-PG8 */
Hans de Goedebbff84b2014-10-03 16:44:57 +0200365 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100366 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbelle24ea552014-05-05 14:42:31 +0100367 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
368 sunxi_gpio_set_drv(pin, 2);
369 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100370#elif defined(CONFIG_MACH_SUN6I)
371 /* SDC1: PG0-PG5 */
372 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
373 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
374 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
375 sunxi_gpio_set_drv(pin, 2);
376 }
377#elif defined(CONFIG_MACH_SUN8I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500378 /* SDC1: PG0-PG5 */
379 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
380 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
381 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
382 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100383 }
384#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100385 break;
386
387 case 2:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100388#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
389 /* SDC2: PC6-PC11 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100390 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100391 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100392 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
393 sunxi_gpio_set_drv(pin, 2);
394 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100395#elif defined(CONFIG_MACH_SUN5I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500396 /* SDC2: PC6-PC15 */
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);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100401 }
402#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500403 /* SDC2: PC6-PC15, PC24 */
404 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
405 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
406 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
407 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100408 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500409
410 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
411 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
412 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800413#elif defined(CONFIG_MACH_SUN8I_R40)
414 /* SDC2: PC6-PC15, PC24 */
415 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
416 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
417 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
418 sunxi_gpio_set_drv(pin, 2);
419 }
420
421 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
422 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
423 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200424#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100425 /* SDC2: PC5-PC6, PC8-PC16 */
426 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
427 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100428 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
429 sunxi_gpio_set_drv(pin, 2);
430 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100431
432 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); 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);
436 }
Icenowy Zheng42956f12018-07-21 16:20:29 +0800437#elif defined(CONFIG_MACH_SUN50I_H6)
438 /* SDC2: PC4-PC14 */
439 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
440 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
441 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
442 sunxi_gpio_set_drv(pin, 2);
443 }
Andre Przywara212224e2021-04-26 00:38:04 +0100444#elif defined(CONFIG_MACH_SUN50I_H616)
445 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
446 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
447 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
448 continue;
449 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
450 continue;
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, 3);
454 }
Philipp Tomsich3ebb4562016-10-28 18:21:33 +0800455#elif defined(CONFIG_MACH_SUN9I)
456 /* SDC2: PC6-PC16 */
457 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); 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);
461 }
Okhunjon Sobirjonov5b7c58f2023-09-25 06:43:28 +0300462#elif defined(CONFIG_MACH_SUN8I_R528)
463 /* SDC2: PC2-PC7 */
464 for (pin = SUNXI_GPC(2); pin <= SUNXI_GPC(7); 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 }
Andre Przywara212224e2021-04-26 00:38:04 +0100469#else
470 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100471#endif
472 break;
473
474 case 3:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800475#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
476 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100477 /* SDC3: PI4-PI9 */
478 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
479 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
480 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
481 sunxi_gpio_set_drv(pin, 2);
482 }
483#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500484 /* SDC3: PC6-PC15, PC24 */
485 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
486 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
487 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
488 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100489 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500490
491 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
492 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
493 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100494#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100495 break;
496
497 default:
498 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
499 break;
500 }
501}
502
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900503int board_mmc_init(struct bd_info *bis)
Ian Campbelle24ea552014-05-05 14:42:31 +0100504{
Andre Przywaraed825862022-11-28 00:03:53 +0000505 /*
506 * The BROM always accesses MMC port 0 (typically an SD card), and
507 * most boards seem to have such a slot. The others haven't reported
508 * any problem with unconditionally enabling this in the SPL.
509 */
Samuel Holland3ba0a252022-04-10 00:13:33 -0500510 if (!IS_ENABLED(CONFIG_UART0_PORT_F)) {
Andre Przywaraed825862022-11-28 00:03:53 +0000511 mmc_pinmux_setup(0);
512 if (!sunxi_mmc_init(0))
Samuel Holland3ba0a252022-04-10 00:13:33 -0500513 return -1;
514 }
Hans de Goedee79c7c82014-10-02 21:13:54 +0200515
Samuel Holland3ba0a252022-04-10 00:13:33 -0500516 if (CONFIG_MMC_SUNXI_SLOT_EXTRA != -1) {
517 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
518 if (!sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA))
519 return -1;
520 }
Hans de Goedee79c7c82014-10-02 21:13:54 +0200521
Ian Campbelle24ea552014-05-05 14:42:31 +0100522 return 0;
523}
Samuel Holland1011ebc2021-04-18 22:16:21 -0500524
525#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
526int mmc_get_env_dev(void)
527{
528 switch (sunxi_get_boot_device()) {
529 case BOOT_DEVICE_MMC1:
530 return 0;
531 case BOOT_DEVICE_MMC2:
532 return 1;
533 default:
534 return CONFIG_SYS_MMC_ENV_DEV;
535 }
536}
537#endif
Andre Przywara64531492022-11-28 00:02:56 +0000538#endif /* CONFIG_MMC */
Ian Campbelle24ea552014-05-05 14:42:31 +0100539
Ian Campbellcba69ee2014-05-05 11:52:26 +0100540#ifdef CONFIG_SPL_BUILD
Andre Przywara57766102018-10-25 17:23:07 +0800541
542static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
543{
544 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
545
546 if (spl == INVALID_SPL_HEADER)
547 return;
548
549 /* Promote the header version for U-Boot proper, if needed. */
550 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
551 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
552
553 spl->dram_size = dram_size >> 20;
554}
555
Ian Campbellcba69ee2014-05-05 11:52:26 +0100556void sunxi_board_init(void)
557{
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200558 int power_failed = 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100559
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +0200560#ifdef CONFIG_LED_STATUS
561 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
562 status_led_init();
563#endif
564
Jelle van der Waa0d8382a2016-02-23 18:47:19 +0100565#ifdef CONFIG_SY8106A_POWER
566 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
567#endif
568
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800569#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100570 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
571 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200572 power_failed = axp_init();
573
Chris Morgan52bcc4f2022-01-21 13:37:32 +0000574 if (IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {
575 u8 boot_reason;
576
577 pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
578 if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
579 printf("Power on by plug-in, shutting down.\n");
580 pmic_bus_write(0x32, BIT(7));
581 }
582 }
583
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800584#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
585 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200586 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede24289202014-06-13 22:55:51 +0200587#endif
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100588#if !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200589 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
590 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100591#endif
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800592#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200593 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200594#endif
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800595#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
596 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200597 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200598#endif
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200599
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800600#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
601 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200602 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
603#endif
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100604#if !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200605 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100606#endif
607#if !defined(CONFIG_AXP152_POWER) && !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200608 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
609#endif
610#ifdef CONFIG_AXP209_POWER
611 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
612#endif
613
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800614#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
615 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800616 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
617 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800618#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800619 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
620 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800621#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200622 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
623 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
624 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
625#endif
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800626
627#ifdef CONFIG_AXP818_POWER
628 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
629 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
630 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800631#endif
632
633#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai15278cc2016-05-02 10:28:12 +0800634 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800635#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200636#endif
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000637 printf("DRAM:");
638 gd->ram_size = sunxi_dram_init();
639 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
640 if (!gd->ram_size)
641 hang();
642
643 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara57766102018-10-25 17:23:07 +0800644
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200645 /*
646 * Only clock up the CPU to full speed if we are reasonably
647 * assured it's being powered with suitable core voltage
648 */
649 if (!power_failed)
Tom Rini2f8a6db2021-12-14 13:36:40 -0500650 clock_set_pll1(get_board_sys_clk());
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200651 else
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000652 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbellcba69ee2014-05-05 11:52:26 +0100653}
Andre Przywara64531492022-11-28 00:02:56 +0000654#endif /* CONFIG_SPL_BUILD */
Jonathan Liub41d7d02014-06-14 08:59:09 +0200655
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100656#ifdef CONFIG_USB_GADGET
657int g_dnl_board_usb_cable_connected(void)
658{
Jagan Teki237050f2018-05-07 13:03:36 +0530659 struct udevice *dev;
660 struct phy phy;
661 int ret;
662
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100663 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki237050f2018-05-07 13:03:36 +0530664 if (ret) {
665 pr_err("%s: Cannot find USB device\n", __func__);
666 return ret;
667 }
668
669 ret = generic_phy_get_by_name(dev, "usb", &phy);
670 if (ret) {
671 pr_err("failed to get %s USB PHY\n", dev->name);
672 return ret;
673 }
674
675 ret = generic_phy_init(&phy);
676 if (ret) {
Patrick Delaunayf286e372020-07-03 17:36:41 +0200677 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki237050f2018-05-07 13:03:36 +0530678 return ret;
679 }
680
Andre Przywarafbd92072021-11-02 19:45:47 +0000681 return sun4i_usb_phy_vbus_detect(&phy);
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100682}
Andre Przywara64531492022-11-28 00:02:56 +0000683#endif /* CONFIG_USB_GADGET */
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100684
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100685#ifdef CONFIG_SERIAL_TAG
686void get_board_serial(struct tag_serialnr *serialnr)
687{
688 char *serial_string;
689 unsigned long long serial;
690
Simon Glass00caae62017-08-03 12:22:12 -0600691 serial_string = env_get("serial#");
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100692
693 if (serial_string) {
694 serial = simple_strtoull(serial_string, NULL, 16);
695
696 serialnr->high = (unsigned int) (serial >> 32);
697 serialnr->low = (unsigned int) (serial & 0xffffffff);
698 } else {
699 serialnr->high = 0;
700 serialnr->low = 0;
701 }
702}
703#endif
704
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200705/*
706 * Check the SPL header for the "sunxi" variant. If found: parse values
707 * that might have been passed by the loader ("fel" utility), and update
708 * the environment accordingly.
709 */
710static void parse_spl_header(const uint32_t spl_addr)
711{
Andre Przywaracff5c132018-10-25 17:23:04 +0800712 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200713
Andre Przywaracff5c132018-10-25 17:23:04 +0800714 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200715 return;
Andre Przywaracff5c132018-10-25 17:23:04 +0800716
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200717 if (!spl->fel_script_address)
718 return;
719
720 if (spl->fel_uEnv_length != 0) {
721 /*
722 * data is expected in uEnv.txt compatible format, so "env
723 * import -t" the string(s) at fel_script_address right away.
724 */
Andre Przywara5a74a392016-09-05 01:32:41 +0100725 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200726 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
727 return;
728 }
729 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass018f5302017-08-03 12:22:10 -0600730 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200731}
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200732
Andre Heider928f4f42021-10-01 19:29:00 +0100733static bool get_unique_sid(unsigned int *sid)
734{
735 if (sunxi_get_sid(sid) != 0)
736 return false;
737
738 if (!sid[0])
739 return false;
740
741 /*
742 * The single words 1 - 3 of the SID have quite a few bits
743 * which are the same on many models, so we take a crc32
744 * of all 3 words, to get a more unique value.
745 *
746 * Note we only do this on newer SoCs as we cannot change
747 * the algorithm on older SoCs since those have been using
748 * fixed mac-addresses based on only using word 3 for a
749 * long time and changing a fixed mac-address with an
750 * u-boot update is not good.
751 */
752#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
753 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
754 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
755 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
756#endif
757
758 /* Ensure the NIC specific bytes of the mac are not all 0 */
759 if ((sid[3] & 0xffffff) == 0)
760 sid[3] |= 0x800000;
761
762 return true;
763}
764
Hans de Goedef2219612016-06-26 13:34:42 +0200765/*
766 * Note this function gets called multiple times.
767 * It must not make any changes to env variables which already exist.
768 */
769static void setup_environment(const void *fdt)
Jonathan Liub41d7d02014-06-14 08:59:09 +0200770{
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100771 char serial_string[17] = { 0 };
Hans de Goedecac5b1c2014-11-26 00:04:24 +0100772 unsigned int sid[4];
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100773 uint8_t mac_addr[6];
Hans de Goedef2219612016-06-26 13:34:42 +0200774 char ethaddr[16];
Andre Heider928f4f42021-10-01 19:29:00 +0100775 int i;
Hans de Goedef2219612016-06-26 13:34:42 +0200776
Andre Heider928f4f42021-10-01 19:29:00 +0100777 if (!get_unique_sid(sid))
778 return;
Hans de Goede3f8ea3b2016-07-29 11:47:03 +0200779
Andre Heider928f4f42021-10-01 19:29:00 +0100780 for (i = 0; i < 4; i++) {
781 sprintf(ethaddr, "ethernet%d", i);
782 if (!fdt_get_alias(fdt, ethaddr))
783 continue;
Hans de Goede97322c32016-07-27 17:58:06 +0200784
Andre Heider928f4f42021-10-01 19:29:00 +0100785 if (i == 0)
786 strcpy(ethaddr, "ethaddr");
787 else
788 sprintf(ethaddr, "eth%daddr", i);
Hans de Goedef2219612016-06-26 13:34:42 +0200789
Andre Heider928f4f42021-10-01 19:29:00 +0100790 if (env_get(ethaddr))
791 continue;
Hans de Goedef2219612016-06-26 13:34:42 +0200792
Andre Heider928f4f42021-10-01 19:29:00 +0100793 /* Non OUI / registered MAC address */
794 mac_addr[0] = (i << 4) | 0x02;
795 mac_addr[1] = (sid[0] >> 0) & 0xff;
796 mac_addr[2] = (sid[3] >> 24) & 0xff;
797 mac_addr[3] = (sid[3] >> 16) & 0xff;
798 mac_addr[4] = (sid[3] >> 8) & 0xff;
799 mac_addr[5] = (sid[3] >> 0) & 0xff;
Hans de Goedef2219612016-06-26 13:34:42 +0200800
Andre Heider928f4f42021-10-01 19:29:00 +0100801 eth_env_set_enetaddr(ethaddr, mac_addr);
802 }
Hans de Goedef2219612016-06-26 13:34:42 +0200803
Andre Heider928f4f42021-10-01 19:29:00 +0100804 if (!env_get("serial#")) {
805 snprintf(serial_string, sizeof(serial_string),
806 "%08x%08x", sid[0], sid[3]);
Hans de Goedef2219612016-06-26 13:34:42 +0200807
Andre Heider928f4f42021-10-01 19:29:00 +0100808 env_set("serial#", serial_string);
Hans de Goedef2219612016-06-26 13:34:42 +0200809 }
810}
811
Hans de Goedef2219612016-06-26 13:34:42 +0200812int misc_init_r(void)
813{
Samuel Holland20f3ee32020-10-24 10:21:54 -0500814 const char *spl_dt_name;
Maxime Ripardf4c35232017-08-23 10:08:29 +0200815 uint boot;
Jonathan Liub41d7d02014-06-14 08:59:09 +0200816
Simon Glass382bee52017-08-03 12:22:09 -0600817 env_set("fel_booted", NULL);
818 env_set("fel_scriptaddr", NULL);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200819 env_set("mmc_bootdev", NULL);
Maxime Ripardf4c35232017-08-23 10:08:29 +0200820
821 boot = sunxi_get_boot_device();
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200822 /* determine if we are running in FEL mode */
Maxime Ripardf4c35232017-08-23 10:08:29 +0200823 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass382bee52017-08-03 12:22:09 -0600824 env_set("fel_booted", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200825 parse_spl_header(SPL_ADDR);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200826 /* or if we booted from MMC, and which one */
827 } else if (boot == BOOT_DEVICE_MMC1) {
828 env_set("mmc_bootdev", "0");
829 } else if (boot == BOOT_DEVICE_MMC2) {
830 env_set("mmc_bootdev", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200831 }
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200832
Samuel Holland20f3ee32020-10-24 10:21:54 -0500833 /* Set fdtfile to match the FIT configuration chosen in SPL. */
834 spl_dt_name = get_spl_dt_name();
835 if (spl_dt_name) {
836 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
837 char str[64];
838
839 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
840 env_set("fdtfile", str);
841 }
842
Hans de Goedef2219612016-06-26 13:34:42 +0200843 setup_environment(gd->fdt_blob);
Jonathan Liub41d7d02014-06-14 08:59:09 +0200844
Andy Shevchenko92600ed2020-12-08 17:45:31 +0200845 return 0;
846}
847
848int board_late_init(void)
849{
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800850#ifdef CONFIG_USB_ETHER
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200851 usb_ether_init();
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800852#endif
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200853
Jonathan Liub41d7d02014-06-14 08:59:09 +0200854 return 0;
855}
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200856
Andre Heider9267ff82021-10-01 19:29:00 +0100857static void bluetooth_dt_fixup(void *blob)
858{
859 /* Some devices ship with a Bluetooth controller default address.
860 * Set a valid address through the device tree.
861 */
862 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
863 unsigned int sid[4];
864 int i;
865
866 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
867 return;
868
869 if (eth_env_get_enetaddr("bdaddr", tmp)) {
870 /* Convert between the binary formats of the corresponding stacks */
871 for (i = 0; i < ETH_ALEN; ++i)
872 bdaddr[i] = tmp[ETH_ALEN - i - 1];
873 } else {
874 if (!get_unique_sid(sid))
875 return;
876
877 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
878 bdaddr[1] = (sid[3] >> 8) & 0xff;
879 bdaddr[2] = (sid[3] >> 16) & 0xff;
880 bdaddr[3] = (sid[3] >> 24) & 0xff;
881 bdaddr[4] = (sid[0] >> 0) & 0xff;
882 bdaddr[5] = 0x02;
883 }
884
885 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
886 "local-bd-address", bdaddr, ETH_ALEN, 1);
887}
888
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900889int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200890{
Hans de Goeded75111a2016-03-22 22:51:52 +0100891 int __maybe_unused r;
892
Hans de Goedef2219612016-06-26 13:34:42 +0200893 /*
Icenowy Zheng2753b072021-09-11 19:39:16 +0200894 * Call setup_environment and fdt_fixup_ethernet again
895 * in case the boot fdt has ethernet aliases the u-boot
896 * copy does not have.
Hans de Goedef2219612016-06-26 13:34:42 +0200897 */
898 setup_environment(blob);
Icenowy Zheng2753b072021-09-11 19:39:16 +0200899 fdt_fixup_ethernet(blob);
Hans de Goedef2219612016-06-26 13:34:42 +0200900
Andre Heider9267ff82021-10-01 19:29:00 +0100901 bluetooth_dt_fixup(blob);
902
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200903#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goeded75111a2016-03-22 22:51:52 +0100904 r = sunxi_simplefb_setup(blob);
905 if (r)
906 return r;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200907#endif
Hans de Goeded75111a2016-03-22 22:51:52 +0100908 return 0;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200909}
Andre Przywara9ea3c352017-04-26 01:32:44 +0100910
911#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland41530cf2020-10-24 10:21:53 -0500912static void set_spl_dt_name(const char *name)
913{
914 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
915
916 if (spl == INVALID_SPL_HEADER)
917 return;
918
919 /* Promote the header version for U-Boot proper, if needed. */
920 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
921 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
922
923 strcpy((char *)&spl->string_pool, name);
924 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
925}
926
Andre Przywara9ea3c352017-04-26 01:32:44 +0100927int board_fit_config_name_match(const char *name)
928{
Samuel Holland467b7e52020-10-24 10:21:50 -0500929 const char *best_dt_name = get_spl_dt_name();
Samuel Holland41530cf2020-10-24 10:21:53 -0500930 int ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100931
932#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Holland467b7e52020-10-24 10:21:50 -0500933 if (best_dt_name == NULL)
Samuel Holland2fcd7482020-10-24 10:21:49 -0500934 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100935#endif
936
Samuel Holland467b7e52020-10-24 10:21:50 -0500937 if (best_dt_name == NULL) {
938 /* No DT name was provided, so accept the first config. */
939 return 0;
940 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800941#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Holland54ac5aa2020-10-24 10:21:51 -0500942 if (strstr(best_dt_name, "-pine64-plus")) {
943 /* Differentiate the Pine A64 boards by their DRAM size. */
944 if ((gd->ram_size == 512 * 1024 * 1024))
945 best_dt_name = "sun50i-a64-pine64";
Andre Przywara9ea3c352017-04-26 01:32:44 +0100946 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800947#endif
Samuel Holland8a8b73b2020-10-24 10:21:52 -0500948#ifdef CONFIG_PINEPHONE_DT_SELECTION
949 if (strstr(best_dt_name, "-pinephone")) {
950 /* Differentiate the PinePhone revisions by GPIO inputs. */
951 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
952 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
953 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
954 udelay(100);
955
956 /* PL6 is pulled low by the modem on v1.2. */
957 if (gpio_get_value(SUNXI_GPL(6)) == 0)
958 best_dt_name = "sun50i-a64-pinephone-1.2";
959 else
960 best_dt_name = "sun50i-a64-pinephone-1.1";
961
962 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
963 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
964 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
965 }
966#endif
967
Samuel Holland41530cf2020-10-24 10:21:53 -0500968 ret = strcmp(name, best_dt_name);
969
970 /*
971 * If one of the FIT configurations matches the most accurate DT name,
972 * update the SPL header to provide that DT name to U-Boot proper.
973 */
974 if (ret == 0)
975 set_spl_dt_name(best_dt_name);
976
977 return ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100978}
Andre Przywara64531492022-11-28 00:02:56 +0000979#endif /* CONFIG_SPL_LOAD_FIT */