blob: fdbcd4026938f4a16f6b34b315d5e3ff7fd2975d [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>
Jagan Teki237050f2018-05-07 13:03:36 +053014#include <dm.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060015#include <env.h>
Simon Glassdb41d652019-12-28 10:45:07 -070016#include <hang.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060017#include <image.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070018#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060019#include <log.h>
Hans de Goedee79c7c82014-10-02 21:13:54 +020020#include <mmc.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020021#include <axp_pmic.h>
Jagan Teki237050f2018-05-07 13:03:36 +053022#include <generic-phy.h>
23#include <phy-sun4i-usb.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010024#include <asm/arch/clock.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020025#include <asm/arch/cpu.h>
Luc Verhaegen2d7a0842014-08-13 07:55:07 +020026#include <asm/arch/display.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010027#include <asm/arch/dram.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010028#include <asm/arch/mmc.h>
Samuel Holland8a8b73b2020-10-24 10:21:52 -050029#include <asm/arch/prcm.h>
Hans de Goede4a8c7c12016-07-09 09:56:56 +020030#include <asm/arch/spl.h>
Simon Glass401d1c42020-10-30 21:38:53 -060031#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060032#include <linux/delay.h>
Simon Glass3db71102019-11-14 12:57:16 -070033#include <u-boot/crc.h>
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020034#ifndef CONFIG_ARM64
35#include <asm/armv7.h>
36#endif
Hans de Goede4f7e01c2015-04-23 23:23:50 +020037#include <asm/gpio.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020038#include <asm/io.h>
Philipp Tomsicha740ee92018-11-25 19:22:18 +010039#include <u-boot/crc.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060040#include <env_internal.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090041#include <linux/libfdt.h>
Andre Heider9267ff82021-10-01 19:29:00 +010042#include <fdt_support.h>
Hans de Goedef62bfa52015-08-15 11:55:26 +020043#include <nand.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020044#include <net.h>
Maxime Ripardf4c35232017-08-23 10:08:29 +020045#include <spl.h>
Jelle van der Waa0d8382a2016-02-23 18:47:19 +010046#include <sy8106a.h>
Simon Glass5d982852017-05-17 08:23:00 -060047#include <asm/setup.h>
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +020048#include <status_led.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010049
50DECLARE_GLOBAL_DATA_PTR;
51
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020052void i2c_init_board(void)
53{
54#ifdef CONFIG_I2C0_ENABLE
55#if defined(CONFIG_MACH_SUN4I) || \
56 defined(CONFIG_MACH_SUN5I) || \
57 defined(CONFIG_MACH_SUN7I) || \
58 defined(CONFIG_MACH_SUN8I_R40)
59 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
60 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
61 clock_twi_onoff(0, 1);
62#elif defined(CONFIG_MACH_SUN6I)
63 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
64 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
65 clock_twi_onoff(0, 1);
Icenowy Zheng8c51c652020-10-26 22:19:34 +080066#elif defined(CONFIG_MACH_SUN8I_V3S)
67 sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
68 sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
69 clock_twi_onoff(0, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020070#elif defined(CONFIG_MACH_SUN8I)
71 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
72 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
73 clock_twi_onoff(0, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +020074#elif defined(CONFIG_MACH_SUN50I)
75 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
76 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
77 clock_twi_onoff(0, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020078#endif
79#endif
80
81#ifdef CONFIG_I2C1_ENABLE
82#if defined(CONFIG_MACH_SUN4I) || \
83 defined(CONFIG_MACH_SUN7I) || \
84 defined(CONFIG_MACH_SUN8I_R40)
85 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
86 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
87 clock_twi_onoff(1, 1);
88#elif defined(CONFIG_MACH_SUN5I)
89 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
90 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
91 clock_twi_onoff(1, 1);
92#elif defined(CONFIG_MACH_SUN6I)
93 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
94 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
95 clock_twi_onoff(1, 1);
96#elif defined(CONFIG_MACH_SUN8I)
97 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
98 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
99 clock_twi_onoff(1, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +0200100#elif defined(CONFIG_MACH_SUN50I)
101 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
102 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
103 clock_twi_onoff(1, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200104#endif
105#endif
106
107#ifdef CONFIG_I2C2_ENABLE
108#if defined(CONFIG_MACH_SUN4I) || \
109 defined(CONFIG_MACH_SUN7I) || \
110 defined(CONFIG_MACH_SUN8I_R40)
111 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
112 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
113 clock_twi_onoff(2, 1);
114#elif defined(CONFIG_MACH_SUN5I)
115 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
116 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
117 clock_twi_onoff(2, 1);
118#elif defined(CONFIG_MACH_SUN6I)
119 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
120 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
121 clock_twi_onoff(2, 1);
122#elif defined(CONFIG_MACH_SUN8I)
123 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
124 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
125 clock_twi_onoff(2, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +0200126#elif defined(CONFIG_MACH_SUN50I)
127 sunxi_gpio_set_cfgpin(SUNXI_GPE(14), SUN50I_GPE_TWI2);
128 sunxi_gpio_set_cfgpin(SUNXI_GPE(15), SUN50I_GPE_TWI2);
129 clock_twi_onoff(2, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200130#endif
131#endif
132
133#ifdef CONFIG_I2C3_ENABLE
134#if defined(CONFIG_MACH_SUN6I)
135 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
136 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
137 clock_twi_onoff(3, 1);
138#elif defined(CONFIG_MACH_SUN7I) || \
139 defined(CONFIG_MACH_SUN8I_R40)
140 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
141 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
142 clock_twi_onoff(3, 1);
143#endif
144#endif
145
146#ifdef CONFIG_I2C4_ENABLE
147#if defined(CONFIG_MACH_SUN7I) || \
148 defined(CONFIG_MACH_SUN8I_R40)
149 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
150 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
151 clock_twi_onoff(4, 1);
152#endif
153#endif
154
155#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800156#ifdef CONFIG_MACH_SUN50I
157 clock_twi_onoff(5, 1);
158 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
159 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
Jernej Skrabecd0b07c12021-01-11 21:11:42 +0100160#elif CONFIG_MACH_SUN50I_H616
161 clock_twi_onoff(5, 1);
162 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN50I_H616_GPL_R_TWI);
163 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN50I_H616_GPL_R_TWI);
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800164#else
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200165 clock_twi_onoff(5, 1);
166 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
167 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
168#endif
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800169#endif
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200170}
171
Maxime Ripardb39117c2018-01-23 21:17:03 +0100172#if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT)
173enum env_location env_get_location(enum env_operation op, int prio)
174{
175 switch (prio) {
176 case 0:
177 return ENVL_FAT;
178
179 case 1:
180 return ENVL_MMC;
181
182 default:
183 return ENVL_UNKNOWN;
184 }
185}
186#endif
187
Andre Przywaraa7ae1592019-01-29 15:54:14 +0000188#ifdef CONFIG_DM_MMC
189static void mmc_pinmux_setup(int sdc);
190#endif
191
Ian Campbellcba69ee2014-05-05 11:52:26 +0100192/* add board specific code here */
193int board_init(void)
194{
Mylène Josserandf5fd7882017-04-02 12:59:10 +0200195 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100196
197 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
198
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200199#ifndef CONFIG_ARM64
Ian Campbellcba69ee2014-05-05 11:52:26 +0100200 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
201 debug("id_pfr1: 0x%08x\n", id_pfr1);
202 /* Generic Timer Extension available? */
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200203 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
204 uint32_t freq;
205
Ian Campbellcba69ee2014-05-05 11:52:26 +0100206 debug("Setting CNTFRQ\n");
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200207
208 /*
209 * CNTFRQ is a secure register, so we will crash if we try to
210 * write this from the non-secure world (read is OK, though).
211 * In case some bootcode has already set the correct value,
212 * we avoid the risk of writing to it.
213 */
214 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Andre Przywarae4916e82017-02-16 01:20:19 +0000215 if (freq != COUNTER_FREQUENCY) {
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200216 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Andre Przywarae4916e82017-02-16 01:20:19 +0000217 freq, COUNTER_FREQUENCY);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200218#ifdef CONFIG_NON_SECURE
219 printf("arch timer frequency is wrong, but cannot adjust it\n");
220#else
221 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Andre Przywarae4916e82017-02-16 01:20:19 +0000222 : : "r"(COUNTER_FREQUENCY));
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200223#endif
224 }
Ian Campbellcba69ee2014-05-05 11:52:26 +0100225 }
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200226#endif /* !CONFIG_ARM64 */
Ian Campbellcba69ee2014-05-05 11:52:26 +0100227
Hans de Goede2fcf0332015-04-25 17:25:14 +0200228 ret = axp_gpio_init();
229 if (ret)
230 return ret;
231
Andre Przywarae9ad1b82021-01-18 23:23:59 +0000232 /* strcmp() would look better, but doesn't get optimised away. */
233 if (CONFIG_SATAPWR[0]) {
234 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
235 if (satapwr_pin >= 0) {
236 gpio_request(satapwr_pin, "satapwr");
237 gpio_direction_output(satapwr_pin, 1);
238
239 /*
240 * Give the attached SATA device time to power-up
241 * to avoid link timeouts
242 */
243 mdelay(500);
244 }
245 }
246
247 if (CONFIG_MACPWR[0]) {
248 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
249 if (macpwr_pin >= 0) {
250 gpio_request(macpwr_pin, "macpwr");
251 gpio_direction_output(macpwr_pin, 1);
252 }
253 }
Hans de Goedefc8991c2016-03-17 13:53:03 +0100254
Igor Opaniuk2147a162021-02-09 13:52:45 +0200255#if CONFIG_IS_ENABLED(DM_I2C)
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +0200256 /*
257 * Temporary workaround for enabling I2C clocks until proper sunxi DM
258 * clk, reset and pinctrl drivers land.
259 */
260 i2c_init_board();
261#endif
262
Andre Przywaraa7ae1592019-01-29 15:54:14 +0000263#ifdef CONFIG_DM_MMC
264 /*
265 * Temporary workaround for enabling MMC clocks until a sunxi DM
266 * pinctrl driver lands.
267 */
268 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
269#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
270 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
271#endif
272#endif /* CONFIG_DM_MMC */
273
Samuel Holland24214972021-10-08 00:17:24 -0500274 return 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100275}
276
Andre Przywaracff5c132018-10-25 17:23:04 +0800277/*
278 * On older SoCs the SPL is actually at address zero, so using NULL as
279 * an error value does not work.
280 */
281#define INVALID_SPL_HEADER ((void *)~0UL)
282
283static struct boot_file_head * get_spl_header(uint8_t req_version)
284{
285 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
286 uint8_t spl_header_version = spl->spl_signature[3];
287
288 /* Is there really the SPL header (still) there? */
289 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
290 return INVALID_SPL_HEADER;
291
292 if (spl_header_version < req_version) {
293 printf("sunxi SPL version mismatch: expected %u, got %u\n",
294 req_version, spl_header_version);
295 return INVALID_SPL_HEADER;
296 }
297
298 return spl;
299}
300
Samuel Holland467b7e52020-10-24 10:21:50 -0500301static const char *get_spl_dt_name(void)
302{
303 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
304
305 /* Check if there is a DT name stored in the SPL header. */
306 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
307 return (char *)spl + spl->dt_name_offset;
308
309 return NULL;
310}
Samuel Holland467b7e52020-10-24 10:21:50 -0500311
Ian Campbellcba69ee2014-05-05 11:52:26 +0100312int dram_init(void)
313{
Andre Przywara57766102018-10-25 17:23:07 +0800314 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
315
316 if (spl == INVALID_SPL_HEADER)
317 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
318 PHYS_SDRAM_0_SIZE);
319 else
320 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
321
322 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
323 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100324
325 return 0;
326}
327
Boris Brezillon4ccae812016-06-15 21:09:23 +0200328#if defined(CONFIG_NAND_SUNXI)
Karol Gugalaad008292015-07-23 14:33:01 +0200329static void nand_pinmux_setup(void)
330{
331 unsigned int pin;
Hans de Goede022a99d2015-08-15 13:17:49 +0200332
333 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200334 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
335
Hans de Goede022a99d2015-08-15 13:17:49 +0200336#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
337 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200338 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200339#endif
340 /* sun4i / sun7i do have a PC23, but it is not used for nand,
341 * only sun7i has a PC24 */
342#ifdef CONFIG_MACH_SUN7I
Karol Gugalaad008292015-07-23 14:33:01 +0200343 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200344#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200345}
346
347static void nand_clock_setup(void)
348{
349 struct sunxi_ccm_reg *const ccm =
350 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede31c21472015-08-15 11:58:03 +0200351
Karol Gugalaad008292015-07-23 14:33:01 +0200352 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalba1c98b2018-02-28 20:51:53 +0100353#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
354 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
355 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
356#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200357 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
358}
Hans de Goedef62bfa52015-08-15 11:55:26 +0200359
360void board_nand_init(void)
361{
362 nand_pinmux_setup();
363 nand_clock_setup();
Boris Brezillon4ccae812016-06-15 21:09:23 +0200364#ifndef CONFIG_SPL_BUILD
365 sunxi_nand_init();
366#endif
Hans de Goedef62bfa52015-08-15 11:55:26 +0200367}
Karol Gugalaad008292015-07-23 14:33:01 +0200368#endif
369
Masahiro Yamada4aa2ba32017-05-09 20:31:39 +0900370#ifdef CONFIG_MMC
Ian Campbelle24ea552014-05-05 14:42:31 +0100371static void mmc_pinmux_setup(int sdc)
372{
373 unsigned int pin;
374
375 switch (sdc) {
376 case 0:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100377 /* SDC0: PF0-PF5 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100378 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100379 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100380 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
381 sunxi_gpio_set_drv(pin, 2);
382 }
383 break;
384
385 case 1:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800386#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
387 defined(CONFIG_MACH_SUN8I_R40)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500388 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100389 /* SDC1: PH22-PH-27 */
390 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
391 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
392 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
393 sunxi_gpio_set_drv(pin, 2);
394 }
395 } else {
396 /* SDC1: PG0-PG5 */
397 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
398 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
399 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
400 sunxi_gpio_set_drv(pin, 2);
401 }
402 }
403#elif defined(CONFIG_MACH_SUN5I)
404 /* SDC1: PG3-PG8 */
Hans de Goedebbff84b2014-10-03 16:44:57 +0200405 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100406 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbelle24ea552014-05-05 14:42:31 +0100407 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
408 sunxi_gpio_set_drv(pin, 2);
409 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100410#elif defined(CONFIG_MACH_SUN6I)
411 /* SDC1: PG0-PG5 */
412 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
413 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
414 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
415 sunxi_gpio_set_drv(pin, 2);
416 }
417#elif defined(CONFIG_MACH_SUN8I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500418 /* SDC1: PG0-PG5 */
419 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
420 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
421 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
422 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100423 }
424#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100425 break;
426
427 case 2:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100428#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
429 /* SDC2: PC6-PC11 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100430 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100431 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100432 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
433 sunxi_gpio_set_drv(pin, 2);
434 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100435#elif defined(CONFIG_MACH_SUN5I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500436 /* SDC2: PC6-PC15 */
437 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
438 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
439 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
440 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100441 }
442#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500443 /* SDC2: PC6-PC15, PC24 */
444 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
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, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100448 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500449
450 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
451 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
452 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800453#elif defined(CONFIG_MACH_SUN8I_R40)
454 /* SDC2: PC6-PC15, PC24 */
455 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
456 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
457 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
458 sunxi_gpio_set_drv(pin, 2);
459 }
460
461 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
462 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
463 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200464#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100465 /* SDC2: PC5-PC6, PC8-PC16 */
466 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
467 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100468 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
469 sunxi_gpio_set_drv(pin, 2);
470 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100471
472 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
473 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
474 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
475 sunxi_gpio_set_drv(pin, 2);
476 }
Icenowy Zheng42956f12018-07-21 16:20:29 +0800477#elif defined(CONFIG_MACH_SUN50I_H6)
478 /* SDC2: PC4-PC14 */
479 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
480 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
481 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
482 sunxi_gpio_set_drv(pin, 2);
483 }
Andre Przywara212224e2021-04-26 00:38:04 +0100484#elif defined(CONFIG_MACH_SUN50I_H616)
485 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
486 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
487 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
488 continue;
489 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
490 continue;
491 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
492 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
493 sunxi_gpio_set_drv(pin, 3);
494 }
Philipp Tomsich3ebb4562016-10-28 18:21:33 +0800495#elif defined(CONFIG_MACH_SUN9I)
496 /* SDC2: PC6-PC16 */
497 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
498 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
499 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
500 sunxi_gpio_set_drv(pin, 2);
501 }
Andre Przywara212224e2021-04-26 00:38:04 +0100502#else
503 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100504#endif
505 break;
506
507 case 3:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800508#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
509 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100510 /* SDC3: PI4-PI9 */
511 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
512 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
513 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
514 sunxi_gpio_set_drv(pin, 2);
515 }
516#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500517 /* SDC3: PC6-PC15, PC24 */
518 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
519 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
520 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
521 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100522 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500523
524 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
525 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
526 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100527#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100528 break;
529
530 default:
531 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
532 break;
533 }
534}
535
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900536int board_mmc_init(struct bd_info *bis)
Ian Campbelle24ea552014-05-05 14:42:31 +0100537{
Hans de Goedee79c7c82014-10-02 21:13:54 +0200538 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goedee79c7c82014-10-02 21:13:54 +0200539
Ian Campbelle24ea552014-05-05 14:42:31 +0100540 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200541 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
542 if (!mmc0)
543 return -1;
544
Hans de Goede2ccfac02014-10-02 20:43:50 +0200545#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbelle24ea552014-05-05 14:42:31 +0100546 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200547 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
548 if (!mmc1)
549 return -1;
550#endif
551
Ian Campbelle24ea552014-05-05 14:42:31 +0100552 return 0;
553}
Samuel Holland1011ebc2021-04-18 22:16:21 -0500554
555#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
556int mmc_get_env_dev(void)
557{
558 switch (sunxi_get_boot_device()) {
559 case BOOT_DEVICE_MMC1:
560 return 0;
561 case BOOT_DEVICE_MMC2:
562 return 1;
563 default:
564 return CONFIG_SYS_MMC_ENV_DEV;
565 }
566}
567#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100568#endif
569
Ian Campbellcba69ee2014-05-05 11:52:26 +0100570#ifdef CONFIG_SPL_BUILD
Andre Przywara57766102018-10-25 17:23:07 +0800571
572static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
573{
574 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
575
576 if (spl == INVALID_SPL_HEADER)
577 return;
578
579 /* Promote the header version for U-Boot proper, if needed. */
580 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
581 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
582
583 spl->dram_size = dram_size >> 20;
584}
585
Ian Campbellcba69ee2014-05-05 11:52:26 +0100586void sunxi_board_init(void)
587{
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200588 int power_failed = 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100589
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +0200590#ifdef CONFIG_LED_STATUS
591 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
592 status_led_init();
593#endif
594
Jelle van der Waa0d8382a2016-02-23 18:47:19 +0100595#ifdef CONFIG_SY8106A_POWER
596 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
597#endif
598
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800599#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100600 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
601 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200602 power_failed = axp_init();
603
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800604#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
605 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200606 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede24289202014-06-13 22:55:51 +0200607#endif
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100608#if !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200609 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
610 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100611#endif
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800612#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200613 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200614#endif
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800615#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
616 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200617 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200618#endif
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200619
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800620#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
621 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200622 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
623#endif
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100624#if !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200625 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100626#endif
627#if !defined(CONFIG_AXP152_POWER) && !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200628 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
629#endif
630#ifdef CONFIG_AXP209_POWER
631 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
632#endif
633
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800634#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
635 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800636 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
637 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800638#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800639 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
640 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800641#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200642 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
643 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
644 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
645#endif
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800646
647#ifdef CONFIG_AXP818_POWER
648 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
649 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
650 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800651#endif
652
653#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai15278cc2016-05-02 10:28:12 +0800654 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800655#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200656#endif
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000657 printf("DRAM:");
658 gd->ram_size = sunxi_dram_init();
659 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
660 if (!gd->ram_size)
661 hang();
662
663 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara57766102018-10-25 17:23:07 +0800664
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200665 /*
666 * Only clock up the CPU to full speed if we are reasonably
667 * assured it's being powered with suitable core voltage
668 */
669 if (!power_failed)
Iain Patone71b4222015-03-28 10:26:38 +0000670 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200671 else
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000672 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbellcba69ee2014-05-05 11:52:26 +0100673}
674#endif
Jonathan Liub41d7d02014-06-14 08:59:09 +0200675
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100676#ifdef CONFIG_USB_GADGET
677int g_dnl_board_usb_cable_connected(void)
678{
Jagan Teki237050f2018-05-07 13:03:36 +0530679 struct udevice *dev;
680 struct phy phy;
681 int ret;
682
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100683 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki237050f2018-05-07 13:03:36 +0530684 if (ret) {
685 pr_err("%s: Cannot find USB device\n", __func__);
686 return ret;
687 }
688
689 ret = generic_phy_get_by_name(dev, "usb", &phy);
690 if (ret) {
691 pr_err("failed to get %s USB PHY\n", dev->name);
692 return ret;
693 }
694
695 ret = generic_phy_init(&phy);
696 if (ret) {
Patrick Delaunayf286e372020-07-03 17:36:41 +0200697 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki237050f2018-05-07 13:03:36 +0530698 return ret;
699 }
700
Andre Przywarafbd92072021-11-02 19:45:47 +0000701 return sun4i_usb_phy_vbus_detect(&phy);
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100702}
703#endif
704
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100705#ifdef CONFIG_SERIAL_TAG
706void get_board_serial(struct tag_serialnr *serialnr)
707{
708 char *serial_string;
709 unsigned long long serial;
710
Simon Glass00caae62017-08-03 12:22:12 -0600711 serial_string = env_get("serial#");
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100712
713 if (serial_string) {
714 serial = simple_strtoull(serial_string, NULL, 16);
715
716 serialnr->high = (unsigned int) (serial >> 32);
717 serialnr->low = (unsigned int) (serial & 0xffffffff);
718 } else {
719 serialnr->high = 0;
720 serialnr->low = 0;
721 }
722}
723#endif
724
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200725/*
726 * Check the SPL header for the "sunxi" variant. If found: parse values
727 * that might have been passed by the loader ("fel" utility), and update
728 * the environment accordingly.
729 */
730static void parse_spl_header(const uint32_t spl_addr)
731{
Andre Przywaracff5c132018-10-25 17:23:04 +0800732 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200733
Andre Przywaracff5c132018-10-25 17:23:04 +0800734 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200735 return;
Andre Przywaracff5c132018-10-25 17:23:04 +0800736
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200737 if (!spl->fel_script_address)
738 return;
739
740 if (spl->fel_uEnv_length != 0) {
741 /*
742 * data is expected in uEnv.txt compatible format, so "env
743 * import -t" the string(s) at fel_script_address right away.
744 */
Andre Przywara5a74a392016-09-05 01:32:41 +0100745 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200746 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
747 return;
748 }
749 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass018f5302017-08-03 12:22:10 -0600750 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200751}
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200752
Andre Heider928f4f42021-10-01 19:29:00 +0100753static bool get_unique_sid(unsigned int *sid)
754{
755 if (sunxi_get_sid(sid) != 0)
756 return false;
757
758 if (!sid[0])
759 return false;
760
761 /*
762 * The single words 1 - 3 of the SID have quite a few bits
763 * which are the same on many models, so we take a crc32
764 * of all 3 words, to get a more unique value.
765 *
766 * Note we only do this on newer SoCs as we cannot change
767 * the algorithm on older SoCs since those have been using
768 * fixed mac-addresses based on only using word 3 for a
769 * long time and changing a fixed mac-address with an
770 * u-boot update is not good.
771 */
772#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
773 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
774 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
775 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
776#endif
777
778 /* Ensure the NIC specific bytes of the mac are not all 0 */
779 if ((sid[3] & 0xffffff) == 0)
780 sid[3] |= 0x800000;
781
782 return true;
783}
784
Hans de Goedef2219612016-06-26 13:34:42 +0200785/*
786 * Note this function gets called multiple times.
787 * It must not make any changes to env variables which already exist.
788 */
789static void setup_environment(const void *fdt)
Jonathan Liub41d7d02014-06-14 08:59:09 +0200790{
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100791 char serial_string[17] = { 0 };
Hans de Goedecac5b1c2014-11-26 00:04:24 +0100792 unsigned int sid[4];
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100793 uint8_t mac_addr[6];
Hans de Goedef2219612016-06-26 13:34:42 +0200794 char ethaddr[16];
Andre Heider928f4f42021-10-01 19:29:00 +0100795 int i;
Hans de Goedef2219612016-06-26 13:34:42 +0200796
Andre Heider928f4f42021-10-01 19:29:00 +0100797 if (!get_unique_sid(sid))
798 return;
Hans de Goede3f8ea3b2016-07-29 11:47:03 +0200799
Andre Heider928f4f42021-10-01 19:29:00 +0100800 for (i = 0; i < 4; i++) {
801 sprintf(ethaddr, "ethernet%d", i);
802 if (!fdt_get_alias(fdt, ethaddr))
803 continue;
Hans de Goede97322c32016-07-27 17:58:06 +0200804
Andre Heider928f4f42021-10-01 19:29:00 +0100805 if (i == 0)
806 strcpy(ethaddr, "ethaddr");
807 else
808 sprintf(ethaddr, "eth%daddr", i);
Hans de Goedef2219612016-06-26 13:34:42 +0200809
Andre Heider928f4f42021-10-01 19:29:00 +0100810 if (env_get(ethaddr))
811 continue;
Hans de Goedef2219612016-06-26 13:34:42 +0200812
Andre Heider928f4f42021-10-01 19:29:00 +0100813 /* Non OUI / registered MAC address */
814 mac_addr[0] = (i << 4) | 0x02;
815 mac_addr[1] = (sid[0] >> 0) & 0xff;
816 mac_addr[2] = (sid[3] >> 24) & 0xff;
817 mac_addr[3] = (sid[3] >> 16) & 0xff;
818 mac_addr[4] = (sid[3] >> 8) & 0xff;
819 mac_addr[5] = (sid[3] >> 0) & 0xff;
Hans de Goedef2219612016-06-26 13:34:42 +0200820
Andre Heider928f4f42021-10-01 19:29:00 +0100821 eth_env_set_enetaddr(ethaddr, mac_addr);
822 }
Hans de Goedef2219612016-06-26 13:34:42 +0200823
Andre Heider928f4f42021-10-01 19:29:00 +0100824 if (!env_get("serial#")) {
825 snprintf(serial_string, sizeof(serial_string),
826 "%08x%08x", sid[0], sid[3]);
Hans de Goedef2219612016-06-26 13:34:42 +0200827
Andre Heider928f4f42021-10-01 19:29:00 +0100828 env_set("serial#", serial_string);
Hans de Goedef2219612016-06-26 13:34:42 +0200829 }
830}
831
Hans de Goedef2219612016-06-26 13:34:42 +0200832int misc_init_r(void)
833{
Samuel Holland20f3ee32020-10-24 10:21:54 -0500834 const char *spl_dt_name;
Maxime Ripardf4c35232017-08-23 10:08:29 +0200835 uint boot;
Jonathan Liub41d7d02014-06-14 08:59:09 +0200836
Simon Glass382bee52017-08-03 12:22:09 -0600837 env_set("fel_booted", NULL);
838 env_set("fel_scriptaddr", NULL);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200839 env_set("mmc_bootdev", NULL);
Maxime Ripardf4c35232017-08-23 10:08:29 +0200840
841 boot = sunxi_get_boot_device();
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200842 /* determine if we are running in FEL mode */
Maxime Ripardf4c35232017-08-23 10:08:29 +0200843 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass382bee52017-08-03 12:22:09 -0600844 env_set("fel_booted", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200845 parse_spl_header(SPL_ADDR);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200846 /* or if we booted from MMC, and which one */
847 } else if (boot == BOOT_DEVICE_MMC1) {
848 env_set("mmc_bootdev", "0");
849 } else if (boot == BOOT_DEVICE_MMC2) {
850 env_set("mmc_bootdev", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200851 }
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200852
Samuel Holland20f3ee32020-10-24 10:21:54 -0500853 /* Set fdtfile to match the FIT configuration chosen in SPL. */
854 spl_dt_name = get_spl_dt_name();
855 if (spl_dt_name) {
856 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
857 char str[64];
858
859 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
860 env_set("fdtfile", str);
861 }
862
Hans de Goedef2219612016-06-26 13:34:42 +0200863 setup_environment(gd->fdt_blob);
Jonathan Liub41d7d02014-06-14 08:59:09 +0200864
Andy Shevchenko92600ed2020-12-08 17:45:31 +0200865 return 0;
866}
867
868int board_late_init(void)
869{
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800870#ifdef CONFIG_USB_ETHER
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200871 usb_ether_init();
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800872#endif
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200873
Jonathan Liub41d7d02014-06-14 08:59:09 +0200874 return 0;
875}
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200876
Andre Heider9267ff82021-10-01 19:29:00 +0100877static void bluetooth_dt_fixup(void *blob)
878{
879 /* Some devices ship with a Bluetooth controller default address.
880 * Set a valid address through the device tree.
881 */
882 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
883 unsigned int sid[4];
884 int i;
885
886 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
887 return;
888
889 if (eth_env_get_enetaddr("bdaddr", tmp)) {
890 /* Convert between the binary formats of the corresponding stacks */
891 for (i = 0; i < ETH_ALEN; ++i)
892 bdaddr[i] = tmp[ETH_ALEN - i - 1];
893 } else {
894 if (!get_unique_sid(sid))
895 return;
896
897 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
898 bdaddr[1] = (sid[3] >> 8) & 0xff;
899 bdaddr[2] = (sid[3] >> 16) & 0xff;
900 bdaddr[3] = (sid[3] >> 24) & 0xff;
901 bdaddr[4] = (sid[0] >> 0) & 0xff;
902 bdaddr[5] = 0x02;
903 }
904
905 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
906 "local-bd-address", bdaddr, ETH_ALEN, 1);
907}
908
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900909int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200910{
Hans de Goeded75111a2016-03-22 22:51:52 +0100911 int __maybe_unused r;
912
Hans de Goedef2219612016-06-26 13:34:42 +0200913 /*
914 * Call setup_environment again in case the boot fdt has
915 * ethernet aliases the u-boot copy does not have.
916 */
917 setup_environment(blob);
918
Andre Heider9267ff82021-10-01 19:29:00 +0100919 bluetooth_dt_fixup(blob);
920
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200921#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goeded75111a2016-03-22 22:51:52 +0100922 r = sunxi_simplefb_setup(blob);
923 if (r)
924 return r;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200925#endif
Hans de Goeded75111a2016-03-22 22:51:52 +0100926 return 0;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200927}
Andre Przywara9ea3c352017-04-26 01:32:44 +0100928
929#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland41530cf2020-10-24 10:21:53 -0500930
931static void set_spl_dt_name(const char *name)
932{
933 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
934
935 if (spl == INVALID_SPL_HEADER)
936 return;
937
938 /* Promote the header version for U-Boot proper, if needed. */
939 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
940 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
941
942 strcpy((char *)&spl->string_pool, name);
943 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
944}
945
Andre Przywara9ea3c352017-04-26 01:32:44 +0100946int board_fit_config_name_match(const char *name)
947{
Samuel Holland467b7e52020-10-24 10:21:50 -0500948 const char *best_dt_name = get_spl_dt_name();
Samuel Holland41530cf2020-10-24 10:21:53 -0500949 int ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100950
951#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Holland467b7e52020-10-24 10:21:50 -0500952 if (best_dt_name == NULL)
Samuel Holland2fcd7482020-10-24 10:21:49 -0500953 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100954#endif
955
Samuel Holland467b7e52020-10-24 10:21:50 -0500956 if (best_dt_name == NULL) {
957 /* No DT name was provided, so accept the first config. */
958 return 0;
959 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800960#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Holland54ac5aa2020-10-24 10:21:51 -0500961 if (strstr(best_dt_name, "-pine64-plus")) {
962 /* Differentiate the Pine A64 boards by their DRAM size. */
963 if ((gd->ram_size == 512 * 1024 * 1024))
964 best_dt_name = "sun50i-a64-pine64";
Andre Przywara9ea3c352017-04-26 01:32:44 +0100965 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800966#endif
Samuel Holland8a8b73b2020-10-24 10:21:52 -0500967#ifdef CONFIG_PINEPHONE_DT_SELECTION
968 if (strstr(best_dt_name, "-pinephone")) {
969 /* Differentiate the PinePhone revisions by GPIO inputs. */
970 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
971 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
972 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
973 udelay(100);
974
975 /* PL6 is pulled low by the modem on v1.2. */
976 if (gpio_get_value(SUNXI_GPL(6)) == 0)
977 best_dt_name = "sun50i-a64-pinephone-1.2";
978 else
979 best_dt_name = "sun50i-a64-pinephone-1.1";
980
981 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
982 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
983 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
984 }
985#endif
986
Samuel Holland41530cf2020-10-24 10:21:53 -0500987 ret = strcmp(name, best_dt_name);
988
989 /*
990 * If one of the FIT configurations matches the most accurate DT name,
991 * update the SPL header to provide that DT name to U-Boot proper.
992 */
993 if (ret == 0)
994 set_spl_dt_name(best_dt_name);
995
996 return ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100997}
998#endif