blob: 1a46100e408d4f3a959a0ce175976781dec9a6eb [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>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053014#include <dm.h>
Simon Glass313112a2019-08-01 09:46:46 -060015#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070016#include <hang.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060017#include <image.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070018#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Hans de Goede63deaa82014-10-02 21:13:54 +020020#include <mmc.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020021#include <axp_pmic.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053022#include <generic-phy.h>
23#include <phy-sun4i-usb.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010024#include <asm/arch/clock.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020025#include <asm/arch/cpu.h>
Luc Verhaegen4869a8c2014-08-13 07:55:07 +020026#include <asm/arch/display.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010027#include <asm/arch/dram.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010028#include <asm/arch/gpio.h>
29#include <asm/arch/mmc.h>
Samuel Holland9c7cefc2020-10-24 10:21:52 -050030#include <asm/arch/prcm.h>
Hans de Goedea146c502016-07-09 09:56:56 +020031#include <asm/arch/spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060032#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060033#include <linux/delay.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070034#include <u-boot/crc.h>
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020035#ifndef CONFIG_ARM64
36#include <asm/armv7.h>
37#endif
Hans de Goeded9d05652015-04-23 23:23:50 +020038#include <asm/gpio.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020039#include <asm/io.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010040#include <u-boot/crc.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060041#include <env_internal.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090042#include <linux/libfdt.h>
Andre Heiderbf8c8102021-10-01 19:29:00 +010043#include <fdt_support.h>
Hans de Goede5ed52f62015-08-15 11:55:26 +020044#include <nand.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020045#include <net.h>
Maxime Ripardae56d972017-08-23 10:08:29 +020046#include <spl.h>
Jelle van der Waa3f3a3092016-02-23 18:47:19 +010047#include <sy8106a.h>
Simon Glassd9a766f2017-05-17 08:23:00 -060048#include <asm/setup.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010049
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010050#if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD)
51/* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */
52int soft_i2c_gpio_sda;
53int soft_i2c_gpio_scl;
Hans de Goeded9d05652015-04-23 23:23:50 +020054
55static int soft_i2c_board_init(void)
56{
57 int ret;
58
59 soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA);
60 if (soft_i2c_gpio_sda < 0) {
61 printf("Error invalid soft i2c sda pin: '%s', err %d\n",
62 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda);
63 return soft_i2c_gpio_sda;
64 }
65 ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda");
66 if (ret) {
67 printf("Error requesting soft i2c sda pin: '%s', err %d\n",
68 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret);
69 return ret;
70 }
71
72 soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL);
73 if (soft_i2c_gpio_scl < 0) {
74 printf("Error invalid soft i2c scl pin: '%s', err %d\n",
75 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl);
76 return soft_i2c_gpio_scl;
77 }
78 ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl");
79 if (ret) {
80 printf("Error requesting soft i2c scl pin: '%s', err %d\n",
81 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret);
82 return ret;
83 }
84
85 return 0;
86}
87#else
88static int soft_i2c_board_init(void) { return 0; }
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010089#endif
90
Ian Campbell6efe3692014-05-05 11:52:26 +010091DECLARE_GLOBAL_DATA_PTR;
92
Jernej Skrabec07da8802017-04-27 00:03:35 +020093void i2c_init_board(void)
94{
95#ifdef CONFIG_I2C0_ENABLE
96#if defined(CONFIG_MACH_SUN4I) || \
97 defined(CONFIG_MACH_SUN5I) || \
98 defined(CONFIG_MACH_SUN7I) || \
99 defined(CONFIG_MACH_SUN8I_R40)
100 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
101 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
102 clock_twi_onoff(0, 1);
103#elif defined(CONFIG_MACH_SUN6I)
104 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
105 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
106 clock_twi_onoff(0, 1);
Icenowy Zheng365951a2020-10-26 22:19:34 +0800107#elif defined(CONFIG_MACH_SUN8I_V3S)
108 sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
109 sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
110 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200111#elif defined(CONFIG_MACH_SUN8I)
112 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
113 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
114 clock_twi_onoff(0, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200115#elif defined(CONFIG_MACH_SUN50I)
116 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
117 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
118 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200119#endif
120#endif
121
122#ifdef CONFIG_I2C1_ENABLE
123#if defined(CONFIG_MACH_SUN4I) || \
124 defined(CONFIG_MACH_SUN7I) || \
125 defined(CONFIG_MACH_SUN8I_R40)
126 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
127 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
128 clock_twi_onoff(1, 1);
129#elif defined(CONFIG_MACH_SUN5I)
130 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
131 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
132 clock_twi_onoff(1, 1);
133#elif defined(CONFIG_MACH_SUN6I)
134 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
135 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
136 clock_twi_onoff(1, 1);
137#elif defined(CONFIG_MACH_SUN8I)
138 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
139 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
140 clock_twi_onoff(1, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200141#elif defined(CONFIG_MACH_SUN50I)
142 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
143 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
144 clock_twi_onoff(1, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200145#endif
146#endif
147
148#ifdef CONFIG_I2C2_ENABLE
149#if defined(CONFIG_MACH_SUN4I) || \
150 defined(CONFIG_MACH_SUN7I) || \
151 defined(CONFIG_MACH_SUN8I_R40)
152 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
153 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
154 clock_twi_onoff(2, 1);
155#elif defined(CONFIG_MACH_SUN5I)
156 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
157 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
158 clock_twi_onoff(2, 1);
159#elif defined(CONFIG_MACH_SUN6I)
160 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
161 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
162 clock_twi_onoff(2, 1);
163#elif defined(CONFIG_MACH_SUN8I)
164 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
165 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
166 clock_twi_onoff(2, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200167#elif defined(CONFIG_MACH_SUN50I)
168 sunxi_gpio_set_cfgpin(SUNXI_GPE(14), SUN50I_GPE_TWI2);
169 sunxi_gpio_set_cfgpin(SUNXI_GPE(15), SUN50I_GPE_TWI2);
170 clock_twi_onoff(2, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200171#endif
172#endif
173
174#ifdef CONFIG_I2C3_ENABLE
175#if defined(CONFIG_MACH_SUN6I)
176 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
177 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
178 clock_twi_onoff(3, 1);
179#elif defined(CONFIG_MACH_SUN7I) || \
180 defined(CONFIG_MACH_SUN8I_R40)
181 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
182 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
183 clock_twi_onoff(3, 1);
184#endif
185#endif
186
187#ifdef CONFIG_I2C4_ENABLE
188#if defined(CONFIG_MACH_SUN7I) || \
189 defined(CONFIG_MACH_SUN8I_R40)
190 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
191 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
192 clock_twi_onoff(4, 1);
193#endif
194#endif
195
196#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800197#ifdef CONFIG_MACH_SUN50I
198 clock_twi_onoff(5, 1);
199 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
200 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
Jernej Skrabec7de8eb02021-01-11 21:11:42 +0100201#elif CONFIG_MACH_SUN50I_H616
202 clock_twi_onoff(5, 1);
203 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN50I_H616_GPL_R_TWI);
204 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN50I_H616_GPL_R_TWI);
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800205#else
Jernej Skrabec07da8802017-04-27 00:03:35 +0200206 clock_twi_onoff(5, 1);
207 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
208 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
209#endif
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800210#endif
Jernej Skrabec07da8802017-04-27 00:03:35 +0200211}
212
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100213#if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT)
214enum env_location env_get_location(enum env_operation op, int prio)
215{
216 switch (prio) {
217 case 0:
218 return ENVL_FAT;
219
220 case 1:
221 return ENVL_MMC;
222
223 default:
224 return ENVL_UNKNOWN;
225 }
226}
227#endif
228
Andre Przywarad7cea362019-01-29 15:54:14 +0000229#ifdef CONFIG_DM_MMC
230static void mmc_pinmux_setup(int sdc);
231#endif
232
Ian Campbell6efe3692014-05-05 11:52:26 +0100233/* add board specific code here */
234int board_init(void)
235{
Mylène Josserand147c6062017-04-02 12:59:10 +0200236 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbell6efe3692014-05-05 11:52:26 +0100237
238 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
239
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200240#ifndef CONFIG_ARM64
Ian Campbell6efe3692014-05-05 11:52:26 +0100241 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
242 debug("id_pfr1: 0x%08x\n", id_pfr1);
243 /* Generic Timer Extension available? */
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200244 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
245 uint32_t freq;
246
Ian Campbell6efe3692014-05-05 11:52:26 +0100247 debug("Setting CNTFRQ\n");
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200248
249 /*
250 * CNTFRQ is a secure register, so we will crash if we try to
251 * write this from the non-secure world (read is OK, though).
252 * In case some bootcode has already set the correct value,
253 * we avoid the risk of writing to it.
254 */
255 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Andre Przywara70c78932017-02-16 01:20:19 +0000256 if (freq != COUNTER_FREQUENCY) {
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200257 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Andre Przywara70c78932017-02-16 01:20:19 +0000258 freq, COUNTER_FREQUENCY);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200259#ifdef CONFIG_NON_SECURE
260 printf("arch timer frequency is wrong, but cannot adjust it\n");
261#else
262 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Andre Przywara70c78932017-02-16 01:20:19 +0000263 : : "r"(COUNTER_FREQUENCY));
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200264#endif
265 }
Ian Campbell6efe3692014-05-05 11:52:26 +0100266 }
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200267#endif /* !CONFIG_ARM64 */
Ian Campbell6efe3692014-05-05 11:52:26 +0100268
Hans de Goede3ae1d132015-04-25 17:25:14 +0200269 ret = axp_gpio_init();
270 if (ret)
271 return ret;
272
Andre Przywara3b2dbb52021-01-18 23:23:59 +0000273 /* strcmp() would look better, but doesn't get optimised away. */
274 if (CONFIG_SATAPWR[0]) {
275 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
276 if (satapwr_pin >= 0) {
277 gpio_request(satapwr_pin, "satapwr");
278 gpio_direction_output(satapwr_pin, 1);
279
280 /*
281 * Give the attached SATA device time to power-up
282 * to avoid link timeouts
283 */
284 mdelay(500);
285 }
286 }
287
288 if (CONFIG_MACPWR[0]) {
289 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
290 if (macpwr_pin >= 0) {
291 gpio_request(macpwr_pin, "macpwr");
292 gpio_direction_output(macpwr_pin, 1);
293 }
294 }
Hans de Goede42cbbe32016-03-17 13:53:03 +0100295
Igor Opaniukf7c91762021-02-09 13:52:45 +0200296#if CONFIG_IS_ENABLED(DM_I2C)
Jernej Skrabec9220d502017-04-27 00:03:36 +0200297 /*
298 * Temporary workaround for enabling I2C clocks until proper sunxi DM
299 * clk, reset and pinctrl drivers land.
300 */
301 i2c_init_board();
302#endif
Andre Przywarad7cea362019-01-29 15:54:14 +0000303
304#ifdef CONFIG_DM_MMC
305 /*
306 * Temporary workaround for enabling MMC clocks until a sunxi DM
307 * pinctrl driver lands.
308 */
309 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
310#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
311 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
312#endif
313#endif /* CONFIG_DM_MMC */
Jernej Skrabec9220d502017-04-27 00:03:36 +0200314
Hans de Goeded9d05652015-04-23 23:23:50 +0200315 /* Uses dm gpio code so do this here and not in i2c_init_board() */
316 return soft_i2c_board_init();
Ian Campbell6efe3692014-05-05 11:52:26 +0100317}
318
Andre Przywara14a25392018-10-25 17:23:04 +0800319/*
320 * On older SoCs the SPL is actually at address zero, so using NULL as
321 * an error value does not work.
322 */
323#define INVALID_SPL_HEADER ((void *)~0UL)
324
325static struct boot_file_head * get_spl_header(uint8_t req_version)
326{
327 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
328 uint8_t spl_header_version = spl->spl_signature[3];
329
330 /* Is there really the SPL header (still) there? */
331 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
332 return INVALID_SPL_HEADER;
333
334 if (spl_header_version < req_version) {
335 printf("sunxi SPL version mismatch: expected %u, got %u\n",
336 req_version, spl_header_version);
337 return INVALID_SPL_HEADER;
338 }
339
340 return spl;
341}
342
Samuel Hollandba44e942020-10-24 10:21:50 -0500343static const char *get_spl_dt_name(void)
344{
345 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
346
347 /* Check if there is a DT name stored in the SPL header. */
348 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
349 return (char *)spl + spl->dt_name_offset;
350
351 return NULL;
352}
Samuel Hollandba44e942020-10-24 10:21:50 -0500353
Ian Campbell6efe3692014-05-05 11:52:26 +0100354int dram_init(void)
355{
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800356 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
357
358 if (spl == INVALID_SPL_HEADER)
359 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
360 PHYS_SDRAM_0_SIZE);
361 else
362 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
363
364 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
365 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbell6efe3692014-05-05 11:52:26 +0100366
367 return 0;
368}
369
Boris Brezillon57f20382016-06-15 21:09:23 +0200370#if defined(CONFIG_NAND_SUNXI)
Karol Gugala7bea8932015-07-23 14:33:01 +0200371static void nand_pinmux_setup(void)
372{
373 unsigned int pin;
Karol Gugala7bea8932015-07-23 14:33:01 +0200374
Hans de Goeded2236782015-08-15 13:17:49 +0200375 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugala7bea8932015-07-23 14:33:01 +0200376 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
377
Hans de Goeded2236782015-08-15 13:17:49 +0200378#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
379 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
380 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
381#endif
382 /* sun4i / sun7i do have a PC23, but it is not used for nand,
383 * only sun7i has a PC24 */
384#ifdef CONFIG_MACH_SUN7I
Karol Gugala7bea8932015-07-23 14:33:01 +0200385 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goeded2236782015-08-15 13:17:49 +0200386#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200387}
388
389static void nand_clock_setup(void)
390{
391 struct sunxi_ccm_reg *const ccm =
392 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedee5561a82015-08-15 11:58:03 +0200393
Karol Gugala7bea8932015-07-23 14:33:01 +0200394 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalebeeb802018-02-28 20:51:53 +0100395#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
396 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
397 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
398#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200399 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
400}
Hans de Goede5ed52f62015-08-15 11:55:26 +0200401
402void board_nand_init(void)
403{
404 nand_pinmux_setup();
405 nand_clock_setup();
Boris Brezillon57f20382016-06-15 21:09:23 +0200406#ifndef CONFIG_SPL_BUILD
407 sunxi_nand_init();
408#endif
Hans de Goede5ed52f62015-08-15 11:55:26 +0200409}
Karol Gugala7bea8932015-07-23 14:33:01 +0200410#endif
411
Masahiro Yamada0a780172017-05-09 20:31:39 +0900412#ifdef CONFIG_MMC
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100413static void mmc_pinmux_setup(int sdc)
414{
415 unsigned int pin;
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100416 __maybe_unused int pins;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100417
418 switch (sdc) {
419 case 0:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100420 /* SDC0: PF0-PF5 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100421 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100422 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100423 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
424 sunxi_gpio_set_drv(pin, 2);
425 }
426 break;
427
428 case 1:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100429 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS);
430
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800431#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
432 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100433 if (pins == SUNXI_GPIO_H) {
434 /* SDC1: PH22-PH-27 */
435 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
436 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
437 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
438 sunxi_gpio_set_drv(pin, 2);
439 }
440 } else {
441 /* SDC1: PG0-PG5 */
442 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
443 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
444 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
445 sunxi_gpio_set_drv(pin, 2);
446 }
447 }
448#elif defined(CONFIG_MACH_SUN5I)
449 /* SDC1: PG3-PG8 */
Hans de Goede4dccfd42014-10-03 16:44:57 +0200450 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100451 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100452 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
453 sunxi_gpio_set_drv(pin, 2);
454 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100455#elif defined(CONFIG_MACH_SUN6I)
456 /* SDC1: PG0-PG5 */
457 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
458 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
459 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
460 sunxi_gpio_set_drv(pin, 2);
461 }
462#elif defined(CONFIG_MACH_SUN8I)
463 if (pins == SUNXI_GPIO_D) {
464 /* SDC1: PD2-PD7 */
465 for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) {
466 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1);
467 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
468 sunxi_gpio_set_drv(pin, 2);
469 }
470 } else {
471 /* SDC1: PG0-PG5 */
472 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
473 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
474 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
475 sunxi_gpio_set_drv(pin, 2);
476 }
477 }
478#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100479 break;
480
481 case 2:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100482 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS);
483
484#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
485 /* SDC2: PC6-PC11 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100486 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100487 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100488 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
489 sunxi_gpio_set_drv(pin, 2);
490 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100491#elif defined(CONFIG_MACH_SUN5I)
492 if (pins == SUNXI_GPIO_E) {
493 /* SDC2: PE4-PE9 */
494 for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) {
495 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2);
496 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
497 sunxi_gpio_set_drv(pin, 2);
498 }
499 } else {
500 /* SDC2: PC6-PC15 */
501 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
502 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
503 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
504 sunxi_gpio_set_drv(pin, 2);
505 }
506 }
507#elif defined(CONFIG_MACH_SUN6I)
508 if (pins == SUNXI_GPIO_A) {
509 /* SDC2: PA9-PA14 */
510 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
511 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2);
512 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
513 sunxi_gpio_set_drv(pin, 2);
514 }
515 } else {
516 /* SDC2: PC6-PC15, PC24 */
517 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
518 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
519 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
520 sunxi_gpio_set_drv(pin, 2);
521 }
522
523 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
524 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
525 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
526 }
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800527#elif defined(CONFIG_MACH_SUN8I_R40)
528 /* SDC2: PC6-PC15, PC24 */
529 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
530 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
531 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
532 sunxi_gpio_set_drv(pin, 2);
533 }
534
535 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
536 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
537 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200538#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100539 /* SDC2: PC5-PC6, PC8-PC16 */
540 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
541 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
542 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
543 sunxi_gpio_set_drv(pin, 2);
544 }
545
546 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
547 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
548 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
549 sunxi_gpio_set_drv(pin, 2);
550 }
Icenowy Zhenga838a152018-07-21 16:20:29 +0800551#elif defined(CONFIG_MACH_SUN50I_H6)
552 /* SDC2: PC4-PC14 */
553 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
554 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
555 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
556 sunxi_gpio_set_drv(pin, 2);
557 }
Andre Przywara96f55642021-04-26 00:38:04 +0100558#elif defined(CONFIG_MACH_SUN50I_H616)
559 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
560 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
561 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
562 continue;
563 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
564 continue;
565 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
566 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
567 sunxi_gpio_set_drv(pin, 3);
568 }
Philipp Tomsicha0c7c712016-10-28 18:21:33 +0800569#elif defined(CONFIG_MACH_SUN9I)
570 /* SDC2: PC6-PC16 */
571 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
572 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
573 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
574 sunxi_gpio_set_drv(pin, 2);
575 }
Andre Przywara96f55642021-04-26 00:38:04 +0100576#else
577 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100578#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100579 break;
580
581 case 3:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100582 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS);
583
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800584#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
585 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100586 /* SDC3: PI4-PI9 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100587 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100588 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100589 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
590 sunxi_gpio_set_drv(pin, 2);
591 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100592#elif defined(CONFIG_MACH_SUN6I)
593 if (pins == SUNXI_GPIO_A) {
594 /* SDC3: PA9-PA14 */
595 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
596 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3);
597 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
598 sunxi_gpio_set_drv(pin, 2);
599 }
600 } else {
601 /* SDC3: PC6-PC15, PC24 */
602 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
603 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
604 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
605 sunxi_gpio_set_drv(pin, 2);
606 }
607
608 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
609 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
610 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
611 }
612#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100613 break;
614
615 default:
616 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
617 break;
618 }
619}
620
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900621int board_mmc_init(struct bd_info *bis)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100622{
Hans de Goede63deaa82014-10-02 21:13:54 +0200623 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goede63deaa82014-10-02 21:13:54 +0200624
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100625 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goede63deaa82014-10-02 21:13:54 +0200626 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
627 if (!mmc0)
628 return -1;
629
Hans de Goedeaf593e42014-10-02 20:43:50 +0200630#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100631 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goede63deaa82014-10-02 21:13:54 +0200632 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
633 if (!mmc1)
634 return -1;
635#endif
636
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100637 return 0;
638}
Samuel Hollandbc42abb2021-04-18 22:16:21 -0500639
640#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
641int mmc_get_env_dev(void)
642{
643 switch (sunxi_get_boot_device()) {
644 case BOOT_DEVICE_MMC1:
645 return 0;
646 case BOOT_DEVICE_MMC2:
647 return 1;
648 default:
649 return CONFIG_SYS_MMC_ENV_DEV;
650 }
651}
652#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100653#endif
654
Ian Campbell6efe3692014-05-05 11:52:26 +0100655#ifdef CONFIG_SPL_BUILD
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800656
657static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
658{
659 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
660
661 if (spl == INVALID_SPL_HEADER)
662 return;
663
664 /* Promote the header version for U-Boot proper, if needed. */
665 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
666 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
667
668 spl->dram_size = dram_size >> 20;
669}
670
Ian Campbell6efe3692014-05-05 11:52:26 +0100671void sunxi_board_init(void)
672{
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200673 int power_failed = 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100674
Jelle van der Waa3f3a3092016-02-23 18:47:19 +0100675#ifdef CONFIG_SY8106A_POWER
676 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
677#endif
678
vishnupatekar1895dfd2015-11-29 01:07:22 +0800679#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100680 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
681 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200682 power_failed = axp_init();
683
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800684#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
685 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200686 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede1f247362014-06-13 22:55:51 +0200687#endif
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100688#if !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200689 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
690 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100691#endif
vishnupatekar1895dfd2015-11-29 01:07:22 +0800692#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200693 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200694#endif
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800695#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
696 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200697 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200698#endif
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200699
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800700#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
701 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200702 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
703#endif
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100704#if !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200705 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100706#endif
707#if !defined(CONFIG_AXP152_POWER) && !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200708 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
709#endif
710#ifdef CONFIG_AXP209_POWER
711 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
712#endif
713
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800714#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
715 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800716 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
717 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800718#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800719 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
720 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800721#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200722 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
723 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
724 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
725#endif
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800726
727#ifdef CONFIG_AXP818_POWER
728 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
729 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
730 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800731#endif
732
733#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai0e3efd32016-05-02 10:28:12 +0800734 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800735#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200736#endif
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000737 printf("DRAM:");
738 gd->ram_size = sunxi_dram_init();
739 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
740 if (!gd->ram_size)
741 hang();
742
743 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800744
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200745 /*
746 * Only clock up the CPU to full speed if we are reasonably
747 * assured it's being powered with suitable core voltage
748 */
749 if (!power_failed)
Iain Paton630df142015-03-28 10:26:38 +0000750 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200751 else
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000752 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbell6efe3692014-05-05 11:52:26 +0100753}
754#endif
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200755
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100756#ifdef CONFIG_USB_GADGET
757int g_dnl_board_usb_cable_connected(void)
758{
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530759 struct udevice *dev;
760 struct phy phy;
761 int ret;
762
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100763 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530764 if (ret) {
765 pr_err("%s: Cannot find USB device\n", __func__);
766 return ret;
767 }
768
769 ret = generic_phy_get_by_name(dev, "usb", &phy);
770 if (ret) {
771 pr_err("failed to get %s USB PHY\n", dev->name);
772 return ret;
773 }
774
775 ret = generic_phy_init(&phy);
776 if (ret) {
Patrick Delaunay287e33c2020-07-03 17:36:41 +0200777 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530778 return ret;
779 }
780
781 ret = sun4i_usb_phy_vbus_detect(&phy);
782 if (ret == 1) {
783 pr_err("A charger is plugged into the OTG\n");
784 return -ENODEV;
785 }
786
787 return ret;
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100788}
789#endif
790
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100791#ifdef CONFIG_SERIAL_TAG
792void get_board_serial(struct tag_serialnr *serialnr)
793{
794 char *serial_string;
795 unsigned long long serial;
796
Simon Glass64b723f2017-08-03 12:22:12 -0600797 serial_string = env_get("serial#");
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100798
799 if (serial_string) {
800 serial = simple_strtoull(serial_string, NULL, 16);
801
802 serialnr->high = (unsigned int) (serial >> 32);
803 serialnr->low = (unsigned int) (serial & 0xffffffff);
804 } else {
805 serialnr->high = 0;
806 serialnr->low = 0;
807 }
808}
809#endif
810
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200811/*
812 * Check the SPL header for the "sunxi" variant. If found: parse values
813 * that might have been passed by the loader ("fel" utility), and update
814 * the environment accordingly.
815 */
816static void parse_spl_header(const uint32_t spl_addr)
817{
Andre Przywara14a25392018-10-25 17:23:04 +0800818 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200819
Andre Przywara14a25392018-10-25 17:23:04 +0800820 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200821 return;
Andre Przywara14a25392018-10-25 17:23:04 +0800822
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200823 if (!spl->fel_script_address)
824 return;
825
826 if (spl->fel_uEnv_length != 0) {
827 /*
828 * data is expected in uEnv.txt compatible format, so "env
829 * import -t" the string(s) at fel_script_address right away.
830 */
Andre Przywaraac4e6732016-09-05 01:32:41 +0100831 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200832 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
833 return;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200834 }
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200835 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass4d949a22017-08-03 12:22:10 -0600836 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200837}
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200838
Andre Heiderebdc3d42021-10-01 19:29:00 +0100839static bool get_unique_sid(unsigned int *sid)
840{
841 if (sunxi_get_sid(sid) != 0)
842 return false;
843
844 if (!sid[0])
845 return false;
846
847 /*
848 * The single words 1 - 3 of the SID have quite a few bits
849 * which are the same on many models, so we take a crc32
850 * of all 3 words, to get a more unique value.
851 *
852 * Note we only do this on newer SoCs as we cannot change
853 * the algorithm on older SoCs since those have been using
854 * fixed mac-addresses based on only using word 3 for a
855 * long time and changing a fixed mac-address with an
856 * u-boot update is not good.
857 */
858#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
859 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
860 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
861 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
862#endif
863
864 /* Ensure the NIC specific bytes of the mac are not all 0 */
865 if ((sid[3] & 0xffffff) == 0)
866 sid[3] |= 0x800000;
867
868 return true;
869}
870
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200871/*
872 * Note this function gets called multiple times.
873 * It must not make any changes to env variables which already exist.
874 */
875static void setup_environment(const void *fdt)
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200876{
Paul Kocialkowski92935942015-03-28 18:35:35 +0100877 char serial_string[17] = { 0 };
Hans de Goede11d70982014-11-26 00:04:24 +0100878 unsigned int sid[4];
Paul Kocialkowski92935942015-03-28 18:35:35 +0100879 uint8_t mac_addr[6];
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200880 char ethaddr[16];
Andre Heiderebdc3d42021-10-01 19:29:00 +0100881 int i;
Hans de Goedee5fe5482016-07-29 11:47:03 +0200882
Andre Heiderebdc3d42021-10-01 19:29:00 +0100883 if (!get_unique_sid(sid))
884 return;
Hans de Goedeabca8432016-07-27 17:58:06 +0200885
Andre Heiderebdc3d42021-10-01 19:29:00 +0100886 for (i = 0; i < 4; i++) {
887 sprintf(ethaddr, "ethernet%d", i);
888 if (!fdt_get_alias(fdt, ethaddr))
889 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200890
Andre Heiderebdc3d42021-10-01 19:29:00 +0100891 if (i == 0)
892 strcpy(ethaddr, "ethaddr");
893 else
894 sprintf(ethaddr, "eth%daddr", i);
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200895
Andre Heiderebdc3d42021-10-01 19:29:00 +0100896 if (env_get(ethaddr))
897 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200898
Andre Heiderebdc3d42021-10-01 19:29:00 +0100899 /* Non OUI / registered MAC address */
900 mac_addr[0] = (i << 4) | 0x02;
901 mac_addr[1] = (sid[0] >> 0) & 0xff;
902 mac_addr[2] = (sid[3] >> 24) & 0xff;
903 mac_addr[3] = (sid[3] >> 16) & 0xff;
904 mac_addr[4] = (sid[3] >> 8) & 0xff;
905 mac_addr[5] = (sid[3] >> 0) & 0xff;
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200906
Andre Heiderebdc3d42021-10-01 19:29:00 +0100907 eth_env_set_enetaddr(ethaddr, mac_addr);
908 }
Paul Kocialkowski92935942015-03-28 18:35:35 +0100909
Andre Heiderebdc3d42021-10-01 19:29:00 +0100910 if (!env_get("serial#")) {
911 snprintf(serial_string, sizeof(serial_string),
912 "%08x%08x", sid[0], sid[3]);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200913
Andre Heiderebdc3d42021-10-01 19:29:00 +0100914 env_set("serial#", serial_string);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200915 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200916}
917
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200918int misc_init_r(void)
919{
Samuel Holland87f940a2020-10-24 10:21:54 -0500920 const char *spl_dt_name;
Maxime Ripardae56d972017-08-23 10:08:29 +0200921 uint boot;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200922
Simon Glass6a38e412017-08-03 12:22:09 -0600923 env_set("fel_booted", NULL);
924 env_set("fel_scriptaddr", NULL);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200925 env_set("mmc_bootdev", NULL);
Maxime Ripardae56d972017-08-23 10:08:29 +0200926
927 boot = sunxi_get_boot_device();
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200928 /* determine if we are running in FEL mode */
Maxime Ripardae56d972017-08-23 10:08:29 +0200929 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass6a38e412017-08-03 12:22:09 -0600930 env_set("fel_booted", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200931 parse_spl_header(SPL_ADDR);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200932 /* or if we booted from MMC, and which one */
933 } else if (boot == BOOT_DEVICE_MMC1) {
934 env_set("mmc_bootdev", "0");
935 } else if (boot == BOOT_DEVICE_MMC2) {
936 env_set("mmc_bootdev", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200937 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200938
Samuel Holland87f940a2020-10-24 10:21:54 -0500939 /* Set fdtfile to match the FIT configuration chosen in SPL. */
940 spl_dt_name = get_spl_dt_name();
941 if (spl_dt_name) {
942 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
943 char str[64];
944
945 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
946 env_set("fdtfile", str);
947 }
948
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200949 setup_environment(gd->fdt_blob);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200950
Andy Shevchenko1facc0f2020-12-08 17:45:31 +0200951 return 0;
952}
953
954int board_late_init(void)
955{
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800956#ifdef CONFIG_USB_ETHER
Maxime Ripardf54aba32017-09-06 22:25:03 +0200957 usb_ether_init();
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800958#endif
Maxime Ripardf54aba32017-09-06 22:25:03 +0200959
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200960 return 0;
961}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200962
Andre Heiderbf8c8102021-10-01 19:29:00 +0100963static void bluetooth_dt_fixup(void *blob)
964{
965 /* Some devices ship with a Bluetooth controller default address.
966 * Set a valid address through the device tree.
967 */
968 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
969 unsigned int sid[4];
970 int i;
971
972 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
973 return;
974
975 if (eth_env_get_enetaddr("bdaddr", tmp)) {
976 /* Convert between the binary formats of the corresponding stacks */
977 for (i = 0; i < ETH_ALEN; ++i)
978 bdaddr[i] = tmp[ETH_ALEN - i - 1];
979 } else {
980 if (!get_unique_sid(sid))
981 return;
982
983 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
984 bdaddr[1] = (sid[3] >> 8) & 0xff;
985 bdaddr[2] = (sid[3] >> 16) & 0xff;
986 bdaddr[3] = (sid[3] >> 24) & 0xff;
987 bdaddr[4] = (sid[0] >> 0) & 0xff;
988 bdaddr[5] = 0x02;
989 }
990
991 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
992 "local-bd-address", bdaddr, ETH_ALEN, 1);
993}
994
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900995int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200996{
Hans de Goede48a234a2016-03-22 22:51:52 +0100997 int __maybe_unused r;
998
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200999 /*
1000 * Call setup_environment again in case the boot fdt has
1001 * ethernet aliases the u-boot copy does not have.
1002 */
1003 setup_environment(blob);
1004
Andre Heiderbf8c8102021-10-01 19:29:00 +01001005 bluetooth_dt_fixup(blob);
1006
Luc Verhaegen4869a8c2014-08-13 07:55:07 +02001007#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goede48a234a2016-03-22 22:51:52 +01001008 r = sunxi_simplefb_setup(blob);
1009 if (r)
1010 return r;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +02001011#endif
Hans de Goede48a234a2016-03-22 22:51:52 +01001012 return 0;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +02001013}
Andre Przywara1bd5ca32017-04-26 01:32:44 +01001014
1015#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland64933e92020-10-24 10:21:53 -05001016
1017static void set_spl_dt_name(const char *name)
1018{
1019 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
1020
1021 if (spl == INVALID_SPL_HEADER)
1022 return;
1023
1024 /* Promote the header version for U-Boot proper, if needed. */
1025 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
1026 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
1027
1028 strcpy((char *)&spl->string_pool, name);
1029 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
1030}
1031
Andre Przywara1bd5ca32017-04-26 01:32:44 +01001032int board_fit_config_name_match(const char *name)
1033{
Samuel Hollandba44e942020-10-24 10:21:50 -05001034 const char *best_dt_name = get_spl_dt_name();
Samuel Holland64933e92020-10-24 10:21:53 -05001035 int ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +01001036
1037#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Hollandba44e942020-10-24 10:21:50 -05001038 if (best_dt_name == NULL)
Samuel Holland37b86202020-10-24 10:21:49 -05001039 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara1bd5ca32017-04-26 01:32:44 +01001040#endif
1041
Samuel Hollandba44e942020-10-24 10:21:50 -05001042 if (best_dt_name == NULL) {
1043 /* No DT name was provided, so accept the first config. */
1044 return 0;
1045 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +08001046#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Hollandf2352dd2020-10-24 10:21:51 -05001047 if (strstr(best_dt_name, "-pine64-plus")) {
1048 /* Differentiate the Pine A64 boards by their DRAM size. */
1049 if ((gd->ram_size == 512 * 1024 * 1024))
1050 best_dt_name = "sun50i-a64-pine64";
Andre Przywara1bd5ca32017-04-26 01:32:44 +01001051 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +08001052#endif
Samuel Holland9c7cefc2020-10-24 10:21:52 -05001053#ifdef CONFIG_PINEPHONE_DT_SELECTION
1054 if (strstr(best_dt_name, "-pinephone")) {
1055 /* Differentiate the PinePhone revisions by GPIO inputs. */
1056 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
1057 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
1058 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
1059 udelay(100);
1060
1061 /* PL6 is pulled low by the modem on v1.2. */
1062 if (gpio_get_value(SUNXI_GPL(6)) == 0)
1063 best_dt_name = "sun50i-a64-pinephone-1.2";
1064 else
1065 best_dt_name = "sun50i-a64-pinephone-1.1";
1066
1067 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
1068 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
1069 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
1070 }
1071#endif
1072
Samuel Holland64933e92020-10-24 10:21:53 -05001073 ret = strcmp(name, best_dt_name);
1074
1075 /*
1076 * If one of the FIT configurations matches the most accurate DT name,
1077 * update the SPL header to provide that DT name to U-Boot proper.
1078 */
1079 if (ret == 0)
1080 set_spl_dt_name(best_dt_name);
1081
1082 return ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +01001083}
1084#endif