blob: f8d5dba91c8a04ce5875a02bbb58f68148fbe097 [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>
Hans de Goedea146c502016-07-09 09:56:56 +020030#include <asm/arch/spl.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070031#include <u-boot/crc.h>
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020032#ifndef CONFIG_ARM64
33#include <asm/armv7.h>
34#endif
Hans de Goeded9d05652015-04-23 23:23:50 +020035#include <asm/gpio.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020036#include <asm/io.h>
Philipp Tomsich36b26d12018-11-25 19:22:18 +010037#include <u-boot/crc.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060038#include <env_internal.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090039#include <linux/libfdt.h>
Hans de Goede5ed52f62015-08-15 11:55:26 +020040#include <nand.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020041#include <net.h>
Maxime Ripardae56d972017-08-23 10:08:29 +020042#include <spl.h>
Jelle van der Waa3f3a3092016-02-23 18:47:19 +010043#include <sy8106a.h>
Simon Glassd9a766f2017-05-17 08:23:00 -060044#include <asm/setup.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010045
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010046#if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD)
47/* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */
48int soft_i2c_gpio_sda;
49int soft_i2c_gpio_scl;
Hans de Goeded9d05652015-04-23 23:23:50 +020050
51static int soft_i2c_board_init(void)
52{
53 int ret;
54
55 soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA);
56 if (soft_i2c_gpio_sda < 0) {
57 printf("Error invalid soft i2c sda pin: '%s', err %d\n",
58 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda);
59 return soft_i2c_gpio_sda;
60 }
61 ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda");
62 if (ret) {
63 printf("Error requesting soft i2c sda pin: '%s', err %d\n",
64 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret);
65 return ret;
66 }
67
68 soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL);
69 if (soft_i2c_gpio_scl < 0) {
70 printf("Error invalid soft i2c scl pin: '%s', err %d\n",
71 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl);
72 return soft_i2c_gpio_scl;
73 }
74 ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl");
75 if (ret) {
76 printf("Error requesting soft i2c scl pin: '%s', err %d\n",
77 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret);
78 return ret;
79 }
80
81 return 0;
82}
83#else
84static int soft_i2c_board_init(void) { return 0; }
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010085#endif
86
Ian Campbell6efe3692014-05-05 11:52:26 +010087DECLARE_GLOBAL_DATA_PTR;
88
Jernej Skrabec07da8802017-04-27 00:03:35 +020089void i2c_init_board(void)
90{
91#ifdef CONFIG_I2C0_ENABLE
92#if defined(CONFIG_MACH_SUN4I) || \
93 defined(CONFIG_MACH_SUN5I) || \
94 defined(CONFIG_MACH_SUN7I) || \
95 defined(CONFIG_MACH_SUN8I_R40)
96 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
97 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
98 clock_twi_onoff(0, 1);
99#elif defined(CONFIG_MACH_SUN6I)
100 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
101 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
102 clock_twi_onoff(0, 1);
103#elif defined(CONFIG_MACH_SUN8I)
104 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
105 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
106 clock_twi_onoff(0, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200107#elif defined(CONFIG_MACH_SUN50I)
108 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
109 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
110 clock_twi_onoff(0, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200111#endif
112#endif
113
114#ifdef CONFIG_I2C1_ENABLE
115#if defined(CONFIG_MACH_SUN4I) || \
116 defined(CONFIG_MACH_SUN7I) || \
117 defined(CONFIG_MACH_SUN8I_R40)
118 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
119 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
120 clock_twi_onoff(1, 1);
121#elif defined(CONFIG_MACH_SUN5I)
122 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
123 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
124 clock_twi_onoff(1, 1);
125#elif defined(CONFIG_MACH_SUN6I)
126 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
127 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
128 clock_twi_onoff(1, 1);
129#elif defined(CONFIG_MACH_SUN8I)
130 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
131 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
132 clock_twi_onoff(1, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200133#elif defined(CONFIG_MACH_SUN50I)
134 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
135 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
136 clock_twi_onoff(1, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200137#endif
138#endif
139
140#ifdef CONFIG_I2C2_ENABLE
141#if defined(CONFIG_MACH_SUN4I) || \
142 defined(CONFIG_MACH_SUN7I) || \
143 defined(CONFIG_MACH_SUN8I_R40)
144 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
145 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
146 clock_twi_onoff(2, 1);
147#elif defined(CONFIG_MACH_SUN5I)
148 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
149 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
150 clock_twi_onoff(2, 1);
151#elif defined(CONFIG_MACH_SUN6I)
152 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
153 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
154 clock_twi_onoff(2, 1);
155#elif defined(CONFIG_MACH_SUN8I)
156 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
157 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
158 clock_twi_onoff(2, 1);
Stefan Mavrodievcabe9922019-01-08 12:04:30 +0200159#elif defined(CONFIG_MACH_SUN50I)
160 sunxi_gpio_set_cfgpin(SUNXI_GPE(14), SUN50I_GPE_TWI2);
161 sunxi_gpio_set_cfgpin(SUNXI_GPE(15), SUN50I_GPE_TWI2);
162 clock_twi_onoff(2, 1);
Jernej Skrabec07da8802017-04-27 00:03:35 +0200163#endif
164#endif
165
166#ifdef CONFIG_I2C3_ENABLE
167#if defined(CONFIG_MACH_SUN6I)
168 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
169 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
170 clock_twi_onoff(3, 1);
171#elif defined(CONFIG_MACH_SUN7I) || \
172 defined(CONFIG_MACH_SUN8I_R40)
173 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
174 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
175 clock_twi_onoff(3, 1);
176#endif
177#endif
178
179#ifdef CONFIG_I2C4_ENABLE
180#if defined(CONFIG_MACH_SUN7I) || \
181 defined(CONFIG_MACH_SUN8I_R40)
182 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
183 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
184 clock_twi_onoff(4, 1);
185#endif
186#endif
187
188#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800189#ifdef CONFIG_MACH_SUN50I
190 clock_twi_onoff(5, 1);
191 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
192 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
193#else
Jernej Skrabec07da8802017-04-27 00:03:35 +0200194 clock_twi_onoff(5, 1);
195 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
196 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
197#endif
Vasily Khoruzhick6f4c3442018-11-05 20:24:30 -0800198#endif
Jernej Skrabec07da8802017-04-27 00:03:35 +0200199}
200
Maxime Ripard9ba2ac72018-01-23 21:17:03 +0100201#if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT)
202enum env_location env_get_location(enum env_operation op, int prio)
203{
204 switch (prio) {
205 case 0:
206 return ENVL_FAT;
207
208 case 1:
209 return ENVL_MMC;
210
211 default:
212 return ENVL_UNKNOWN;
213 }
214}
215#endif
216
Andre Przywarad7cea362019-01-29 15:54:14 +0000217#ifdef CONFIG_DM_MMC
218static void mmc_pinmux_setup(int sdc);
219#endif
220
Ian Campbell6efe3692014-05-05 11:52:26 +0100221/* add board specific code here */
222int board_init(void)
223{
Mylène Josserand147c6062017-04-02 12:59:10 +0200224 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbell6efe3692014-05-05 11:52:26 +0100225
226 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
227
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200228#ifndef CONFIG_ARM64
Ian Campbell6efe3692014-05-05 11:52:26 +0100229 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
230 debug("id_pfr1: 0x%08x\n", id_pfr1);
231 /* Generic Timer Extension available? */
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200232 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
233 uint32_t freq;
234
Ian Campbell6efe3692014-05-05 11:52:26 +0100235 debug("Setting CNTFRQ\n");
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200236
237 /*
238 * CNTFRQ is a secure register, so we will crash if we try to
239 * write this from the non-secure world (read is OK, though).
240 * In case some bootcode has already set the correct value,
241 * we avoid the risk of writing to it.
242 */
243 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Andre Przywara70c78932017-02-16 01:20:19 +0000244 if (freq != COUNTER_FREQUENCY) {
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200245 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Andre Przywara70c78932017-02-16 01:20:19 +0000246 freq, COUNTER_FREQUENCY);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200247#ifdef CONFIG_NON_SECURE
248 printf("arch timer frequency is wrong, but cannot adjust it\n");
249#else
250 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Andre Przywara70c78932017-02-16 01:20:19 +0000251 : : "r"(COUNTER_FREQUENCY));
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200252#endif
253 }
Ian Campbell6efe3692014-05-05 11:52:26 +0100254 }
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200255#endif /* !CONFIG_ARM64 */
Ian Campbell6efe3692014-05-05 11:52:26 +0100256
Hans de Goede3ae1d132015-04-25 17:25:14 +0200257 ret = axp_gpio_init();
258 if (ret)
259 return ret;
260
Hans de Goede9c34c3e2016-03-22 20:10:30 +0100261#ifdef CONFIG_SATAPWR
Mylène Josserand628426a2017-04-02 12:59:09 +0200262 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
263 gpio_request(satapwr_pin, "satapwr");
264 gpio_direction_output(satapwr_pin, 1);
Werner Böllmanne58f8302017-11-10 19:14:20 +0530265 /* Give attached sata device time to power-up to avoid link timeouts */
266 mdelay(500);
Hans de Goede9c34c3e2016-03-22 20:10:30 +0100267#endif
Hans de Goede42cbbe32016-03-17 13:53:03 +0100268#ifdef CONFIG_MACPWR
Mylène Josserand147c6062017-04-02 12:59:10 +0200269 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
270 gpio_request(macpwr_pin, "macpwr");
271 gpio_direction_output(macpwr_pin, 1);
Hans de Goede42cbbe32016-03-17 13:53:03 +0100272#endif
273
Jernej Skrabec9220d502017-04-27 00:03:36 +0200274#ifdef CONFIG_DM_I2C
275 /*
276 * Temporary workaround for enabling I2C clocks until proper sunxi DM
277 * clk, reset and pinctrl drivers land.
278 */
279 i2c_init_board();
280#endif
Andre Przywarad7cea362019-01-29 15:54:14 +0000281
282#ifdef CONFIG_DM_MMC
283 /*
284 * Temporary workaround for enabling MMC clocks until a sunxi DM
285 * pinctrl driver lands.
286 */
287 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
288#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
289 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
290#endif
291#endif /* CONFIG_DM_MMC */
Jernej Skrabec9220d502017-04-27 00:03:36 +0200292
Hans de Goeded9d05652015-04-23 23:23:50 +0200293 /* Uses dm gpio code so do this here and not in i2c_init_board() */
294 return soft_i2c_board_init();
Ian Campbell6efe3692014-05-05 11:52:26 +0100295}
296
Andre Przywara14a25392018-10-25 17:23:04 +0800297/*
298 * On older SoCs the SPL is actually at address zero, so using NULL as
299 * an error value does not work.
300 */
301#define INVALID_SPL_HEADER ((void *)~0UL)
302
303static struct boot_file_head * get_spl_header(uint8_t req_version)
304{
305 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
306 uint8_t spl_header_version = spl->spl_signature[3];
307
308 /* Is there really the SPL header (still) there? */
309 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
310 return INVALID_SPL_HEADER;
311
312 if (spl_header_version < req_version) {
313 printf("sunxi SPL version mismatch: expected %u, got %u\n",
314 req_version, spl_header_version);
315 return INVALID_SPL_HEADER;
316 }
317
318 return spl;
319}
320
Ian Campbell6efe3692014-05-05 11:52:26 +0100321int dram_init(void)
322{
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800323 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
324
325 if (spl == INVALID_SPL_HEADER)
326 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
327 PHYS_SDRAM_0_SIZE);
328 else
329 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
330
331 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
332 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbell6efe3692014-05-05 11:52:26 +0100333
334 return 0;
335}
336
Boris Brezillon57f20382016-06-15 21:09:23 +0200337#if defined(CONFIG_NAND_SUNXI)
Karol Gugala7bea8932015-07-23 14:33:01 +0200338static void nand_pinmux_setup(void)
339{
340 unsigned int pin;
Karol Gugala7bea8932015-07-23 14:33:01 +0200341
Hans de Goeded2236782015-08-15 13:17:49 +0200342 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugala7bea8932015-07-23 14:33:01 +0200343 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
344
Hans de Goeded2236782015-08-15 13:17:49 +0200345#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
346 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
347 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
348#endif
349 /* sun4i / sun7i do have a PC23, but it is not used for nand,
350 * only sun7i has a PC24 */
351#ifdef CONFIG_MACH_SUN7I
Karol Gugala7bea8932015-07-23 14:33:01 +0200352 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goeded2236782015-08-15 13:17:49 +0200353#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200354}
355
356static void nand_clock_setup(void)
357{
358 struct sunxi_ccm_reg *const ccm =
359 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedee5561a82015-08-15 11:58:03 +0200360
Karol Gugala7bea8932015-07-23 14:33:01 +0200361 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalebeeb802018-02-28 20:51:53 +0100362#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
363 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
364 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
365#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200366 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
367}
Hans de Goede5ed52f62015-08-15 11:55:26 +0200368
369void board_nand_init(void)
370{
371 nand_pinmux_setup();
372 nand_clock_setup();
Boris Brezillon57f20382016-06-15 21:09:23 +0200373#ifndef CONFIG_SPL_BUILD
374 sunxi_nand_init();
375#endif
Hans de Goede5ed52f62015-08-15 11:55:26 +0200376}
Karol Gugala7bea8932015-07-23 14:33:01 +0200377#endif
378
Masahiro Yamada0a780172017-05-09 20:31:39 +0900379#ifdef CONFIG_MMC
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100380static void mmc_pinmux_setup(int sdc)
381{
382 unsigned int pin;
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100383 __maybe_unused int pins;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100384
385 switch (sdc) {
386 case 0:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100387 /* SDC0: PF0-PF5 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100388 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100389 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100390 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
391 sunxi_gpio_set_drv(pin, 2);
392 }
393 break;
394
395 case 1:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100396 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS);
397
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800398#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
399 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100400 if (pins == SUNXI_GPIO_H) {
401 /* SDC1: PH22-PH-27 */
402 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
403 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
404 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
405 sunxi_gpio_set_drv(pin, 2);
406 }
407 } else {
408 /* SDC1: PG0-PG5 */
409 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
410 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
411 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
412 sunxi_gpio_set_drv(pin, 2);
413 }
414 }
415#elif defined(CONFIG_MACH_SUN5I)
416 /* SDC1: PG3-PG8 */
Hans de Goede4dccfd42014-10-03 16:44:57 +0200417 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100418 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100419 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
420 sunxi_gpio_set_drv(pin, 2);
421 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100422#elif defined(CONFIG_MACH_SUN6I)
423 /* SDC1: PG0-PG5 */
424 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
425 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
426 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
427 sunxi_gpio_set_drv(pin, 2);
428 }
429#elif defined(CONFIG_MACH_SUN8I)
430 if (pins == SUNXI_GPIO_D) {
431 /* SDC1: PD2-PD7 */
432 for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) {
433 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1);
434 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
435 sunxi_gpio_set_drv(pin, 2);
436 }
437 } else {
438 /* SDC1: PG0-PG5 */
439 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
440 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
441 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
442 sunxi_gpio_set_drv(pin, 2);
443 }
444 }
445#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100446 break;
447
448 case 2:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100449 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS);
450
451#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
452 /* SDC2: PC6-PC11 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100453 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100454 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100455 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
456 sunxi_gpio_set_drv(pin, 2);
457 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100458#elif defined(CONFIG_MACH_SUN5I)
459 if (pins == SUNXI_GPIO_E) {
460 /* SDC2: PE4-PE9 */
461 for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) {
462 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2);
463 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
464 sunxi_gpio_set_drv(pin, 2);
465 }
466 } else {
467 /* SDC2: PC6-PC15 */
468 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
469 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
470 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
471 sunxi_gpio_set_drv(pin, 2);
472 }
473 }
474#elif defined(CONFIG_MACH_SUN6I)
475 if (pins == SUNXI_GPIO_A) {
476 /* SDC2: PA9-PA14 */
477 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
478 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2);
479 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
480 sunxi_gpio_set_drv(pin, 2);
481 }
482 } else {
483 /* SDC2: PC6-PC15, PC24 */
484 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
485 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
486 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
487 sunxi_gpio_set_drv(pin, 2);
488 }
489
490 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
491 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
492 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
493 }
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800494#elif defined(CONFIG_MACH_SUN8I_R40)
495 /* SDC2: PC6-PC15, PC24 */
496 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
497 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
498 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
499 sunxi_gpio_set_drv(pin, 2);
500 }
501
502 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
503 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
504 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200505#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100506 /* SDC2: PC5-PC6, PC8-PC16 */
507 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
508 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
509 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
510 sunxi_gpio_set_drv(pin, 2);
511 }
512
513 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); 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 }
Icenowy Zhenga838a152018-07-21 16:20:29 +0800518#elif defined(CONFIG_MACH_SUN50I_H6)
519 /* SDC2: PC4-PC14 */
520 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
521 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
522 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
523 sunxi_gpio_set_drv(pin, 2);
524 }
Philipp Tomsicha0c7c712016-10-28 18:21:33 +0800525#elif defined(CONFIG_MACH_SUN9I)
526 /* SDC2: PC6-PC16 */
527 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
528 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
529 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
530 sunxi_gpio_set_drv(pin, 2);
531 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100532#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100533 break;
534
535 case 3:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100536 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS);
537
Chen-Yu Tsai111bc592016-11-30 16:28:34 +0800538#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
539 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100540 /* SDC3: PI4-PI9 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100541 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100542 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100543 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
544 sunxi_gpio_set_drv(pin, 2);
545 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100546#elif defined(CONFIG_MACH_SUN6I)
547 if (pins == SUNXI_GPIO_A) {
548 /* SDC3: PA9-PA14 */
549 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
550 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3);
551 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
552 sunxi_gpio_set_drv(pin, 2);
553 }
554 } else {
555 /* SDC3: PC6-PC15, PC24 */
556 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
557 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
558 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
559 sunxi_gpio_set_drv(pin, 2);
560 }
561
562 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
563 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
564 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
565 }
566#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100567 break;
568
569 default:
570 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
571 break;
572 }
573}
574
575int board_mmc_init(bd_t *bis)
576{
Hans de Goede63deaa82014-10-02 21:13:54 +0200577 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goede63deaa82014-10-02 21:13:54 +0200578
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100579 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goede63deaa82014-10-02 21:13:54 +0200580 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
581 if (!mmc0)
582 return -1;
583
Hans de Goedeaf593e42014-10-02 20:43:50 +0200584#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100585 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goede63deaa82014-10-02 21:13:54 +0200586 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
587 if (!mmc1)
588 return -1;
589#endif
590
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100591 return 0;
592}
593#endif
594
Ian Campbell6efe3692014-05-05 11:52:26 +0100595#ifdef CONFIG_SPL_BUILD
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800596
597static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
598{
599 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
600
601 if (spl == INVALID_SPL_HEADER)
602 return;
603
604 /* Promote the header version for U-Boot proper, if needed. */
605 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
606 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
607
608 spl->dram_size = dram_size >> 20;
609}
610
Ian Campbell6efe3692014-05-05 11:52:26 +0100611void sunxi_board_init(void)
612{
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200613 int power_failed = 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100614
Jelle van der Waa3f3a3092016-02-23 18:47:19 +0100615#ifdef CONFIG_SY8106A_POWER
616 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
617#endif
618
vishnupatekar1895dfd2015-11-29 01:07:22 +0800619#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800620 defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
621 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200622 power_failed = axp_init();
623
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800624#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
625 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200626 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede1f247362014-06-13 22:55:51 +0200627#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200628 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
629 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
vishnupatekar1895dfd2015-11-29 01:07:22 +0800630#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200631 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200632#endif
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800633#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
634 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200635 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200636#endif
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200637
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800638#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
639 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200640 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
641#endif
642 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Chen-Yu Tsaic05aa392016-01-12 14:42:40 +0800643#if !defined(CONFIG_AXP152_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200644 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
645#endif
646#ifdef CONFIG_AXP209_POWER
647 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
648#endif
649
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800650#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
651 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800652 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
653 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800654#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800655 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
656 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800657#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200658 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
659 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
660 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
661#endif
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800662
663#ifdef CONFIG_AXP818_POWER
664 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
665 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
666 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800667#endif
668
669#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai0e3efd32016-05-02 10:28:12 +0800670 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800671#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200672#endif
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000673 printf("DRAM:");
674 gd->ram_size = sunxi_dram_init();
675 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
676 if (!gd->ram_size)
677 hang();
678
679 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara08ee1ba2018-10-25 17:23:07 +0800680
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200681 /*
682 * Only clock up the CPU to full speed if we are reasonably
683 * assured it's being powered with suitable core voltage
684 */
685 if (!power_failed)
Iain Paton630df142015-03-28 10:26:38 +0000686 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200687 else
From: Karl Palsson0a0bcde2018-12-19 13:00:39 +0000688 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbell6efe3692014-05-05 11:52:26 +0100689}
690#endif
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200691
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100692#ifdef CONFIG_USB_GADGET
693int g_dnl_board_usb_cable_connected(void)
694{
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530695 struct udevice *dev;
696 struct phy phy;
697 int ret;
698
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +0100699 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki73a3ecf2018-05-07 13:03:36 +0530700 if (ret) {
701 pr_err("%s: Cannot find USB device\n", __func__);
702 return ret;
703 }
704
705 ret = generic_phy_get_by_name(dev, "usb", &phy);
706 if (ret) {
707 pr_err("failed to get %s USB PHY\n", dev->name);
708 return ret;
709 }
710
711 ret = generic_phy_init(&phy);
712 if (ret) {
713 pr_err("failed to init %s USB PHY\n", dev->name);
714 return ret;
715 }
716
717 ret = sun4i_usb_phy_vbus_detect(&phy);
718 if (ret == 1) {
719 pr_err("A charger is plugged into the OTG\n");
720 return -ENODEV;
721 }
722
723 return ret;
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100724}
725#endif
726
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100727#ifdef CONFIG_SERIAL_TAG
728void get_board_serial(struct tag_serialnr *serialnr)
729{
730 char *serial_string;
731 unsigned long long serial;
732
Simon Glass64b723f2017-08-03 12:22:12 -0600733 serial_string = env_get("serial#");
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100734
735 if (serial_string) {
736 serial = simple_strtoull(serial_string, NULL, 16);
737
738 serialnr->high = (unsigned int) (serial >> 32);
739 serialnr->low = (unsigned int) (serial & 0xffffffff);
740 } else {
741 serialnr->high = 0;
742 serialnr->low = 0;
743 }
744}
745#endif
746
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200747/*
748 * Check the SPL header for the "sunxi" variant. If found: parse values
749 * that might have been passed by the loader ("fel" utility), and update
750 * the environment accordingly.
751 */
752static void parse_spl_header(const uint32_t spl_addr)
753{
Andre Przywara14a25392018-10-25 17:23:04 +0800754 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200755
Andre Przywara14a25392018-10-25 17:23:04 +0800756 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200757 return;
Andre Przywara14a25392018-10-25 17:23:04 +0800758
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200759 if (!spl->fel_script_address)
760 return;
761
762 if (spl->fel_uEnv_length != 0) {
763 /*
764 * data is expected in uEnv.txt compatible format, so "env
765 * import -t" the string(s) at fel_script_address right away.
766 */
Andre Przywaraac4e6732016-09-05 01:32:41 +0100767 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200768 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
769 return;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200770 }
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200771 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass4d949a22017-08-03 12:22:10 -0600772 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200773}
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200774
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200775/*
776 * Note this function gets called multiple times.
777 * It must not make any changes to env variables which already exist.
778 */
779static void setup_environment(const void *fdt)
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200780{
Paul Kocialkowski92935942015-03-28 18:35:35 +0100781 char serial_string[17] = { 0 };
Hans de Goede11d70982014-11-26 00:04:24 +0100782 unsigned int sid[4];
Paul Kocialkowski92935942015-03-28 18:35:35 +0100783 uint8_t mac_addr[6];
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200784 char ethaddr[16];
785 int i, ret;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200786
Paul Kocialkowski92935942015-03-28 18:35:35 +0100787 ret = sunxi_get_sid(sid);
Hans de Goedee5fe5482016-07-29 11:47:03 +0200788 if (ret == 0 && sid[0] != 0) {
789 /*
790 * The single words 1 - 3 of the SID have quite a few bits
791 * which are the same on many models, so we take a crc32
792 * of all 3 words, to get a more unique value.
793 *
794 * Note we only do this on newer SoCs as we cannot change
795 * the algorithm on older SoCs since those have been using
796 * fixed mac-addresses based on only using word 3 for a
797 * long time and changing a fixed mac-address with an
798 * u-boot update is not good.
799 */
800#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
801 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
802 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
803 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
804#endif
805
Hans de Goedeabca8432016-07-27 17:58:06 +0200806 /* Ensure the NIC specific bytes of the mac are not all 0 */
807 if ((sid[3] & 0xffffff) == 0)
808 sid[3] |= 0x800000;
809
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200810 for (i = 0; i < 4; i++) {
811 sprintf(ethaddr, "ethernet%d", i);
812 if (!fdt_get_alias(fdt, ethaddr))
813 continue;
814
815 if (i == 0)
816 strcpy(ethaddr, "ethaddr");
817 else
818 sprintf(ethaddr, "eth%daddr", i);
819
Simon Glass64b723f2017-08-03 12:22:12 -0600820 if (env_get(ethaddr))
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200821 continue;
822
Paul Kocialkowski92935942015-03-28 18:35:35 +0100823 /* Non OUI / registered MAC address */
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200824 mac_addr[0] = (i << 4) | 0x02;
Paul Kocialkowski92935942015-03-28 18:35:35 +0100825 mac_addr[1] = (sid[0] >> 0) & 0xff;
826 mac_addr[2] = (sid[3] >> 24) & 0xff;
827 mac_addr[3] = (sid[3] >> 16) & 0xff;
828 mac_addr[4] = (sid[3] >> 8) & 0xff;
829 mac_addr[5] = (sid[3] >> 0) & 0xff;
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200830
Simon Glass8551d552017-08-03 12:22:11 -0600831 eth_env_set_enetaddr(ethaddr, mac_addr);
Paul Kocialkowski92935942015-03-28 18:35:35 +0100832 }
833
Simon Glass64b723f2017-08-03 12:22:12 -0600834 if (!env_get("serial#")) {
Paul Kocialkowski92935942015-03-28 18:35:35 +0100835 snprintf(serial_string, sizeof(serial_string),
836 "%08x%08x", sid[0], sid[3]);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200837
Simon Glass6a38e412017-08-03 12:22:09 -0600838 env_set("serial#", serial_string);
Paul Kocialkowski92935942015-03-28 18:35:35 +0100839 }
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200840 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200841}
842
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200843int misc_init_r(void)
844{
Maxime Ripardae56d972017-08-23 10:08:29 +0200845 uint boot;
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200846
Simon Glass6a38e412017-08-03 12:22:09 -0600847 env_set("fel_booted", NULL);
848 env_set("fel_scriptaddr", NULL);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200849 env_set("mmc_bootdev", NULL);
Maxime Ripardae56d972017-08-23 10:08:29 +0200850
851 boot = sunxi_get_boot_device();
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200852 /* determine if we are running in FEL mode */
Maxime Ripardae56d972017-08-23 10:08:29 +0200853 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass6a38e412017-08-03 12:22:09 -0600854 env_set("fel_booted", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200855 parse_spl_header(SPL_ADDR);
Maxime Ripard65cefba2017-08-23 10:12:22 +0200856 /* or if we booted from MMC, and which one */
857 } else if (boot == BOOT_DEVICE_MMC1) {
858 env_set("mmc_bootdev", "0");
859 } else if (boot == BOOT_DEVICE_MMC2) {
860 env_set("mmc_bootdev", "1");
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200861 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200862
863 setup_environment(gd->fdt_blob);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200864
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800865#ifdef CONFIG_USB_ETHER
Maxime Ripardf54aba32017-09-06 22:25:03 +0200866 usb_ether_init();
Icenowy Zhengf4116b62017-09-28 22:16:38 +0800867#endif
Maxime Ripardf54aba32017-09-06 22:25:03 +0200868
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200869 return 0;
870}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200871
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200872int ft_board_setup(void *blob, bd_t *bd)
873{
Hans de Goede48a234a2016-03-22 22:51:52 +0100874 int __maybe_unused r;
875
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200876 /*
877 * Call setup_environment again in case the boot fdt has
878 * ethernet aliases the u-boot copy does not have.
879 */
880 setup_environment(blob);
881
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200882#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goede48a234a2016-03-22 22:51:52 +0100883 r = sunxi_simplefb_setup(blob);
884 if (r)
885 return r;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200886#endif
Hans de Goede48a234a2016-03-22 22:51:52 +0100887 return 0;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200888}
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100889
890#ifdef CONFIG_SPL_LOAD_FIT
891int board_fit_config_name_match(const char *name)
892{
Andre Przywara14a25392018-10-25 17:23:04 +0800893 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
894 const char *cmp_str = (const char *)spl;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100895
Andre Przywara4f99ea62017-04-26 01:32:50 +0100896 /* Check if there is a DT name stored in the SPL header and use that. */
Andre Przywara14a25392018-10-25 17:23:04 +0800897 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset) {
Andre Przywara4f99ea62017-04-26 01:32:50 +0100898 cmp_str += spl->dt_name_offset;
899 } else {
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100900#ifdef CONFIG_DEFAULT_DEVICE_TREE
Andre Przywara4f99ea62017-04-26 01:32:50 +0100901 cmp_str = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100902#else
Andre Przywara4f99ea62017-04-26 01:32:50 +0100903 return 0;
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100904#endif
Andre Przywara4f99ea62017-04-26 01:32:50 +0100905 };
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100906
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800907#ifdef CONFIG_PINE64_DT_SELECTION
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100908/* Differentiate the two Pine64 board DTs by their DRAM size. */
909 if (strstr(name, "-pine64") && strstr(cmp_str, "-pine64")) {
910 if ((gd->ram_size > 512 * 1024 * 1024))
911 return !strstr(name, "plus");
912 else
913 return !!strstr(name, "plus");
914 } else {
915 return strcmp(name, cmp_str);
916 }
Icenowy Zheng2a269d32018-10-25 17:23:02 +0800917#endif
918 return strcmp(name, cmp_str);
Andre Przywara1bd5ca32017-04-26 01:32:44 +0100919}
920#endif