blob: 708a27ed78e96f8a6d903839ca7430b94ec11396 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbell6efe3692014-05-05 11:52:26 +01002/*
3 * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net>
4 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 *
6 * (C) Copyright 2007-2011
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * Tom Cubie <tangliang@allwinnertech.com>
9 *
10 * Some board init for the Allwinner A10-evb board.
Ian Campbell6efe3692014-05-05 11:52:26 +010011 */
12
13#include <common.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053014#include <dm.h>
Simon Glass313112a2019-08-01 09:46:46 -060015#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070016#include <hang.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060017#include <image.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070018#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Hans de Goede63deaa82014-10-02 21:13:54 +020020#include <mmc.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020021#include <axp_pmic.h>
Jagan Teki73a3ecf2018-05-07 13:03:36 +053022#include <generic-phy.h>
23#include <phy-sun4i-usb.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010024#include <asm/arch/clock.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020025#include <asm/arch/cpu.h>
Luc Verhaegen4869a8c2014-08-13 07:55:07 +020026#include <asm/arch/display.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010027#include <asm/arch/dram.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010028#include <asm/arch/gpio.h>
29#include <asm/arch/mmc.h>
Samuel Holland9c7cefc2020-10-24 10:21:52 -050030#include <asm/arch/prcm.h>
Hans de Goedea146c502016-07-09 09:56:56 +020031#include <asm/arch/spl.h>
Simon Glassdbd79542020-05-10 11:40:11 -060032#include <linux/delay.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070033#include <u-boot/crc.h>
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020034#ifndef CONFIG_ARM64
35#include <asm/armv7.h>
36#endif
Hans de Goeded9d05652015-04-23 23:23:50 +020037#include <asm/gpio.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020038#include <asm/io.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010039#include <u-boot/crc.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060040#include <env_internal.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090041#include <linux/libfdt.h>
Hans de Goede5ed52f62015-08-15 11:55:26 +020042#include <nand.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020043#include <net.h>
Maxime Ripardae56d972017-08-23 10:08:29 +020044#include <spl.h>
Jelle van der Waa3f3a3092016-02-23 18:47:19 +010045#include <sy8106a.h>
Simon Glassd9a766f2017-05-17 08:23:00 -060046#include <asm/setup.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010047
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010048#if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD)
49/* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */
50int soft_i2c_gpio_sda;
51int soft_i2c_gpio_scl;
Hans de Goeded9d05652015-04-23 23:23:50 +020052
53static int soft_i2c_board_init(void)
54{
55 int ret;
56
57 soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA);
58 if (soft_i2c_gpio_sda < 0) {
59 printf("Error invalid soft i2c sda pin: '%s', err %d\n",
60 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda);
61 return soft_i2c_gpio_sda;
62 }
63 ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda");
64 if (ret) {
65 printf("Error requesting soft i2c sda pin: '%s', err %d\n",
66 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret);
67 return ret;
68 }
69
70 soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL);
71 if (soft_i2c_gpio_scl < 0) {
72 printf("Error invalid soft i2c scl pin: '%s', err %d\n",
73 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl);
74 return soft_i2c_gpio_scl;
75 }
76 ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl");
77 if (ret) {
78 printf("Error requesting soft i2c scl pin: '%s', err %d\n",
79 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret);
80 return ret;
81 }
82
83 return 0;
84}
85#else
86static int soft_i2c_board_init(void) { return 0; }
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010087#endif
88
Ian Campbell6efe3692014-05-05 11:52:26 +010089DECLARE_GLOBAL_DATA_PTR;
90
Jernej Skrabec07da8802017-04-27 00:03:35 +020091void i2c_init_board(void)
92{
93#ifdef CONFIG_I2C0_ENABLE
94#if defined(CONFIG_MACH_SUN4I) || \
95 defined(CONFIG_MACH_SUN5I) || \
96 defined(CONFIG_MACH_SUN7I) || \
97 defined(CONFIG_MACH_SUN8I_R40)
98 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
99 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
100 clock_twi_onoff(0, 1);
101#elif defined(CONFIG_MACH_SUN6I)
102 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
103 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
104 clock_twi_onoff(0, 1);
Icenowy Zheng365951a2020-10-26 22:19:34 +0800105#elif defined(CONFIG_MACH_SUN8I_V3S)
106 sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
107 sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
108 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200109#elif defined(CONFIG_MACH_SUN8I)
110 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
111 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
112 clock_twi_onoff(0, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200113#elif defined(CONFIG_MACH_SUN50I)
114 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
115 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
116 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200117#endif
118#endif
119
120#ifdef CONFIG_I2C1_ENABLE
121#if defined(CONFIG_MACH_SUN4I) || \
122 defined(CONFIG_MACH_SUN7I) || \
123 defined(CONFIG_MACH_SUN8I_R40)
124 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
125 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
126 clock_twi_onoff(1, 1);
127#elif defined(CONFIG_MACH_SUN5I)
128 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
129 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
130 clock_twi_onoff(1, 1);
131#elif defined(CONFIG_MACH_SUN6I)
132 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
133 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
134 clock_twi_onoff(1, 1);
135#elif defined(CONFIG_MACH_SUN8I)
136 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
137 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
138 clock_twi_onoff(1, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200139#elif defined(CONFIG_MACH_SUN50I)
140 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
141 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
142 clock_twi_onoff(1, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200143#endif
144#endif
145
146#ifdef CONFIG_I2C2_ENABLE
147#if defined(CONFIG_MACH_SUN4I) || \
148 defined(CONFIG_MACH_SUN7I) || \
149 defined(CONFIG_MACH_SUN8I_R40)
150 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
151 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
152 clock_twi_onoff(2, 1);
153#elif defined(CONFIG_MACH_SUN5I)
154 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
155 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
156 clock_twi_onoff(2, 1);
157#elif defined(CONFIG_MACH_SUN6I)
158 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
159 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
160 clock_twi_onoff(2, 1);
161#elif defined(CONFIG_MACH_SUN8I)
162 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
163 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
164 clock_twi_onoff(2, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200165#elif defined(CONFIG_MACH_SUN50I)
166 sunxi_gpio_set_cfgpin(SUNXI_GPE(14), SUN50I_GPE_TWI2);
167 sunxi_gpio_set_cfgpin(SUNXI_GPE(15), SUN50I_GPE_TWI2);
168 clock_twi_onoff(2, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200169#endif
170#endif
171
172#ifdef CONFIG_I2C3_ENABLE
173#if defined(CONFIG_MACH_SUN6I)
174 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
175 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
176 clock_twi_onoff(3, 1);
177#elif defined(CONFIG_MACH_SUN7I) || \
178 defined(CONFIG_MACH_SUN8I_R40)
179 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
180 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
181 clock_twi_onoff(3, 1);
182#endif
183#endif
184
185#ifdef CONFIG_I2C4_ENABLE
186#if defined(CONFIG_MACH_SUN7I) || \
187 defined(CONFIG_MACH_SUN8I_R40)
188 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
189 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
190 clock_twi_onoff(4, 1);
191#endif
192#endif
193
194#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800195#ifdef CONFIG_MACH_SUN50I
196 clock_twi_onoff(5, 1);
197 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
198 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
199#else
Jernej Skrabec07da8802017-04-27 00:03:35 +0200200 clock_twi_onoff(5, 1);
201 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
202 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
203#endif
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800204#endif
Jernej Skrabec07da8802017-04-27 00:03:35 +0200205}
206
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100207#if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT)
208enum env_location env_get_location(enum env_operation op, int prio)
209{
210 switch (prio) {
211 case 0:
212 return ENVL_FAT;
213
214 case 1:
215 return ENVL_MMC;
216
217 default:
218 return ENVL_UNKNOWN;
219 }
220}
221#endif
222
Andre Przywarad7cea362019-01-29 15:54:14 +0000223#ifdef CONFIG_DM_MMC
224static void mmc_pinmux_setup(int sdc);
225#endif
226
Ian Campbell6efe3692014-05-05 11:52:26 +0100227/* add board specific code here */
228int board_init(void)
229{
Mylène Josserand147c6062017-04-02 12:59:10 +0200230 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbell6efe3692014-05-05 11:52:26 +0100231
232 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
233
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200234#ifndef CONFIG_ARM64
Ian Campbell6efe3692014-05-05 11:52:26 +0100235 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
236 debug("id_pfr1: 0x%08x\n", id_pfr1);
237 /* Generic Timer Extension available? */
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200238 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
239 uint32_t freq;
240
Ian Campbell6efe3692014-05-05 11:52:26 +0100241 debug("Setting CNTFRQ\n");
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200242
243 /*
244 * CNTFRQ is a secure register, so we will crash if we try to
245 * write this from the non-secure world (read is OK, though).
246 * In case some bootcode has already set the correct value,
247 * we avoid the risk of writing to it.
248 */
249 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Andre Przywara70c78932017-02-16 01:20:19 +0000250 if (freq != COUNTER_FREQUENCY) {
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200251 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Andre Przywara70c78932017-02-16 01:20:19 +0000252 freq, COUNTER_FREQUENCY);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200253#ifdef CONFIG_NON_SECURE
254 printf("arch timer frequency is wrong, but cannot adjust it\n");
255#else
256 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Andre Przywara70c78932017-02-16 01:20:19 +0000257 : : "r"(COUNTER_FREQUENCY));
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200258#endif
259 }
Ian Campbell6efe3692014-05-05 11:52:26 +0100260 }
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200261#endif /* !CONFIG_ARM64 */
Ian Campbell6efe3692014-05-05 11:52:26 +0100262
Hans de Goede3ae1d132015-04-25 17:25:14 +0200263 ret = axp_gpio_init();
264 if (ret)
265 return ret;
266
Hans de Goede9c34c3e2016-03-22 20:10:30 +0100267#ifdef CONFIG_SATAPWR
Mylène Josserand628426a2017-04-02 12:59:09 +0200268 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
269 gpio_request(satapwr_pin, "satapwr");
270 gpio_direction_output(satapwr_pin, 1);
Werner Böllmanne58f8302017-11-10 19:14:20 +0530271 /* Give attached sata device time to power-up to avoid link timeouts */
272 mdelay(500);
Hans de Goede9c34c3e2016-03-22 20:10:30 +0100273#endif
Hans de Goede42cbbe32016-03-17 13:53:03 +0100274#ifdef CONFIG_MACPWR
Mylène Josserand147c6062017-04-02 12:59:10 +0200275 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
276 gpio_request(macpwr_pin, "macpwr");
277 gpio_direction_output(macpwr_pin, 1);
Hans de Goede42cbbe32016-03-17 13:53:03 +0100278#endif
279
Jernej Skrabec9220d502017-04-27 00:03:36 +0200280#ifdef CONFIG_DM_I2C
281 /*
282 * Temporary workaround for enabling I2C clocks until proper sunxi DM
283 * clk, reset and pinctrl drivers land.
284 */
285 i2c_init_board();
286#endif
Andre Przywarad7cea362019-01-29 15:54:14 +0000287
288#ifdef CONFIG_DM_MMC
289 /*
290 * Temporary workaround for enabling MMC clocks until a sunxi DM
291 * pinctrl driver lands.
292 */
293 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
294#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
295 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
296#endif
297#endif /* CONFIG_DM_MMC */
Jernej Skrabec9220d502017-04-27 00:03:36 +0200298
Hans de Goeded9d05652015-04-23 23:23:50 +0200299 /* Uses dm gpio code so do this here and not in i2c_init_board() */
300 return soft_i2c_board_init();
Ian Campbell6efe3692014-05-05 11:52:26 +0100301}
302
Andre Przywara14a25392018-10-25 17:23:04 +0800303/*
304 * On older SoCs the SPL is actually at address zero, so using NULL as
305 * an error value does not work.
306 */
307#define INVALID_SPL_HEADER ((void *)~0UL)
308
309static struct boot_file_head * get_spl_header(uint8_t req_version)
310{
311 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
312 uint8_t spl_header_version = spl->spl_signature[3];
313
314 /* Is there really the SPL header (still) there? */
315 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
316 return INVALID_SPL_HEADER;
317
318 if (spl_header_version < req_version) {
319 printf("sunxi SPL version mismatch: expected %u, got %u\n",
320 req_version, spl_header_version);
321 return INVALID_SPL_HEADER;
322 }
323
324 return spl;
325}
326
Samuel Hollandba44e942020-10-24 10:21:50 -0500327static const char *get_spl_dt_name(void)
328{
329 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
330
331 /* Check if there is a DT name stored in the SPL header. */
332 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
333 return (char *)spl + spl->dt_name_offset;
334
335 return NULL;
336}
Samuel Hollandba44e942020-10-24 10:21:50 -0500337
Ian Campbell6efe3692014-05-05 11:52:26 +0100338int dram_init(void)
339{
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800340 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
341
342 if (spl == INVALID_SPL_HEADER)
343 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
344 PHYS_SDRAM_0_SIZE);
345 else
346 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
347
348 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
349 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbell6efe3692014-05-05 11:52:26 +0100350
351 return 0;
352}
353
Boris Brezillon57f20382016-06-15 21:09:23 +0200354#if defined(CONFIG_NAND_SUNXI)
Karol Gugala7bea8932015-07-23 14:33:01 +0200355static void nand_pinmux_setup(void)
356{
357 unsigned int pin;
Karol Gugala7bea8932015-07-23 14:33:01 +0200358
Hans de Goeded2236782015-08-15 13:17:49 +0200359 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugala7bea8932015-07-23 14:33:01 +0200360 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
361
Hans de Goeded2236782015-08-15 13:17:49 +0200362#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
363 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
364 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
365#endif
366 /* sun4i / sun7i do have a PC23, but it is not used for nand,
367 * only sun7i has a PC24 */
368#ifdef CONFIG_MACH_SUN7I
Karol Gugala7bea8932015-07-23 14:33:01 +0200369 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goeded2236782015-08-15 13:17:49 +0200370#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200371}
372
373static void nand_clock_setup(void)
374{
375 struct sunxi_ccm_reg *const ccm =
376 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedee5561a82015-08-15 11:58:03 +0200377
Karol Gugala7bea8932015-07-23 14:33:01 +0200378 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalebeeb802018-02-28 20:51:53 +0100379#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
380 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
381 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
382#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200383 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
384}
Hans de Goede5ed52f62015-08-15 11:55:26 +0200385
386void board_nand_init(void)
387{
388 nand_pinmux_setup();
389 nand_clock_setup();
Boris Brezillon57f20382016-06-15 21:09:23 +0200390#ifndef CONFIG_SPL_BUILD
391 sunxi_nand_init();
392#endif
Hans de Goede5ed52f62015-08-15 11:55:26 +0200393}
Karol Gugala7bea8932015-07-23 14:33:01 +0200394#endif
395
Masahiro Yamada0a780172017-05-09 20:31:39 +0900396#ifdef CONFIG_MMC
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100397static void mmc_pinmux_setup(int sdc)
398{
399 unsigned int pin;
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100400 __maybe_unused int pins;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100401
402 switch (sdc) {
403 case 0:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100404 /* SDC0: PF0-PF5 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100405 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100406 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100407 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
408 sunxi_gpio_set_drv(pin, 2);
409 }
410 break;
411
412 case 1:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100413 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS);
414
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800415#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
416 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100417 if (pins == SUNXI_GPIO_H) {
418 /* SDC1: PH22-PH-27 */
419 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
420 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
421 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
422 sunxi_gpio_set_drv(pin, 2);
423 }
424 } else {
425 /* SDC1: PG0-PG5 */
426 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
427 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
428 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
429 sunxi_gpio_set_drv(pin, 2);
430 }
431 }
432#elif defined(CONFIG_MACH_SUN5I)
433 /* SDC1: PG3-PG8 */
Hans de Goede4dccfd42014-10-03 16:44:57 +0200434 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100435 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100436 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
437 sunxi_gpio_set_drv(pin, 2);
438 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100439#elif defined(CONFIG_MACH_SUN6I)
440 /* SDC1: PG0-PG5 */
441 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
442 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
443 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
444 sunxi_gpio_set_drv(pin, 2);
445 }
446#elif defined(CONFIG_MACH_SUN8I)
447 if (pins == SUNXI_GPIO_D) {
448 /* SDC1: PD2-PD7 */
449 for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) {
450 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1);
451 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
452 sunxi_gpio_set_drv(pin, 2);
453 }
454 } else {
455 /* SDC1: PG0-PG5 */
456 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
457 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
458 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
459 sunxi_gpio_set_drv(pin, 2);
460 }
461 }
462#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100463 break;
464
465 case 2:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100466 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS);
467
468#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
469 /* SDC2: PC6-PC11 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100470 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100471 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100472 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
473 sunxi_gpio_set_drv(pin, 2);
474 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100475#elif defined(CONFIG_MACH_SUN5I)
476 if (pins == SUNXI_GPIO_E) {
477 /* SDC2: PE4-PE9 */
478 for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) {
479 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2);
480 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
481 sunxi_gpio_set_drv(pin, 2);
482 }
483 } else {
484 /* SDC2: PC6-PC15 */
485 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
486 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
487 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
488 sunxi_gpio_set_drv(pin, 2);
489 }
490 }
491#elif defined(CONFIG_MACH_SUN6I)
492 if (pins == SUNXI_GPIO_A) {
493 /* SDC2: PA9-PA14 */
494 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
495 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2);
496 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
497 sunxi_gpio_set_drv(pin, 2);
498 }
499 } else {
500 /* SDC2: PC6-PC15, PC24 */
501 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
502 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
503 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
504 sunxi_gpio_set_drv(pin, 2);
505 }
506
507 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
508 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
509 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
510 }
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800511#elif defined(CONFIG_MACH_SUN8I_R40)
512 /* SDC2: PC6-PC15, PC24 */
513 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
514 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
515 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
516 sunxi_gpio_set_drv(pin, 2);
517 }
518
519 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
520 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
521 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200522#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100523 /* SDC2: PC5-PC6, PC8-PC16 */
524 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
525 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
526 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
527 sunxi_gpio_set_drv(pin, 2);
528 }
529
530 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
531 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
532 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
533 sunxi_gpio_set_drv(pin, 2);
534 }
Icenowy Zhenga838a152018-07-21 16:20:29 +0800535#elif defined(CONFIG_MACH_SUN50I_H6)
536 /* SDC2: PC4-PC14 */
537 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
538 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
539 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
540 sunxi_gpio_set_drv(pin, 2);
541 }
Philipp Tomsicha0c7c712016-10-28 18:21:33 +0800542#elif defined(CONFIG_MACH_SUN9I)
543 /* SDC2: PC6-PC16 */
544 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
545 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
546 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
547 sunxi_gpio_set_drv(pin, 2);
548 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100549#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100550 break;
551
552 case 3:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100553 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS);
554
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800555#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
556 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100557 /* SDC3: PI4-PI9 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100558 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100559 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100560 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
561 sunxi_gpio_set_drv(pin, 2);
562 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100563#elif defined(CONFIG_MACH_SUN6I)
564 if (pins == SUNXI_GPIO_A) {
565 /* SDC3: PA9-PA14 */
566 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
567 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3);
568 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
569 sunxi_gpio_set_drv(pin, 2);
570 }
571 } else {
572 /* SDC3: PC6-PC15, PC24 */
573 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
574 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
575 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
576 sunxi_gpio_set_drv(pin, 2);
577 }
578
579 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
580 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
581 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
582 }
583#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100584 break;
585
586 default:
587 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
588 break;
589 }
590}
591
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900592int board_mmc_init(struct bd_info *bis)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100593{
Hans de Goede63deaa82014-10-02 21:13:54 +0200594 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goede63deaa82014-10-02 21:13:54 +0200595
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100596 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goede63deaa82014-10-02 21:13:54 +0200597 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
598 if (!mmc0)
599 return -1;
600
Hans de Goedeaf593e42014-10-02 20:43:50 +0200601#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100602 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goede63deaa82014-10-02 21:13:54 +0200603 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
604 if (!mmc1)
605 return -1;
606#endif
607
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100608 return 0;
609}
610#endif
611
Ian Campbell6efe3692014-05-05 11:52:26 +0100612#ifdef CONFIG_SPL_BUILD
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800613
614static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
615{
616 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
617
618 if (spl == INVALID_SPL_HEADER)
619 return;
620
621 /* Promote the header version for U-Boot proper, if needed. */
622 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
623 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
624
625 spl->dram_size = dram_size >> 20;
626}
627
Ian Campbell6efe3692014-05-05 11:52:26 +0100628void sunxi_board_init(void)
629{
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200630 int power_failed = 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100631
Jelle van der Waa3f3a3092016-02-23 18:47:19 +0100632#ifdef CONFIG_SY8106A_POWER
633 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
634#endif
635
vishnupatekar1895dfd2015-11-29 01:07:22 +0800636#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800637 defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
638 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200639 power_failed = axp_init();
640
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800641#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
642 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200643 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede1f247362014-06-13 22:55:51 +0200644#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200645 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
646 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
vishnupatekar1895dfd2015-11-29 01:07:22 +0800647#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200648 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200649#endif
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800650#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
651 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200652 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200653#endif
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200654
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800655#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
656 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200657 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
658#endif
659 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Chen-Yu Tsaic05aa392016-01-12 14:42:40 +0800660#if !defined(CONFIG_AXP152_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200661 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
662#endif
663#ifdef CONFIG_AXP209_POWER
664 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
665#endif
666
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800667#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
668 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800669 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
670 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800671#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800672 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
673 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800674#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200675 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
676 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
677 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
678#endif
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800679
680#ifdef CONFIG_AXP818_POWER
681 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
682 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
683 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800684#endif
685
686#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai0e3efd32016-05-02 10:28:12 +0800687 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800688#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200689#endif
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000690 printf("DRAM:");
691 gd->ram_size = sunxi_dram_init();
692 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
693 if (!gd->ram_size)
694 hang();
695
696 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800697
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200698 /*
699 * Only clock up the CPU to full speed if we are reasonably
700 * assured it's being powered with suitable core voltage
701 */
702 if (!power_failed)
Iain Paton630df142015-03-28 10:26:38 +0000703 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200704 else
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000705 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbell6efe3692014-05-05 11:52:26 +0100706}
707#endif
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200708
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100709#ifdef CONFIG_USB_GADGET
710int g_dnl_board_usb_cable_connected(void)
711{
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530712 struct udevice *dev;
713 struct phy phy;
714 int ret;
715
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100716 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530717 if (ret) {
718 pr_err("%s: Cannot find USB device\n", __func__);
719 return ret;
720 }
721
722 ret = generic_phy_get_by_name(dev, "usb", &phy);
723 if (ret) {
724 pr_err("failed to get %s USB PHY\n", dev->name);
725 return ret;
726 }
727
728 ret = generic_phy_init(&phy);
729 if (ret) {
Patrick Delaunay287e33c2020-07-03 17:36:41 +0200730 pr_debug("failed to init %s USB PHY\n", dev->name);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530731 return ret;
732 }
733
734 ret = sun4i_usb_phy_vbus_detect(&phy);
735 if (ret == 1) {
736 pr_err("A charger is plugged into the OTG\n");
737 return -ENODEV;
738 }
739
740 return ret;
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100741}
742#endif
743
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100744#ifdef CONFIG_SERIAL_TAG
745void get_board_serial(struct tag_serialnr *serialnr)
746{
747 char *serial_string;
748 unsigned long long serial;
749
Simon Glass64b723f2017-08-03 12:22:12 -0600750 serial_string = env_get("serial#");
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100751
752 if (serial_string) {
753 serial = simple_strtoull(serial_string, NULL, 16);
754
755 serialnr->high = (unsigned int) (serial >> 32);
756 serialnr->low = (unsigned int) (serial & 0xffffffff);
757 } else {
758 serialnr->high = 0;
759 serialnr->low = 0;
760 }
761}
762#endif
763
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200764/*
765 * Check the SPL header for the "sunxi" variant. If found: parse values
766 * that might have been passed by the loader ("fel" utility), and update
767 * the environment accordingly.
768 */
769static void parse_spl_header(const uint32_t spl_addr)
770{
Andre Przywara14a25392018-10-25 17:23:04 +0800771 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200772
Andre Przywara14a25392018-10-25 17:23:04 +0800773 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200774 return;
Andre Przywara14a25392018-10-25 17:23:04 +0800775
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200776 if (!spl->fel_script_address)
777 return;
778
779 if (spl->fel_uEnv_length != 0) {
780 /*
781 * data is expected in uEnv.txt compatible format, so "env
782 * import -t" the string(s) at fel_script_address right away.
783 */
Andre Przywaraac4e6732016-09-05 01:32:41 +0100784 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200785 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
786 return;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200787 }
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200788 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass4d949a22017-08-03 12:22:10 -0600789 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200790}
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200791
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200792/*
793 * Note this function gets called multiple times.
794 * It must not make any changes to env variables which already exist.
795 */
796static void setup_environment(const void *fdt)
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200797{
Paul Kocialkowski92935942015-03-28 18:35:35 +0100798 char serial_string[17] = { 0 };
Hans de Goede11d70982014-11-26 00:04:24 +0100799 unsigned int sid[4];
Paul Kocialkowski92935942015-03-28 18:35:35 +0100800 uint8_t mac_addr[6];
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200801 char ethaddr[16];
802 int i, ret;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200803
Paul Kocialkowski92935942015-03-28 18:35:35 +0100804 ret = sunxi_get_sid(sid);
Hans de Goedee5fe5482016-07-29 11:47:03 +0200805 if (ret == 0 && sid[0] != 0) {
806 /*
807 * The single words 1 - 3 of the SID have quite a few bits
808 * which are the same on many models, so we take a crc32
809 * of all 3 words, to get a more unique value.
810 *
811 * Note we only do this on newer SoCs as we cannot change
812 * the algorithm on older SoCs since those have been using
813 * fixed mac-addresses based on only using word 3 for a
814 * long time and changing a fixed mac-address with an
815 * u-boot update is not good.
816 */
817#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
818 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
819 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
820 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
821#endif
822
Hans de Goedeabca8432016-07-27 17:58:06 +0200823 /* Ensure the NIC specific bytes of the mac are not all 0 */
824 if ((sid[3] & 0xffffff) == 0)
825 sid[3] |= 0x800000;
826
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200827 for (i = 0; i < 4; i++) {
828 sprintf(ethaddr, "ethernet%d", i);
829 if (!fdt_get_alias(fdt, ethaddr))
830 continue;
831
832 if (i == 0)
833 strcpy(ethaddr, "ethaddr");
834 else
835 sprintf(ethaddr, "eth%daddr", i);
836
Simon Glass64b723f2017-08-03 12:22:12 -0600837 if (env_get(ethaddr))
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200838 continue;
839
Paul Kocialkowski92935942015-03-28 18:35:35 +0100840 /* Non OUI / registered MAC address */
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200841 mac_addr[0] = (i << 4) | 0x02;
Paul Kocialkowski92935942015-03-28 18:35:35 +0100842 mac_addr[1] = (sid[0] >> 0) & 0xff;
843 mac_addr[2] = (sid[3] >> 24) & 0xff;
844 mac_addr[3] = (sid[3] >> 16) & 0xff;
845 mac_addr[4] = (sid[3] >> 8) & 0xff;
846 mac_addr[5] = (sid[3] >> 0) & 0xff;
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200847
Simon Glass8551d552017-08-03 12:22:11 -0600848 eth_env_set_enetaddr(ethaddr, mac_addr);
Paul Kocialkowski92935942015-03-28 18:35:35 +0100849 }
850
Simon Glass64b723f2017-08-03 12:22:12 -0600851 if (!env_get("serial#")) {
Paul Kocialkowski92935942015-03-28 18:35:35 +0100852 snprintf(serial_string, sizeof(serial_string),
853 "%08x%08x", sid[0], sid[3]);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200854
Simon Glass6a38e412017-08-03 12:22:09 -0600855 env_set("serial#", serial_string);
Paul Kocialkowski92935942015-03-28 18:35:35 +0100856 }
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200857 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200858}
859
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200860int misc_init_r(void)
861{
Samuel Holland87f940a2020-10-24 10:21:54 -0500862 const char *spl_dt_name;
Maxime Ripardae56d972017-08-23 10:08:29 +0200863 uint boot;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200864
Simon Glass6a38e412017-08-03 12:22:09 -0600865 env_set("fel_booted", NULL);
866 env_set("fel_scriptaddr", NULL);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200867 env_set("mmc_bootdev", NULL);
Maxime Ripardae56d972017-08-23 10:08:29 +0200868
869 boot = sunxi_get_boot_device();
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200870 /* determine if we are running in FEL mode */
Maxime Ripardae56d972017-08-23 10:08:29 +0200871 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass6a38e412017-08-03 12:22:09 -0600872 env_set("fel_booted", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200873 parse_spl_header(SPL_ADDR);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200874 /* or if we booted from MMC, and which one */
875 } else if (boot == BOOT_DEVICE_MMC1) {
876 env_set("mmc_bootdev", "0");
877 } else if (boot == BOOT_DEVICE_MMC2) {
878 env_set("mmc_bootdev", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200879 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200880
Samuel Holland87f940a2020-10-24 10:21:54 -0500881 /* Set fdtfile to match the FIT configuration chosen in SPL. */
882 spl_dt_name = get_spl_dt_name();
883 if (spl_dt_name) {
884 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
885 char str[64];
886
887 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
888 env_set("fdtfile", str);
889 }
890
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200891 setup_environment(gd->fdt_blob);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200892
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800893#ifdef CONFIG_USB_ETHER
Maxime Ripardf54aba32017-09-06 22:25:03 +0200894 usb_ether_init();
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800895#endif
Maxime Ripardf54aba32017-09-06 22:25:03 +0200896
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200897 return 0;
898}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200899
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900900int ft_board_setup(void *blob, struct bd_info *bd)
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200901{
Hans de Goede48a234a2016-03-22 22:51:52 +0100902 int __maybe_unused r;
903
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200904 /*
905 * Call setup_environment again in case the boot fdt has
906 * ethernet aliases the u-boot copy does not have.
907 */
908 setup_environment(blob);
909
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200910#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goede48a234a2016-03-22 22:51:52 +0100911 r = sunxi_simplefb_setup(blob);
912 if (r)
913 return r;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200914#endif
Hans de Goede48a234a2016-03-22 22:51:52 +0100915 return 0;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200916}
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100917
918#ifdef CONFIG_SPL_LOAD_FIT
Samuel Holland64933e92020-10-24 10:21:53 -0500919
920static void set_spl_dt_name(const char *name)
921{
922 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
923
924 if (spl == INVALID_SPL_HEADER)
925 return;
926
927 /* Promote the header version for U-Boot proper, if needed. */
928 if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
929 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
930
931 strcpy((char *)&spl->string_pool, name);
932 spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
933}
934
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100935int board_fit_config_name_match(const char *name)
936{
Samuel Hollandba44e942020-10-24 10:21:50 -0500937 const char *best_dt_name = get_spl_dt_name();
Samuel Holland64933e92020-10-24 10:21:53 -0500938 int ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100939
940#ifdef CONFIG_DEFAULT_DEVICE_TREE
Samuel Hollandba44e942020-10-24 10:21:50 -0500941 if (best_dt_name == NULL)
Samuel Holland37b86202020-10-24 10:21:49 -0500942 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100943#endif
944
Samuel Hollandba44e942020-10-24 10:21:50 -0500945 if (best_dt_name == NULL) {
946 /* No DT name was provided, so accept the first config. */
947 return 0;
948 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800949#ifdef CONFIG_PINE64_DT_SELECTION
Samuel Hollandf2352dd2020-10-24 10:21:51 -0500950 if (strstr(best_dt_name, "-pine64-plus")) {
951 /* Differentiate the Pine A64 boards by their DRAM size. */
952 if ((gd->ram_size == 512 * 1024 * 1024))
953 best_dt_name = "sun50i-a64-pine64";
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100954 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800955#endif
Samuel Holland9c7cefc2020-10-24 10:21:52 -0500956#ifdef CONFIG_PINEPHONE_DT_SELECTION
957 if (strstr(best_dt_name, "-pinephone")) {
958 /* Differentiate the PinePhone revisions by GPIO inputs. */
959 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
960 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
961 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
962 udelay(100);
963
964 /* PL6 is pulled low by the modem on v1.2. */
965 if (gpio_get_value(SUNXI_GPL(6)) == 0)
966 best_dt_name = "sun50i-a64-pinephone-1.2";
967 else
968 best_dt_name = "sun50i-a64-pinephone-1.1";
969
970 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
971 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
972 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
973 }
974#endif
975
Samuel Holland64933e92020-10-24 10:21:53 -0500976 ret = strcmp(name, best_dt_name);
977
978 /*
979 * If one of the FIT configurations matches the most accurate DT name,
980 * update the SPL header to provide that DT name to U-Boot proper.
981 */
982 if (ret == 0)
983 set_spl_dt_name(best_dt_name);
984
985 return ret;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100986}
987#endif