blob: 8c12c8deade3d802373937f34406735f5f57f44a [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>
Tom Rinif054f122023-07-17 15:29:20 -040037#include <linux/types.h>
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020038#ifndef CONFIG_ARM64
39#include <asm/armv7.h>
40#endif
Hans de Goede4f7e01c2015-04-23 23:23:50 +020041#include <asm/gpio.h>
Andre Przywara207ed0a2022-09-06 10:36:38 +010042#include <sunxi_gpio.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020043#include <asm/io.h>
Philipp Tomsicha740ee92018-11-25 19:22:18 +010044#include <u-boot/crc.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060045#include <env_internal.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090046#include <linux/libfdt.h>
Andre Heider9267ff82021-10-01 19:29:00 +010047#include <fdt_support.h>
Hans de Goedef62bfa52015-08-15 11:55:26 +020048#include <nand.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020049#include <net.h>
Maxime Ripardf4c35232017-08-23 10:08:29 +020050#include <spl.h>
Jelle van der Waa0d8382a2016-02-23 18:47:19 +010051#include <sy8106a.h>
Simon Glass5d982852017-05-17 08:23:00 -060052#include <asm/setup.h>
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +020053#include <status_led.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010054
55DECLARE_GLOBAL_DATA_PTR;
56
Jernej Skrabecacbc7e02017-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 Zheng8c51c652020-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 Skrabecacbc7e02017-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 Mavrodievda1ae592019-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 Skrabecacbc7e02017-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 Mavrodievda1ae592019-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 Skrabecacbc7e02017-04-27 00:03:35 +0200109#endif
110#endif
111
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200112#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick31a4ac42018-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 Skrabecd0b07c12021-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 Khoruzhick31a4ac42018-11-05 20:24:30 -0800121#else
Jernej Skrabecacbc7e02017-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 Khoruzhick31a4ac42018-11-05 20:24:30 -0800126#endif
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200127}
128
Andre Przywarae42dad42022-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 Hollande008e512022-04-20 23:15:39 +0100133 * When booting from NAND we try UBI first, then NAND directly.
Andre Przywarae42dad42022-01-11 12:46:04 +0000134 * SPI flash falls back to FAT (on SD card).
135 */
Maxime Ripardb39117c2018-01-23 21:17:03 +0100136enum env_location env_get_location(enum env_operation op, int prio)
137{
Samuel Hollande008e512022-04-20 23:15:39 +0100138 if (prio > 1)
139 return ENVL_UNKNOWN;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100140
Samuel Hollande008e512022-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 Ripardb39117c2018-01-23 21:17:03 +0100144
Andre Przywarae42dad42022-01-11 12:46:04 +0000145 switch (sunxi_get_boot_device()) {
146 case BOOT_DEVICE_MMC1:
147 case BOOT_DEVICE_MMC2:
Samuel Hollande008e512022-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 Przywarae42dad42022-01-11 12:46:04 +0000152 break;
153 case BOOT_DEVICE_NAND:
Samuel Hollande008e512022-04-20 23:15:39 +0100154 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
155 return ENVL_UBI;
Andre Przywarae42dad42022-01-11 12:46:04 +0000156 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
Samuel Hollande008e512022-04-20 23:15:39 +0100157 return ENVL_NAND;
Andre Przywarae42dad42022-01-11 12:46:04 +0000158 break;
159 case BOOT_DEVICE_SPI:
Samuel Hollande008e512022-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 Przywarae42dad42022-01-11 12:46:04 +0000164 break;
165 case BOOT_DEVICE_BOARD:
166 break;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100167 default:
Andre Przywarae42dad42022-01-11 12:46:04 +0000168 break;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100169 }
Andre Przywarae42dad42022-01-11 12:46:04 +0000170
Samuel Hollande008e512022-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 Przywarae42dad42022-01-11 12:46:04 +0000181 return ENVL_FAT;
Samuel Hollande008e512022-04-20 23:15:39 +0100182 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
183 return ENVL_UBI;
Andre Przywarae42dad42022-01-11 12:46:04 +0000184 }
185
186 return ENVL_UNKNOWN;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100187}
Maxime Ripardb39117c2018-01-23 21:17:03 +0100188
Ian Campbellcba69ee2014-05-05 11:52:26 +0100189/* add board specific code here */
190int board_init(void)
191{
Andre Przywara5ad98c52022-06-08 14:56:56 +0100192 __maybe_unused int id_pfr1, ret;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100193
194 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
195
Icenowy Zheng116e1ed2022-01-29 10:23:05 -0500196#if !defined(CONFIG_ARM64) && !defined(CONFIG_MACH_SUNIV)
Ian Campbellcba69ee2014-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 Siamashkad96ebc42016-03-29 17:29:10 +0200200 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
201 uint32_t freq;
202
Ian Campbellcba69ee2014-05-05 11:52:26 +0100203 debug("Setting CNTFRQ\n");
Siarhei Siamashkad96ebc42016-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 Fan151a0302022-04-13 17:47:22 +0800212 if (freq != CONFIG_COUNTER_FREQUENCY) {
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200213 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Peng Fan151a0302022-04-13 17:47:22 +0800214 freq, CONFIG_COUNTER_FREQUENCY);
Siarhei Siamashkad96ebc42016-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 Fan151a0302022-04-13 17:47:22 +0800219 : : "r"(CONFIG_COUNTER_FREQUENCY));
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200220#endif
221 }
Ian Campbellcba69ee2014-05-05 11:52:26 +0100222 }
Icenowy Zheng116e1ed2022-01-29 10:23:05 -0500223#endif /* !CONFIG_ARM64 && !CONFIG_MACH_SUNIV */
Ian Campbellcba69ee2014-05-05 11:52:26 +0100224
Hans de Goede2fcf0332015-04-25 17:25:14 +0200225 ret = axp_gpio_init();
226 if (ret)
227 return ret;
228
Igor Opaniuk2147a162021-02-09 13:52:45 +0200229#if CONFIG_IS_ENABLED(DM_I2C)
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +0200230 /*
231 * Temporary workaround for enabling I2C clocks until proper sunxi DM
232 * clk, reset and pinctrl drivers land.
233 */
234 i2c_init_board();
235#endif
Andre Przywarae9437532022-03-15 00:00:53 +0000236 eth_init_board();
237
Samuel Holland24214972021-10-08 00:17:24 -0500238 return 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100239}
240
Andre Przywaracff5c132018-10-25 17:23:04 +0800241/*
242 * On older SoCs the SPL is actually at address zero, so using NULL as
243 * an error value does not work.
244 */
245#define INVALID_SPL_HEADER ((void *)~0UL)
246
247static struct boot_file_head * get_spl_header(uint8_t req_version)
248{
249 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
250 uint8_t spl_header_version = spl->spl_signature[3];
251
252 /* Is there really the SPL header (still) there? */
253 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
254 return INVALID_SPL_HEADER;
255
256 if (spl_header_version < req_version) {
257 printf("sunxi SPL version mismatch: expected %u, got %u\n",
258 req_version, spl_header_version);
259 return INVALID_SPL_HEADER;
260 }
261
262 return spl;
263}
264
Samuel Holland467b7e52020-10-24 10:21:50 -0500265static const char *get_spl_dt_name(void)
266{
267 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
268
269 /* Check if there is a DT name stored in the SPL header. */
270 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
271 return (char *)spl + spl->dt_name_offset;
272
273 return NULL;
274}
Samuel Holland467b7e52020-10-24 10:21:50 -0500275
Ian Campbellcba69ee2014-05-05 11:52:26 +0100276int dram_init(void)
277{
Andre Przywara57766102018-10-25 17:23:07 +0800278 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
279
280 if (spl == INVALID_SPL_HEADER)
281 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
282 PHYS_SDRAM_0_SIZE);
283 else
284 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
285
286 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
287 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100288
289 return 0;
290}
291
Samuel Holland21b790f2023-01-22 16:06:35 -0600292#if defined(CONFIG_NAND_SUNXI) && defined(CONFIG_SPL_BUILD)
Karol Gugalaad008292015-07-23 14:33:01 +0200293static void nand_pinmux_setup(void)
294{
295 unsigned int pin;
Hans de Goede022a99d2015-08-15 13:17:49 +0200296
297 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200298 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
299
Hans de Goede022a99d2015-08-15 13:17:49 +0200300#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
301 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200302 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200303#endif
304 /* sun4i / sun7i do have a PC23, but it is not used for nand,
305 * only sun7i has a PC24 */
306#ifdef CONFIG_MACH_SUN7I
Karol Gugalaad008292015-07-23 14:33:01 +0200307 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200308#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200309}
310
311static void nand_clock_setup(void)
312{
313 struct sunxi_ccm_reg *const ccm =
314 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede31c21472015-08-15 11:58:03 +0200315
Karol Gugalaad008292015-07-23 14:33:01 +0200316 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalba1c98b2018-02-28 20:51:53 +0100317#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
318 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
319 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
320#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200321 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
322}
Hans de Goedef62bfa52015-08-15 11:55:26 +0200323
324void board_nand_init(void)
325{
326 nand_pinmux_setup();
327 nand_clock_setup();
328}
Andre Przywara64531492022-11-28 00:02:56 +0000329#endif /* CONFIG_NAND_SUNXI */
Karol Gugalaad008292015-07-23 14:33:01 +0200330
Masahiro Yamada4aa2ba32017-05-09 20:31:39 +0900331#ifdef CONFIG_MMC
Ian Campbelle24ea552014-05-05 14:42:31 +0100332static void mmc_pinmux_setup(int sdc)
333{
334 unsigned int pin;
335
336 switch (sdc) {
337 case 0:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100338 /* SDC0: PF0-PF5 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100339 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100340 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100341 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
342 sunxi_gpio_set_drv(pin, 2);
343 }
344 break;
345
346 case 1:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800347#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
348 defined(CONFIG_MACH_SUN8I_R40)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500349 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100350 /* SDC1: PH22-PH-27 */
351 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
352 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
353 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
354 sunxi_gpio_set_drv(pin, 2);
355 }
356 } else {
357 /* SDC1: PG0-PG5 */
358 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
359 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
360 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
361 sunxi_gpio_set_drv(pin, 2);
362 }
363 }
364#elif defined(CONFIG_MACH_SUN5I)
365 /* SDC1: PG3-PG8 */
Hans de Goedebbff84b2014-10-03 16:44:57 +0200366 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100367 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbelle24ea552014-05-05 14:42:31 +0100368 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
369 sunxi_gpio_set_drv(pin, 2);
370 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100371#elif defined(CONFIG_MACH_SUN6I)
372 /* SDC1: PG0-PG5 */
373 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
374 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
375 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
376 sunxi_gpio_set_drv(pin, 2);
377 }
378#elif defined(CONFIG_MACH_SUN8I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500379 /* SDC1: PG0-PG5 */
380 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
381 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
382 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
383 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100384 }
385#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100386 break;
387
388 case 2:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100389#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
390 /* SDC2: PC6-PC11 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100391 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100392 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100393 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
394 sunxi_gpio_set_drv(pin, 2);
395 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100396#elif defined(CONFIG_MACH_SUN5I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500397 /* SDC2: PC6-PC15 */
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 Kocialkowski8deacca2015-03-22 18:12:23 +0100402 }
403#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500404 /* SDC2: PC6-PC15, PC24 */
405 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
406 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
407 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
408 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100409 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500410
411 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
412 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
413 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800414#elif defined(CONFIG_MACH_SUN8I_R40)
415 /* SDC2: PC6-PC15, PC24 */
416 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
417 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
418 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
419 sunxi_gpio_set_drv(pin, 2);
420 }
421
422 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
423 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
424 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200425#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100426 /* SDC2: PC5-PC6, PC8-PC16 */
427 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
428 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100429 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
430 sunxi_gpio_set_drv(pin, 2);
431 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100432
433 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); 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 }
Icenowy Zheng42956f12018-07-21 16:20:29 +0800438#elif defined(CONFIG_MACH_SUN50I_H6)
439 /* SDC2: PC4-PC14 */
440 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
441 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
442 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
443 sunxi_gpio_set_drv(pin, 2);
444 }
Andre Przywara212224e2021-04-26 00:38:04 +0100445#elif defined(CONFIG_MACH_SUN50I_H616)
446 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
447 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
448 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
449 continue;
450 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
451 continue;
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, 3);
455 }
Philipp Tomsich3ebb4562016-10-28 18:21:33 +0800456#elif defined(CONFIG_MACH_SUN9I)
457 /* SDC2: PC6-PC16 */
458 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); 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 }
Okhunjon Sobirjonov5b7c58f2023-09-25 06:43:28 +0300463#elif defined(CONFIG_MACH_SUN8I_R528)
464 /* SDC2: PC2-PC7 */
465 for (pin = SUNXI_GPC(2); pin <= SUNXI_GPC(7); pin++) {
466 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
467 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
468 sunxi_gpio_set_drv(pin, 2);
469 }
Andre Przywara212224e2021-04-26 00:38:04 +0100470#else
471 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100472#endif
473 break;
474
475 case 3:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800476#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
477 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100478 /* SDC3: PI4-PI9 */
479 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
480 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
481 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
482 sunxi_gpio_set_drv(pin, 2);
483 }
484#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500485 /* SDC3: PC6-PC15, PC24 */
486 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
487 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
488 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
489 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100490 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500491
492 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
493 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
494 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100495#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100496 break;
497
498 default:
499 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
500 break;
501 }
502}
503
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900504int board_mmc_init(struct bd_info *bis)
Ian Campbelle24ea552014-05-05 14:42:31 +0100505{
Andre Przywaraed825862022-11-28 00:03:53 +0000506 /*
507 * The BROM always accesses MMC port 0 (typically an SD card), and
508 * most boards seem to have such a slot. The others haven't reported
509 * any problem with unconditionally enabling this in the SPL.
510 */
Samuel Holland3ba0a252022-04-10 00:13:33 -0500511 if (!IS_ENABLED(CONFIG_UART0_PORT_F)) {
Andre Przywaraed825862022-11-28 00:03:53 +0000512 mmc_pinmux_setup(0);
513 if (!sunxi_mmc_init(0))
Samuel Holland3ba0a252022-04-10 00:13:33 -0500514 return -1;
515 }
Hans de Goedee79c7c82014-10-02 21:13:54 +0200516
Samuel Holland3ba0a252022-04-10 00:13:33 -0500517 if (CONFIG_MMC_SUNXI_SLOT_EXTRA != -1) {
518 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
519 if (!sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA))
520 return -1;
521 }
Hans de Goedee79c7c82014-10-02 21:13:54 +0200522
Ian Campbelle24ea552014-05-05 14:42:31 +0100523 return 0;
524}
Samuel Holland1011ebc2021-04-18 22:16:21 -0500525
526#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
527int mmc_get_env_dev(void)
528{
529 switch (sunxi_get_boot_device()) {
530 case BOOT_DEVICE_MMC1:
531 return 0;
532 case BOOT_DEVICE_MMC2:
533 return 1;
534 default:
535 return CONFIG_SYS_MMC_ENV_DEV;
536 }
537}
538#endif
Andre Przywara64531492022-11-28 00:02:56 +0000539#endif /* CONFIG_MMC */
Ian Campbelle24ea552014-05-05 14:42:31 +0100540
Ian Campbellcba69ee2014-05-05 11:52:26 +0100541#ifdef CONFIG_SPL_BUILD
Andre Przywara57766102018-10-25 17:23:07 +0800542
543static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
544{
545 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
546
547 if (spl == INVALID_SPL_HEADER)
548 return;
549
550 /* Promote the header version for U-Boot proper, if needed. */
551 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
552 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
553
554 spl->dram_size = dram_size >> 20;
555}
556
Ian Campbellcba69ee2014-05-05 11:52:26 +0100557void sunxi_board_init(void)
558{
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200559 int power_failed = 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100560
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +0200561#ifdef CONFIG_LED_STATUS
562 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
563 status_led_init();
564#endif
565
Jelle van der Waa0d8382a2016-02-23 18:47:19 +0100566#ifdef CONFIG_SY8106A_POWER
567 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
568#endif
569
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800570#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100571 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
Andre Przywarad17d0512023-07-30 01:11:01 +0100572 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER || \
573 defined CONFIG_AXP313_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200574 power_failed = axp_init();
575
Chris Morgan52bcc4f2022-01-21 13:37:32 +0000576 if (IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {
577 u8 boot_reason;
578
579 pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
580 if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
581 printf("Power on by plug-in, shutting down.\n");
582 pmic_bus_write(0x32, BIT(7));
583 }
584 }
585
Andre Przywaraffb02942021-06-27 01:13:09 +0100586#ifdef CONFIG_AXP_DCDC1_VOLT
Hans de Goede6944aff2015-10-03 15:18:33 +0200587 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Andre Przywaraffb02942021-06-27 01:13:09 +0100588 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Hans de Goede24289202014-06-13 22:55:51 +0200589#endif
Andre Przywaraffb02942021-06-27 01:13:09 +0100590#ifdef CONFIG_AXP_DCDC2_VOLT
Hans de Goede6944aff2015-10-03 15:18:33 +0200591 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
592 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100593#endif
Andre Przywaraffb02942021-06-27 01:13:09 +0100594#ifdef CONFIG_AXP_DCDC4_VOLT
Hans de Goede6944aff2015-10-03 15:18:33 +0200595 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200596#endif
597
Andre Przywaraffb02942021-06-27 01:13:09 +0100598#ifdef CONFIG_AXP_ALDO1_VOLT
Hans de Goede6944aff2015-10-03 15:18:33 +0200599 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
600#endif
Andre Przywaraffb02942021-06-27 01:13:09 +0100601#ifdef CONFIG_AXP_ALDO2_VOLT
Hans de Goede6944aff2015-10-03 15:18:33 +0200602 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100603#endif
Andre Przywaraffb02942021-06-27 01:13:09 +0100604#ifdef CONFIG_AXP_ALDO3_VOLT
Hans de Goede6944aff2015-10-03 15:18:33 +0200605 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
606#endif
Andre Przywaraffb02942021-06-27 01:13:09 +0100607#ifdef CONFIG_AXP_ALDO4_VOLT
Hans de Goede6944aff2015-10-03 15:18:33 +0200608 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
609#endif
610
Andre Przywaraffb02942021-06-27 01:13:09 +0100611#ifdef CONFIG_AXP_DLDO1_VOLT
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800612 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
613 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Andre Przywaraffb02942021-06-27 01:13:09 +0100614#endif
615#ifdef CONFIG_AXP_DLDO3_VOLT
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800616 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
617 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800618#endif
Andre Przywaraffb02942021-06-27 01:13:09 +0100619#ifdef CONFIG_AXP_ELDO1_VOLT
Hans de Goede6944aff2015-10-03 15:18:33 +0200620 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
621 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
622 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
623#endif
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800624
Andre Przywaraffb02942021-06-27 01:13:09 +0100625#ifdef CONFIG_AXP_FLDO1_VOLT
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800626 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
627 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
628 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800629#endif
630
631#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai15278cc2016-05-02 10:28:12 +0800632 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800633#endif
Andre Przywaraffb02942021-06-27 01:13:09 +0100634#endif /* CONFIG_AXPxxx_POWER */
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000635 printf("DRAM:");
636 gd->ram_size = sunxi_dram_init();
637 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
638 if (!gd->ram_size)
639 hang();
640
641 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara57766102018-10-25 17:23:07 +0800642
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200643 /*
644 * Only clock up the CPU to full speed if we are reasonably
645 * assured it's being powered with suitable core voltage
646 */
647 if (!power_failed)
Tom Rini2f8a6db2021-12-14 13:36:40 -0500648 clock_set_pll1(get_board_sys_clk());
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200649 else
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000650 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbellcba69ee2014-05-05 11:52:26 +0100651}
Andre Przywara64531492022-11-28 00:02:56 +0000652#endif /* CONFIG_SPL_BUILD */
Jonathan Liub41d7d02014-06-14 08:59:09 +0200653
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100654#ifdef CONFIG_USB_GADGET
655int g_dnl_board_usb_cable_connected(void)
656{
Jagan Teki237050f2018-05-07 13:03:36 +0530657 struct udevice *dev;
658 struct phy phy;
659 int ret;
660
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100661 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki237050f2018-05-07 13:03:36 +0530662 if (ret) {
663 pr_err("%s: Cannot find USB device\n", __func__);
664 return ret;
665 }
666
667 ret = generic_phy_get_by_name(dev, "usb", &phy);
668 if (ret) {
669 pr_err("failed to get %s USB PHY\n", dev->name);
670 return ret;
671 }
672
673 ret = generic_phy_init(&phy);
674 if (ret) {
Patrick Delaunayf286e372020-07-03 17:36:41 +0200675 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki237050f2018-05-07 13:03:36 +0530676 return ret;
677 }
678
Andre Przywarafbd92072021-11-02 19:45:47 +0000679 return sun4i_usb_phy_vbus_detect(&phy);
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100680}
Andre Przywara64531492022-11-28 00:02:56 +0000681#endif /* CONFIG_USB_GADGET */
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100682
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100683#ifdef CONFIG_SERIAL_TAG
684void get_board_serial(struct tag_serialnr *serialnr)
685{
686 char *serial_string;
687 unsigned long long serial;
688
Simon Glass00caae62017-08-03 12:22:12 -0600689 serial_string = env_get("serial#");
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100690
691 if (serial_string) {
692 serial = simple_strtoull(serial_string, NULL, 16);
693
694 serialnr->high = (unsigned int) (serial >> 32);
695 serialnr->low = (unsigned int) (serial & 0xffffffff);
696 } else {
697 serialnr->high = 0;
698 serialnr->low = 0;
699 }
700}
701#endif
702
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200703/*
704 * Check the SPL header for the "sunxi" variant. If found: parse values
705 * that might have been passed by the loader ("fel" utility), and update
706 * the environment accordingly.
707 */
708static void parse_spl_header(const uint32_t spl_addr)
709{
Andre Przywaracff5c132018-10-25 17:23:04 +0800710 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200711
Andre Przywaracff5c132018-10-25 17:23:04 +0800712 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200713 return;
Andre Przywaracff5c132018-10-25 17:23:04 +0800714
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200715 if (!spl->fel_script_address)
716 return;
717
718 if (spl->fel_uEnv_length != 0) {
719 /*
720 * data is expected in uEnv.txt compatible format, so "env
721 * import -t" the string(s) at fel_script_address right away.
722 */
Andre Przywara5a74a392016-09-05 01:32:41 +0100723 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200724 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
725 return;
726 }
727 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass018f5302017-08-03 12:22:10 -0600728 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200729}
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200730
Andre Heider928f4f42021-10-01 19:29:00 +0100731static bool get_unique_sid(unsigned int *sid)
732{
733 if (sunxi_get_sid(sid) != 0)
734 return false;
735
736 if (!sid[0])
737 return false;
738
739 /*
740 * The single words 1 - 3 of the SID have quite a few bits
741 * which are the same on many models, so we take a crc32
742 * of all 3 words, to get a more unique value.
743 *
744 * Note we only do this on newer SoCs as we cannot change
745 * the algorithm on older SoCs since those have been using
746 * fixed mac-addresses based on only using word 3 for a
747 * long time and changing a fixed mac-address with an
748 * u-boot update is not good.
749 */
750#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
751 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
752 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
753 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
754#endif
755
756 /* Ensure the NIC specific bytes of the mac are not all 0 */
757 if ((sid[3] & 0xffffff) == 0)
758 sid[3] |= 0x800000;
759
760 return true;
761}
762
Hans de Goedef2219612016-06-26 13:34:42 +0200763/*
764 * Note this function gets called multiple times.
765 * It must not make any changes to env variables which already exist.
766 */
767static void setup_environment(const void *fdt)
Jonathan Liub41d7d02014-06-14 08:59:09 +0200768{
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100769 char serial_string[17] = { 0 };
Hans de Goedecac5b1c2014-11-26 00:04:24 +0100770 unsigned int sid[4];
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100771 uint8_t mac_addr[6];
Hans de Goedef2219612016-06-26 13:34:42 +0200772 char ethaddr[16];
Andre Heider928f4f42021-10-01 19:29:00 +0100773 int i;
Hans de Goedef2219612016-06-26 13:34:42 +0200774
Andre Heider928f4f42021-10-01 19:29:00 +0100775 if (!get_unique_sid(sid))
776 return;
Hans de Goede3f8ea3b2016-07-29 11:47:03 +0200777
Andre Heider928f4f42021-10-01 19:29:00 +0100778 for (i = 0; i < 4; i++) {
779 sprintf(ethaddr, "ethernet%d", i);
780 if (!fdt_get_alias(fdt, ethaddr))
781 continue;
Hans de Goede97322c32016-07-27 17:58:06 +0200782
Andre Heider928f4f42021-10-01 19:29:00 +0100783 if (i == 0)
784 strcpy(ethaddr, "ethaddr");
785 else
786 sprintf(ethaddr, "eth%daddr", i);
Hans de Goedef2219612016-06-26 13:34:42 +0200787
Andre Heider928f4f42021-10-01 19:29:00 +0100788 if (env_get(ethaddr))
789 continue;
Hans de Goedef2219612016-06-26 13:34:42 +0200790
Andre Heider928f4f42021-10-01 19:29:00 +0100791 /* Non OUI / registered MAC address */
792 mac_addr[0] = (i << 4) | 0x02;
793 mac_addr[1] = (sid[0] >> 0) & 0xff;
794 mac_addr[2] = (sid[3] >> 24) & 0xff;
795 mac_addr[3] = (sid[3] >> 16) & 0xff;
796 mac_addr[4] = (sid[3] >> 8) & 0xff;
797 mac_addr[5] = (sid[3] >> 0) & 0xff;
Hans de Goedef2219612016-06-26 13:34:42 +0200798
Andre Heider928f4f42021-10-01 19:29:00 +0100799 eth_env_set_enetaddr(ethaddr, mac_addr);
800 }
Hans de Goedef2219612016-06-26 13:34:42 +0200801
Andre Heider928f4f42021-10-01 19:29:00 +0100802 if (!env_get("serial#")) {
803 snprintf(serial_string, sizeof(serial_string),
804 "%08x%08x", sid[0], sid[3]);
Hans de Goedef2219612016-06-26 13:34:42 +0200805
Andre Heider928f4f42021-10-01 19:29:00 +0100806 env_set("serial#", serial_string);
Hans de Goedef2219612016-06-26 13:34:42 +0200807 }
808}
809
Hans de Goedef2219612016-06-26 13:34:42 +0200810int misc_init_r(void)
811{
Samuel Holland20f3ee32020-10-24 10:21:54 -0500812 const char *spl_dt_name;
Maxime Ripardf4c35232017-08-23 10:08:29 +0200813 uint boot;
Jonathan Liub41d7d02014-06-14 08:59:09 +0200814
Simon Glass382bee52017-08-03 12:22:09 -0600815 env_set("fel_booted", NULL);
816 env_set("fel_scriptaddr", NULL);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200817 env_set("mmc_bootdev", NULL);
Maxime Ripardf4c35232017-08-23 10:08:29 +0200818
819 boot = sunxi_get_boot_device();
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200820 /* determine if we are running in FEL mode */
Maxime Ripardf4c35232017-08-23 10:08:29 +0200821 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass382bee52017-08-03 12:22:09 -0600822 env_set("fel_booted", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200823 parse_spl_header(SPL_ADDR);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200824 /* or if we booted from MMC, and which one */
825 } else if (boot == BOOT_DEVICE_MMC1) {
826 env_set("mmc_bootdev", "0");
827 } else if (boot == BOOT_DEVICE_MMC2) {
828 env_set("mmc_bootdev", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200829 }
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200830
Samuel Holland20f3ee32020-10-24 10:21:54 -0500831 /* Set fdtfile to match the FIT configuration chosen in SPL. */
832 spl_dt_name = get_spl_dt_name();
833 if (spl_dt_name) {
834 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
835 char str[64];
836
837 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
838 env_set("fdtfile", str);
839 }
840
Hans de Goedef2219612016-06-26 13:34:42 +0200841 setup_environment(gd->fdt_blob);
Jonathan Liub41d7d02014-06-14 08:59:09 +0200842
Andy Shevchenko92600ed2020-12-08 17:45:31 +0200843 return 0;
844}
845
846int board_late_init(void)
847{
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800848#ifdef CONFIG_USB_ETHER
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200849 usb_ether_init();
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800850#endif
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200851
Jonathan Liub41d7d02014-06-14 08:59:09 +0200852 return 0;
853}
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200854
Andre Heider9267ff82021-10-01 19:29:00 +0100855static void bluetooth_dt_fixup(void *blob)
856{
857 /* Some devices ship with a Bluetooth controller default address.
858 * Set a valid address through the device tree.
859 */
860 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
861 unsigned int sid[4];
862 int i;
863
864 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
865 return;
866
867 if (eth_env_get_enetaddr("bdaddr", tmp)) {
868 /* Convert between the binary formats of the corresponding stacks */
869 for (i = 0; i < ETH_ALEN; ++i)
870 bdaddr[i] = tmp[ETH_ALEN - i - 1];
871 } else {
872 if (!get_unique_sid(sid))
873 return;
874
875 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
876 bdaddr[1] = (sid[3] >> 8) & 0xff;
877 bdaddr[2] = (sid[3] >> 16) & 0xff;
878 bdaddr[3] = (sid[3] >> 24) & 0xff;
879 bdaddr[4] = (sid[0] >> 0) & 0xff;
880 bdaddr[5] = 0x02;
881 }
882
883 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
884 "local-bd-address", bdaddr, ETH_ALEN, 1);
885}
886
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900887int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200888{
Hans de Goeded75111a2016-03-22 22:51:52 +0100889 int __maybe_unused r;
890
Hans de Goedef2219612016-06-26 13:34:42 +0200891 /*
Icenowy Zheng2753b072021-09-11 19:39:16 +0200892 * Call setup_environment and fdt_fixup_ethernet again
893 * in case the boot fdt has ethernet aliases the u-boot
894 * copy does not have.
Hans de Goedef2219612016-06-26 13:34:42 +0200895 */
896 setup_environment(blob);
Icenowy Zheng2753b072021-09-11 19:39:16 +0200897 fdt_fixup_ethernet(blob);
Hans de Goedef2219612016-06-26 13:34:42 +0200898
Andre Heider9267ff82021-10-01 19:29:00 +0100899 bluetooth_dt_fixup(blob);
900
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200901#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goeded75111a2016-03-22 22:51:52 +0100902 r = sunxi_simplefb_setup(blob);
903 if (r)
904 return r;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200905#endif
Hans de Goeded75111a2016-03-22 22:51:52 +0100906 return 0;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200907}
Andre Przywara9ea3c352017-04-26 01:32:44 +0100908
909#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland41530cf2020-10-24 10:21:53 -0500910static void set_spl_dt_name(const char *name)
911{
912 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
913
914 if (spl == INVALID_SPL_HEADER)
915 return;
916
917 /* Promote the header version for U-Boot proper, if needed. */
918 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
919 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
920
921 strcpy((char *)&spl->string_pool, name);
922 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
923}
924
Andre Przywara9ea3c352017-04-26 01:32:44 +0100925int board_fit_config_name_match(const char *name)
926{
Samuel Holland467b7e52020-10-24 10:21:50 -0500927 const char *best_dt_name = get_spl_dt_name();
Samuel Holland41530cf2020-10-24 10:21:53 -0500928 int ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100929
930#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Holland467b7e52020-10-24 10:21:50 -0500931 if (best_dt_name == NULL)
Samuel Holland2fcd7482020-10-24 10:21:49 -0500932 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100933#endif
934
Samuel Holland467b7e52020-10-24 10:21:50 -0500935 if (best_dt_name == NULL) {
936 /* No DT name was provided, so accept the first config. */
937 return 0;
938 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800939#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Holland54ac5aa2020-10-24 10:21:51 -0500940 if (strstr(best_dt_name, "-pine64-plus")) {
941 /* Differentiate the Pine A64 boards by their DRAM size. */
Tom Rinif054f122023-07-17 15:29:20 -0400942 if (gd->ram_size == SZ_512M)
Samuel Holland54ac5aa2020-10-24 10:21:51 -0500943 best_dt_name = "sun50i-a64-pine64";
Andre Przywara9ea3c352017-04-26 01:32:44 +0100944 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800945#endif
Samuel Holland8a8b73b2020-10-24 10:21:52 -0500946#ifdef CONFIG_PINEPHONE_DT_SELECTION
947 if (strstr(best_dt_name, "-pinephone")) {
948 /* Differentiate the PinePhone revisions by GPIO inputs. */
949 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
950 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
951 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
952 udelay(100);
953
954 /* PL6 is pulled low by the modem on v1.2. */
955 if (gpio_get_value(SUNXI_GPL(6)) == 0)
956 best_dt_name = "sun50i-a64-pinephone-1.2";
957 else
958 best_dt_name = "sun50i-a64-pinephone-1.1";
959
960 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
961 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
962 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
963 }
964#endif
965
Samuel Holland41530cf2020-10-24 10:21:53 -0500966 ret = strcmp(name, best_dt_name);
967
968 /*
969 * If one of the FIT configurations matches the most accurate DT name,
970 * update the SPL header to provide that DT name to U-Boot proper.
971 */
972 if (ret == 0)
973 set_spl_dt_name(best_dt_name);
974
975 return ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100976}
Andre Przywara64531492022-11-28 00:02:56 +0000977#endif /* CONFIG_SPL_LOAD_FIT */