blob: a09615904791b61af5307b83e95d99370f1b7dee [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>
Simon Glass401d1c42020-10-30 21:38:53 -060033#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060034#include <linux/delay.h>
Simon Glass3db71102019-11-14 12:57:16 -070035#include <u-boot/crc.h>
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020036#ifndef CONFIG_ARM64
37#include <asm/armv7.h>
38#endif
Hans de Goede4f7e01c2015-04-23 23:23:50 +020039#include <asm/gpio.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020040#include <asm/io.h>
Philipp Tomsicha740ee92018-11-25 19:22:18 +010041#include <u-boot/crc.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060042#include <env_internal.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090043#include <linux/libfdt.h>
Andre Heider9267ff82021-10-01 19:29:00 +010044#include <fdt_support.h>
Hans de Goedef62bfa52015-08-15 11:55:26 +020045#include <nand.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020046#include <net.h>
Maxime Ripardf4c35232017-08-23 10:08:29 +020047#include <spl.h>
Jelle van der Waa0d8382a2016-02-23 18:47:19 +010048#include <sy8106a.h>
Simon Glass5d982852017-05-17 08:23:00 -060049#include <asm/setup.h>
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +020050#include <status_led.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010051
52DECLARE_GLOBAL_DATA_PTR;
53
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020054void i2c_init_board(void)
55{
56#ifdef CONFIG_I2C0_ENABLE
57#if defined(CONFIG_MACH_SUN4I) || \
58 defined(CONFIG_MACH_SUN5I) || \
59 defined(CONFIG_MACH_SUN7I) || \
60 defined(CONFIG_MACH_SUN8I_R40)
61 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
62 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
63 clock_twi_onoff(0, 1);
64#elif defined(CONFIG_MACH_SUN6I)
65 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
66 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
67 clock_twi_onoff(0, 1);
Icenowy Zheng8c51c652020-10-26 22:19:34 +080068#elif defined(CONFIG_MACH_SUN8I_V3S)
69 sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
70 sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
71 clock_twi_onoff(0, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020072#elif defined(CONFIG_MACH_SUN8I)
73 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
74 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
75 clock_twi_onoff(0, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +020076#elif defined(CONFIG_MACH_SUN50I)
77 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
78 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
79 clock_twi_onoff(0, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020080#endif
81#endif
82
83#ifdef CONFIG_I2C1_ENABLE
84#if defined(CONFIG_MACH_SUN4I) || \
85 defined(CONFIG_MACH_SUN7I) || \
86 defined(CONFIG_MACH_SUN8I_R40)
87 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
88 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
89 clock_twi_onoff(1, 1);
90#elif defined(CONFIG_MACH_SUN5I)
91 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
92 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
93 clock_twi_onoff(1, 1);
94#elif defined(CONFIG_MACH_SUN6I)
95 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
96 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
97 clock_twi_onoff(1, 1);
98#elif defined(CONFIG_MACH_SUN8I)
99 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
100 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
101 clock_twi_onoff(1, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +0200102#elif defined(CONFIG_MACH_SUN50I)
103 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
104 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
105 clock_twi_onoff(1, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200106#endif
107#endif
108
109#ifdef CONFIG_I2C2_ENABLE
110#if defined(CONFIG_MACH_SUN4I) || \
111 defined(CONFIG_MACH_SUN7I) || \
112 defined(CONFIG_MACH_SUN8I_R40)
113 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
114 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
115 clock_twi_onoff(2, 1);
116#elif defined(CONFIG_MACH_SUN5I)
117 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
118 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
119 clock_twi_onoff(2, 1);
120#elif defined(CONFIG_MACH_SUN6I)
121 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
122 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
123 clock_twi_onoff(2, 1);
124#elif defined(CONFIG_MACH_SUN8I)
125 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
126 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
127 clock_twi_onoff(2, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +0200128#elif defined(CONFIG_MACH_SUN50I)
129 sunxi_gpio_set_cfgpin(SUNXI_GPE(14), SUN50I_GPE_TWI2);
130 sunxi_gpio_set_cfgpin(SUNXI_GPE(15), SUN50I_GPE_TWI2);
131 clock_twi_onoff(2, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200132#endif
133#endif
134
135#ifdef CONFIG_I2C3_ENABLE
136#if defined(CONFIG_MACH_SUN6I)
137 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
138 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
139 clock_twi_onoff(3, 1);
140#elif defined(CONFIG_MACH_SUN7I) || \
141 defined(CONFIG_MACH_SUN8I_R40)
142 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
143 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
144 clock_twi_onoff(3, 1);
145#endif
146#endif
147
148#ifdef CONFIG_I2C4_ENABLE
149#if defined(CONFIG_MACH_SUN7I) || \
150 defined(CONFIG_MACH_SUN8I_R40)
151 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
152 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
153 clock_twi_onoff(4, 1);
154#endif
155#endif
156
157#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800158#ifdef CONFIG_MACH_SUN50I
159 clock_twi_onoff(5, 1);
160 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
161 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
Jernej Skrabecd0b07c12021-01-11 21:11:42 +0100162#elif CONFIG_MACH_SUN50I_H616
163 clock_twi_onoff(5, 1);
164 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN50I_H616_GPL_R_TWI);
165 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN50I_H616_GPL_R_TWI);
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800166#else
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200167 clock_twi_onoff(5, 1);
168 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
169 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
170#endif
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800171#endif
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200172}
173
Andre Przywarae42dad42022-01-11 12:46:04 +0000174/*
175 * Try to use the environment from the boot source first.
176 * For MMC, this means a FAT partition on the boot device (SD or eMMC).
177 * If the raw MMC environment is also enabled, this is tried next.
178 * SPI flash falls back to FAT (on SD card).
179 */
Maxime Ripardb39117c2018-01-23 21:17:03 +0100180enum env_location env_get_location(enum env_operation op, int prio)
181{
Andre Przywarae42dad42022-01-11 12:46:04 +0000182 enum env_location boot_loc = ENVL_FAT;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100183
Andre Przywarae42dad42022-01-11 12:46:04 +0000184 gd->env_load_prio = prio;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100185
Andre Przywarae42dad42022-01-11 12:46:04 +0000186 switch (sunxi_get_boot_device()) {
187 case BOOT_DEVICE_MMC1:
188 case BOOT_DEVICE_MMC2:
189 boot_loc = ENVL_FAT;
190 break;
191 case BOOT_DEVICE_NAND:
192 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
193 boot_loc = ENVL_NAND;
194 break;
195 case BOOT_DEVICE_SPI:
196 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
197 boot_loc = ENVL_SPI_FLASH;
198 break;
199 case BOOT_DEVICE_BOARD:
200 break;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100201 default:
Andre Przywarae42dad42022-01-11 12:46:04 +0000202 break;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100203 }
Andre Przywarae42dad42022-01-11 12:46:04 +0000204
205 /* Always try to access the environment on the boot device first. */
206 if (prio == 0)
207 return boot_loc;
208
209 if (prio == 1) {
210 switch (boot_loc) {
211 case ENVL_SPI_FLASH:
212 return ENVL_FAT;
213 case ENVL_FAT:
214 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
215 return ENVL_MMC;
216 break;
217 default:
218 break;
219 }
220 }
221
222 return ENVL_UNKNOWN;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100223}
Maxime Ripardb39117c2018-01-23 21:17:03 +0100224
Andre Przywaraa7ae1592019-01-29 15:54:14 +0000225#ifdef CONFIG_DM_MMC
226static void mmc_pinmux_setup(int sdc);
227#endif
228
Ian Campbellcba69ee2014-05-05 11:52:26 +0100229/* add board specific code here */
230int board_init(void)
231{
Mylène Josserandf5fd7882017-04-02 12:59:10 +0200232 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100233
234 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
235
Icenowy Zheng116e1ed2022-01-29 10:23:05 -0500236#if !defined(CONFIG_ARM64) && !defined(CONFIG_MACH_SUNIV)
Ian Campbellcba69ee2014-05-05 11:52:26 +0100237 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
238 debug("id_pfr1: 0x%08x\n", id_pfr1);
239 /* Generic Timer Extension available? */
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200240 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
241 uint32_t freq;
242
Ian Campbellcba69ee2014-05-05 11:52:26 +0100243 debug("Setting CNTFRQ\n");
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200244
245 /*
246 * CNTFRQ is a secure register, so we will crash if we try to
247 * write this from the non-secure world (read is OK, though).
248 * In case some bootcode has already set the correct value,
249 * we avoid the risk of writing to it.
250 */
251 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Andre Przywarae4916e82017-02-16 01:20:19 +0000252 if (freq != COUNTER_FREQUENCY) {
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200253 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Andre Przywarae4916e82017-02-16 01:20:19 +0000254 freq, COUNTER_FREQUENCY);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200255#ifdef CONFIG_NON_SECURE
256 printf("arch timer frequency is wrong, but cannot adjust it\n");
257#else
258 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Andre Przywarae4916e82017-02-16 01:20:19 +0000259 : : "r"(COUNTER_FREQUENCY));
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200260#endif
261 }
Ian Campbellcba69ee2014-05-05 11:52:26 +0100262 }
Icenowy Zheng116e1ed2022-01-29 10:23:05 -0500263#endif /* !CONFIG_ARM64 && !CONFIG_MACH_SUNIV */
Ian Campbellcba69ee2014-05-05 11:52:26 +0100264
Hans de Goede2fcf0332015-04-25 17:25:14 +0200265 ret = axp_gpio_init();
266 if (ret)
267 return ret;
268
Andre Przywarae9ad1b82021-01-18 23:23:59 +0000269 /* strcmp() would look better, but doesn't get optimised away. */
270 if (CONFIG_SATAPWR[0]) {
271 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
272 if (satapwr_pin >= 0) {
273 gpio_request(satapwr_pin, "satapwr");
274 gpio_direction_output(satapwr_pin, 1);
275
276 /*
277 * Give the attached SATA device time to power-up
278 * to avoid link timeouts
279 */
280 mdelay(500);
281 }
282 }
283
284 if (CONFIG_MACPWR[0]) {
285 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
286 if (macpwr_pin >= 0) {
287 gpio_request(macpwr_pin, "macpwr");
288 gpio_direction_output(macpwr_pin, 1);
289 }
290 }
Hans de Goedefc8991c2016-03-17 13:53:03 +0100291
Igor Opaniuk2147a162021-02-09 13:52:45 +0200292#if CONFIG_IS_ENABLED(DM_I2C)
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +0200293 /*
294 * Temporary workaround for enabling I2C clocks until proper sunxi DM
295 * clk, reset and pinctrl drivers land.
296 */
297 i2c_init_board();
298#endif
299
Andre Przywaraa7ae1592019-01-29 15:54:14 +0000300#ifdef CONFIG_DM_MMC
301 /*
302 * Temporary workaround for enabling MMC clocks until a sunxi DM
303 * pinctrl driver lands.
304 */
305 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
306#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
307 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
308#endif
309#endif /* CONFIG_DM_MMC */
310
Samuel Holland24214972021-10-08 00:17:24 -0500311 return 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100312}
313
Andre Przywaracff5c132018-10-25 17:23:04 +0800314/*
315 * On older SoCs the SPL is actually at address zero, so using NULL as
316 * an error value does not work.
317 */
318#define INVALID_SPL_HEADER ((void *)~0UL)
319
320static struct boot_file_head * get_spl_header(uint8_t req_version)
321{
322 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
323 uint8_t spl_header_version = spl->spl_signature[3];
324
325 /* Is there really the SPL header (still) there? */
326 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
327 return INVALID_SPL_HEADER;
328
329 if (spl_header_version < req_version) {
330 printf("sunxi SPL version mismatch: expected %u, got %u\n",
331 req_version, spl_header_version);
332 return INVALID_SPL_HEADER;
333 }
334
335 return spl;
336}
337
Samuel Holland467b7e52020-10-24 10:21:50 -0500338static const char *get_spl_dt_name(void)
339{
340 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
341
342 /* Check if there is a DT name stored in the SPL header. */
343 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
344 return (char *)spl + spl->dt_name_offset;
345
346 return NULL;
347}
Samuel Holland467b7e52020-10-24 10:21:50 -0500348
Ian Campbellcba69ee2014-05-05 11:52:26 +0100349int dram_init(void)
350{
Andre Przywara57766102018-10-25 17:23:07 +0800351 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
352
353 if (spl == INVALID_SPL_HEADER)
354 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
355 PHYS_SDRAM_0_SIZE);
356 else
357 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
358
359 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
360 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100361
362 return 0;
363}
364
Boris Brezillon4ccae812016-06-15 21:09:23 +0200365#if defined(CONFIG_NAND_SUNXI)
Karol Gugalaad008292015-07-23 14:33:01 +0200366static void nand_pinmux_setup(void)
367{
368 unsigned int pin;
Hans de Goede022a99d2015-08-15 13:17:49 +0200369
370 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200371 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
372
Hans de Goede022a99d2015-08-15 13:17:49 +0200373#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
374 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200375 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200376#endif
377 /* sun4i / sun7i do have a PC23, but it is not used for nand,
378 * only sun7i has a PC24 */
379#ifdef CONFIG_MACH_SUN7I
Karol Gugalaad008292015-07-23 14:33:01 +0200380 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200381#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200382}
383
384static void nand_clock_setup(void)
385{
386 struct sunxi_ccm_reg *const ccm =
387 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede31c21472015-08-15 11:58:03 +0200388
Karol Gugalaad008292015-07-23 14:33:01 +0200389 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalba1c98b2018-02-28 20:51:53 +0100390#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
391 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
392 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
393#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200394 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
395}
Hans de Goedef62bfa52015-08-15 11:55:26 +0200396
397void board_nand_init(void)
398{
399 nand_pinmux_setup();
400 nand_clock_setup();
Boris Brezillon4ccae812016-06-15 21:09:23 +0200401#ifndef CONFIG_SPL_BUILD
402 sunxi_nand_init();
403#endif
Hans de Goedef62bfa52015-08-15 11:55:26 +0200404}
Karol Gugalaad008292015-07-23 14:33:01 +0200405#endif
406
Masahiro Yamada4aa2ba32017-05-09 20:31:39 +0900407#ifdef CONFIG_MMC
Ian Campbelle24ea552014-05-05 14:42:31 +0100408static void mmc_pinmux_setup(int sdc)
409{
410 unsigned int pin;
411
412 switch (sdc) {
413 case 0:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100414 /* SDC0: PF0-PF5 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100415 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100416 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100417 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
418 sunxi_gpio_set_drv(pin, 2);
419 }
420 break;
421
422 case 1:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800423#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
424 defined(CONFIG_MACH_SUN8I_R40)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500425 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100426 /* SDC1: PH22-PH-27 */
427 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
428 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
429 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
430 sunxi_gpio_set_drv(pin, 2);
431 }
432 } else {
433 /* SDC1: PG0-PG5 */
434 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
435 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
436 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
437 sunxi_gpio_set_drv(pin, 2);
438 }
439 }
440#elif defined(CONFIG_MACH_SUN5I)
441 /* SDC1: PG3-PG8 */
Hans de Goedebbff84b2014-10-03 16:44:57 +0200442 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100443 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbelle24ea552014-05-05 14:42:31 +0100444 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
445 sunxi_gpio_set_drv(pin, 2);
446 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100447#elif defined(CONFIG_MACH_SUN6I)
448 /* SDC1: PG0-PG5 */
449 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
450 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
451 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
452 sunxi_gpio_set_drv(pin, 2);
453 }
454#elif defined(CONFIG_MACH_SUN8I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500455 /* SDC1: PG0-PG5 */
456 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
457 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
458 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
459 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100460 }
461#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100462 break;
463
464 case 2:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100465#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
466 /* SDC2: PC6-PC11 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100467 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100468 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100469 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
470 sunxi_gpio_set_drv(pin, 2);
471 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100472#elif defined(CONFIG_MACH_SUN5I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500473 /* SDC2: PC6-PC15 */
474 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
475 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
476 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
477 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100478 }
479#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500480 /* SDC2: PC6-PC15, PC24 */
481 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
482 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
483 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
484 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100485 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500486
487 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
488 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
489 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800490#elif defined(CONFIG_MACH_SUN8I_R40)
491 /* SDC2: PC6-PC15, PC24 */
492 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
493 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
494 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
495 sunxi_gpio_set_drv(pin, 2);
496 }
497
498 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
499 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
500 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200501#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100502 /* SDC2: PC5-PC6, PC8-PC16 */
503 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
504 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100505 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
506 sunxi_gpio_set_drv(pin, 2);
507 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100508
509 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
510 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
511 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
512 sunxi_gpio_set_drv(pin, 2);
513 }
Icenowy Zheng42956f12018-07-21 16:20:29 +0800514#elif defined(CONFIG_MACH_SUN50I_H6)
515 /* SDC2: PC4-PC14 */
516 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
517 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
518 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
519 sunxi_gpio_set_drv(pin, 2);
520 }
Andre Przywara212224e2021-04-26 00:38:04 +0100521#elif defined(CONFIG_MACH_SUN50I_H616)
522 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
523 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
524 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
525 continue;
526 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
527 continue;
528 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
529 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
530 sunxi_gpio_set_drv(pin, 3);
531 }
Philipp Tomsich3ebb4562016-10-28 18:21:33 +0800532#elif defined(CONFIG_MACH_SUN9I)
533 /* SDC2: PC6-PC16 */
534 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
535 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
536 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
537 sunxi_gpio_set_drv(pin, 2);
538 }
Andre Przywara212224e2021-04-26 00:38:04 +0100539#else
540 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100541#endif
542 break;
543
544 case 3:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800545#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
546 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100547 /* SDC3: PI4-PI9 */
548 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
549 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
550 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
551 sunxi_gpio_set_drv(pin, 2);
552 }
553#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500554 /* SDC3: PC6-PC15, PC24 */
555 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
556 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
557 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
558 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100559 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500560
561 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
562 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
563 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100564#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100565 break;
566
567 default:
568 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
569 break;
570 }
571}
572
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900573int board_mmc_init(struct bd_info *bis)
Ian Campbelle24ea552014-05-05 14:42:31 +0100574{
Hans de Goedee79c7c82014-10-02 21:13:54 +0200575 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goedee79c7c82014-10-02 21:13:54 +0200576
Ian Campbelle24ea552014-05-05 14:42:31 +0100577 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200578 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
579 if (!mmc0)
580 return -1;
581
Hans de Goede2ccfac02014-10-02 20:43:50 +0200582#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbelle24ea552014-05-05 14:42:31 +0100583 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200584 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
585 if (!mmc1)
586 return -1;
587#endif
588
Ian Campbelle24ea552014-05-05 14:42:31 +0100589 return 0;
590}
Samuel Holland1011ebc2021-04-18 22:16:21 -0500591
592#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
593int mmc_get_env_dev(void)
594{
595 switch (sunxi_get_boot_device()) {
596 case BOOT_DEVICE_MMC1:
597 return 0;
598 case BOOT_DEVICE_MMC2:
599 return 1;
600 default:
601 return CONFIG_SYS_MMC_ENV_DEV;
602 }
603}
604#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100605#endif
606
Ian Campbellcba69ee2014-05-05 11:52:26 +0100607#ifdef CONFIG_SPL_BUILD
Andre Przywara57766102018-10-25 17:23:07 +0800608
609static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
610{
611 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
612
613 if (spl == INVALID_SPL_HEADER)
614 return;
615
616 /* Promote the header version for U-Boot proper, if needed. */
617 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
618 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
619
620 spl->dram_size = dram_size >> 20;
621}
622
Ian Campbellcba69ee2014-05-05 11:52:26 +0100623void sunxi_board_init(void)
624{
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200625 int power_failed = 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100626
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +0200627#ifdef CONFIG_LED_STATUS
628 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
629 status_led_init();
630#endif
631
Jelle van der Waa0d8382a2016-02-23 18:47:19 +0100632#ifdef CONFIG_SY8106A_POWER
633 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
634#endif
635
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800636#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100637 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
638 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200639 power_failed = axp_init();
640
Chris Morgan52bcc4f2022-01-21 13:37:32 +0000641 if (IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {
642 u8 boot_reason;
643
644 pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
645 if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
646 printf("Power on by plug-in, shutting down.\n");
647 pmic_bus_write(0x32, BIT(7));
648 }
649 }
650
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800651#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
652 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200653 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede24289202014-06-13 22:55:51 +0200654#endif
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100655#if !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200656 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
657 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100658#endif
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800659#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200660 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200661#endif
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800662#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
663 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200664 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200665#endif
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200666
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800667#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
668 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200669 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
670#endif
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100671#if !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200672 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100673#endif
674#if !defined(CONFIG_AXP152_POWER) && !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200675 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
676#endif
677#ifdef CONFIG_AXP209_POWER
678 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
679#endif
680
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800681#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
682 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800683 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
684 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800685#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800686 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
687 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800688#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200689 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
690 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
691 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
692#endif
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800693
694#ifdef CONFIG_AXP818_POWER
695 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
696 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
697 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800698#endif
699
700#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai15278cc2016-05-02 10:28:12 +0800701 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800702#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200703#endif
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000704 printf("DRAM:");
705 gd->ram_size = sunxi_dram_init();
706 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
707 if (!gd->ram_size)
708 hang();
709
710 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara57766102018-10-25 17:23:07 +0800711
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200712 /*
713 * Only clock up the CPU to full speed if we are reasonably
714 * assured it's being powered with suitable core voltage
715 */
716 if (!power_failed)
Tom Rini2f8a6db2021-12-14 13:36:40 -0500717 clock_set_pll1(get_board_sys_clk());
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200718 else
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000719 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbellcba69ee2014-05-05 11:52:26 +0100720}
721#endif
Jonathan Liub41d7d02014-06-14 08:59:09 +0200722
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100723#ifdef CONFIG_USB_GADGET
724int g_dnl_board_usb_cable_connected(void)
725{
Jagan Teki237050f2018-05-07 13:03:36 +0530726 struct udevice *dev;
727 struct phy phy;
728 int ret;
729
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100730 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki237050f2018-05-07 13:03:36 +0530731 if (ret) {
732 pr_err("%s: Cannot find USB device\n", __func__);
733 return ret;
734 }
735
736 ret = generic_phy_get_by_name(dev, "usb", &phy);
737 if (ret) {
738 pr_err("failed to get %s USB PHY\n", dev->name);
739 return ret;
740 }
741
742 ret = generic_phy_init(&phy);
743 if (ret) {
Patrick Delaunayf286e372020-07-03 17:36:41 +0200744 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki237050f2018-05-07 13:03:36 +0530745 return ret;
746 }
747
Andre Przywarafbd92072021-11-02 19:45:47 +0000748 return sun4i_usb_phy_vbus_detect(&phy);
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100749}
750#endif
751
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100752#ifdef CONFIG_SERIAL_TAG
753void get_board_serial(struct tag_serialnr *serialnr)
754{
755 char *serial_string;
756 unsigned long long serial;
757
Simon Glass00caae62017-08-03 12:22:12 -0600758 serial_string = env_get("serial#");
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100759
760 if (serial_string) {
761 serial = simple_strtoull(serial_string, NULL, 16);
762
763 serialnr->high = (unsigned int) (serial >> 32);
764 serialnr->low = (unsigned int) (serial & 0xffffffff);
765 } else {
766 serialnr->high = 0;
767 serialnr->low = 0;
768 }
769}
770#endif
771
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200772/*
773 * Check the SPL header for the "sunxi" variant. If found: parse values
774 * that might have been passed by the loader ("fel" utility), and update
775 * the environment accordingly.
776 */
777static void parse_spl_header(const uint32_t spl_addr)
778{
Andre Przywaracff5c132018-10-25 17:23:04 +0800779 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200780
Andre Przywaracff5c132018-10-25 17:23:04 +0800781 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200782 return;
Andre Przywaracff5c132018-10-25 17:23:04 +0800783
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200784 if (!spl->fel_script_address)
785 return;
786
787 if (spl->fel_uEnv_length != 0) {
788 /*
789 * data is expected in uEnv.txt compatible format, so "env
790 * import -t" the string(s) at fel_script_address right away.
791 */
Andre Przywara5a74a392016-09-05 01:32:41 +0100792 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200793 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
794 return;
795 }
796 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass018f5302017-08-03 12:22:10 -0600797 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200798}
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200799
Andre Heider928f4f42021-10-01 19:29:00 +0100800static bool get_unique_sid(unsigned int *sid)
801{
802 if (sunxi_get_sid(sid) != 0)
803 return false;
804
805 if (!sid[0])
806 return false;
807
808 /*
809 * The single words 1 - 3 of the SID have quite a few bits
810 * which are the same on many models, so we take a crc32
811 * of all 3 words, to get a more unique value.
812 *
813 * Note we only do this on newer SoCs as we cannot change
814 * the algorithm on older SoCs since those have been using
815 * fixed mac-addresses based on only using word 3 for a
816 * long time and changing a fixed mac-address with an
817 * u-boot update is not good.
818 */
819#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
820 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
821 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
822 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
823#endif
824
825 /* Ensure the NIC specific bytes of the mac are not all 0 */
826 if ((sid[3] & 0xffffff) == 0)
827 sid[3] |= 0x800000;
828
829 return true;
830}
831
Hans de Goedef2219612016-06-26 13:34:42 +0200832/*
833 * Note this function gets called multiple times.
834 * It must not make any changes to env variables which already exist.
835 */
836static void setup_environment(const void *fdt)
Jonathan Liub41d7d02014-06-14 08:59:09 +0200837{
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100838 char serial_string[17] = { 0 };
Hans de Goedecac5b1c2014-11-26 00:04:24 +0100839 unsigned int sid[4];
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100840 uint8_t mac_addr[6];
Hans de Goedef2219612016-06-26 13:34:42 +0200841 char ethaddr[16];
Andre Heider928f4f42021-10-01 19:29:00 +0100842 int i;
Hans de Goedef2219612016-06-26 13:34:42 +0200843
Andre Heider928f4f42021-10-01 19:29:00 +0100844 if (!get_unique_sid(sid))
845 return;
Hans de Goede3f8ea3b2016-07-29 11:47:03 +0200846
Andre Heider928f4f42021-10-01 19:29:00 +0100847 for (i = 0; i < 4; i++) {
848 sprintf(ethaddr, "ethernet%d", i);
849 if (!fdt_get_alias(fdt, ethaddr))
850 continue;
Hans de Goede97322c32016-07-27 17:58:06 +0200851
Andre Heider928f4f42021-10-01 19:29:00 +0100852 if (i == 0)
853 strcpy(ethaddr, "ethaddr");
854 else
855 sprintf(ethaddr, "eth%daddr", i);
Hans de Goedef2219612016-06-26 13:34:42 +0200856
Andre Heider928f4f42021-10-01 19:29:00 +0100857 if (env_get(ethaddr))
858 continue;
Hans de Goedef2219612016-06-26 13:34:42 +0200859
Andre Heider928f4f42021-10-01 19:29:00 +0100860 /* Non OUI / registered MAC address */
861 mac_addr[0] = (i << 4) | 0x02;
862 mac_addr[1] = (sid[0] >> 0) & 0xff;
863 mac_addr[2] = (sid[3] >> 24) & 0xff;
864 mac_addr[3] = (sid[3] >> 16) & 0xff;
865 mac_addr[4] = (sid[3] >> 8) & 0xff;
866 mac_addr[5] = (sid[3] >> 0) & 0xff;
Hans de Goedef2219612016-06-26 13:34:42 +0200867
Andre Heider928f4f42021-10-01 19:29:00 +0100868 eth_env_set_enetaddr(ethaddr, mac_addr);
869 }
Hans de Goedef2219612016-06-26 13:34:42 +0200870
Andre Heider928f4f42021-10-01 19:29:00 +0100871 if (!env_get("serial#")) {
872 snprintf(serial_string, sizeof(serial_string),
873 "%08x%08x", sid[0], sid[3]);
Hans de Goedef2219612016-06-26 13:34:42 +0200874
Andre Heider928f4f42021-10-01 19:29:00 +0100875 env_set("serial#", serial_string);
Hans de Goedef2219612016-06-26 13:34:42 +0200876 }
877}
878
Hans de Goedef2219612016-06-26 13:34:42 +0200879int misc_init_r(void)
880{
Samuel Holland20f3ee32020-10-24 10:21:54 -0500881 const char *spl_dt_name;
Maxime Ripardf4c35232017-08-23 10:08:29 +0200882 uint boot;
Jonathan Liub41d7d02014-06-14 08:59:09 +0200883
Simon Glass382bee52017-08-03 12:22:09 -0600884 env_set("fel_booted", NULL);
885 env_set("fel_scriptaddr", NULL);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200886 env_set("mmc_bootdev", NULL);
Maxime Ripardf4c35232017-08-23 10:08:29 +0200887
888 boot = sunxi_get_boot_device();
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200889 /* determine if we are running in FEL mode */
Maxime Ripardf4c35232017-08-23 10:08:29 +0200890 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass382bee52017-08-03 12:22:09 -0600891 env_set("fel_booted", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200892 parse_spl_header(SPL_ADDR);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200893 /* or if we booted from MMC, and which one */
894 } else if (boot == BOOT_DEVICE_MMC1) {
895 env_set("mmc_bootdev", "0");
896 } else if (boot == BOOT_DEVICE_MMC2) {
897 env_set("mmc_bootdev", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200898 }
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200899
Samuel Holland20f3ee32020-10-24 10:21:54 -0500900 /* Set fdtfile to match the FIT configuration chosen in SPL. */
901 spl_dt_name = get_spl_dt_name();
902 if (spl_dt_name) {
903 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
904 char str[64];
905
906 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
907 env_set("fdtfile", str);
908 }
909
Hans de Goedef2219612016-06-26 13:34:42 +0200910 setup_environment(gd->fdt_blob);
Jonathan Liub41d7d02014-06-14 08:59:09 +0200911
Andy Shevchenko92600ed2020-12-08 17:45:31 +0200912 return 0;
913}
914
915int board_late_init(void)
916{
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800917#ifdef CONFIG_USB_ETHER
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200918 usb_ether_init();
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800919#endif
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200920
Jonathan Liub41d7d02014-06-14 08:59:09 +0200921 return 0;
922}
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200923
Andre Heider9267ff82021-10-01 19:29:00 +0100924static void bluetooth_dt_fixup(void *blob)
925{
926 /* Some devices ship with a Bluetooth controller default address.
927 * Set a valid address through the device tree.
928 */
929 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
930 unsigned int sid[4];
931 int i;
932
933 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
934 return;
935
936 if (eth_env_get_enetaddr("bdaddr", tmp)) {
937 /* Convert between the binary formats of the corresponding stacks */
938 for (i = 0; i < ETH_ALEN; ++i)
939 bdaddr[i] = tmp[ETH_ALEN - i - 1];
940 } else {
941 if (!get_unique_sid(sid))
942 return;
943
944 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
945 bdaddr[1] = (sid[3] >> 8) & 0xff;
946 bdaddr[2] = (sid[3] >> 16) & 0xff;
947 bdaddr[3] = (sid[3] >> 24) & 0xff;
948 bdaddr[4] = (sid[0] >> 0) & 0xff;
949 bdaddr[5] = 0x02;
950 }
951
952 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
953 "local-bd-address", bdaddr, ETH_ALEN, 1);
954}
955
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900956int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200957{
Hans de Goeded75111a2016-03-22 22:51:52 +0100958 int __maybe_unused r;
959
Hans de Goedef2219612016-06-26 13:34:42 +0200960 /*
Icenowy Zheng2753b072021-09-11 19:39:16 +0200961 * Call setup_environment and fdt_fixup_ethernet again
962 * in case the boot fdt has ethernet aliases the u-boot
963 * copy does not have.
Hans de Goedef2219612016-06-26 13:34:42 +0200964 */
965 setup_environment(blob);
Icenowy Zheng2753b072021-09-11 19:39:16 +0200966 fdt_fixup_ethernet(blob);
Hans de Goedef2219612016-06-26 13:34:42 +0200967
Andre Heider9267ff82021-10-01 19:29:00 +0100968 bluetooth_dt_fixup(blob);
969
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200970#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goeded75111a2016-03-22 22:51:52 +0100971 r = sunxi_simplefb_setup(blob);
972 if (r)
973 return r;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200974#endif
Hans de Goeded75111a2016-03-22 22:51:52 +0100975 return 0;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200976}
Andre Przywara9ea3c352017-04-26 01:32:44 +0100977
978#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland41530cf2020-10-24 10:21:53 -0500979
980static void set_spl_dt_name(const char *name)
981{
982 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
983
984 if (spl == INVALID_SPL_HEADER)
985 return;
986
987 /* Promote the header version for U-Boot proper, if needed. */
988 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
989 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
990
991 strcpy((char *)&spl->string_pool, name);
992 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
993}
994
Andre Przywara9ea3c352017-04-26 01:32:44 +0100995int board_fit_config_name_match(const char *name)
996{
Samuel Holland467b7e52020-10-24 10:21:50 -0500997 const char *best_dt_name = get_spl_dt_name();
Samuel Holland41530cf2020-10-24 10:21:53 -0500998 int ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100999
1000#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Holland467b7e52020-10-24 10:21:50 -05001001 if (best_dt_name == NULL)
Samuel Holland2fcd7482020-10-24 10:21:49 -05001002 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara9ea3c352017-04-26 01:32:44 +01001003#endif
1004
Samuel Holland467b7e52020-10-24 10:21:50 -05001005 if (best_dt_name == NULL) {
1006 /* No DT name was provided, so accept the first config. */
1007 return 0;
1008 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +08001009#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Holland54ac5aa2020-10-24 10:21:51 -05001010 if (strstr(best_dt_name, "-pine64-plus")) {
1011 /* Differentiate the Pine A64 boards by their DRAM size. */
1012 if ((gd->ram_size == 512 * 1024 * 1024))
1013 best_dt_name = "sun50i-a64-pine64";
Andre Przywara9ea3c352017-04-26 01:32:44 +01001014 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +08001015#endif
Samuel Holland8a8b73b2020-10-24 10:21:52 -05001016#ifdef CONFIG_PINEPHONE_DT_SELECTION
1017 if (strstr(best_dt_name, "-pinephone")) {
1018 /* Differentiate the PinePhone revisions by GPIO inputs. */
1019 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
1020 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
1021 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
1022 udelay(100);
1023
1024 /* PL6 is pulled low by the modem on v1.2. */
1025 if (gpio_get_value(SUNXI_GPL(6)) == 0)
1026 best_dt_name = "sun50i-a64-pinephone-1.2";
1027 else
1028 best_dt_name = "sun50i-a64-pinephone-1.1";
1029
1030 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
1031 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
1032 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
1033 }
1034#endif
1035
Samuel Holland41530cf2020-10-24 10:21:53 -05001036 ret = strcmp(name, best_dt_name);
1037
1038 /*
1039 * If one of the FIT configurations matches the most accurate DT name,
1040 * update the SPL header to provide that DT name to U-Boot proper.
1041 */
1042 if (ret == 0)
1043 set_spl_dt_name(best_dt_name);
1044
1045 return ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +01001046}
1047#endif