blob: 827e545032ebb82b5a0f5266dfd722d73095d286 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbellcba69ee2014-05-05 11:52:26 +01002/*
3 * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net>
4 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 *
6 * (C) Copyright 2007-2011
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * Tom Cubie <tangliang@allwinnertech.com>
9 *
10 * Some board init for the Allwinner A10-evb board.
Ian Campbellcba69ee2014-05-05 11:52:26 +010011 */
12
13#include <common.h>
Tom Rini2f8a6db2021-12-14 13:36:40 -050014#include <clock_legacy.h>
Jagan Teki237050f2018-05-07 13:03:36 +053015#include <dm.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060016#include <env.h>
Simon Glassdb41d652019-12-28 10:45:07 -070017#include <hang.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060018#include <image.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070019#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060020#include <log.h>
Hans de Goedee79c7c82014-10-02 21:13:54 +020021#include <mmc.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020022#include <axp_pmic.h>
Jagan Teki237050f2018-05-07 13:03:36 +053023#include <generic-phy.h>
24#include <phy-sun4i-usb.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010025#include <asm/arch/clock.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020026#include <asm/arch/cpu.h>
Luc Verhaegen2d7a0842014-08-13 07:55:07 +020027#include <asm/arch/display.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010028#include <asm/arch/dram.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010029#include <asm/arch/mmc.h>
Samuel Holland8a8b73b2020-10-24 10:21:52 -050030#include <asm/arch/prcm.h>
Chris Morgan52bcc4f2022-01-21 13:37:32 +000031#include <asm/arch/pmic_bus.h>
Hans de Goede4a8c7c12016-07-09 09:56:56 +020032#include <asm/arch/spl.h>
Andre Przywarae9437532022-03-15 00:00:53 +000033#include <asm/arch/sys_proto.h>
Simon Glass401d1c42020-10-30 21:38:53 -060034#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060035#include <linux/delay.h>
Simon Glass3db71102019-11-14 12:57:16 -070036#include <u-boot/crc.h>
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020037#ifndef CONFIG_ARM64
38#include <asm/armv7.h>
39#endif
Hans de Goede4f7e01c2015-04-23 23:23:50 +020040#include <asm/gpio.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020041#include <asm/io.h>
Philipp Tomsicha740ee92018-11-25 19:22:18 +010042#include <u-boot/crc.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060043#include <env_internal.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090044#include <linux/libfdt.h>
Andre Heider9267ff82021-10-01 19:29:00 +010045#include <fdt_support.h>
Hans de Goedef62bfa52015-08-15 11:55:26 +020046#include <nand.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020047#include <net.h>
Maxime Ripardf4c35232017-08-23 10:08:29 +020048#include <spl.h>
Jelle van der Waa0d8382a2016-02-23 18:47:19 +010049#include <sy8106a.h>
Simon Glass5d982852017-05-17 08:23:00 -060050#include <asm/setup.h>
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +020051#include <status_led.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010052
53DECLARE_GLOBAL_DATA_PTR;
54
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020055void i2c_init_board(void)
56{
57#ifdef CONFIG_I2C0_ENABLE
58#if defined(CONFIG_MACH_SUN4I) || \
59 defined(CONFIG_MACH_SUN5I) || \
60 defined(CONFIG_MACH_SUN7I) || \
61 defined(CONFIG_MACH_SUN8I_R40)
62 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
63 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
64 clock_twi_onoff(0, 1);
65#elif defined(CONFIG_MACH_SUN6I)
66 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
67 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
68 clock_twi_onoff(0, 1);
Icenowy Zheng8c51c652020-10-26 22:19:34 +080069#elif defined(CONFIG_MACH_SUN8I_V3S)
70 sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
71 sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
72 clock_twi_onoff(0, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020073#elif defined(CONFIG_MACH_SUN8I)
74 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
75 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
76 clock_twi_onoff(0, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +020077#elif defined(CONFIG_MACH_SUN50I)
78 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
79 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
80 clock_twi_onoff(0, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020081#endif
82#endif
83
84#ifdef CONFIG_I2C1_ENABLE
85#if defined(CONFIG_MACH_SUN4I) || \
86 defined(CONFIG_MACH_SUN7I) || \
87 defined(CONFIG_MACH_SUN8I_R40)
88 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
89 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
90 clock_twi_onoff(1, 1);
91#elif defined(CONFIG_MACH_SUN5I)
92 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
93 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
94 clock_twi_onoff(1, 1);
95#elif defined(CONFIG_MACH_SUN6I)
96 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
97 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
98 clock_twi_onoff(1, 1);
99#elif defined(CONFIG_MACH_SUN8I)
100 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
101 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
102 clock_twi_onoff(1, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +0200103#elif defined(CONFIG_MACH_SUN50I)
104 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
105 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
106 clock_twi_onoff(1, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200107#endif
108#endif
109
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200110#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800111#ifdef CONFIG_MACH_SUN50I
112 clock_twi_onoff(5, 1);
113 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
114 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
Jernej Skrabecd0b07c12021-01-11 21:11:42 +0100115#elif CONFIG_MACH_SUN50I_H616
116 clock_twi_onoff(5, 1);
117 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN50I_H616_GPL_R_TWI);
118 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN50I_H616_GPL_R_TWI);
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800119#else
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200120 clock_twi_onoff(5, 1);
121 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
122 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
123#endif
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800124#endif
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200125}
126
Andre Przywarae42dad42022-01-11 12:46:04 +0000127/*
128 * Try to use the environment from the boot source first.
129 * For MMC, this means a FAT partition on the boot device (SD or eMMC).
130 * If the raw MMC environment is also enabled, this is tried next.
Samuel Hollande008e512022-04-20 23:15:39 +0100131 * When booting from NAND we try UBI first, then NAND directly.
Andre Przywarae42dad42022-01-11 12:46:04 +0000132 * SPI flash falls back to FAT (on SD card).
133 */
Maxime Ripardb39117c2018-01-23 21:17:03 +0100134enum env_location env_get_location(enum env_operation op, int prio)
135{
Samuel Hollande008e512022-04-20 23:15:39 +0100136 if (prio > 1)
137 return ENVL_UNKNOWN;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100138
Samuel Hollande008e512022-04-20 23:15:39 +0100139 /* NOWHERE is exclusive, no other option can be defined. */
140 if (IS_ENABLED(CONFIG_ENV_IS_NOWHERE))
141 return ENVL_NOWHERE;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100142
Andre Przywarae42dad42022-01-11 12:46:04 +0000143 switch (sunxi_get_boot_device()) {
144 case BOOT_DEVICE_MMC1:
145 case BOOT_DEVICE_MMC2:
Samuel Hollande008e512022-04-20 23:15:39 +0100146 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
147 return ENVL_FAT;
148 if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
149 return ENVL_MMC;
Andre Przywarae42dad42022-01-11 12:46:04 +0000150 break;
151 case BOOT_DEVICE_NAND:
Samuel Hollande008e512022-04-20 23:15:39 +0100152 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
153 return ENVL_UBI;
Andre Przywarae42dad42022-01-11 12:46:04 +0000154 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
Samuel Hollande008e512022-04-20 23:15:39 +0100155 return ENVL_NAND;
Andre Przywarae42dad42022-01-11 12:46:04 +0000156 break;
157 case BOOT_DEVICE_SPI:
Samuel Hollande008e512022-04-20 23:15:39 +0100158 if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
159 return ENVL_SPI_FLASH;
160 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
161 return ENVL_FAT;
Andre Przywarae42dad42022-01-11 12:46:04 +0000162 break;
163 case BOOT_DEVICE_BOARD:
164 break;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100165 default:
Andre Przywarae42dad42022-01-11 12:46:04 +0000166 break;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100167 }
Andre Przywarae42dad42022-01-11 12:46:04 +0000168
Samuel Hollande008e512022-04-20 23:15:39 +0100169 /*
170 * If we come here for the first time, we *must* return a valid
171 * environment location other than ENVL_UNKNOWN, or the setup sequence
172 * in board_f() will silently hang. This is arguably a bug in
173 * env_init(), but for now pick one environment for which we know for
174 * sure to have a driver for. For all defconfigs this is either FAT
175 * or UBI, or NOWHERE, which is already handled above.
176 */
177 if (prio == 0) {
178 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
Andre Przywarae42dad42022-01-11 12:46:04 +0000179 return ENVL_FAT;
Samuel Hollande008e512022-04-20 23:15:39 +0100180 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
181 return ENVL_UBI;
Andre Przywarae42dad42022-01-11 12:46:04 +0000182 }
183
184 return ENVL_UNKNOWN;
Maxime Ripardb39117c2018-01-23 21:17:03 +0100185}
Maxime Ripardb39117c2018-01-23 21:17:03 +0100186
Ian Campbellcba69ee2014-05-05 11:52:26 +0100187/* add board specific code here */
188int board_init(void)
189{
Mylène Josserandf5fd7882017-04-02 12:59:10 +0200190 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100191
192 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
193
Icenowy Zheng116e1ed2022-01-29 10:23:05 -0500194#if !defined(CONFIG_ARM64) && !defined(CONFIG_MACH_SUNIV)
Ian Campbellcba69ee2014-05-05 11:52:26 +0100195 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
196 debug("id_pfr1: 0x%08x\n", id_pfr1);
197 /* Generic Timer Extension available? */
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200198 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
199 uint32_t freq;
200
Ian Campbellcba69ee2014-05-05 11:52:26 +0100201 debug("Setting CNTFRQ\n");
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200202
203 /*
204 * CNTFRQ is a secure register, so we will crash if we try to
205 * write this from the non-secure world (read is OK, though).
206 * In case some bootcode has already set the correct value,
207 * we avoid the risk of writing to it.
208 */
209 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Peng Fan151a0302022-04-13 17:47:22 +0800210 if (freq != CONFIG_COUNTER_FREQUENCY) {
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200211 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Peng Fan151a0302022-04-13 17:47:22 +0800212 freq, CONFIG_COUNTER_FREQUENCY);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200213#ifdef CONFIG_NON_SECURE
214 printf("arch timer frequency is wrong, but cannot adjust it\n");
215#else
216 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Peng Fan151a0302022-04-13 17:47:22 +0800217 : : "r"(CONFIG_COUNTER_FREQUENCY));
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200218#endif
219 }
Ian Campbellcba69ee2014-05-05 11:52:26 +0100220 }
Icenowy Zheng116e1ed2022-01-29 10:23:05 -0500221#endif /* !CONFIG_ARM64 && !CONFIG_MACH_SUNIV */
Ian Campbellcba69ee2014-05-05 11:52:26 +0100222
Hans de Goede2fcf0332015-04-25 17:25:14 +0200223 ret = axp_gpio_init();
224 if (ret)
225 return ret;
226
Andre Przywarae9ad1b82021-01-18 23:23:59 +0000227 /* strcmp() would look better, but doesn't get optimised away. */
228 if (CONFIG_SATAPWR[0]) {
229 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
230 if (satapwr_pin >= 0) {
231 gpio_request(satapwr_pin, "satapwr");
232 gpio_direction_output(satapwr_pin, 1);
233
234 /*
235 * Give the attached SATA device time to power-up
236 * to avoid link timeouts
237 */
238 mdelay(500);
239 }
240 }
241
242 if (CONFIG_MACPWR[0]) {
243 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
244 if (macpwr_pin >= 0) {
245 gpio_request(macpwr_pin, "macpwr");
246 gpio_direction_output(macpwr_pin, 1);
247 }
248 }
Hans de Goedefc8991c2016-03-17 13:53:03 +0100249
Igor Opaniuk2147a162021-02-09 13:52:45 +0200250#if CONFIG_IS_ENABLED(DM_I2C)
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +0200251 /*
252 * Temporary workaround for enabling I2C clocks until proper sunxi DM
253 * clk, reset and pinctrl drivers land.
254 */
255 i2c_init_board();
256#endif
257
Andre Przywarae9437532022-03-15 00:00:53 +0000258 eth_init_board();
259
Samuel Holland24214972021-10-08 00:17:24 -0500260 return 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100261}
262
Andre Przywaracff5c132018-10-25 17:23:04 +0800263/*
264 * On older SoCs the SPL is actually at address zero, so using NULL as
265 * an error value does not work.
266 */
267#define INVALID_SPL_HEADER ((void *)~0UL)
268
269static struct boot_file_head * get_spl_header(uint8_t req_version)
270{
271 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
272 uint8_t spl_header_version = spl->spl_signature[3];
273
274 /* Is there really the SPL header (still) there? */
275 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
276 return INVALID_SPL_HEADER;
277
278 if (spl_header_version < req_version) {
279 printf("sunxi SPL version mismatch: expected %u, got %u\n",
280 req_version, spl_header_version);
281 return INVALID_SPL_HEADER;
282 }
283
284 return spl;
285}
286
Samuel Holland467b7e52020-10-24 10:21:50 -0500287static const char *get_spl_dt_name(void)
288{
289 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
290
291 /* Check if there is a DT name stored in the SPL header. */
292 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
293 return (char *)spl + spl->dt_name_offset;
294
295 return NULL;
296}
Samuel Holland467b7e52020-10-24 10:21:50 -0500297
Ian Campbellcba69ee2014-05-05 11:52:26 +0100298int dram_init(void)
299{
Andre Przywara57766102018-10-25 17:23:07 +0800300 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
301
302 if (spl == INVALID_SPL_HEADER)
303 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
304 PHYS_SDRAM_0_SIZE);
305 else
306 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
307
308 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
309 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100310
311 return 0;
312}
313
Boris Brezillon4ccae812016-06-15 21:09:23 +0200314#if defined(CONFIG_NAND_SUNXI)
Karol Gugalaad008292015-07-23 14:33:01 +0200315static void nand_pinmux_setup(void)
316{
317 unsigned int pin;
Hans de Goede022a99d2015-08-15 13:17:49 +0200318
319 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200320 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
321
Hans de Goede022a99d2015-08-15 13:17:49 +0200322#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
323 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200324 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200325#endif
326 /* sun4i / sun7i do have a PC23, but it is not used for nand,
327 * only sun7i has a PC24 */
328#ifdef CONFIG_MACH_SUN7I
Karol Gugalaad008292015-07-23 14:33:01 +0200329 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200330#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200331}
332
333static void nand_clock_setup(void)
334{
335 struct sunxi_ccm_reg *const ccm =
336 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede31c21472015-08-15 11:58:03 +0200337
Karol Gugalaad008292015-07-23 14:33:01 +0200338 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalba1c98b2018-02-28 20:51:53 +0100339#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
340 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
341 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
342#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200343 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
344}
Hans de Goedef62bfa52015-08-15 11:55:26 +0200345
346void board_nand_init(void)
347{
348 nand_pinmux_setup();
349 nand_clock_setup();
Boris Brezillon4ccae812016-06-15 21:09:23 +0200350#ifndef CONFIG_SPL_BUILD
351 sunxi_nand_init();
352#endif
Hans de Goedef62bfa52015-08-15 11:55:26 +0200353}
Andre Przywara64531492022-11-28 00:02:56 +0000354#endif /* CONFIG_NAND_SUNXI */
Karol Gugalaad008292015-07-23 14:33:01 +0200355
Masahiro Yamada4aa2ba32017-05-09 20:31:39 +0900356#ifdef CONFIG_MMC
Ian Campbelle24ea552014-05-05 14:42:31 +0100357static void mmc_pinmux_setup(int sdc)
358{
359 unsigned int pin;
360
361 switch (sdc) {
362 case 0:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100363 /* SDC0: PF0-PF5 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100364 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100365 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100366 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
367 sunxi_gpio_set_drv(pin, 2);
368 }
369 break;
370
371 case 1:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800372#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
373 defined(CONFIG_MACH_SUN8I_R40)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500374 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100375 /* SDC1: PH22-PH-27 */
376 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
377 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
378 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
379 sunxi_gpio_set_drv(pin, 2);
380 }
381 } else {
382 /* SDC1: PG0-PG5 */
383 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
384 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
385 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
386 sunxi_gpio_set_drv(pin, 2);
387 }
388 }
389#elif defined(CONFIG_MACH_SUN5I)
390 /* SDC1: PG3-PG8 */
Hans de Goedebbff84b2014-10-03 16:44:57 +0200391 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100392 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbelle24ea552014-05-05 14:42:31 +0100393 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
394 sunxi_gpio_set_drv(pin, 2);
395 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100396#elif defined(CONFIG_MACH_SUN6I)
397 /* SDC1: PG0-PG5 */
398 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
399 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
400 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
401 sunxi_gpio_set_drv(pin, 2);
402 }
403#elif defined(CONFIG_MACH_SUN8I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500404 /* SDC1: PG0-PG5 */
405 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
406 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
407 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
408 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100409 }
410#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100411 break;
412
413 case 2:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100414#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
415 /* SDC2: PC6-PC11 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100416 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100417 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100418 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
419 sunxi_gpio_set_drv(pin, 2);
420 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100421#elif defined(CONFIG_MACH_SUN5I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500422 /* SDC2: PC6-PC15 */
423 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
424 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
425 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
426 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100427 }
428#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500429 /* SDC2: PC6-PC15, PC24 */
430 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
431 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
432 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
433 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100434 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500435
436 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
437 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
438 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800439#elif defined(CONFIG_MACH_SUN8I_R40)
440 /* SDC2: PC6-PC15, PC24 */
441 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
442 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
443 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
444 sunxi_gpio_set_drv(pin, 2);
445 }
446
447 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
448 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
449 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200450#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100451 /* SDC2: PC5-PC6, PC8-PC16 */
452 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
453 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100454 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
455 sunxi_gpio_set_drv(pin, 2);
456 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100457
458 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
459 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
460 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
461 sunxi_gpio_set_drv(pin, 2);
462 }
Icenowy Zheng42956f12018-07-21 16:20:29 +0800463#elif defined(CONFIG_MACH_SUN50I_H6)
464 /* SDC2: PC4-PC14 */
465 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
466 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
467 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
468 sunxi_gpio_set_drv(pin, 2);
469 }
Andre Przywara212224e2021-04-26 00:38:04 +0100470#elif defined(CONFIG_MACH_SUN50I_H616)
471 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
472 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
473 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
474 continue;
475 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
476 continue;
477 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
478 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
479 sunxi_gpio_set_drv(pin, 3);
480 }
Philipp Tomsich3ebb4562016-10-28 18:21:33 +0800481#elif defined(CONFIG_MACH_SUN9I)
482 /* SDC2: PC6-PC16 */
483 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
484 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
485 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
486 sunxi_gpio_set_drv(pin, 2);
487 }
Andre Przywara212224e2021-04-26 00:38:04 +0100488#else
489 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100490#endif
491 break;
492
493 case 3:
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800494#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
495 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100496 /* SDC3: PI4-PI9 */
497 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
498 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
499 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
500 sunxi_gpio_set_drv(pin, 2);
501 }
502#elif defined(CONFIG_MACH_SUN6I)
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500503 /* SDC3: PC6-PC15, PC24 */
504 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
505 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
506 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
507 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100508 }
Samuel Hollanddda9fa72021-09-12 10:28:35 -0500509
510 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
511 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
512 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100513#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100514 break;
515
516 default:
517 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
518 break;
519 }
520}
521
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900522int board_mmc_init(struct bd_info *bis)
Ian Campbelle24ea552014-05-05 14:42:31 +0100523{
Andre Przywaraed825862022-11-28 00:03:53 +0000524 /*
525 * The BROM always accesses MMC port 0 (typically an SD card), and
526 * most boards seem to have such a slot. The others haven't reported
527 * any problem with unconditionally enabling this in the SPL.
528 */
Samuel Holland3ba0a252022-04-10 00:13:33 -0500529 if (!IS_ENABLED(CONFIG_UART0_PORT_F)) {
Andre Przywaraed825862022-11-28 00:03:53 +0000530 mmc_pinmux_setup(0);
531 if (!sunxi_mmc_init(0))
Samuel Holland3ba0a252022-04-10 00:13:33 -0500532 return -1;
533 }
Hans de Goedee79c7c82014-10-02 21:13:54 +0200534
Samuel Holland3ba0a252022-04-10 00:13:33 -0500535 if (CONFIG_MMC_SUNXI_SLOT_EXTRA != -1) {
536 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
537 if (!sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA))
538 return -1;
539 }
Hans de Goedee79c7c82014-10-02 21:13:54 +0200540
Ian Campbelle24ea552014-05-05 14:42:31 +0100541 return 0;
542}
Samuel Holland1011ebc2021-04-18 22:16:21 -0500543
544#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
545int mmc_get_env_dev(void)
546{
547 switch (sunxi_get_boot_device()) {
548 case BOOT_DEVICE_MMC1:
549 return 0;
550 case BOOT_DEVICE_MMC2:
551 return 1;
552 default:
553 return CONFIG_SYS_MMC_ENV_DEV;
554 }
555}
556#endif
Andre Przywara64531492022-11-28 00:02:56 +0000557#endif /* CONFIG_MMC */
Ian Campbelle24ea552014-05-05 14:42:31 +0100558
Ian Campbellcba69ee2014-05-05 11:52:26 +0100559#ifdef CONFIG_SPL_BUILD
Andre Przywara57766102018-10-25 17:23:07 +0800560
561static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
562{
563 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
564
565 if (spl == INVALID_SPL_HEADER)
566 return;
567
568 /* Promote the header version for U-Boot proper, if needed. */
569 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
570 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
571
572 spl->dram_size = dram_size >> 20;
573}
574
Ian Campbellcba69ee2014-05-05 11:52:26 +0100575void sunxi_board_init(void)
576{
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200577 int power_failed = 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100578
Arnaud Ferraris8f872bb2021-09-08 21:14:19 +0200579#ifdef CONFIG_LED_STATUS
580 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
581 status_led_init();
582#endif
583
Jelle van der Waa0d8382a2016-02-23 18:47:19 +0100584#ifdef CONFIG_SY8106A_POWER
585 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
586#endif
587
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800588#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100589 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
590 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200591 power_failed = axp_init();
592
Chris Morgan52bcc4f2022-01-21 13:37:32 +0000593 if (IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {
594 u8 boot_reason;
595
596 pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
597 if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
598 printf("Power on by plug-in, shutting down.\n");
599 pmic_bus_write(0x32, BIT(7));
600 }
601 }
602
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800603#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
604 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200605 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede24289202014-06-13 22:55:51 +0200606#endif
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100607#if !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200608 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
609 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100610#endif
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800611#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200612 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200613#endif
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800614#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
615 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200616 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200617#endif
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200618
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800619#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
620 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200621 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
622#endif
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100623#if !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200624 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfbd37d82021-01-11 21:11:33 +0100625#endif
626#if !defined(CONFIG_AXP152_POWER) && !defined(CONFIG_AXP305_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200627 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
628#endif
629#ifdef CONFIG_AXP209_POWER
630 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
631#endif
632
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800633#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
634 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800635 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
636 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800637#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800638 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
639 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800640#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200641 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
642 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
643 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
644#endif
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800645
646#ifdef CONFIG_AXP818_POWER
647 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
648 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
649 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800650#endif
651
652#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai15278cc2016-05-02 10:28:12 +0800653 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800654#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200655#endif
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000656 printf("DRAM:");
657 gd->ram_size = sunxi_dram_init();
658 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
659 if (!gd->ram_size)
660 hang();
661
662 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara57766102018-10-25 17:23:07 +0800663
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200664 /*
665 * Only clock up the CPU to full speed if we are reasonably
666 * assured it's being powered with suitable core voltage
667 */
668 if (!power_failed)
Tom Rini2f8a6db2021-12-14 13:36:40 -0500669 clock_set_pll1(get_board_sys_clk());
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200670 else
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000671 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbellcba69ee2014-05-05 11:52:26 +0100672}
Andre Przywara64531492022-11-28 00:02:56 +0000673#endif /* CONFIG_SPL_BUILD */
Jonathan Liub41d7d02014-06-14 08:59:09 +0200674
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100675#ifdef CONFIG_USB_GADGET
676int g_dnl_board_usb_cable_connected(void)
677{
Jagan Teki237050f2018-05-07 13:03:36 +0530678 struct udevice *dev;
679 struct phy phy;
680 int ret;
681
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100682 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki237050f2018-05-07 13:03:36 +0530683 if (ret) {
684 pr_err("%s: Cannot find USB device\n", __func__);
685 return ret;
686 }
687
688 ret = generic_phy_get_by_name(dev, "usb", &phy);
689 if (ret) {
690 pr_err("failed to get %s USB PHY\n", dev->name);
691 return ret;
692 }
693
694 ret = generic_phy_init(&phy);
695 if (ret) {
Patrick Delaunayf286e372020-07-03 17:36:41 +0200696 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki237050f2018-05-07 13:03:36 +0530697 return ret;
698 }
699
Andre Przywarafbd92072021-11-02 19:45:47 +0000700 return sun4i_usb_phy_vbus_detect(&phy);
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100701}
Andre Przywara64531492022-11-28 00:02:56 +0000702#endif /* CONFIG_USB_GADGET */
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100703
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100704#ifdef CONFIG_SERIAL_TAG
705void get_board_serial(struct tag_serialnr *serialnr)
706{
707 char *serial_string;
708 unsigned long long serial;
709
Simon Glass00caae62017-08-03 12:22:12 -0600710 serial_string = env_get("serial#");
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100711
712 if (serial_string) {
713 serial = simple_strtoull(serial_string, NULL, 16);
714
715 serialnr->high = (unsigned int) (serial >> 32);
716 serialnr->low = (unsigned int) (serial & 0xffffffff);
717 } else {
718 serialnr->high = 0;
719 serialnr->low = 0;
720 }
721}
722#endif
723
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200724/*
725 * Check the SPL header for the "sunxi" variant. If found: parse values
726 * that might have been passed by the loader ("fel" utility), and update
727 * the environment accordingly.
728 */
729static void parse_spl_header(const uint32_t spl_addr)
730{
Andre Przywaracff5c132018-10-25 17:23:04 +0800731 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200732
Andre Przywaracff5c132018-10-25 17:23:04 +0800733 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200734 return;
Andre Przywaracff5c132018-10-25 17:23:04 +0800735
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200736 if (!spl->fel_script_address)
737 return;
738
739 if (spl->fel_uEnv_length != 0) {
740 /*
741 * data is expected in uEnv.txt compatible format, so "env
742 * import -t" the string(s) at fel_script_address right away.
743 */
Andre Przywara5a74a392016-09-05 01:32:41 +0100744 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200745 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
746 return;
747 }
748 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass018f5302017-08-03 12:22:10 -0600749 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200750}
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200751
Andre Heider928f4f42021-10-01 19:29:00 +0100752static bool get_unique_sid(unsigned int *sid)
753{
754 if (sunxi_get_sid(sid) != 0)
755 return false;
756
757 if (!sid[0])
758 return false;
759
760 /*
761 * The single words 1 - 3 of the SID have quite a few bits
762 * which are the same on many models, so we take a crc32
763 * of all 3 words, to get a more unique value.
764 *
765 * Note we only do this on newer SoCs as we cannot change
766 * the algorithm on older SoCs since those have been using
767 * fixed mac-addresses based on only using word 3 for a
768 * long time and changing a fixed mac-address with an
769 * u-boot update is not good.
770 */
771#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
772 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
773 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
774 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
775#endif
776
777 /* Ensure the NIC specific bytes of the mac are not all 0 */
778 if ((sid[3] & 0xffffff) == 0)
779 sid[3] |= 0x800000;
780
781 return true;
782}
783
Hans de Goedef2219612016-06-26 13:34:42 +0200784/*
785 * Note this function gets called multiple times.
786 * It must not make any changes to env variables which already exist.
787 */
788static void setup_environment(const void *fdt)
Jonathan Liub41d7d02014-06-14 08:59:09 +0200789{
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100790 char serial_string[17] = { 0 };
Hans de Goedecac5b1c2014-11-26 00:04:24 +0100791 unsigned int sid[4];
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100792 uint8_t mac_addr[6];
Hans de Goedef2219612016-06-26 13:34:42 +0200793 char ethaddr[16];
Andre Heider928f4f42021-10-01 19:29:00 +0100794 int i;
Hans de Goedef2219612016-06-26 13:34:42 +0200795
Andre Heider928f4f42021-10-01 19:29:00 +0100796 if (!get_unique_sid(sid))
797 return;
Hans de Goede3f8ea3b2016-07-29 11:47:03 +0200798
Andre Heider928f4f42021-10-01 19:29:00 +0100799 for (i = 0; i < 4; i++) {
800 sprintf(ethaddr, "ethernet%d", i);
801 if (!fdt_get_alias(fdt, ethaddr))
802 continue;
Hans de Goede97322c32016-07-27 17:58:06 +0200803
Andre Heider928f4f42021-10-01 19:29:00 +0100804 if (i == 0)
805 strcpy(ethaddr, "ethaddr");
806 else
807 sprintf(ethaddr, "eth%daddr", i);
Hans de Goedef2219612016-06-26 13:34:42 +0200808
Andre Heider928f4f42021-10-01 19:29:00 +0100809 if (env_get(ethaddr))
810 continue;
Hans de Goedef2219612016-06-26 13:34:42 +0200811
Andre Heider928f4f42021-10-01 19:29:00 +0100812 /* Non OUI / registered MAC address */
813 mac_addr[0] = (i << 4) | 0x02;
814 mac_addr[1] = (sid[0] >> 0) & 0xff;
815 mac_addr[2] = (sid[3] >> 24) & 0xff;
816 mac_addr[3] = (sid[3] >> 16) & 0xff;
817 mac_addr[4] = (sid[3] >> 8) & 0xff;
818 mac_addr[5] = (sid[3] >> 0) & 0xff;
Hans de Goedef2219612016-06-26 13:34:42 +0200819
Andre Heider928f4f42021-10-01 19:29:00 +0100820 eth_env_set_enetaddr(ethaddr, mac_addr);
821 }
Hans de Goedef2219612016-06-26 13:34:42 +0200822
Andre Heider928f4f42021-10-01 19:29:00 +0100823 if (!env_get("serial#")) {
824 snprintf(serial_string, sizeof(serial_string),
825 "%08x%08x", sid[0], sid[3]);
Hans de Goedef2219612016-06-26 13:34:42 +0200826
Andre Heider928f4f42021-10-01 19:29:00 +0100827 env_set("serial#", serial_string);
Hans de Goedef2219612016-06-26 13:34:42 +0200828 }
829}
830
Hans de Goedef2219612016-06-26 13:34:42 +0200831int misc_init_r(void)
832{
Samuel Holland20f3ee32020-10-24 10:21:54 -0500833 const char *spl_dt_name;
Maxime Ripardf4c35232017-08-23 10:08:29 +0200834 uint boot;
Jonathan Liub41d7d02014-06-14 08:59:09 +0200835
Simon Glass382bee52017-08-03 12:22:09 -0600836 env_set("fel_booted", NULL);
837 env_set("fel_scriptaddr", NULL);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200838 env_set("mmc_bootdev", NULL);
Maxime Ripardf4c35232017-08-23 10:08:29 +0200839
840 boot = sunxi_get_boot_device();
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200841 /* determine if we are running in FEL mode */
Maxime Ripardf4c35232017-08-23 10:08:29 +0200842 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass382bee52017-08-03 12:22:09 -0600843 env_set("fel_booted", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200844 parse_spl_header(SPL_ADDR);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200845 /* or if we booted from MMC, and which one */
846 } else if (boot == BOOT_DEVICE_MMC1) {
847 env_set("mmc_bootdev", "0");
848 } else if (boot == BOOT_DEVICE_MMC2) {
849 env_set("mmc_bootdev", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200850 }
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200851
Samuel Holland20f3ee32020-10-24 10:21:54 -0500852 /* Set fdtfile to match the FIT configuration chosen in SPL. */
853 spl_dt_name = get_spl_dt_name();
854 if (spl_dt_name) {
855 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
856 char str[64];
857
858 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
859 env_set("fdtfile", str);
860 }
861
Hans de Goedef2219612016-06-26 13:34:42 +0200862 setup_environment(gd->fdt_blob);
Jonathan Liub41d7d02014-06-14 08:59:09 +0200863
Andy Shevchenko92600ed2020-12-08 17:45:31 +0200864 return 0;
865}
866
867int board_late_init(void)
868{
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800869#ifdef CONFIG_USB_ETHER
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200870 usb_ether_init();
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800871#endif
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200872
Jonathan Liub41d7d02014-06-14 08:59:09 +0200873 return 0;
874}
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200875
Andre Heider9267ff82021-10-01 19:29:00 +0100876static void bluetooth_dt_fixup(void *blob)
877{
878 /* Some devices ship with a Bluetooth controller default address.
879 * Set a valid address through the device tree.
880 */
881 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
882 unsigned int sid[4];
883 int i;
884
885 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
886 return;
887
888 if (eth_env_get_enetaddr("bdaddr", tmp)) {
889 /* Convert between the binary formats of the corresponding stacks */
890 for (i = 0; i < ETH_ALEN; ++i)
891 bdaddr[i] = tmp[ETH_ALEN - i - 1];
892 } else {
893 if (!get_unique_sid(sid))
894 return;
895
896 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
897 bdaddr[1] = (sid[3] >> 8) & 0xff;
898 bdaddr[2] = (sid[3] >> 16) & 0xff;
899 bdaddr[3] = (sid[3] >> 24) & 0xff;
900 bdaddr[4] = (sid[0] >> 0) & 0xff;
901 bdaddr[5] = 0x02;
902 }
903
904 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
905 "local-bd-address", bdaddr, ETH_ALEN, 1);
906}
907
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900908int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200909{
Hans de Goeded75111a2016-03-22 22:51:52 +0100910 int __maybe_unused r;
911
Hans de Goedef2219612016-06-26 13:34:42 +0200912 /*
Icenowy Zheng2753b072021-09-11 19:39:16 +0200913 * Call setup_environment and fdt_fixup_ethernet again
914 * in case the boot fdt has ethernet aliases the u-boot
915 * copy does not have.
Hans de Goedef2219612016-06-26 13:34:42 +0200916 */
917 setup_environment(blob);
Icenowy Zheng2753b072021-09-11 19:39:16 +0200918 fdt_fixup_ethernet(blob);
Hans de Goedef2219612016-06-26 13:34:42 +0200919
Andre Heider9267ff82021-10-01 19:29:00 +0100920 bluetooth_dt_fixup(blob);
921
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200922#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goeded75111a2016-03-22 22:51:52 +0100923 r = sunxi_simplefb_setup(blob);
924 if (r)
925 return r;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200926#endif
Hans de Goeded75111a2016-03-22 22:51:52 +0100927 return 0;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200928}
Andre Przywara9ea3c352017-04-26 01:32:44 +0100929
930#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland41530cf2020-10-24 10:21:53 -0500931static void set_spl_dt_name(const char *name)
932{
933 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
934
935 if (spl == INVALID_SPL_HEADER)
936 return;
937
938 /* Promote the header version for U-Boot proper, if needed. */
939 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
940 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
941
942 strcpy((char *)&spl->string_pool, name);
943 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
944}
945
Andre Przywara9ea3c352017-04-26 01:32:44 +0100946int board_fit_config_name_match(const char *name)
947{
Samuel Holland467b7e52020-10-24 10:21:50 -0500948 const char *best_dt_name = get_spl_dt_name();
Samuel Holland41530cf2020-10-24 10:21:53 -0500949 int ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100950
951#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Holland467b7e52020-10-24 10:21:50 -0500952 if (best_dt_name == NULL)
Samuel Holland2fcd7482020-10-24 10:21:49 -0500953 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100954#endif
955
Samuel Holland467b7e52020-10-24 10:21:50 -0500956 if (best_dt_name == NULL) {
957 /* No DT name was provided, so accept the first config. */
958 return 0;
959 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800960#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Holland54ac5aa2020-10-24 10:21:51 -0500961 if (strstr(best_dt_name, "-pine64-plus")) {
962 /* Differentiate the Pine A64 boards by their DRAM size. */
963 if ((gd->ram_size == 512 * 1024 * 1024))
964 best_dt_name = "sun50i-a64-pine64";
Andre Przywara9ea3c352017-04-26 01:32:44 +0100965 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800966#endif
Samuel Holland8a8b73b2020-10-24 10:21:52 -0500967#ifdef CONFIG_PINEPHONE_DT_SELECTION
968 if (strstr(best_dt_name, "-pinephone")) {
969 /* Differentiate the PinePhone revisions by GPIO inputs. */
970 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
971 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
972 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
973 udelay(100);
974
975 /* PL6 is pulled low by the modem on v1.2. */
976 if (gpio_get_value(SUNXI_GPL(6)) == 0)
977 best_dt_name = "sun50i-a64-pinephone-1.2";
978 else
979 best_dt_name = "sun50i-a64-pinephone-1.1";
980
981 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
982 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
983 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
984 }
985#endif
986
Samuel Holland41530cf2020-10-24 10:21:53 -0500987 ret = strcmp(name, best_dt_name);
988
989 /*
990 * If one of the FIT configurations matches the most accurate DT name,
991 * update the SPL header to provide that DT name to U-Boot proper.
992 */
993 if (ret == 0)
994 set_spl_dt_name(best_dt_name);
995
996 return ret;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100997}
Andre Przywara64531492022-11-28 00:02:56 +0000998#endif /* CONFIG_SPL_LOAD_FIT */