blob: 371ed9eebaf3d3504a0acadef6cf4784af4d9266 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbell6efe3692014-05-05 11:52:26 +01002/*
3 * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net>
4 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 *
6 * (C) Copyright 2007-2011
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * Tom Cubie <tangliang@allwinnertech.com>
9 *
10 * Some board init for the Allwinner A10-evb board.
Ian Campbell6efe3692014-05-05 11:52:26 +010011 */
12
13#include <common.h>
Tom Rini8c70baa2021-12-14 13:36:40 -050014#include <clock_legacy.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053015#include <dm.h>
Simon Glass313112a2019-08-01 09:46:46 -060016#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070017#include <hang.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060018#include <image.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070019#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060020#include <log.h>
Hans de Goede63deaa82014-10-02 21:13:54 +020021#include <mmc.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020022#include <axp_pmic.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053023#include <generic-phy.h>
24#include <phy-sun4i-usb.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010025#include <asm/arch/clock.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020026#include <asm/arch/cpu.h>
Luc Verhaegen4869a8c2014-08-13 07:55:07 +020027#include <asm/arch/display.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010028#include <asm/arch/dram.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010029#include <asm/arch/mmc.h>
Samuel Holland9c7cefc2020-10-24 10:21:52 -050030#include <asm/arch/prcm.h>
Chris Morgan2ff2a1d2022-01-21 13:37:32 +000031#include <asm/arch/pmic_bus.h>
Hans de Goedea146c502016-07-09 09:56:56 +020032#include <asm/arch/spl.h>
Andre Przywara1823c232022-03-15 00:00:53 +000033#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060034#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060035#include <linux/delay.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070036#include <u-boot/crc.h>
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020037#ifndef CONFIG_ARM64
38#include <asm/armv7.h>
39#endif
Hans de Goeded9d05652015-04-23 23:23:50 +020040#include <asm/gpio.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020041#include <asm/io.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010042#include <u-boot/crc.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060043#include <env_internal.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090044#include <linux/libfdt.h>
Andre Heiderbf8c8102021-10-01 19:29:00 +010045#include <fdt_support.h>
Hans de Goede5ed52f62015-08-15 11:55:26 +020046#include <nand.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020047#include <net.h>
Maxime Ripardae56d972017-08-23 10:08:29 +020048#include <spl.h>
Jelle van der Waa3f3a3092016-02-23 18:47:19 +010049#include <sy8106a.h>
Simon Glassd9a766f2017-05-17 08:23:00 -060050#include <asm/setup.h>
Arnaud Ferraris61485e92021-09-08 21:14:19 +020051#include <status_led.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010052
53DECLARE_GLOBAL_DATA_PTR;
54
Jernej Skrabec07da8802017-04-27 00:03:35 +020055void i2c_init_board(void)
56{
57#ifdef CONFIG_I2C0_ENABLE
58#if defined(CONFIG_MACH_SUN4I) || \
59 defined(CONFIG_MACH_SUN5I) || \
60 defined(CONFIG_MACH_SUN7I) || \
61 defined(CONFIG_MACH_SUN8I_R40)
62 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
63 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
64 clock_twi_onoff(0, 1);
65#elif defined(CONFIG_MACH_SUN6I)
66 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
67 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
68 clock_twi_onoff(0, 1);
Icenowy Zheng365951a2020-10-26 22:19:34 +080069#elif defined(CONFIG_MACH_SUN8I_V3S)
70 sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
71 sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
72 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +020073#elif defined(CONFIG_MACH_SUN8I)
74 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
75 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
76 clock_twi_onoff(0, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +020077#elif defined(CONFIG_MACH_SUN50I)
78 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
79 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
80 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +020081#endif
82#endif
83
84#ifdef CONFIG_I2C1_ENABLE
85#if defined(CONFIG_MACH_SUN4I) || \
86 defined(CONFIG_MACH_SUN7I) || \
87 defined(CONFIG_MACH_SUN8I_R40)
88 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
89 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
90 clock_twi_onoff(1, 1);
91#elif defined(CONFIG_MACH_SUN5I)
92 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
93 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
94 clock_twi_onoff(1, 1);
95#elif defined(CONFIG_MACH_SUN6I)
96 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
97 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
98 clock_twi_onoff(1, 1);
99#elif defined(CONFIG_MACH_SUN8I)
100 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
101 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
102 clock_twi_onoff(1, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200103#elif defined(CONFIG_MACH_SUN50I)
104 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
105 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
106 clock_twi_onoff(1, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200107#endif
108#endif
109
Jernej Skrabec07da8802017-04-27 00:03:35 +0200110#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800111#ifdef CONFIG_MACH_SUN50I
112 clock_twi_onoff(5, 1);
113 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
114 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
Jernej Skrabec7de8eb02021-01-11 21:11:42 +0100115#elif CONFIG_MACH_SUN50I_H616
116 clock_twi_onoff(5, 1);
117 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN50I_H616_GPL_R_TWI);
118 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN50I_H616_GPL_R_TWI);
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800119#else
Jernej Skrabec07da8802017-04-27 00:03:35 +0200120 clock_twi_onoff(5, 1);
121 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
122 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
123#endif
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800124#endif
Jernej Skrabec07da8802017-04-27 00:03:35 +0200125}
126
Andre Przywarab176bf32022-01-11 12:46:04 +0000127/*
128 * Try to use the environment from the boot source first.
129 * For MMC, this means a FAT partition on the boot device (SD or eMMC).
130 * If the raw MMC environment is also enabled, this is tried next.
131 * SPI flash falls back to FAT (on SD card).
132 */
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100133enum env_location env_get_location(enum env_operation op, int prio)
134{
Andre Przywarab176bf32022-01-11 12:46:04 +0000135 enum env_location boot_loc = ENVL_FAT;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100136
Andre Przywarab176bf32022-01-11 12:46:04 +0000137 gd->env_load_prio = prio;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100138
Andre Przywarab176bf32022-01-11 12:46:04 +0000139 switch (sunxi_get_boot_device()) {
140 case BOOT_DEVICE_MMC1:
141 case BOOT_DEVICE_MMC2:
142 boot_loc = ENVL_FAT;
143 break;
144 case BOOT_DEVICE_NAND:
145 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
146 boot_loc = ENVL_NAND;
147 break;
148 case BOOT_DEVICE_SPI:
149 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
150 boot_loc = ENVL_SPI_FLASH;
151 break;
152 case BOOT_DEVICE_BOARD:
153 break;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100154 default:
Andre Przywarab176bf32022-01-11 12:46:04 +0000155 break;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100156 }
Andre Przywarab176bf32022-01-11 12:46:04 +0000157
158 /* Always try to access the environment on the boot device first. */
159 if (prio == 0)
160 return boot_loc;
161
162 if (prio == 1) {
163 switch (boot_loc) {
164 case ENVL_SPI_FLASH:
165 return ENVL_FAT;
166 case ENVL_FAT:
167 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
168 return ENVL_MMC;
169 break;
170 default:
171 break;
172 }
173 }
174
175 return ENVL_UNKNOWN;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100176}
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100177
Andre Przywarad7cea362019-01-29 15:54:14 +0000178#ifdef CONFIG_DM_MMC
179static void mmc_pinmux_setup(int sdc);
180#endif
181
Ian Campbell6efe3692014-05-05 11:52:26 +0100182/* add board specific code here */
183int board_init(void)
184{
Mylène Josserand147c6062017-04-02 12:59:10 +0200185 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbell6efe3692014-05-05 11:52:26 +0100186
187 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
188
Icenowy Zheng3a3b7342022-01-29 10:23:05 -0500189#if !defined(CONFIG_ARM64) && !defined(CONFIG_MACH_SUNIV)
Ian Campbell6efe3692014-05-05 11:52:26 +0100190 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
191 debug("id_pfr1: 0x%08x\n", id_pfr1);
192 /* Generic Timer Extension available? */
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200193 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
194 uint32_t freq;
195
Ian Campbell6efe3692014-05-05 11:52:26 +0100196 debug("Setting CNTFRQ\n");
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200197
198 /*
199 * CNTFRQ is a secure register, so we will crash if we try to
200 * write this from the non-secure world (read is OK, though).
201 * In case some bootcode has already set the correct value,
202 * we avoid the risk of writing to it.
203 */
204 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Peng Fane7c59392022-04-13 17:47:22 +0800205 if (freq != CONFIG_COUNTER_FREQUENCY) {
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200206 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Peng Fane7c59392022-04-13 17:47:22 +0800207 freq, CONFIG_COUNTER_FREQUENCY);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200208#ifdef CONFIG_NON_SECURE
209 printf("arch timer frequency is wrong, but cannot adjust it\n");
210#else
211 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Peng Fane7c59392022-04-13 17:47:22 +0800212 : : "r"(CONFIG_COUNTER_FREQUENCY));
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200213#endif
214 }
Ian Campbell6efe3692014-05-05 11:52:26 +0100215 }
Icenowy Zheng3a3b7342022-01-29 10:23:05 -0500216#endif /* !CONFIG_ARM64 && !CONFIG_MACH_SUNIV */
Ian Campbell6efe3692014-05-05 11:52:26 +0100217
Hans de Goede3ae1d132015-04-25 17:25:14 +0200218 ret = axp_gpio_init();
219 if (ret)
220 return ret;
221
Andre Przywara3b2dbb52021-01-18 23:23:59 +0000222 /* strcmp() would look better, but doesn't get optimised away. */
223 if (CONFIG_SATAPWR[0]) {
224 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
225 if (satapwr_pin >= 0) {
226 gpio_request(satapwr_pin, "satapwr");
227 gpio_direction_output(satapwr_pin, 1);
228
229 /*
230 * Give the attached SATA device time to power-up
231 * to avoid link timeouts
232 */
233 mdelay(500);
234 }
235 }
236
237 if (CONFIG_MACPWR[0]) {
238 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
239 if (macpwr_pin >= 0) {
240 gpio_request(macpwr_pin, "macpwr");
241 gpio_direction_output(macpwr_pin, 1);
242 }
243 }
Hans de Goede42cbbe32016-03-17 13:53:03 +0100244
Igor Opaniukf7c91762021-02-09 13:52:45 +0200245#if CONFIG_IS_ENABLED(DM_I2C)
Jernej Skrabec9220d502017-04-27 00:03:36 +0200246 /*
247 * Temporary workaround for enabling I2C clocks until proper sunxi DM
248 * clk, reset and pinctrl drivers land.
249 */
250 i2c_init_board();
251#endif
Andre Przywarad7cea362019-01-29 15:54:14 +0000252
Andre Przywara1823c232022-03-15 00:00:53 +0000253 eth_init_board();
254
Samuel Holland75fe0f42021-10-08 00:17:24 -0500255 return 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100256}
257
Andre Przywara14a25392018-10-25 17:23:04 +0800258/*
259 * On older SoCs the SPL is actually at address zero, so using NULL as
260 * an error value does not work.
261 */
262#define INVALID_SPL_HEADER ((void *)~0UL)
263
264static struct boot_file_head * get_spl_header(uint8_t req_version)
265{
266 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
267 uint8_t spl_header_version = spl->spl_signature[3];
268
269 /* Is there really the SPL header (still) there? */
270 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
271 return INVALID_SPL_HEADER;
272
273 if (spl_header_version < req_version) {
274 printf("sunxi SPL version mismatch: expected %u, got %u\n",
275 req_version, spl_header_version);
276 return INVALID_SPL_HEADER;
277 }
278
279 return spl;
280}
281
Samuel Hollandba44e942020-10-24 10:21:50 -0500282static const char *get_spl_dt_name(void)
283{
284 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
285
286 /* Check if there is a DT name stored in the SPL header. */
287 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
288 return (char *)spl + spl->dt_name_offset;
289
290 return NULL;
291}
Samuel Hollandba44e942020-10-24 10:21:50 -0500292
Ian Campbell6efe3692014-05-05 11:52:26 +0100293int dram_init(void)
294{
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800295 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
296
297 if (spl == INVALID_SPL_HEADER)
298 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
299 PHYS_SDRAM_0_SIZE);
300 else
301 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
302
303 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
304 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbell6efe3692014-05-05 11:52:26 +0100305
306 return 0;
307}
308
Boris Brezillon57f20382016-06-15 21:09:23 +0200309#if defined(CONFIG_NAND_SUNXI)
Karol Gugala7bea8932015-07-23 14:33:01 +0200310static void nand_pinmux_setup(void)
311{
312 unsigned int pin;
Karol Gugala7bea8932015-07-23 14:33:01 +0200313
Hans de Goeded2236782015-08-15 13:17:49 +0200314 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugala7bea8932015-07-23 14:33:01 +0200315 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
316
Hans de Goeded2236782015-08-15 13:17:49 +0200317#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
318 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
319 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
320#endif
321 /* sun4i / sun7i do have a PC23, but it is not used for nand,
322 * only sun7i has a PC24 */
323#ifdef CONFIG_MACH_SUN7I
Karol Gugala7bea8932015-07-23 14:33:01 +0200324 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goeded2236782015-08-15 13:17:49 +0200325#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200326}
327
328static void nand_clock_setup(void)
329{
330 struct sunxi_ccm_reg *const ccm =
331 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedee5561a82015-08-15 11:58:03 +0200332
Karol Gugala7bea8932015-07-23 14:33:01 +0200333 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalebeeb802018-02-28 20:51:53 +0100334#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
335 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
336 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
337#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200338 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
339}
Hans de Goede5ed52f62015-08-15 11:55:26 +0200340
341void board_nand_init(void)
342{
343 nand_pinmux_setup();
344 nand_clock_setup();
Boris Brezillon57f20382016-06-15 21:09:23 +0200345#ifndef CONFIG_SPL_BUILD
346 sunxi_nand_init();
347#endif
Hans de Goede5ed52f62015-08-15 11:55:26 +0200348}
Karol Gugala7bea8932015-07-23 14:33:01 +0200349#endif
350
Masahiro Yamada0a780172017-05-09 20:31:39 +0900351#ifdef CONFIG_MMC
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100352static void mmc_pinmux_setup(int sdc)
353{
354 unsigned int pin;
355
356 switch (sdc) {
357 case 0:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100358 /* SDC0: PF0-PF5 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100359 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100360 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100361 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
362 sunxi_gpio_set_drv(pin, 2);
363 }
364 break;
365
366 case 1:
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800367#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
368 defined(CONFIG_MACH_SUN8I_R40)
Samuel Holland51951052021-09-12 10:28:35 -0500369 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100370 /* SDC1: PH22-PH-27 */
371 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
372 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
373 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
374 sunxi_gpio_set_drv(pin, 2);
375 }
376 } else {
377 /* SDC1: PG0-PG5 */
378 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
379 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
380 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
381 sunxi_gpio_set_drv(pin, 2);
382 }
383 }
384#elif defined(CONFIG_MACH_SUN5I)
385 /* SDC1: PG3-PG8 */
Hans de Goede4dccfd42014-10-03 16:44:57 +0200386 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100387 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100388 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
389 sunxi_gpio_set_drv(pin, 2);
390 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100391#elif defined(CONFIG_MACH_SUN6I)
392 /* SDC1: PG0-PG5 */
393 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
394 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
395 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
396 sunxi_gpio_set_drv(pin, 2);
397 }
398#elif defined(CONFIG_MACH_SUN8I)
Samuel Holland51951052021-09-12 10:28:35 -0500399 /* SDC1: PG0-PG5 */
400 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
401 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
402 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
403 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100404 }
405#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100406 break;
407
408 case 2:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100409#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
410 /* SDC2: PC6-PC11 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100411 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100412 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100413 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
414 sunxi_gpio_set_drv(pin, 2);
415 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100416#elif defined(CONFIG_MACH_SUN5I)
Samuel Holland51951052021-09-12 10:28:35 -0500417 /* SDC2: PC6-PC15 */
418 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
419 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
420 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
421 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100422 }
423#elif defined(CONFIG_MACH_SUN6I)
Samuel Holland51951052021-09-12 10:28:35 -0500424 /* SDC2: PC6-PC15, PC24 */
425 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
426 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
427 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
428 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100429 }
Samuel Holland51951052021-09-12 10:28:35 -0500430
431 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
432 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
433 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800434#elif defined(CONFIG_MACH_SUN8I_R40)
435 /* SDC2: PC6-PC15, PC24 */
436 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
437 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
438 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
439 sunxi_gpio_set_drv(pin, 2);
440 }
441
442 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
443 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
444 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200445#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100446 /* SDC2: PC5-PC6, PC8-PC16 */
447 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
448 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
449 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
450 sunxi_gpio_set_drv(pin, 2);
451 }
452
453 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
454 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
455 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
456 sunxi_gpio_set_drv(pin, 2);
457 }
Icenowy Zhenga838a152018-07-21 16:20:29 +0800458#elif defined(CONFIG_MACH_SUN50I_H6)
459 /* SDC2: PC4-PC14 */
460 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
461 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
462 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
463 sunxi_gpio_set_drv(pin, 2);
464 }
Andre Przywara96f55642021-04-26 00:38:04 +0100465#elif defined(CONFIG_MACH_SUN50I_H616)
466 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
467 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
468 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
469 continue;
470 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
471 continue;
472 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
473 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
474 sunxi_gpio_set_drv(pin, 3);
475 }
Philipp Tomsicha0c7c712016-10-28 18:21:33 +0800476#elif defined(CONFIG_MACH_SUN9I)
477 /* SDC2: PC6-PC16 */
478 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
479 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
480 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
481 sunxi_gpio_set_drv(pin, 2);
482 }
Andre Przywara96f55642021-04-26 00:38:04 +0100483#else
484 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100485#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100486 break;
487
488 case 3:
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800489#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
490 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100491 /* SDC3: PI4-PI9 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100492 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100493 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100494 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
495 sunxi_gpio_set_drv(pin, 2);
496 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100497#elif defined(CONFIG_MACH_SUN6I)
Samuel Holland51951052021-09-12 10:28:35 -0500498 /* SDC3: PC6-PC15, PC24 */
499 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
500 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
501 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
502 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100503 }
Samuel Holland51951052021-09-12 10:28:35 -0500504
505 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
506 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
507 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100508#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100509 break;
510
511 default:
512 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
513 break;
514 }
515}
516
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900517int board_mmc_init(struct bd_info *bis)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100518{
Hans de Goede63deaa82014-10-02 21:13:54 +0200519 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goede63deaa82014-10-02 21:13:54 +0200520
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100521 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goede63deaa82014-10-02 21:13:54 +0200522 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
523 if (!mmc0)
524 return -1;
525
Hans de Goedeaf593e42014-10-02 20:43:50 +0200526#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100527 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goede63deaa82014-10-02 21:13:54 +0200528 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
529 if (!mmc1)
530 return -1;
531#endif
532
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100533 return 0;
534}
Samuel Hollandbc42abb2021-04-18 22:16:21 -0500535
536#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
537int mmc_get_env_dev(void)
538{
539 switch (sunxi_get_boot_device()) {
540 case BOOT_DEVICE_MMC1:
541 return 0;
542 case BOOT_DEVICE_MMC2:
543 return 1;
544 default:
545 return CONFIG_SYS_MMC_ENV_DEV;
546 }
547}
548#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100549#endif
550
Ian Campbell6efe3692014-05-05 11:52:26 +0100551#ifdef CONFIG_SPL_BUILD
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800552
553static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
554{
555 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
556
557 if (spl == INVALID_SPL_HEADER)
558 return;
559
560 /* Promote the header version for U-Boot proper, if needed. */
561 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
562 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
563
564 spl->dram_size = dram_size >> 20;
565}
566
Ian Campbell6efe3692014-05-05 11:52:26 +0100567void sunxi_board_init(void)
568{
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200569 int power_failed = 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100570
Arnaud Ferraris61485e92021-09-08 21:14:19 +0200571#ifdef CONFIG_LED_STATUS
572 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
573 status_led_init();
574#endif
575
Jelle van der Waa3f3a3092016-02-23 18:47:19 +0100576#ifdef CONFIG_SY8106A_POWER
577 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
578#endif
579
vishnupatekar1895dfd2015-11-29 01:07:22 +0800580#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100581 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
582 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200583 power_failed = axp_init();
584
Chris Morgan2ff2a1d2022-01-21 13:37:32 +0000585 if (IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {
586 u8 boot_reason;
587
588 pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
589 if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
590 printf("Power on by plug-in, shutting down.\n");
591 pmic_bus_write(0x32, BIT(7));
592 }
593 }
594
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800595#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
596 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200597 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede1f247362014-06-13 22:55:51 +0200598#endif
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100599#if !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200600 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
601 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100602#endif
vishnupatekar1895dfd2015-11-29 01:07:22 +0800603#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200604 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200605#endif
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800606#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
607 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200608 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200609#endif
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200610
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800611#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
612 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200613 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
614#endif
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100615#if !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200616 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100617#endif
618#if !defined(CONFIG_AXP152_POWER) && !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200619 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
620#endif
621#ifdef CONFIG_AXP209_POWER
622 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
623#endif
624
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800625#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
626 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800627 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
628 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800629#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800630 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
631 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800632#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200633 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
634 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
635 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
636#endif
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800637
638#ifdef CONFIG_AXP818_POWER
639 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
640 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
641 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800642#endif
643
644#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai0e3efd32016-05-02 10:28:12 +0800645 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800646#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200647#endif
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000648 printf("DRAM:");
649 gd->ram_size = sunxi_dram_init();
650 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
651 if (!gd->ram_size)
652 hang();
653
654 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800655
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200656 /*
657 * Only clock up the CPU to full speed if we are reasonably
658 * assured it's being powered with suitable core voltage
659 */
660 if (!power_failed)
Tom Rini8c70baa2021-12-14 13:36:40 -0500661 clock_set_pll1(get_board_sys_clk());
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200662 else
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000663 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbell6efe3692014-05-05 11:52:26 +0100664}
665#endif
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200666
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100667#ifdef CONFIG_USB_GADGET
668int g_dnl_board_usb_cable_connected(void)
669{
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530670 struct udevice *dev;
671 struct phy phy;
672 int ret;
673
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100674 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530675 if (ret) {
676 pr_err("%s: Cannot find USB device\n", __func__);
677 return ret;
678 }
679
680 ret = generic_phy_get_by_name(dev, "usb", &phy);
681 if (ret) {
682 pr_err("failed to get %s USB PHY\n", dev->name);
683 return ret;
684 }
685
686 ret = generic_phy_init(&phy);
687 if (ret) {
Patrick Delaunay287e33c2020-07-03 17:36:41 +0200688 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530689 return ret;
690 }
691
Andre Przywarae79ee612021-11-02 19:45:47 +0000692 return sun4i_usb_phy_vbus_detect(&phy);
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100693}
694#endif
695
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100696#ifdef CONFIG_SERIAL_TAG
697void get_board_serial(struct tag_serialnr *serialnr)
698{
699 char *serial_string;
700 unsigned long long serial;
701
Simon Glass64b723f2017-08-03 12:22:12 -0600702 serial_string = env_get("serial#");
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100703
704 if (serial_string) {
705 serial = simple_strtoull(serial_string, NULL, 16);
706
707 serialnr->high = (unsigned int) (serial >> 32);
708 serialnr->low = (unsigned int) (serial & 0xffffffff);
709 } else {
710 serialnr->high = 0;
711 serialnr->low = 0;
712 }
713}
714#endif
715
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200716/*
717 * Check the SPL header for the "sunxi" variant. If found: parse values
718 * that might have been passed by the loader ("fel" utility), and update
719 * the environment accordingly.
720 */
721static void parse_spl_header(const uint32_t spl_addr)
722{
Andre Przywara14a25392018-10-25 17:23:04 +0800723 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200724
Andre Przywara14a25392018-10-25 17:23:04 +0800725 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200726 return;
Andre Przywara14a25392018-10-25 17:23:04 +0800727
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200728 if (!spl->fel_script_address)
729 return;
730
731 if (spl->fel_uEnv_length != 0) {
732 /*
733 * data is expected in uEnv.txt compatible format, so "env
734 * import -t" the string(s) at fel_script_address right away.
735 */
Andre Przywaraac4e6732016-09-05 01:32:41 +0100736 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200737 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
738 return;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200739 }
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200740 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass4d949a22017-08-03 12:22:10 -0600741 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200742}
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200743
Andre Heiderebdc3d42021-10-01 19:29:00 +0100744static bool get_unique_sid(unsigned int *sid)
745{
746 if (sunxi_get_sid(sid) != 0)
747 return false;
748
749 if (!sid[0])
750 return false;
751
752 /*
753 * The single words 1 - 3 of the SID have quite a few bits
754 * which are the same on many models, so we take a crc32
755 * of all 3 words, to get a more unique value.
756 *
757 * Note we only do this on newer SoCs as we cannot change
758 * the algorithm on older SoCs since those have been using
759 * fixed mac-addresses based on only using word 3 for a
760 * long time and changing a fixed mac-address with an
761 * u-boot update is not good.
762 */
763#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
764 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
765 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
766 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
767#endif
768
769 /* Ensure the NIC specific bytes of the mac are not all 0 */
770 if ((sid[3] & 0xffffff) == 0)
771 sid[3] |= 0x800000;
772
773 return true;
774}
775
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200776/*
777 * Note this function gets called multiple times.
778 * It must not make any changes to env variables which already exist.
779 */
780static void setup_environment(const void *fdt)
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200781{
Paul Kocialkowski92935942015-03-28 18:35:35 +0100782 char serial_string[17] = { 0 };
Hans de Goede11d70982014-11-26 00:04:24 +0100783 unsigned int sid[4];
Paul Kocialkowski92935942015-03-28 18:35:35 +0100784 uint8_t mac_addr[6];
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200785 char ethaddr[16];
Andre Heiderebdc3d42021-10-01 19:29:00 +0100786 int i;
Hans de Goedee5fe5482016-07-29 11:47:03 +0200787
Andre Heiderebdc3d42021-10-01 19:29:00 +0100788 if (!get_unique_sid(sid))
789 return;
Hans de Goedeabca8432016-07-27 17:58:06 +0200790
Andre Heiderebdc3d42021-10-01 19:29:00 +0100791 for (i = 0; i < 4; i++) {
792 sprintf(ethaddr, "ethernet%d", i);
793 if (!fdt_get_alias(fdt, ethaddr))
794 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200795
Andre Heiderebdc3d42021-10-01 19:29:00 +0100796 if (i == 0)
797 strcpy(ethaddr, "ethaddr");
798 else
799 sprintf(ethaddr, "eth%daddr", i);
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200800
Andre Heiderebdc3d42021-10-01 19:29:00 +0100801 if (env_get(ethaddr))
802 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200803
Andre Heiderebdc3d42021-10-01 19:29:00 +0100804 /* Non OUI / registered MAC address */
805 mac_addr[0] = (i << 4) | 0x02;
806 mac_addr[1] = (sid[0] >> 0) & 0xff;
807 mac_addr[2] = (sid[3] >> 24) & 0xff;
808 mac_addr[3] = (sid[3] >> 16) & 0xff;
809 mac_addr[4] = (sid[3] >> 8) & 0xff;
810 mac_addr[5] = (sid[3] >> 0) & 0xff;
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200811
Andre Heiderebdc3d42021-10-01 19:29:00 +0100812 eth_env_set_enetaddr(ethaddr, mac_addr);
813 }
Paul Kocialkowski92935942015-03-28 18:35:35 +0100814
Andre Heiderebdc3d42021-10-01 19:29:00 +0100815 if (!env_get("serial#")) {
816 snprintf(serial_string, sizeof(serial_string),
817 "%08x%08x", sid[0], sid[3]);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200818
Andre Heiderebdc3d42021-10-01 19:29:00 +0100819 env_set("serial#", serial_string);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200820 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200821}
822
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200823int misc_init_r(void)
824{
Samuel Holland87f940a2020-10-24 10:21:54 -0500825 const char *spl_dt_name;
Maxime Ripardae56d972017-08-23 10:08:29 +0200826 uint boot;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200827
Simon Glass6a38e412017-08-03 12:22:09 -0600828 env_set("fel_booted", NULL);
829 env_set("fel_scriptaddr", NULL);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200830 env_set("mmc_bootdev", NULL);
Maxime Ripardae56d972017-08-23 10:08:29 +0200831
832 boot = sunxi_get_boot_device();
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200833 /* determine if we are running in FEL mode */
Maxime Ripardae56d972017-08-23 10:08:29 +0200834 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass6a38e412017-08-03 12:22:09 -0600835 env_set("fel_booted", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200836 parse_spl_header(SPL_ADDR);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200837 /* or if we booted from MMC, and which one */
838 } else if (boot == BOOT_DEVICE_MMC1) {
839 env_set("mmc_bootdev", "0");
840 } else if (boot == BOOT_DEVICE_MMC2) {
841 env_set("mmc_bootdev", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200842 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200843
Samuel Holland87f940a2020-10-24 10:21:54 -0500844 /* Set fdtfile to match the FIT configuration chosen in SPL. */
845 spl_dt_name = get_spl_dt_name();
846 if (spl_dt_name) {
847 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
848 char str[64];
849
850 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
851 env_set("fdtfile", str);
852 }
853
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200854 setup_environment(gd->fdt_blob);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200855
Andy Shevchenko1facc0f2020-12-08 17:45:31 +0200856 return 0;
857}
858
859int board_late_init(void)
860{
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800861#ifdef CONFIG_USB_ETHER
Maxime Ripardf54aba32017-09-06 22:25:03 +0200862 usb_ether_init();
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800863#endif
Maxime Ripardf54aba32017-09-06 22:25:03 +0200864
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200865 return 0;
866}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200867
Andre Heiderbf8c8102021-10-01 19:29:00 +0100868static void bluetooth_dt_fixup(void *blob)
869{
870 /* Some devices ship with a Bluetooth controller default address.
871 * Set a valid address through the device tree.
872 */
873 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
874 unsigned int sid[4];
875 int i;
876
877 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
878 return;
879
880 if (eth_env_get_enetaddr("bdaddr", tmp)) {
881 /* Convert between the binary formats of the corresponding stacks */
882 for (i = 0; i < ETH_ALEN; ++i)
883 bdaddr[i] = tmp[ETH_ALEN - i - 1];
884 } else {
885 if (!get_unique_sid(sid))
886 return;
887
888 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
889 bdaddr[1] = (sid[3] >> 8) & 0xff;
890 bdaddr[2] = (sid[3] >> 16) & 0xff;
891 bdaddr[3] = (sid[3] >> 24) & 0xff;
892 bdaddr[4] = (sid[0] >> 0) & 0xff;
893 bdaddr[5] = 0x02;
894 }
895
896 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
897 "local-bd-address", bdaddr, ETH_ALEN, 1);
898}
899
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900900int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200901{
Hans de Goede48a234a2016-03-22 22:51:52 +0100902 int __maybe_unused r;
903
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200904 /*
Icenowy Zheng5a1456b2021-09-11 19:39:16 +0200905 * Call setup_environment and fdt_fixup_ethernet again
906 * in case the boot fdt has ethernet aliases the u-boot
907 * copy does not have.
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200908 */
909 setup_environment(blob);
Icenowy Zheng5a1456b2021-09-11 19:39:16 +0200910 fdt_fixup_ethernet(blob);
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200911
Andre Heiderbf8c8102021-10-01 19:29:00 +0100912 bluetooth_dt_fixup(blob);
913
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200914#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goede48a234a2016-03-22 22:51:52 +0100915 r = sunxi_simplefb_setup(blob);
916 if (r)
917 return r;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200918#endif
Hans de Goede48a234a2016-03-22 22:51:52 +0100919 return 0;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200920}
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100921
922#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland64933e92020-10-24 10:21:53 -0500923
924static void set_spl_dt_name(const char *name)
925{
926 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
927
928 if (spl == INVALID_SPL_HEADER)
929 return;
930
931 /* Promote the header version for U-Boot proper, if needed. */
932 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
933 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
934
935 strcpy((char *)&spl->string_pool, name);
936 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
937}
938
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100939int board_fit_config_name_match(const char *name)
940{
Samuel Hollandba44e942020-10-24 10:21:50 -0500941 const char *best_dt_name = get_spl_dt_name();
Samuel Holland64933e92020-10-24 10:21:53 -0500942 int ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100943
944#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Hollandba44e942020-10-24 10:21:50 -0500945 if (best_dt_name == NULL)
Samuel Holland37b86202020-10-24 10:21:49 -0500946 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100947#endif
948
Samuel Hollandba44e942020-10-24 10:21:50 -0500949 if (best_dt_name == NULL) {
950 /* No DT name was provided, so accept the first config. */
951 return 0;
952 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800953#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Hollandf2352dd2020-10-24 10:21:51 -0500954 if (strstr(best_dt_name, "-pine64-plus")) {
955 /* Differentiate the Pine A64 boards by their DRAM size. */
956 if ((gd->ram_size == 512 * 1024 * 1024))
957 best_dt_name = "sun50i-a64-pine64";
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100958 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800959#endif
Samuel Holland9c7cefc2020-10-24 10:21:52 -0500960#ifdef CONFIG_PINEPHONE_DT_SELECTION
961 if (strstr(best_dt_name, "-pinephone")) {
962 /* Differentiate the PinePhone revisions by GPIO inputs. */
963 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
964 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
965 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
966 udelay(100);
967
968 /* PL6 is pulled low by the modem on v1.2. */
969 if (gpio_get_value(SUNXI_GPL(6)) == 0)
970 best_dt_name = "sun50i-a64-pinephone-1.2";
971 else
972 best_dt_name = "sun50i-a64-pinephone-1.1";
973
974 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
975 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
976 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
977 }
978#endif
979
Samuel Holland64933e92020-10-24 10:21:53 -0500980 ret = strcmp(name, best_dt_name);
981
982 /*
983 * If one of the FIT configurations matches the most accurate DT name,
984 * update the SPL header to provide that DT name to U-Boot proper.
985 */
986 if (ret == 0)
987 set_spl_dt_name(best_dt_name);
988
989 return ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100990}
991#endif