blob: de0f3505e5286ac15447f69420ba674b8ce175fd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbell6efe3692014-05-05 11:52:26 +01002/*
3 * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net>
4 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 *
6 * (C) Copyright 2007-2011
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * Tom Cubie <tangliang@allwinnertech.com>
9 *
10 * Some board init for the Allwinner A10-evb board.
Ian Campbell6efe3692014-05-05 11:52:26 +010011 */
12
13#include <common.h>
Tom Rini8c70baa2021-12-14 13:36:40 -050014#include <clock_legacy.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053015#include <dm.h>
Simon Glass313112a2019-08-01 09:46:46 -060016#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070017#include <hang.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060018#include <image.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070019#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060020#include <log.h>
Hans de Goede63deaa82014-10-02 21:13:54 +020021#include <mmc.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020022#include <axp_pmic.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053023#include <generic-phy.h>
24#include <phy-sun4i-usb.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010025#include <asm/arch/clock.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020026#include <asm/arch/cpu.h>
Luc Verhaegen4869a8c2014-08-13 07:55:07 +020027#include <asm/arch/display.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010028#include <asm/arch/dram.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010029#include <asm/arch/mmc.h>
Samuel Holland9c7cefc2020-10-24 10:21:52 -050030#include <asm/arch/prcm.h>
Chris Morgan2ff2a1d2022-01-21 13:37:32 +000031#include <asm/arch/pmic_bus.h>
Hans de Goedea146c502016-07-09 09:56:56 +020032#include <asm/arch/spl.h>
Andre Przywara1823c232022-03-15 00:00:53 +000033#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060034#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060035#include <linux/delay.h>
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020036#ifndef CONFIG_ARM64
37#include <asm/armv7.h>
38#endif
Hans de Goeded9d05652015-04-23 23:23:50 +020039#include <asm/gpio.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020040#include <asm/io.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010041#include <u-boot/crc.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060042#include <env_internal.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090043#include <linux/libfdt.h>
Andre Heiderbf8c8102021-10-01 19:29:00 +010044#include <fdt_support.h>
Hans de Goede5ed52f62015-08-15 11:55:26 +020045#include <nand.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020046#include <net.h>
Maxime Ripardae56d972017-08-23 10:08:29 +020047#include <spl.h>
Jelle van der Waa3f3a3092016-02-23 18:47:19 +010048#include <sy8106a.h>
Simon Glassd9a766f2017-05-17 08:23:00 -060049#include <asm/setup.h>
Arnaud Ferraris61485e92021-09-08 21:14:19 +020050#include <status_led.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010051
52DECLARE_GLOBAL_DATA_PTR;
53
Jernej Skrabec07da8802017-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 Zheng365951a2020-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 Skrabec07da8802017-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 Mavrodievcabe9922019-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 Skrabec07da8802017-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 Mavrodievcabe9922019-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 Skrabec07da8802017-04-27 00:03:35 +0200106#endif
107#endif
108
Jernej Skrabec07da8802017-04-27 00:03:35 +0200109#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800110#ifdef CONFIG_MACH_SUN50I
111 clock_twi_onoff(5, 1);
112 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
113 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
Jernej Skrabec7de8eb02021-01-11 21:11:42 +0100114#elif CONFIG_MACH_SUN50I_H616
115 clock_twi_onoff(5, 1);
116 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN50I_H616_GPL_R_TWI);
117 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN50I_H616_GPL_R_TWI);
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800118#else
Jernej Skrabec07da8802017-04-27 00:03:35 +0200119 clock_twi_onoff(5, 1);
120 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
121 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
122#endif
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800123#endif
Jernej Skrabec07da8802017-04-27 00:03:35 +0200124}
125
Andre Przywarab176bf32022-01-11 12:46:04 +0000126/*
127 * Try to use the environment from the boot source first.
128 * For MMC, this means a FAT partition on the boot device (SD or eMMC).
129 * If the raw MMC environment is also enabled, this is tried next.
Samuel Hollandf7135742022-04-20 23:15:39 +0100130 * When booting from NAND we try UBI first, then NAND directly.
Andre Przywarab176bf32022-01-11 12:46:04 +0000131 * SPI flash falls back to FAT (on SD card).
132 */
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100133enum env_location env_get_location(enum env_operation op, int prio)
134{
Samuel Hollandf7135742022-04-20 23:15:39 +0100135 if (prio > 1)
136 return ENVL_UNKNOWN;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100137
Samuel Hollandf7135742022-04-20 23:15:39 +0100138 /* NOWHERE is exclusive, no other option can be defined. */
139 if (IS_ENABLED(CONFIG_ENV_IS_NOWHERE))
140 return ENVL_NOWHERE;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100141
Andre Przywarab176bf32022-01-11 12:46:04 +0000142 switch (sunxi_get_boot_device()) {
143 case BOOT_DEVICE_MMC1:
144 case BOOT_DEVICE_MMC2:
Samuel Hollandf7135742022-04-20 23:15:39 +0100145 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
146 return ENVL_FAT;
147 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
148 return ENVL_MMC;
Andre Przywarab176bf32022-01-11 12:46:04 +0000149 break;
150 case BOOT_DEVICE_NAND:
Samuel Hollandf7135742022-04-20 23:15:39 +0100151 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
152 return ENVL_UBI;
Andre Przywarab176bf32022-01-11 12:46:04 +0000153 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
Samuel Hollandf7135742022-04-20 23:15:39 +0100154 return ENVL_NAND;
Andre Przywarab176bf32022-01-11 12:46:04 +0000155 break;
156 case BOOT_DEVICE_SPI:
Samuel Hollandf7135742022-04-20 23:15:39 +0100157 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
158 return ENVL_SPI_FLASH;
159 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
160 return ENVL_FAT;
Andre Przywarab176bf32022-01-11 12:46:04 +0000161 break;
162 case BOOT_DEVICE_BOARD:
163 break;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100164 default:
Andre Przywarab176bf32022-01-11 12:46:04 +0000165 break;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100166 }
Andre Przywarab176bf32022-01-11 12:46:04 +0000167
Samuel Hollandf7135742022-04-20 23:15:39 +0100168 /*
169 * If we come here for the first time, we *must* return a valid
170 * environment location other than ENVL_UNKNOWN, or the setup sequence
171 * in board_f() will silently hang. This is arguably a bug in
172 * env_init(), but for now pick one environment for which we know for
173 * sure to have a driver for. For all defconfigs this is either FAT
174 * or UBI, or NOWHERE, which is already handled above.
175 */
176 if (prio == 0) {
177 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
Andre Przywarab176bf32022-01-11 12:46:04 +0000178 return ENVL_FAT;
Samuel Hollandf7135742022-04-20 23:15:39 +0100179 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
180 return ENVL_UBI;
Andre Przywarab176bf32022-01-11 12:46:04 +0000181 }
182
183 return ENVL_UNKNOWN;
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100184}
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100185
Ian Campbell6efe3692014-05-05 11:52:26 +0100186/* add board specific code here */
187int board_init(void)
188{
Mylène Josserand147c6062017-04-02 12:59:10 +0200189 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbell6efe3692014-05-05 11:52:26 +0100190
191 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
192
Icenowy Zheng3a3b7342022-01-29 10:23:05 -0500193#if !defined(CONFIG_ARM64) && !defined(CONFIG_MACH_SUNIV)
Ian Campbell6efe3692014-05-05 11:52:26 +0100194 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
195 debug("id_pfr1: 0x%08x\n", id_pfr1);
196 /* Generic Timer Extension available? */
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200197 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
198 uint32_t freq;
199
Ian Campbell6efe3692014-05-05 11:52:26 +0100200 debug("Setting CNTFRQ\n");
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200201
202 /*
203 * CNTFRQ is a secure register, so we will crash if we try to
204 * write this from the non-secure world (read is OK, though).
205 * In case some bootcode has already set the correct value,
206 * we avoid the risk of writing to it.
207 */
208 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Peng Fane7c59392022-04-13 17:47:22 +0800209 if (freq != CONFIG_COUNTER_FREQUENCY) {
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200210 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Peng Fane7c59392022-04-13 17:47:22 +0800211 freq, CONFIG_COUNTER_FREQUENCY);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200212#ifdef CONFIG_NON_SECURE
213 printf("arch timer frequency is wrong, but cannot adjust it\n");
214#else
215 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Peng Fane7c59392022-04-13 17:47:22 +0800216 : : "r"(CONFIG_COUNTER_FREQUENCY));
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200217#endif
218 }
Ian Campbell6efe3692014-05-05 11:52:26 +0100219 }
Icenowy Zheng3a3b7342022-01-29 10:23:05 -0500220#endif /* !CONFIG_ARM64 && !CONFIG_MACH_SUNIV */
Ian Campbell6efe3692014-05-05 11:52:26 +0100221
Hans de Goede3ae1d132015-04-25 17:25:14 +0200222 ret = axp_gpio_init();
223 if (ret)
224 return ret;
225
Andre Przywara3b2dbb52021-01-18 23:23:59 +0000226 /* strcmp() would look better, but doesn't get optimised away. */
227 if (CONFIG_SATAPWR[0]) {
228 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
229 if (satapwr_pin >= 0) {
230 gpio_request(satapwr_pin, "satapwr");
231 gpio_direction_output(satapwr_pin, 1);
232
233 /*
234 * Give the attached SATA device time to power-up
235 * to avoid link timeouts
236 */
237 mdelay(500);
238 }
239 }
240
241 if (CONFIG_MACPWR[0]) {
242 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
243 if (macpwr_pin >= 0) {
244 gpio_request(macpwr_pin, "macpwr");
245 gpio_direction_output(macpwr_pin, 1);
246 }
247 }
Hans de Goede42cbbe32016-03-17 13:53:03 +0100248
Igor Opaniukf7c91762021-02-09 13:52:45 +0200249#if CONFIG_IS_ENABLED(DM_I2C)
Jernej Skrabec9220d502017-04-27 00:03:36 +0200250 /*
251 * Temporary workaround for enabling I2C clocks until proper sunxi DM
252 * clk, reset and pinctrl drivers land.
253 */
254 i2c_init_board();
255#endif
Andre Przywarad7cea362019-01-29 15:54:14 +0000256
Andre Przywara1823c232022-03-15 00:00:53 +0000257 eth_init_board();
258
Samuel Holland75fe0f42021-10-08 00:17:24 -0500259 return 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100260}
261
Andre Przywara14a25392018-10-25 17:23:04 +0800262/*
263 * On older SoCs the SPL is actually at address zero, so using NULL as
264 * an error value does not work.
265 */
266#define INVALID_SPL_HEADER ((void *)~0UL)
267
268static struct boot_file_head * get_spl_header(uint8_t req_version)
269{
270 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
271 uint8_t spl_header_version = spl->spl_signature[3];
272
273 /* Is there really the SPL header (still) there? */
274 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
275 return INVALID_SPL_HEADER;
276
277 if (spl_header_version < req_version) {
278 printf("sunxi SPL version mismatch: expected %u, got %u\n",
279 req_version, spl_header_version);
280 return INVALID_SPL_HEADER;
281 }
282
283 return spl;
284}
285
Samuel Hollandba44e942020-10-24 10:21:50 -0500286static const char *get_spl_dt_name(void)
287{
288 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
289
290 /* Check if there is a DT name stored in the SPL header. */
291 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
292 return (char *)spl + spl->dt_name_offset;
293
294 return NULL;
295}
Samuel Hollandba44e942020-10-24 10:21:50 -0500296
Ian Campbell6efe3692014-05-05 11:52:26 +0100297int dram_init(void)
298{
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800299 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
300
301 if (spl == INVALID_SPL_HEADER)
302 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
303 PHYS_SDRAM_0_SIZE);
304 else
305 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
306
307 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
308 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbell6efe3692014-05-05 11:52:26 +0100309
310 return 0;
311}
312
Samuel Holland890f7de2023-01-22 16:06:35 -0600313#if defined(CONFIG_NAND_SUNXI) && defined(CONFIG_SPL_BUILD)
Karol Gugala7bea8932015-07-23 14:33:01 +0200314static void nand_pinmux_setup(void)
315{
316 unsigned int pin;
Karol Gugala7bea8932015-07-23 14:33:01 +0200317
Hans de Goeded2236782015-08-15 13:17:49 +0200318 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugala7bea8932015-07-23 14:33:01 +0200319 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
320
Hans de Goeded2236782015-08-15 13:17:49 +0200321#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
322 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
323 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
324#endif
325 /* sun4i / sun7i do have a PC23, but it is not used for nand,
326 * only sun7i has a PC24 */
327#ifdef CONFIG_MACH_SUN7I
Karol Gugala7bea8932015-07-23 14:33:01 +0200328 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goeded2236782015-08-15 13:17:49 +0200329#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200330}
331
332static void nand_clock_setup(void)
333{
334 struct sunxi_ccm_reg *const ccm =
335 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedee5561a82015-08-15 11:58:03 +0200336
Karol Gugala7bea8932015-07-23 14:33:01 +0200337 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalebeeb802018-02-28 20:51:53 +0100338#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
339 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
340 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
341#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200342 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
343}
Hans de Goede5ed52f62015-08-15 11:55:26 +0200344
345void board_nand_init(void)
346{
347 nand_pinmux_setup();
348 nand_clock_setup();
349}
Andre Przywaraa9aab242022-11-28 00:02:56 +0000350#endif /* CONFIG_NAND_SUNXI */
Karol Gugala7bea8932015-07-23 14:33:01 +0200351
Masahiro Yamada0a780172017-05-09 20:31:39 +0900352#ifdef CONFIG_MMC
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100353static void mmc_pinmux_setup(int sdc)
354{
355 unsigned int pin;
356
357 switch (sdc) {
358 case 0:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100359 /* SDC0: PF0-PF5 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100360 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100361 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100362 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
363 sunxi_gpio_set_drv(pin, 2);
364 }
365 break;
366
367 case 1:
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800368#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
369 defined(CONFIG_MACH_SUN8I_R40)
Samuel Holland51951052021-09-12 10:28:35 -0500370 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100371 /* SDC1: PH22-PH-27 */
372 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
373 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
374 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
375 sunxi_gpio_set_drv(pin, 2);
376 }
377 } else {
378 /* SDC1: PG0-PG5 */
379 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
380 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
381 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
382 sunxi_gpio_set_drv(pin, 2);
383 }
384 }
385#elif defined(CONFIG_MACH_SUN5I)
386 /* SDC1: PG3-PG8 */
Hans de Goede4dccfd42014-10-03 16:44:57 +0200387 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100388 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100389 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
390 sunxi_gpio_set_drv(pin, 2);
391 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100392#elif defined(CONFIG_MACH_SUN6I)
393 /* SDC1: PG0-PG5 */
394 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
395 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
396 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
397 sunxi_gpio_set_drv(pin, 2);
398 }
399#elif defined(CONFIG_MACH_SUN8I)
Samuel Holland51951052021-09-12 10:28:35 -0500400 /* SDC1: PG0-PG5 */
401 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
402 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
403 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
404 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100405 }
406#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100407 break;
408
409 case 2:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100410#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
411 /* SDC2: PC6-PC11 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100412 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100413 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100414 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
415 sunxi_gpio_set_drv(pin, 2);
416 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100417#elif defined(CONFIG_MACH_SUN5I)
Samuel Holland51951052021-09-12 10:28:35 -0500418 /* SDC2: PC6-PC15 */
419 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
420 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
421 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
422 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100423 }
424#elif defined(CONFIG_MACH_SUN6I)
Samuel Holland51951052021-09-12 10:28:35 -0500425 /* SDC2: PC6-PC15, PC24 */
426 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
427 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
428 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
429 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100430 }
Samuel Holland51951052021-09-12 10:28:35 -0500431
432 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
433 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
434 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800435#elif defined(CONFIG_MACH_SUN8I_R40)
436 /* SDC2: PC6-PC15, PC24 */
437 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
438 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
439 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
440 sunxi_gpio_set_drv(pin, 2);
441 }
442
443 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
444 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
445 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200446#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100447 /* SDC2: PC5-PC6, PC8-PC16 */
448 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
449 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
450 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
451 sunxi_gpio_set_drv(pin, 2);
452 }
453
454 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
455 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
456 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
457 sunxi_gpio_set_drv(pin, 2);
458 }
Icenowy Zhenga838a152018-07-21 16:20:29 +0800459#elif defined(CONFIG_MACH_SUN50I_H6)
460 /* SDC2: PC4-PC14 */
461 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
462 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
463 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
464 sunxi_gpio_set_drv(pin, 2);
465 }
Andre Przywara96f55642021-04-26 00:38:04 +0100466#elif defined(CONFIG_MACH_SUN50I_H616)
467 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
468 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
469 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
470 continue;
471 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
472 continue;
473 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
474 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
475 sunxi_gpio_set_drv(pin, 3);
476 }
Philipp Tomsicha0c7c712016-10-28 18:21:33 +0800477#elif defined(CONFIG_MACH_SUN9I)
478 /* SDC2: PC6-PC16 */
479 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
480 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
481 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
482 sunxi_gpio_set_drv(pin, 2);
483 }
Andre Przywara96f55642021-04-26 00:38:04 +0100484#else
485 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100486#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100487 break;
488
489 case 3:
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800490#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
491 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100492 /* SDC3: PI4-PI9 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100493 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100494 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100495 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
496 sunxi_gpio_set_drv(pin, 2);
497 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100498#elif defined(CONFIG_MACH_SUN6I)
Samuel Holland51951052021-09-12 10:28:35 -0500499 /* SDC3: PC6-PC15, PC24 */
500 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
501 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
502 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
503 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100504 }
Samuel Holland51951052021-09-12 10:28:35 -0500505
506 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
507 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
508 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100509#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100510 break;
511
512 default:
513 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
514 break;
515 }
516}
517
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900518int board_mmc_init(struct bd_info *bis)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100519{
Andre Przywaraff32afe2022-11-28 00:03:53 +0000520 /*
521 * The BROM always accesses MMC port 0 (typically an SD card), and
522 * most boards seem to have such a slot. The others haven't reported
523 * any problem with unconditionally enabling this in the SPL.
524 */
Samuel Holland35663cf2022-04-10 00:13:33 -0500525 if (!IS_ENABLED(CONFIG_UART0_PORT_F)) {
Andre Przywaraff32afe2022-11-28 00:03:53 +0000526 mmc_pinmux_setup(0);
527 if (!sunxi_mmc_init(0))
Samuel Holland35663cf2022-04-10 00:13:33 -0500528 return -1;
529 }
Hans de Goede63deaa82014-10-02 21:13:54 +0200530
Samuel Holland35663cf2022-04-10 00:13:33 -0500531 if (CONFIG_MMC_SUNXI_SLOT_EXTRA != -1) {
532 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
533 if (!sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA))
534 return -1;
535 }
Hans de Goede63deaa82014-10-02 21:13:54 +0200536
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100537 return 0;
538}
Samuel Hollandbc42abb2021-04-18 22:16:21 -0500539
540#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
541int mmc_get_env_dev(void)
542{
543 switch (sunxi_get_boot_device()) {
544 case BOOT_DEVICE_MMC1:
545 return 0;
546 case BOOT_DEVICE_MMC2:
547 return 1;
548 default:
549 return CONFIG_SYS_MMC_ENV_DEV;
550 }
551}
552#endif
Andre Przywaraa9aab242022-11-28 00:02:56 +0000553#endif /* CONFIG_MMC */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100554
Ian Campbell6efe3692014-05-05 11:52:26 +0100555#ifdef CONFIG_SPL_BUILD
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800556
557static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
558{
559 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
560
561 if (spl == INVALID_SPL_HEADER)
562 return;
563
564 /* Promote the header version for U-Boot proper, if needed. */
565 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
566 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
567
568 spl->dram_size = dram_size >> 20;
569}
570
Ian Campbell6efe3692014-05-05 11:52:26 +0100571void sunxi_board_init(void)
572{
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200573 int power_failed = 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100574
Arnaud Ferraris61485e92021-09-08 21:14:19 +0200575#ifdef CONFIG_LED_STATUS
576 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
577 status_led_init();
578#endif
579
Jelle van der Waa3f3a3092016-02-23 18:47:19 +0100580#ifdef CONFIG_SY8106A_POWER
581 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
582#endif
583
vishnupatekar1895dfd2015-11-29 01:07:22 +0800584#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100585 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
586 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200587 power_failed = axp_init();
588
Chris Morgan2ff2a1d2022-01-21 13:37:32 +0000589 if (IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {
590 u8 boot_reason;
591
592 pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
593 if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
594 printf("Power on by plug-in, shutting down.\n");
595 pmic_bus_write(0x32, BIT(7));
596 }
597 }
598
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800599#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
600 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200601 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede1f247362014-06-13 22:55:51 +0200602#endif
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100603#if !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200604 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
605 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100606#endif
vishnupatekar1895dfd2015-11-29 01:07:22 +0800607#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200608 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200609#endif
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800610#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
611 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200612 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200613#endif
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200614
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800615#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
616 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200617 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
618#endif
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100619#if !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200620 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100621#endif
622#if !defined(CONFIG_AXP152_POWER) && !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200623 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
624#endif
625#ifdef CONFIG_AXP209_POWER
626 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
627#endif
628
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800629#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
630 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800631 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
632 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800633#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800634 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
635 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800636#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200637 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
638 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
639 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
640#endif
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800641
642#ifdef CONFIG_AXP818_POWER
643 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
644 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
645 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800646#endif
647
648#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai0e3efd32016-05-02 10:28:12 +0800649 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800650#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200651#endif
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000652 printf("DRAM:");
653 gd->ram_size = sunxi_dram_init();
654 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
655 if (!gd->ram_size)
656 hang();
657
658 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800659
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200660 /*
661 * Only clock up the CPU to full speed if we are reasonably
662 * assured it's being powered with suitable core voltage
663 */
664 if (!power_failed)
Tom Rini8c70baa2021-12-14 13:36:40 -0500665 clock_set_pll1(get_board_sys_clk());
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200666 else
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000667 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbell6efe3692014-05-05 11:52:26 +0100668}
Andre Przywaraa9aab242022-11-28 00:02:56 +0000669#endif /* CONFIG_SPL_BUILD */
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200670
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100671#ifdef CONFIG_USB_GADGET
672int g_dnl_board_usb_cable_connected(void)
673{
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530674 struct udevice *dev;
675 struct phy phy;
676 int ret;
677
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100678 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530679 if (ret) {
680 pr_err("%s: Cannot find USB device\n", __func__);
681 return ret;
682 }
683
684 ret = generic_phy_get_by_name(dev, "usb", &phy);
685 if (ret) {
686 pr_err("failed to get %s USB PHY\n", dev->name);
687 return ret;
688 }
689
690 ret = generic_phy_init(&phy);
691 if (ret) {
Patrick Delaunay287e33c2020-07-03 17:36:41 +0200692 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530693 return ret;
694 }
695
Andre Przywarae79ee612021-11-02 19:45:47 +0000696 return sun4i_usb_phy_vbus_detect(&phy);
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100697}
Andre Przywaraa9aab242022-11-28 00:02:56 +0000698#endif /* CONFIG_USB_GADGET */
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100699
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100700#ifdef CONFIG_SERIAL_TAG
701void get_board_serial(struct tag_serialnr *serialnr)
702{
703 char *serial_string;
704 unsigned long long serial;
705
Simon Glass64b723f2017-08-03 12:22:12 -0600706 serial_string = env_get("serial#");
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100707
708 if (serial_string) {
709 serial = simple_strtoull(serial_string, NULL, 16);
710
711 serialnr->high = (unsigned int) (serial >> 32);
712 serialnr->low = (unsigned int) (serial & 0xffffffff);
713 } else {
714 serialnr->high = 0;
715 serialnr->low = 0;
716 }
717}
718#endif
719
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200720/*
721 * Check the SPL header for the "sunxi" variant. If found: parse values
722 * that might have been passed by the loader ("fel" utility), and update
723 * the environment accordingly.
724 */
725static void parse_spl_header(const uint32_t spl_addr)
726{
Andre Przywara14a25392018-10-25 17:23:04 +0800727 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200728
Andre Przywara14a25392018-10-25 17:23:04 +0800729 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200730 return;
Andre Przywara14a25392018-10-25 17:23:04 +0800731
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200732 if (!spl->fel_script_address)
733 return;
734
735 if (spl->fel_uEnv_length != 0) {
736 /*
737 * data is expected in uEnv.txt compatible format, so "env
738 * import -t" the string(s) at fel_script_address right away.
739 */
Andre Przywaraac4e6732016-09-05 01:32:41 +0100740 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200741 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
742 return;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200743 }
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200744 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass4d949a22017-08-03 12:22:10 -0600745 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200746}
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200747
Andre Heiderebdc3d42021-10-01 19:29:00 +0100748static bool get_unique_sid(unsigned int *sid)
749{
750 if (sunxi_get_sid(sid) != 0)
751 return false;
752
753 if (!sid[0])
754 return false;
755
756 /*
757 * The single words 1 - 3 of the SID have quite a few bits
758 * which are the same on many models, so we take a crc32
759 * of all 3 words, to get a more unique value.
760 *
761 * Note we only do this on newer SoCs as we cannot change
762 * the algorithm on older SoCs since those have been using
763 * fixed mac-addresses based on only using word 3 for a
764 * long time and changing a fixed mac-address with an
765 * u-boot update is not good.
766 */
767#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
768 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
769 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
770 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
771#endif
772
773 /* Ensure the NIC specific bytes of the mac are not all 0 */
774 if ((sid[3] & 0xffffff) == 0)
775 sid[3] |= 0x800000;
776
777 return true;
778}
779
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200780/*
781 * Note this function gets called multiple times.
782 * It must not make any changes to env variables which already exist.
783 */
784static void setup_environment(const void *fdt)
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200785{
Paul Kocialkowski92935942015-03-28 18:35:35 +0100786 char serial_string[17] = { 0 };
Hans de Goede11d70982014-11-26 00:04:24 +0100787 unsigned int sid[4];
Paul Kocialkowski92935942015-03-28 18:35:35 +0100788 uint8_t mac_addr[6];
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200789 char ethaddr[16];
Andre Heiderebdc3d42021-10-01 19:29:00 +0100790 int i;
Hans de Goedee5fe5482016-07-29 11:47:03 +0200791
Andre Heiderebdc3d42021-10-01 19:29:00 +0100792 if (!get_unique_sid(sid))
793 return;
Hans de Goedeabca8432016-07-27 17:58:06 +0200794
Andre Heiderebdc3d42021-10-01 19:29:00 +0100795 for (i = 0; i < 4; i++) {
796 sprintf(ethaddr, "ethernet%d", i);
797 if (!fdt_get_alias(fdt, ethaddr))
798 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200799
Andre Heiderebdc3d42021-10-01 19:29:00 +0100800 if (i == 0)
801 strcpy(ethaddr, "ethaddr");
802 else
803 sprintf(ethaddr, "eth%daddr", i);
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200804
Andre Heiderebdc3d42021-10-01 19:29:00 +0100805 if (env_get(ethaddr))
806 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200807
Andre Heiderebdc3d42021-10-01 19:29:00 +0100808 /* Non OUI / registered MAC address */
809 mac_addr[0] = (i << 4) | 0x02;
810 mac_addr[1] = (sid[0] >> 0) & 0xff;
811 mac_addr[2] = (sid[3] >> 24) & 0xff;
812 mac_addr[3] = (sid[3] >> 16) & 0xff;
813 mac_addr[4] = (sid[3] >> 8) & 0xff;
814 mac_addr[5] = (sid[3] >> 0) & 0xff;
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200815
Andre Heiderebdc3d42021-10-01 19:29:00 +0100816 eth_env_set_enetaddr(ethaddr, mac_addr);
817 }
Paul Kocialkowski92935942015-03-28 18:35:35 +0100818
Andre Heiderebdc3d42021-10-01 19:29:00 +0100819 if (!env_get("serial#")) {
820 snprintf(serial_string, sizeof(serial_string),
821 "%08x%08x", sid[0], sid[3]);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200822
Andre Heiderebdc3d42021-10-01 19:29:00 +0100823 env_set("serial#", serial_string);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200824 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200825}
826
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200827int misc_init_r(void)
828{
Samuel Holland87f940a2020-10-24 10:21:54 -0500829 const char *spl_dt_name;
Maxime Ripardae56d972017-08-23 10:08:29 +0200830 uint boot;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200831
Simon Glass6a38e412017-08-03 12:22:09 -0600832 env_set("fel_booted", NULL);
833 env_set("fel_scriptaddr", NULL);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200834 env_set("mmc_bootdev", NULL);
Maxime Ripardae56d972017-08-23 10:08:29 +0200835
836 boot = sunxi_get_boot_device();
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200837 /* determine if we are running in FEL mode */
Maxime Ripardae56d972017-08-23 10:08:29 +0200838 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass6a38e412017-08-03 12:22:09 -0600839 env_set("fel_booted", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200840 parse_spl_header(SPL_ADDR);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200841 /* or if we booted from MMC, and which one */
842 } else if (boot == BOOT_DEVICE_MMC1) {
843 env_set("mmc_bootdev", "0");
844 } else if (boot == BOOT_DEVICE_MMC2) {
845 env_set("mmc_bootdev", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200846 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200847
Samuel Holland87f940a2020-10-24 10:21:54 -0500848 /* Set fdtfile to match the FIT configuration chosen in SPL. */
849 spl_dt_name = get_spl_dt_name();
850 if (spl_dt_name) {
851 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
852 char str[64];
853
854 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
855 env_set("fdtfile", str);
856 }
857
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200858 setup_environment(gd->fdt_blob);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200859
Andy Shevchenko1facc0f2020-12-08 17:45:31 +0200860 return 0;
861}
862
863int board_late_init(void)
864{
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800865#ifdef CONFIG_USB_ETHER
Maxime Ripardf54aba32017-09-06 22:25:03 +0200866 usb_ether_init();
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800867#endif
Maxime Ripardf54aba32017-09-06 22:25:03 +0200868
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200869 return 0;
870}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200871
Andre Heiderbf8c8102021-10-01 19:29:00 +0100872static void bluetooth_dt_fixup(void *blob)
873{
874 /* Some devices ship with a Bluetooth controller default address.
875 * Set a valid address through the device tree.
876 */
877 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
878 unsigned int sid[4];
879 int i;
880
881 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
882 return;
883
884 if (eth_env_get_enetaddr("bdaddr", tmp)) {
885 /* Convert between the binary formats of the corresponding stacks */
886 for (i = 0; i < ETH_ALEN; ++i)
887 bdaddr[i] = tmp[ETH_ALEN - i - 1];
888 } else {
889 if (!get_unique_sid(sid))
890 return;
891
892 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
893 bdaddr[1] = (sid[3] >> 8) & 0xff;
894 bdaddr[2] = (sid[3] >> 16) & 0xff;
895 bdaddr[3] = (sid[3] >> 24) & 0xff;
896 bdaddr[4] = (sid[0] >> 0) & 0xff;
897 bdaddr[5] = 0x02;
898 }
899
900 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
901 "local-bd-address", bdaddr, ETH_ALEN, 1);
902}
903
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900904int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200905{
Hans de Goede48a234a2016-03-22 22:51:52 +0100906 int __maybe_unused r;
907
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200908 /*
Icenowy Zheng5a1456b2021-09-11 19:39:16 +0200909 * Call setup_environment and fdt_fixup_ethernet again
910 * in case the boot fdt has ethernet aliases the u-boot
911 * copy does not have.
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200912 */
913 setup_environment(blob);
Icenowy Zheng5a1456b2021-09-11 19:39:16 +0200914 fdt_fixup_ethernet(blob);
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200915
Andre Heiderbf8c8102021-10-01 19:29:00 +0100916 bluetooth_dt_fixup(blob);
917
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200918#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goede48a234a2016-03-22 22:51:52 +0100919 r = sunxi_simplefb_setup(blob);
920 if (r)
921 return r;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200922#endif
Hans de Goede48a234a2016-03-22 22:51:52 +0100923 return 0;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200924}
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100925
926#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland64933e92020-10-24 10:21:53 -0500927static void set_spl_dt_name(const char *name)
928{
929 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
930
931 if (spl == INVALID_SPL_HEADER)
932 return;
933
934 /* Promote the header version for U-Boot proper, if needed. */
935 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
936 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
937
938 strcpy((char *)&spl->string_pool, name);
939 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
940}
941
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100942int board_fit_config_name_match(const char *name)
943{
Samuel Hollandba44e942020-10-24 10:21:50 -0500944 const char *best_dt_name = get_spl_dt_name();
Samuel Holland64933e92020-10-24 10:21:53 -0500945 int ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100946
947#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Hollandba44e942020-10-24 10:21:50 -0500948 if (best_dt_name == NULL)
Samuel Holland37b86202020-10-24 10:21:49 -0500949 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100950#endif
951
Samuel Hollandba44e942020-10-24 10:21:50 -0500952 if (best_dt_name == NULL) {
953 /* No DT name was provided, so accept the first config. */
954 return 0;
955 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800956#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Hollandf2352dd2020-10-24 10:21:51 -0500957 if (strstr(best_dt_name, "-pine64-plus")) {
958 /* Differentiate the Pine A64 boards by their DRAM size. */
959 if ((gd->ram_size == 512 * 1024 * 1024))
960 best_dt_name = "sun50i-a64-pine64";
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100961 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800962#endif
Samuel Holland9c7cefc2020-10-24 10:21:52 -0500963#ifdef CONFIG_PINEPHONE_DT_SELECTION
964 if (strstr(best_dt_name, "-pinephone")) {
965 /* Differentiate the PinePhone revisions by GPIO inputs. */
966 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
967 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
968 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
969 udelay(100);
970
971 /* PL6 is pulled low by the modem on v1.2. */
972 if (gpio_get_value(SUNXI_GPL(6)) == 0)
973 best_dt_name = "sun50i-a64-pinephone-1.2";
974 else
975 best_dt_name = "sun50i-a64-pinephone-1.1";
976
977 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
978 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
979 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
980 }
981#endif
982
Samuel Holland64933e92020-10-24 10:21:53 -0500983 ret = strcmp(name, best_dt_name);
984
985 /*
986 * If one of the FIT configurations matches the most accurate DT name,
987 * update the SPL header to provide that DT name to U-Boot proper.
988 */
989 if (ret == 0)
990 set_spl_dt_name(best_dt_name);
991
992 return ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100993}
Andre Przywaraa9aab242022-11-28 00:02:56 +0000994#endif /* CONFIG_SPL_LOAD_FIT */