blob: 2790a0f9e874477f646fb2963970ec5a869540c1 [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>
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>
Arnaud Ferraris61485e92021-09-08 21:14:19 +020049#include <status_led.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010050
51DECLARE_GLOBAL_DATA_PTR;
52
Jernej Skrabec07da8802017-04-27 00:03:35 +020053void i2c_init_board(void)
54{
55#ifdef CONFIG_I2C0_ENABLE
56#if defined(CONFIG_MACH_SUN4I) || \
57 defined(CONFIG_MACH_SUN5I) || \
58 defined(CONFIG_MACH_SUN7I) || \
59 defined(CONFIG_MACH_SUN8I_R40)
60 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
61 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
62 clock_twi_onoff(0, 1);
63#elif defined(CONFIG_MACH_SUN6I)
64 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
65 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
66 clock_twi_onoff(0, 1);
Icenowy Zheng365951a2020-10-26 22:19:34 +080067#elif defined(CONFIG_MACH_SUN8I_V3S)
68 sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
69 sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
70 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +020071#elif defined(CONFIG_MACH_SUN8I)
72 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
73 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
74 clock_twi_onoff(0, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +020075#elif defined(CONFIG_MACH_SUN50I)
76 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
77 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
78 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +020079#endif
80#endif
81
82#ifdef CONFIG_I2C1_ENABLE
83#if defined(CONFIG_MACH_SUN4I) || \
84 defined(CONFIG_MACH_SUN7I) || \
85 defined(CONFIG_MACH_SUN8I_R40)
86 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
87 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
88 clock_twi_onoff(1, 1);
89#elif defined(CONFIG_MACH_SUN5I)
90 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
91 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
92 clock_twi_onoff(1, 1);
93#elif defined(CONFIG_MACH_SUN6I)
94 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
95 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
96 clock_twi_onoff(1, 1);
97#elif defined(CONFIG_MACH_SUN8I)
98 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
99 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
100 clock_twi_onoff(1, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200101#elif defined(CONFIG_MACH_SUN50I)
102 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
103 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
104 clock_twi_onoff(1, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200105#endif
106#endif
107
108#ifdef CONFIG_I2C2_ENABLE
109#if defined(CONFIG_MACH_SUN4I) || \
110 defined(CONFIG_MACH_SUN7I) || \
111 defined(CONFIG_MACH_SUN8I_R40)
112 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
113 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
114 clock_twi_onoff(2, 1);
115#elif defined(CONFIG_MACH_SUN5I)
116 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
117 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
118 clock_twi_onoff(2, 1);
119#elif defined(CONFIG_MACH_SUN6I)
120 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
121 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
122 clock_twi_onoff(2, 1);
123#elif defined(CONFIG_MACH_SUN8I)
124 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
125 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
126 clock_twi_onoff(2, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200127#elif defined(CONFIG_MACH_SUN50I)
128 sunxi_gpio_set_cfgpin(SUNXI_GPE(14), SUN50I_GPE_TWI2);
129 sunxi_gpio_set_cfgpin(SUNXI_GPE(15), SUN50I_GPE_TWI2);
130 clock_twi_onoff(2, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200131#endif
132#endif
133
134#ifdef CONFIG_I2C3_ENABLE
135#if defined(CONFIG_MACH_SUN6I)
136 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
137 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
138 clock_twi_onoff(3, 1);
139#elif defined(CONFIG_MACH_SUN7I) || \
140 defined(CONFIG_MACH_SUN8I_R40)
141 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
142 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
143 clock_twi_onoff(3, 1);
144#endif
145#endif
146
147#ifdef CONFIG_I2C4_ENABLE
148#if defined(CONFIG_MACH_SUN7I) || \
149 defined(CONFIG_MACH_SUN8I_R40)
150 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
151 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
152 clock_twi_onoff(4, 1);
153#endif
154#endif
155
156#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800157#ifdef CONFIG_MACH_SUN50I
158 clock_twi_onoff(5, 1);
159 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
160 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
Jernej Skrabec7de8eb02021-01-11 21:11:42 +0100161#elif CONFIG_MACH_SUN50I_H616
162 clock_twi_onoff(5, 1);
163 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN50I_H616_GPL_R_TWI);
164 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN50I_H616_GPL_R_TWI);
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800165#else
Jernej Skrabec07da8802017-04-27 00:03:35 +0200166 clock_twi_onoff(5, 1);
167 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
168 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
169#endif
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800170#endif
Jernej Skrabec07da8802017-04-27 00:03:35 +0200171}
172
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100173#if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT)
174enum env_location env_get_location(enum env_operation op, int prio)
175{
176 switch (prio) {
177 case 0:
178 return ENVL_FAT;
179
180 case 1:
181 return ENVL_MMC;
182
183 default:
184 return ENVL_UNKNOWN;
185 }
186}
187#endif
188
Andre Przywarad7cea362019-01-29 15:54:14 +0000189#ifdef CONFIG_DM_MMC
190static void mmc_pinmux_setup(int sdc);
191#endif
192
Ian Campbell6efe3692014-05-05 11:52:26 +0100193/* add board specific code here */
194int board_init(void)
195{
Mylène Josserand147c6062017-04-02 12:59:10 +0200196 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbell6efe3692014-05-05 11:52:26 +0100197
198 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
199
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200200#ifndef CONFIG_ARM64
Ian Campbell6efe3692014-05-05 11:52:26 +0100201 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
202 debug("id_pfr1: 0x%08x\n", id_pfr1);
203 /* Generic Timer Extension available? */
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200204 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
205 uint32_t freq;
206
Ian Campbell6efe3692014-05-05 11:52:26 +0100207 debug("Setting CNTFRQ\n");
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200208
209 /*
210 * CNTFRQ is a secure register, so we will crash if we try to
211 * write this from the non-secure world (read is OK, though).
212 * In case some bootcode has already set the correct value,
213 * we avoid the risk of writing to it.
214 */
215 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Andre Przywara70c78932017-02-16 01:20:19 +0000216 if (freq != COUNTER_FREQUENCY) {
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200217 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Andre Przywara70c78932017-02-16 01:20:19 +0000218 freq, COUNTER_FREQUENCY);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200219#ifdef CONFIG_NON_SECURE
220 printf("arch timer frequency is wrong, but cannot adjust it\n");
221#else
222 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Andre Przywara70c78932017-02-16 01:20:19 +0000223 : : "r"(COUNTER_FREQUENCY));
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200224#endif
225 }
Ian Campbell6efe3692014-05-05 11:52:26 +0100226 }
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200227#endif /* !CONFIG_ARM64 */
Ian Campbell6efe3692014-05-05 11:52:26 +0100228
Hans de Goede3ae1d132015-04-25 17:25:14 +0200229 ret = axp_gpio_init();
230 if (ret)
231 return ret;
232
Andre Przywara3b2dbb52021-01-18 23:23:59 +0000233 /* strcmp() would look better, but doesn't get optimised away. */
234 if (CONFIG_SATAPWR[0]) {
235 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
236 if (satapwr_pin >= 0) {
237 gpio_request(satapwr_pin, "satapwr");
238 gpio_direction_output(satapwr_pin, 1);
239
240 /*
241 * Give the attached SATA device time to power-up
242 * to avoid link timeouts
243 */
244 mdelay(500);
245 }
246 }
247
248 if (CONFIG_MACPWR[0]) {
249 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
250 if (macpwr_pin >= 0) {
251 gpio_request(macpwr_pin, "macpwr");
252 gpio_direction_output(macpwr_pin, 1);
253 }
254 }
Hans de Goede42cbbe32016-03-17 13:53:03 +0100255
Igor Opaniukf7c91762021-02-09 13:52:45 +0200256#if CONFIG_IS_ENABLED(DM_I2C)
Jernej Skrabec9220d502017-04-27 00:03:36 +0200257 /*
258 * Temporary workaround for enabling I2C clocks until proper sunxi DM
259 * clk, reset and pinctrl drivers land.
260 */
261 i2c_init_board();
262#endif
Andre Przywarad7cea362019-01-29 15:54:14 +0000263
264#ifdef CONFIG_DM_MMC
265 /*
266 * Temporary workaround for enabling MMC clocks until a sunxi DM
267 * pinctrl driver lands.
268 */
269 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
270#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
271 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
272#endif
273#endif /* CONFIG_DM_MMC */
Jernej Skrabec9220d502017-04-27 00:03:36 +0200274
Samuel Holland75fe0f42021-10-08 00:17:24 -0500275 return 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100276}
277
Andre Przywara14a25392018-10-25 17:23:04 +0800278/*
279 * On older SoCs the SPL is actually at address zero, so using NULL as
280 * an error value does not work.
281 */
282#define INVALID_SPL_HEADER ((void *)~0UL)
283
284static struct boot_file_head * get_spl_header(uint8_t req_version)
285{
286 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
287 uint8_t spl_header_version = spl->spl_signature[3];
288
289 /* Is there really the SPL header (still) there? */
290 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
291 return INVALID_SPL_HEADER;
292
293 if (spl_header_version < req_version) {
294 printf("sunxi SPL version mismatch: expected %u, got %u\n",
295 req_version, spl_header_version);
296 return INVALID_SPL_HEADER;
297 }
298
299 return spl;
300}
301
Samuel Hollandba44e942020-10-24 10:21:50 -0500302static const char *get_spl_dt_name(void)
303{
304 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
305
306 /* Check if there is a DT name stored in the SPL header. */
307 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
308 return (char *)spl + spl->dt_name_offset;
309
310 return NULL;
311}
Samuel Hollandba44e942020-10-24 10:21:50 -0500312
Ian Campbell6efe3692014-05-05 11:52:26 +0100313int dram_init(void)
314{
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800315 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
316
317 if (spl == INVALID_SPL_HEADER)
318 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
319 PHYS_SDRAM_0_SIZE);
320 else
321 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
322
323 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
324 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbell6efe3692014-05-05 11:52:26 +0100325
326 return 0;
327}
328
Boris Brezillon57f20382016-06-15 21:09:23 +0200329#if defined(CONFIG_NAND_SUNXI)
Karol Gugala7bea8932015-07-23 14:33:01 +0200330static void nand_pinmux_setup(void)
331{
332 unsigned int pin;
Karol Gugala7bea8932015-07-23 14:33:01 +0200333
Hans de Goeded2236782015-08-15 13:17:49 +0200334 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugala7bea8932015-07-23 14:33:01 +0200335 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
336
Hans de Goeded2236782015-08-15 13:17:49 +0200337#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
338 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
339 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
340#endif
341 /* sun4i / sun7i do have a PC23, but it is not used for nand,
342 * only sun7i has a PC24 */
343#ifdef CONFIG_MACH_SUN7I
Karol Gugala7bea8932015-07-23 14:33:01 +0200344 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goeded2236782015-08-15 13:17:49 +0200345#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200346}
347
348static void nand_clock_setup(void)
349{
350 struct sunxi_ccm_reg *const ccm =
351 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedee5561a82015-08-15 11:58:03 +0200352
Karol Gugala7bea8932015-07-23 14:33:01 +0200353 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalebeeb802018-02-28 20:51:53 +0100354#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
355 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
356 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
357#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200358 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
359}
Hans de Goede5ed52f62015-08-15 11:55:26 +0200360
361void board_nand_init(void)
362{
363 nand_pinmux_setup();
364 nand_clock_setup();
Boris Brezillon57f20382016-06-15 21:09:23 +0200365#ifndef CONFIG_SPL_BUILD
366 sunxi_nand_init();
367#endif
Hans de Goede5ed52f62015-08-15 11:55:26 +0200368}
Karol Gugala7bea8932015-07-23 14:33:01 +0200369#endif
370
Masahiro Yamada0a780172017-05-09 20:31:39 +0900371#ifdef CONFIG_MMC
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100372static void mmc_pinmux_setup(int sdc)
373{
374 unsigned int pin;
375
376 switch (sdc) {
377 case 0:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100378 /* SDC0: PF0-PF5 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100379 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100380 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100381 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
382 sunxi_gpio_set_drv(pin, 2);
383 }
384 break;
385
386 case 1:
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800387#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
388 defined(CONFIG_MACH_SUN8I_R40)
Samuel Holland51951052021-09-12 10:28:35 -0500389 if (IS_ENABLED(CONFIG_MMC1_PINS_PH)) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100390 /* SDC1: PH22-PH-27 */
391 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
392 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
393 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
394 sunxi_gpio_set_drv(pin, 2);
395 }
396 } else {
397 /* SDC1: PG0-PG5 */
398 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
399 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
400 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
401 sunxi_gpio_set_drv(pin, 2);
402 }
403 }
404#elif defined(CONFIG_MACH_SUN5I)
405 /* SDC1: PG3-PG8 */
Hans de Goede4dccfd42014-10-03 16:44:57 +0200406 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100407 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100408 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
409 sunxi_gpio_set_drv(pin, 2);
410 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100411#elif defined(CONFIG_MACH_SUN6I)
412 /* SDC1: PG0-PG5 */
413 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
414 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
415 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
416 sunxi_gpio_set_drv(pin, 2);
417 }
418#elif defined(CONFIG_MACH_SUN8I)
Samuel Holland51951052021-09-12 10:28:35 -0500419 /* SDC1: PG0-PG5 */
420 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
421 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
422 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
423 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100424 }
425#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100426 break;
427
428 case 2:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100429#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
430 /* SDC2: PC6-PC11 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100431 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100432 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100433 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
434 sunxi_gpio_set_drv(pin, 2);
435 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100436#elif defined(CONFIG_MACH_SUN5I)
Samuel Holland51951052021-09-12 10:28:35 -0500437 /* SDC2: PC6-PC15 */
438 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
439 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
440 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
441 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100442 }
443#elif defined(CONFIG_MACH_SUN6I)
Samuel Holland51951052021-09-12 10:28:35 -0500444 /* SDC2: PC6-PC15, PC24 */
445 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
446 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
447 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
448 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100449 }
Samuel Holland51951052021-09-12 10:28:35 -0500450
451 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
452 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
453 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800454#elif defined(CONFIG_MACH_SUN8I_R40)
455 /* SDC2: PC6-PC15, PC24 */
456 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
457 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
458 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
459 sunxi_gpio_set_drv(pin, 2);
460 }
461
462 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
463 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
464 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200465#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100466 /* SDC2: PC5-PC6, PC8-PC16 */
467 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
468 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
469 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
470 sunxi_gpio_set_drv(pin, 2);
471 }
472
473 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
474 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
475 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
476 sunxi_gpio_set_drv(pin, 2);
477 }
Icenowy Zhenga838a152018-07-21 16:20:29 +0800478#elif defined(CONFIG_MACH_SUN50I_H6)
479 /* SDC2: PC4-PC14 */
480 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
481 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
482 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
483 sunxi_gpio_set_drv(pin, 2);
484 }
Andre Przywara96f55642021-04-26 00:38:04 +0100485#elif defined(CONFIG_MACH_SUN50I_H616)
486 /* SDC2: PC0-PC1, PC5-PC6, PC8-PC11, PC13-PC16 */
487 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(16); pin++) {
488 if (pin > SUNXI_GPC(1) && pin < SUNXI_GPC(5))
489 continue;
490 if (pin == SUNXI_GPC(7) || pin == SUNXI_GPC(12))
491 continue;
492 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
493 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
494 sunxi_gpio_set_drv(pin, 3);
495 }
Philipp Tomsicha0c7c712016-10-28 18:21:33 +0800496#elif defined(CONFIG_MACH_SUN9I)
497 /* SDC2: PC6-PC16 */
498 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
499 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
500 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
501 sunxi_gpio_set_drv(pin, 2);
502 }
Andre Przywara96f55642021-04-26 00:38:04 +0100503#else
504 puts("ERROR: No pinmux setup defined for MMC2!\n");
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100505#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100506 break;
507
508 case 3:
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800509#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
510 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100511 /* SDC3: PI4-PI9 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100512 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100513 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100514 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
515 sunxi_gpio_set_drv(pin, 2);
516 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100517#elif defined(CONFIG_MACH_SUN6I)
Samuel Holland51951052021-09-12 10:28:35 -0500518 /* SDC3: PC6-PC15, PC24 */
519 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
520 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
521 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
522 sunxi_gpio_set_drv(pin, 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100523 }
Samuel Holland51951052021-09-12 10:28:35 -0500524
525 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
526 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
527 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100528#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100529 break;
530
531 default:
532 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
533 break;
534 }
535}
536
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900537int board_mmc_init(struct bd_info *bis)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100538{
Hans de Goede63deaa82014-10-02 21:13:54 +0200539 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goede63deaa82014-10-02 21:13:54 +0200540
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100541 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goede63deaa82014-10-02 21:13:54 +0200542 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
543 if (!mmc0)
544 return -1;
545
Hans de Goedeaf593e42014-10-02 20:43:50 +0200546#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100547 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goede63deaa82014-10-02 21:13:54 +0200548 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
549 if (!mmc1)
550 return -1;
551#endif
552
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100553 return 0;
554}
Samuel Hollandbc42abb2021-04-18 22:16:21 -0500555
556#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
557int mmc_get_env_dev(void)
558{
559 switch (sunxi_get_boot_device()) {
560 case BOOT_DEVICE_MMC1:
561 return 0;
562 case BOOT_DEVICE_MMC2:
563 return 1;
564 default:
565 return CONFIG_SYS_MMC_ENV_DEV;
566 }
567}
568#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100569#endif
570
Ian Campbell6efe3692014-05-05 11:52:26 +0100571#ifdef CONFIG_SPL_BUILD
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800572
573static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
574{
575 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
576
577 if (spl == INVALID_SPL_HEADER)
578 return;
579
580 /* Promote the header version for U-Boot proper, if needed. */
581 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
582 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
583
584 spl->dram_size = dram_size >> 20;
585}
586
Ian Campbell6efe3692014-05-05 11:52:26 +0100587void sunxi_board_init(void)
588{
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200589 int power_failed = 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100590
Arnaud Ferraris61485e92021-09-08 21:14:19 +0200591#ifdef CONFIG_LED_STATUS
592 if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
593 status_led_init();
594#endif
595
Jelle van der Waa3f3a3092016-02-23 18:47:19 +0100596#ifdef CONFIG_SY8106A_POWER
597 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
598#endif
599
vishnupatekar1895dfd2015-11-29 01:07:22 +0800600#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100601 defined CONFIG_AXP221_POWER || defined CONFIG_AXP305_POWER || \
602 defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200603 power_failed = axp_init();
604
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800605#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
606 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200607 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede1f247362014-06-13 22:55:51 +0200608#endif
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100609#if !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200610 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
611 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100612#endif
vishnupatekar1895dfd2015-11-29 01:07:22 +0800613#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200614 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200615#endif
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800616#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
617 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200618 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200619#endif
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200620
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800621#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
622 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200623 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
624#endif
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100625#if !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200626 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Jernej Skrabecfde828c2021-01-11 21:11:33 +0100627#endif
628#if !defined(CONFIG_AXP152_POWER) && !defined(CONFIG_AXP305_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200629 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
630#endif
631#ifdef CONFIG_AXP209_POWER
632 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
633#endif
634
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800635#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
636 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800637 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
638 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800639#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800640 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
641 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800642#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200643 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
644 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
645 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
646#endif
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800647
648#ifdef CONFIG_AXP818_POWER
649 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
650 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
651 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800652#endif
653
654#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai0e3efd32016-05-02 10:28:12 +0800655 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800656#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200657#endif
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000658 printf("DRAM:");
659 gd->ram_size = sunxi_dram_init();
660 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
661 if (!gd->ram_size)
662 hang();
663
664 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800665
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200666 /*
667 * Only clock up the CPU to full speed if we are reasonably
668 * assured it's being powered with suitable core voltage
669 */
670 if (!power_failed)
Tom Rini8c70baa2021-12-14 13:36:40 -0500671 clock_set_pll1(get_board_sys_clk());
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200672 else
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000673 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbell6efe3692014-05-05 11:52:26 +0100674}
675#endif
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200676
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100677#ifdef CONFIG_USB_GADGET
678int g_dnl_board_usb_cable_connected(void)
679{
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530680 struct udevice *dev;
681 struct phy phy;
682 int ret;
683
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100684 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530685 if (ret) {
686 pr_err("%s: Cannot find USB device\n", __func__);
687 return ret;
688 }
689
690 ret = generic_phy_get_by_name(dev, "usb", &phy);
691 if (ret) {
692 pr_err("failed to get %s USB PHY\n", dev->name);
693 return ret;
694 }
695
696 ret = generic_phy_init(&phy);
697 if (ret) {
Patrick Delaunay287e33c2020-07-03 17:36:41 +0200698 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530699 return ret;
700 }
701
Andre Przywarae79ee612021-11-02 19:45:47 +0000702 return sun4i_usb_phy_vbus_detect(&phy);
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100703}
704#endif
705
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100706#ifdef CONFIG_SERIAL_TAG
707void get_board_serial(struct tag_serialnr *serialnr)
708{
709 char *serial_string;
710 unsigned long long serial;
711
Simon Glass64b723f2017-08-03 12:22:12 -0600712 serial_string = env_get("serial#");
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100713
714 if (serial_string) {
715 serial = simple_strtoull(serial_string, NULL, 16);
716
717 serialnr->high = (unsigned int) (serial >> 32);
718 serialnr->low = (unsigned int) (serial & 0xffffffff);
719 } else {
720 serialnr->high = 0;
721 serialnr->low = 0;
722 }
723}
724#endif
725
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200726/*
727 * Check the SPL header for the "sunxi" variant. If found: parse values
728 * that might have been passed by the loader ("fel" utility), and update
729 * the environment accordingly.
730 */
731static void parse_spl_header(const uint32_t spl_addr)
732{
Andre Przywara14a25392018-10-25 17:23:04 +0800733 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200734
Andre Przywara14a25392018-10-25 17:23:04 +0800735 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200736 return;
Andre Przywara14a25392018-10-25 17:23:04 +0800737
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200738 if (!spl->fel_script_address)
739 return;
740
741 if (spl->fel_uEnv_length != 0) {
742 /*
743 * data is expected in uEnv.txt compatible format, so "env
744 * import -t" the string(s) at fel_script_address right away.
745 */
Andre Przywaraac4e6732016-09-05 01:32:41 +0100746 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200747 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
748 return;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200749 }
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200750 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass4d949a22017-08-03 12:22:10 -0600751 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200752}
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200753
Andre Heiderebdc3d42021-10-01 19:29:00 +0100754static bool get_unique_sid(unsigned int *sid)
755{
756 if (sunxi_get_sid(sid) != 0)
757 return false;
758
759 if (!sid[0])
760 return false;
761
762 /*
763 * The single words 1 - 3 of the SID have quite a few bits
764 * which are the same on many models, so we take a crc32
765 * of all 3 words, to get a more unique value.
766 *
767 * Note we only do this on newer SoCs as we cannot change
768 * the algorithm on older SoCs since those have been using
769 * fixed mac-addresses based on only using word 3 for a
770 * long time and changing a fixed mac-address with an
771 * u-boot update is not good.
772 */
773#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
774 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
775 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
776 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
777#endif
778
779 /* Ensure the NIC specific bytes of the mac are not all 0 */
780 if ((sid[3] & 0xffffff) == 0)
781 sid[3] |= 0x800000;
782
783 return true;
784}
785
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200786/*
787 * Note this function gets called multiple times.
788 * It must not make any changes to env variables which already exist.
789 */
790static void setup_environment(const void *fdt)
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200791{
Paul Kocialkowski92935942015-03-28 18:35:35 +0100792 char serial_string[17] = { 0 };
Hans de Goede11d70982014-11-26 00:04:24 +0100793 unsigned int sid[4];
Paul Kocialkowski92935942015-03-28 18:35:35 +0100794 uint8_t mac_addr[6];
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200795 char ethaddr[16];
Andre Heiderebdc3d42021-10-01 19:29:00 +0100796 int i;
Hans de Goedee5fe5482016-07-29 11:47:03 +0200797
Andre Heiderebdc3d42021-10-01 19:29:00 +0100798 if (!get_unique_sid(sid))
799 return;
Hans de Goedeabca8432016-07-27 17:58:06 +0200800
Andre Heiderebdc3d42021-10-01 19:29:00 +0100801 for (i = 0; i < 4; i++) {
802 sprintf(ethaddr, "ethernet%d", i);
803 if (!fdt_get_alias(fdt, ethaddr))
804 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200805
Andre Heiderebdc3d42021-10-01 19:29:00 +0100806 if (i == 0)
807 strcpy(ethaddr, "ethaddr");
808 else
809 sprintf(ethaddr, "eth%daddr", i);
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200810
Andre Heiderebdc3d42021-10-01 19:29:00 +0100811 if (env_get(ethaddr))
812 continue;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200813
Andre Heiderebdc3d42021-10-01 19:29:00 +0100814 /* Non OUI / registered MAC address */
815 mac_addr[0] = (i << 4) | 0x02;
816 mac_addr[1] = (sid[0] >> 0) & 0xff;
817 mac_addr[2] = (sid[3] >> 24) & 0xff;
818 mac_addr[3] = (sid[3] >> 16) & 0xff;
819 mac_addr[4] = (sid[3] >> 8) & 0xff;
820 mac_addr[5] = (sid[3] >> 0) & 0xff;
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200821
Andre Heiderebdc3d42021-10-01 19:29:00 +0100822 eth_env_set_enetaddr(ethaddr, mac_addr);
823 }
Paul Kocialkowski92935942015-03-28 18:35:35 +0100824
Andre Heiderebdc3d42021-10-01 19:29:00 +0100825 if (!env_get("serial#")) {
826 snprintf(serial_string, sizeof(serial_string),
827 "%08x%08x", sid[0], sid[3]);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200828
Andre Heiderebdc3d42021-10-01 19:29:00 +0100829 env_set("serial#", serial_string);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200830 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200831}
832
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200833int misc_init_r(void)
834{
Samuel Holland87f940a2020-10-24 10:21:54 -0500835 const char *spl_dt_name;
Maxime Ripardae56d972017-08-23 10:08:29 +0200836 uint boot;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200837
Simon Glass6a38e412017-08-03 12:22:09 -0600838 env_set("fel_booted", NULL);
839 env_set("fel_scriptaddr", NULL);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200840 env_set("mmc_bootdev", NULL);
Maxime Ripardae56d972017-08-23 10:08:29 +0200841
842 boot = sunxi_get_boot_device();
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200843 /* determine if we are running in FEL mode */
Maxime Ripardae56d972017-08-23 10:08:29 +0200844 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass6a38e412017-08-03 12:22:09 -0600845 env_set("fel_booted", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200846 parse_spl_header(SPL_ADDR);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200847 /* or if we booted from MMC, and which one */
848 } else if (boot == BOOT_DEVICE_MMC1) {
849 env_set("mmc_bootdev", "0");
850 } else if (boot == BOOT_DEVICE_MMC2) {
851 env_set("mmc_bootdev", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200852 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200853
Samuel Holland87f940a2020-10-24 10:21:54 -0500854 /* Set fdtfile to match the FIT configuration chosen in SPL. */
855 spl_dt_name = get_spl_dt_name();
856 if (spl_dt_name) {
857 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
858 char str[64];
859
860 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
861 env_set("fdtfile", str);
862 }
863
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200864 setup_environment(gd->fdt_blob);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200865
Andy Shevchenko1facc0f2020-12-08 17:45:31 +0200866 return 0;
867}
868
869int board_late_init(void)
870{
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800871#ifdef CONFIG_USB_ETHER
Maxime Ripardf54aba32017-09-06 22:25:03 +0200872 usb_ether_init();
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800873#endif
Maxime Ripardf54aba32017-09-06 22:25:03 +0200874
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200875 return 0;
876}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200877
Andre Heiderbf8c8102021-10-01 19:29:00 +0100878static void bluetooth_dt_fixup(void *blob)
879{
880 /* Some devices ship with a Bluetooth controller default address.
881 * Set a valid address through the device tree.
882 */
883 uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
884 unsigned int sid[4];
885 int i;
886
887 if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
888 return;
889
890 if (eth_env_get_enetaddr("bdaddr", tmp)) {
891 /* Convert between the binary formats of the corresponding stacks */
892 for (i = 0; i < ETH_ALEN; ++i)
893 bdaddr[i] = tmp[ETH_ALEN - i - 1];
894 } else {
895 if (!get_unique_sid(sid))
896 return;
897
898 bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
899 bdaddr[1] = (sid[3] >> 8) & 0xff;
900 bdaddr[2] = (sid[3] >> 16) & 0xff;
901 bdaddr[3] = (sid[3] >> 24) & 0xff;
902 bdaddr[4] = (sid[0] >> 0) & 0xff;
903 bdaddr[5] = 0x02;
904 }
905
906 do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
907 "local-bd-address", bdaddr, ETH_ALEN, 1);
908}
909
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900910int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200911{
Hans de Goede48a234a2016-03-22 22:51:52 +0100912 int __maybe_unused r;
913
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200914 /*
915 * Call setup_environment again in case the boot fdt has
916 * ethernet aliases the u-boot copy does not have.
917 */
918 setup_environment(blob);
919
Andre Heiderbf8c8102021-10-01 19:29:00 +0100920 bluetooth_dt_fixup(blob);
921
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200922#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goede48a234a2016-03-22 22:51:52 +0100923 r = sunxi_simplefb_setup(blob);
924 if (r)
925 return r;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200926#endif
Hans de Goede48a234a2016-03-22 22:51:52 +0100927 return 0;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200928}
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100929
930#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland64933e92020-10-24 10:21:53 -0500931
932static void set_spl_dt_name(const char *name)
933{
934 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
935
936 if (spl == INVALID_SPL_HEADER)
937 return;
938
939 /* Promote the header version for U-Boot proper, if needed. */
940 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
941 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
942
943 strcpy((char *)&spl->string_pool, name);
944 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
945}
946
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100947int board_fit_config_name_match(const char *name)
948{
Samuel Hollandba44e942020-10-24 10:21:50 -0500949 const char *best_dt_name = get_spl_dt_name();
Samuel Holland64933e92020-10-24 10:21:53 -0500950 int ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100951
952#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Hollandba44e942020-10-24 10:21:50 -0500953 if (best_dt_name == NULL)
Samuel Holland37b86202020-10-24 10:21:49 -0500954 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100955#endif
956
Samuel Hollandba44e942020-10-24 10:21:50 -0500957 if (best_dt_name == NULL) {
958 /* No DT name was provided, so accept the first config. */
959 return 0;
960 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800961#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Hollandf2352dd2020-10-24 10:21:51 -0500962 if (strstr(best_dt_name, "-pine64-plus")) {
963 /* Differentiate the Pine A64 boards by their DRAM size. */
964 if ((gd->ram_size == 512 * 1024 * 1024))
965 best_dt_name = "sun50i-a64-pine64";
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100966 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800967#endif
Samuel Holland9c7cefc2020-10-24 10:21:52 -0500968#ifdef CONFIG_PINEPHONE_DT_SELECTION
969 if (strstr(best_dt_name, "-pinephone")) {
970 /* Differentiate the PinePhone revisions by GPIO inputs. */
971 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
972 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
973 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
974 udelay(100);
975
976 /* PL6 is pulled low by the modem on v1.2. */
977 if (gpio_get_value(SUNXI_GPL(6)) == 0)
978 best_dt_name = "sun50i-a64-pinephone-1.2";
979 else
980 best_dt_name = "sun50i-a64-pinephone-1.1";
981
982 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
983 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
984 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
985 }
986#endif
987
Samuel Holland64933e92020-10-24 10:21:53 -0500988 ret = strcmp(name, best_dt_name);
989
990 /*
991 * If one of the FIT configurations matches the most accurate DT name,
992 * update the SPL header to provide that DT name to U-Boot proper.
993 */
994 if (ret == 0)
995 set_spl_dt_name(best_dt_name);
996
997 return ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100998}
999#endif