blob: f022f365e950d29123bed42a7a607b49c8609c6a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbellcba69ee2014-05-05 11:52:26 +01002/*
3 * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net>
4 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 *
6 * (C) Copyright 2007-2011
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * Tom Cubie <tangliang@allwinnertech.com>
9 *
10 * Some board init for the Allwinner A10-evb board.
Ian Campbellcba69ee2014-05-05 11:52:26 +010011 */
12
13#include <common.h>
Jagan Teki237050f2018-05-07 13:03:36 +053014#include <dm.h>
Hans de Goedee79c7c82014-10-02 21:13:54 +020015#include <mmc.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020016#include <axp_pmic.h>
Jagan Teki237050f2018-05-07 13:03:36 +053017#include <generic-phy.h>
18#include <phy-sun4i-usb.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010019#include <asm/arch/clock.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020020#include <asm/arch/cpu.h>
Luc Verhaegen2d7a0842014-08-13 07:55:07 +020021#include <asm/arch/display.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010022#include <asm/arch/dram.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010023#include <asm/arch/gpio.h>
24#include <asm/arch/mmc.h>
Hans de Goede4a8c7c12016-07-09 09:56:56 +020025#include <asm/arch/spl.h>
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020026#ifndef CONFIG_ARM64
27#include <asm/armv7.h>
28#endif
Hans de Goede4f7e01c2015-04-23 23:23:50 +020029#include <asm/gpio.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020030#include <asm/io.h>
Philipp Tomsicha740ee92018-11-25 19:22:18 +010031#include <u-boot/crc.h>
Hans de Goede4a8c7c12016-07-09 09:56:56 +020032#include <environment.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090033#include <linux/libfdt.h>
Hans de Goedef62bfa52015-08-15 11:55:26 +020034#include <nand.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020035#include <net.h>
Maxime Ripardf4c35232017-08-23 10:08:29 +020036#include <spl.h>
Jelle van der Waa0d8382a2016-02-23 18:47:19 +010037#include <sy8106a.h>
Simon Glass5d982852017-05-17 08:23:00 -060038#include <asm/setup.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010039
Hans de Goede55410082015-02-16 17:23:25 +010040#if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD)
41/* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */
42int soft_i2c_gpio_sda;
43int soft_i2c_gpio_scl;
Hans de Goede4f7e01c2015-04-23 23:23:50 +020044
45static int soft_i2c_board_init(void)
46{
47 int ret;
48
49 soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA);
50 if (soft_i2c_gpio_sda < 0) {
51 printf("Error invalid soft i2c sda pin: '%s', err %d\n",
52 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda);
53 return soft_i2c_gpio_sda;
54 }
55 ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda");
56 if (ret) {
57 printf("Error requesting soft i2c sda pin: '%s', err %d\n",
58 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret);
59 return ret;
60 }
61
62 soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL);
63 if (soft_i2c_gpio_scl < 0) {
64 printf("Error invalid soft i2c scl pin: '%s', err %d\n",
65 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl);
66 return soft_i2c_gpio_scl;
67 }
68 ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl");
69 if (ret) {
70 printf("Error requesting soft i2c scl pin: '%s', err %d\n",
71 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret);
72 return ret;
73 }
74
75 return 0;
76}
77#else
78static int soft_i2c_board_init(void) { return 0; }
Hans de Goede55410082015-02-16 17:23:25 +010079#endif
80
Ian Campbellcba69ee2014-05-05 11:52:26 +010081DECLARE_GLOBAL_DATA_PTR;
82
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020083void i2c_init_board(void)
84{
85#ifdef CONFIG_I2C0_ENABLE
86#if defined(CONFIG_MACH_SUN4I) || \
87 defined(CONFIG_MACH_SUN5I) || \
88 defined(CONFIG_MACH_SUN7I) || \
89 defined(CONFIG_MACH_SUN8I_R40)
90 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
91 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
92 clock_twi_onoff(0, 1);
93#elif defined(CONFIG_MACH_SUN6I)
94 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
95 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
96 clock_twi_onoff(0, 1);
97#elif defined(CONFIG_MACH_SUN8I)
98 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
99 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
100 clock_twi_onoff(0, 1);
101#endif
102#endif
103
104#ifdef CONFIG_I2C1_ENABLE
105#if defined(CONFIG_MACH_SUN4I) || \
106 defined(CONFIG_MACH_SUN7I) || \
107 defined(CONFIG_MACH_SUN8I_R40)
108 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
109 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
110 clock_twi_onoff(1, 1);
111#elif defined(CONFIG_MACH_SUN5I)
112 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
113 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
114 clock_twi_onoff(1, 1);
115#elif defined(CONFIG_MACH_SUN6I)
116 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
117 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
118 clock_twi_onoff(1, 1);
119#elif defined(CONFIG_MACH_SUN8I)
120 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
121 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
122 clock_twi_onoff(1, 1);
123#endif
124#endif
125
126#ifdef CONFIG_I2C2_ENABLE
127#if defined(CONFIG_MACH_SUN4I) || \
128 defined(CONFIG_MACH_SUN7I) || \
129 defined(CONFIG_MACH_SUN8I_R40)
130 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
131 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
132 clock_twi_onoff(2, 1);
133#elif defined(CONFIG_MACH_SUN5I)
134 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
135 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
136 clock_twi_onoff(2, 1);
137#elif defined(CONFIG_MACH_SUN6I)
138 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
139 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
140 clock_twi_onoff(2, 1);
141#elif defined(CONFIG_MACH_SUN8I)
142 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
143 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
144 clock_twi_onoff(2, 1);
145#endif
146#endif
147
148#ifdef CONFIG_I2C3_ENABLE
149#if defined(CONFIG_MACH_SUN6I)
150 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
151 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
152 clock_twi_onoff(3, 1);
153#elif defined(CONFIG_MACH_SUN7I) || \
154 defined(CONFIG_MACH_SUN8I_R40)
155 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
156 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
157 clock_twi_onoff(3, 1);
158#endif
159#endif
160
161#ifdef CONFIG_I2C4_ENABLE
162#if defined(CONFIG_MACH_SUN7I) || \
163 defined(CONFIG_MACH_SUN8I_R40)
164 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
165 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
166 clock_twi_onoff(4, 1);
167#endif
168#endif
169
170#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800171#ifdef CONFIG_MACH_SUN50I
172 clock_twi_onoff(5, 1);
173 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
174 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
175#else
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200176 clock_twi_onoff(5, 1);
177 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
178 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
179#endif
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800180#endif
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200181}
182
Maxime Ripardb39117c2018-01-23 21:17:03 +0100183#if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT)
184enum env_location env_get_location(enum env_operation op, int prio)
185{
186 switch (prio) {
187 case 0:
188 return ENVL_FAT;
189
190 case 1:
191 return ENVL_MMC;
192
193 default:
194 return ENVL_UNKNOWN;
195 }
196}
197#endif
198
Ian Campbellcba69ee2014-05-05 11:52:26 +0100199/* add board specific code here */
200int board_init(void)
201{
Mylène Josserandf5fd7882017-04-02 12:59:10 +0200202 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100203
204 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
205
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200206#ifndef CONFIG_ARM64
Ian Campbellcba69ee2014-05-05 11:52:26 +0100207 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
208 debug("id_pfr1: 0x%08x\n", id_pfr1);
209 /* Generic Timer Extension available? */
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200210 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
211 uint32_t freq;
212
Ian Campbellcba69ee2014-05-05 11:52:26 +0100213 debug("Setting CNTFRQ\n");
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200214
215 /*
216 * CNTFRQ is a secure register, so we will crash if we try to
217 * write this from the non-secure world (read is OK, though).
218 * In case some bootcode has already set the correct value,
219 * we avoid the risk of writing to it.
220 */
221 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Andre Przywarae4916e82017-02-16 01:20:19 +0000222 if (freq != COUNTER_FREQUENCY) {
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200223 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Andre Przywarae4916e82017-02-16 01:20:19 +0000224 freq, COUNTER_FREQUENCY);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200225#ifdef CONFIG_NON_SECURE
226 printf("arch timer frequency is wrong, but cannot adjust it\n");
227#else
228 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Andre Przywarae4916e82017-02-16 01:20:19 +0000229 : : "r"(COUNTER_FREQUENCY));
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200230#endif
231 }
Ian Campbellcba69ee2014-05-05 11:52:26 +0100232 }
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200233#endif /* !CONFIG_ARM64 */
Ian Campbellcba69ee2014-05-05 11:52:26 +0100234
Hans de Goede2fcf0332015-04-25 17:25:14 +0200235 ret = axp_gpio_init();
236 if (ret)
237 return ret;
238
Hans de Goede9fbb0c32016-03-22 20:10:30 +0100239#ifdef CONFIG_SATAPWR
Mylène Josserandd7b560e2017-04-02 12:59:09 +0200240 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
241 gpio_request(satapwr_pin, "satapwr");
242 gpio_direction_output(satapwr_pin, 1);
Werner Böllmann8e2c2d42017-11-10 19:14:20 +0530243 /* Give attached sata device time to power-up to avoid link timeouts */
244 mdelay(500);
Hans de Goede9fbb0c32016-03-22 20:10:30 +0100245#endif
Hans de Goedefc8991c2016-03-17 13:53:03 +0100246#ifdef CONFIG_MACPWR
Mylène Josserandf5fd7882017-04-02 12:59:10 +0200247 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
248 gpio_request(macpwr_pin, "macpwr");
249 gpio_direction_output(macpwr_pin, 1);
Hans de Goedefc8991c2016-03-17 13:53:03 +0100250#endif
251
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +0200252#ifdef CONFIG_DM_I2C
253 /*
254 * Temporary workaround for enabling I2C clocks until proper sunxi DM
255 * clk, reset and pinctrl drivers land.
256 */
257 i2c_init_board();
258#endif
259
Hans de Goede4f7e01c2015-04-23 23:23:50 +0200260 /* Uses dm gpio code so do this here and not in i2c_init_board() */
261 return soft_i2c_board_init();
Ian Campbellcba69ee2014-05-05 11:52:26 +0100262}
263
Andre Przywaracff5c132018-10-25 17:23:04 +0800264/*
265 * On older SoCs the SPL is actually at address zero, so using NULL as
266 * an error value does not work.
267 */
268#define INVALID_SPL_HEADER ((void *)~0UL)
269
270static struct boot_file_head * get_spl_header(uint8_t req_version)
271{
272 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
273 uint8_t spl_header_version = spl->spl_signature[3];
274
275 /* Is there really the SPL header (still) there? */
276 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
277 return INVALID_SPL_HEADER;
278
279 if (spl_header_version < req_version) {
280 printf("sunxi SPL version mismatch: expected %u, got %u\n",
281 req_version, spl_header_version);
282 return INVALID_SPL_HEADER;
283 }
284
285 return spl;
286}
287
Ian Campbellcba69ee2014-05-05 11:52:26 +0100288int dram_init(void)
289{
Andre Przywara57766102018-10-25 17:23:07 +0800290 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
291
292 if (spl == INVALID_SPL_HEADER)
293 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
294 PHYS_SDRAM_0_SIZE);
295 else
296 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
297
298 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
299 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100300
301 return 0;
302}
303
Boris Brezillon4ccae812016-06-15 21:09:23 +0200304#if defined(CONFIG_NAND_SUNXI)
Karol Gugalaad008292015-07-23 14:33:01 +0200305static void nand_pinmux_setup(void)
306{
307 unsigned int pin;
Hans de Goede022a99d2015-08-15 13:17:49 +0200308
309 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200310 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
311
Hans de Goede022a99d2015-08-15 13:17:49 +0200312#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
313 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200314 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200315#endif
316 /* sun4i / sun7i do have a PC23, but it is not used for nand,
317 * only sun7i has a PC24 */
318#ifdef CONFIG_MACH_SUN7I
Karol Gugalaad008292015-07-23 14:33:01 +0200319 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200320#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200321}
322
323static void nand_clock_setup(void)
324{
325 struct sunxi_ccm_reg *const ccm =
326 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede31c21472015-08-15 11:58:03 +0200327
Karol Gugalaad008292015-07-23 14:33:01 +0200328 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalba1c98b2018-02-28 20:51:53 +0100329#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
330 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
331 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
332#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200333 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
334}
Hans de Goedef62bfa52015-08-15 11:55:26 +0200335
336void board_nand_init(void)
337{
338 nand_pinmux_setup();
339 nand_clock_setup();
Boris Brezillon4ccae812016-06-15 21:09:23 +0200340#ifndef CONFIG_SPL_BUILD
341 sunxi_nand_init();
342#endif
Hans de Goedef62bfa52015-08-15 11:55:26 +0200343}
Karol Gugalaad008292015-07-23 14:33:01 +0200344#endif
345
Masahiro Yamada4aa2ba32017-05-09 20:31:39 +0900346#ifdef CONFIG_MMC
Ian Campbelle24ea552014-05-05 14:42:31 +0100347static void mmc_pinmux_setup(int sdc)
348{
349 unsigned int pin;
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100350 __maybe_unused int pins;
Ian Campbelle24ea552014-05-05 14:42:31 +0100351
352 switch (sdc) {
353 case 0:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100354 /* SDC0: PF0-PF5 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100355 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100356 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100357 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
358 sunxi_gpio_set_drv(pin, 2);
359 }
360 break;
361
362 case 1:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100363 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS);
364
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800365#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
366 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100367 if (pins == SUNXI_GPIO_H) {
368 /* SDC1: PH22-PH-27 */
369 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
370 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
371 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
372 sunxi_gpio_set_drv(pin, 2);
373 }
374 } else {
375 /* SDC1: PG0-PG5 */
376 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
377 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
378 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
379 sunxi_gpio_set_drv(pin, 2);
380 }
381 }
382#elif defined(CONFIG_MACH_SUN5I)
383 /* SDC1: PG3-PG8 */
Hans de Goedebbff84b2014-10-03 16:44:57 +0200384 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100385 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbelle24ea552014-05-05 14:42:31 +0100386 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
387 sunxi_gpio_set_drv(pin, 2);
388 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100389#elif defined(CONFIG_MACH_SUN6I)
390 /* SDC1: PG0-PG5 */
391 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
392 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
393 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
394 sunxi_gpio_set_drv(pin, 2);
395 }
396#elif defined(CONFIG_MACH_SUN8I)
397 if (pins == SUNXI_GPIO_D) {
398 /* SDC1: PD2-PD7 */
399 for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) {
400 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1);
401 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
402 sunxi_gpio_set_drv(pin, 2);
403 }
404 } else {
405 /* SDC1: PG0-PG5 */
406 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
407 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
408 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
409 sunxi_gpio_set_drv(pin, 2);
410 }
411 }
412#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100413 break;
414
415 case 2:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100416 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS);
417
418#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
419 /* SDC2: PC6-PC11 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100420 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100421 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100422 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
423 sunxi_gpio_set_drv(pin, 2);
424 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100425#elif defined(CONFIG_MACH_SUN5I)
426 if (pins == SUNXI_GPIO_E) {
427 /* SDC2: PE4-PE9 */
428 for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) {
429 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2);
430 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
431 sunxi_gpio_set_drv(pin, 2);
432 }
433 } else {
434 /* SDC2: PC6-PC15 */
435 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
436 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
437 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
438 sunxi_gpio_set_drv(pin, 2);
439 }
440 }
441#elif defined(CONFIG_MACH_SUN6I)
442 if (pins == SUNXI_GPIO_A) {
443 /* SDC2: PA9-PA14 */
444 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
445 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2);
446 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
447 sunxi_gpio_set_drv(pin, 2);
448 }
449 } else {
450 /* SDC2: PC6-PC15, PC24 */
451 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
452 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
453 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
454 sunxi_gpio_set_drv(pin, 2);
455 }
Ian Campbelle24ea552014-05-05 14:42:31 +0100456
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100457 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
458 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
459 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
460 }
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800461#elif defined(CONFIG_MACH_SUN8I_R40)
462 /* SDC2: PC6-PC15, PC24 */
463 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
464 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
465 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
466 sunxi_gpio_set_drv(pin, 2);
467 }
468
469 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
470 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
471 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200472#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100473 /* SDC2: PC5-PC6, PC8-PC16 */
474 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
475 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100476 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
477 sunxi_gpio_set_drv(pin, 2);
478 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100479
480 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); 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 }
Icenowy Zheng42956f12018-07-21 16:20:29 +0800485#elif defined(CONFIG_MACH_SUN50I_H6)
486 /* SDC2: PC4-PC14 */
487 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
488 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
489 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
490 sunxi_gpio_set_drv(pin, 2);
491 }
Philipp Tomsich3ebb4562016-10-28 18:21:33 +0800492#elif defined(CONFIG_MACH_SUN9I)
493 /* SDC2: PC6-PC16 */
494 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
495 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
496 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
497 sunxi_gpio_set_drv(pin, 2);
498 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100499#endif
500 break;
501
502 case 3:
503 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS);
504
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800505#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
506 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100507 /* SDC3: PI4-PI9 */
508 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
509 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
510 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
511 sunxi_gpio_set_drv(pin, 2);
512 }
513#elif defined(CONFIG_MACH_SUN6I)
514 if (pins == SUNXI_GPIO_A) {
515 /* SDC3: PA9-PA14 */
516 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
517 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3);
518 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
519 sunxi_gpio_set_drv(pin, 2);
520 }
521 } else {
522 /* SDC3: PC6-PC15, PC24 */
523 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
524 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
525 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
526 sunxi_gpio_set_drv(pin, 2);
527 }
528
529 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
530 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
531 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
532 }
533#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100534 break;
535
536 default:
537 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
538 break;
539 }
540}
541
542int board_mmc_init(bd_t *bis)
543{
Hans de Goedee79c7c82014-10-02 21:13:54 +0200544 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goedee79c7c82014-10-02 21:13:54 +0200545
Ian Campbelle24ea552014-05-05 14:42:31 +0100546 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200547 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
548 if (!mmc0)
549 return -1;
550
Hans de Goede2ccfac02014-10-02 20:43:50 +0200551#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbelle24ea552014-05-05 14:42:31 +0100552 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200553 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
554 if (!mmc1)
555 return -1;
556#endif
557
Ian Campbelle24ea552014-05-05 14:42:31 +0100558 return 0;
559}
560#endif
561
Ian Campbellcba69ee2014-05-05 11:52:26 +0100562#ifdef CONFIG_SPL_BUILD
Andre Przywara57766102018-10-25 17:23:07 +0800563
564static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
565{
566 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
567
568 if (spl == INVALID_SPL_HEADER)
569 return;
570
571 /* Promote the header version for U-Boot proper, if needed. */
572 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
573 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
574
575 spl->dram_size = dram_size >> 20;
576}
577
Ian Campbellcba69ee2014-05-05 11:52:26 +0100578void sunxi_board_init(void)
579{
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200580 int power_failed = 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100581
Jelle van der Waa0d8382a2016-02-23 18:47:19 +0100582#ifdef CONFIG_SY8106A_POWER
583 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
584#endif
585
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800586#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800587 defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
588 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200589 power_failed = axp_init();
590
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800591#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
592 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200593 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede24289202014-06-13 22:55:51 +0200594#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200595 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
596 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800597#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200598 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200599#endif
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800600#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
601 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200602 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200603#endif
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200604
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800605#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
606 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200607 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
608#endif
609 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Chen-Yu Tsaif3c50452016-01-12 14:42:40 +0800610#if !defined(CONFIG_AXP152_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200611 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
612#endif
613#ifdef CONFIG_AXP209_POWER
614 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
615#endif
616
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800617#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
618 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800619 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
620 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800621#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800622 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
623 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800624#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200625 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
626 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
627 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
628#endif
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800629
630#ifdef CONFIG_AXP818_POWER
631 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
632 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
633 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800634#endif
635
636#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai15278cc2016-05-02 10:28:12 +0800637 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800638#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200639#endif
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000640 printf("DRAM:");
641 gd->ram_size = sunxi_dram_init();
642 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
643 if (!gd->ram_size)
644 hang();
645
646 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara57766102018-10-25 17:23:07 +0800647
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200648 /*
649 * Only clock up the CPU to full speed if we are reasonably
650 * assured it's being powered with suitable core voltage
651 */
652 if (!power_failed)
Iain Patone71b4222015-03-28 10:26:38 +0000653 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200654 else
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000655 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbellcba69ee2014-05-05 11:52:26 +0100656}
657#endif
Jonathan Liub41d7d02014-06-14 08:59:09 +0200658
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100659#ifdef CONFIG_USB_GADGET
660int g_dnl_board_usb_cable_connected(void)
661{
Jagan Teki237050f2018-05-07 13:03:36 +0530662 struct udevice *dev;
663 struct phy phy;
664 int ret;
665
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100666 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki237050f2018-05-07 13:03:36 +0530667 if (ret) {
668 pr_err("%s: Cannot find USB device\n", __func__);
669 return ret;
670 }
671
672 ret = generic_phy_get_by_name(dev, "usb", &phy);
673 if (ret) {
674 pr_err("failed to get %s USB PHY\n", dev->name);
675 return ret;
676 }
677
678 ret = generic_phy_init(&phy);
679 if (ret) {
680 pr_err("failed to init %s USB PHY\n", dev->name);
681 return ret;
682 }
683
684 ret = sun4i_usb_phy_vbus_detect(&phy);
685 if (ret == 1) {
686 pr_err("A charger is plugged into the OTG\n");
687 return -ENODEV;
688 }
689
690 return ret;
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100691}
692#endif
693
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100694#ifdef CONFIG_SERIAL_TAG
695void get_board_serial(struct tag_serialnr *serialnr)
696{
697 char *serial_string;
698 unsigned long long serial;
699
Simon Glass00caae62017-08-03 12:22:12 -0600700 serial_string = env_get("serial#");
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100701
702 if (serial_string) {
703 serial = simple_strtoull(serial_string, NULL, 16);
704
705 serialnr->high = (unsigned int) (serial >> 32);
706 serialnr->low = (unsigned int) (serial & 0xffffffff);
707 } else {
708 serialnr->high = 0;
709 serialnr->low = 0;
710 }
711}
712#endif
713
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200714/*
715 * Check the SPL header for the "sunxi" variant. If found: parse values
716 * that might have been passed by the loader ("fel" utility), and update
717 * the environment accordingly.
718 */
719static void parse_spl_header(const uint32_t spl_addr)
720{
Andre Przywaracff5c132018-10-25 17:23:04 +0800721 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200722
Andre Przywaracff5c132018-10-25 17:23:04 +0800723 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200724 return;
Andre Przywaracff5c132018-10-25 17:23:04 +0800725
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200726 if (!spl->fel_script_address)
727 return;
728
729 if (spl->fel_uEnv_length != 0) {
730 /*
731 * data is expected in uEnv.txt compatible format, so "env
732 * import -t" the string(s) at fel_script_address right away.
733 */
Andre Przywara5a74a392016-09-05 01:32:41 +0100734 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200735 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
736 return;
737 }
738 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass018f5302017-08-03 12:22:10 -0600739 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200740}
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200741
Hans de Goedef2219612016-06-26 13:34:42 +0200742/*
743 * Note this function gets called multiple times.
744 * It must not make any changes to env variables which already exist.
745 */
746static void setup_environment(const void *fdt)
Jonathan Liub41d7d02014-06-14 08:59:09 +0200747{
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100748 char serial_string[17] = { 0 };
Hans de Goedecac5b1c2014-11-26 00:04:24 +0100749 unsigned int sid[4];
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100750 uint8_t mac_addr[6];
Hans de Goedef2219612016-06-26 13:34:42 +0200751 char ethaddr[16];
752 int i, ret;
753
754 ret = sunxi_get_sid(sid);
Hans de Goede3f8ea3b2016-07-29 11:47:03 +0200755 if (ret == 0 && sid[0] != 0) {
756 /*
757 * The single words 1 - 3 of the SID have quite a few bits
758 * which are the same on many models, so we take a crc32
759 * of all 3 words, to get a more unique value.
760 *
761 * Note we only do this on newer SoCs as we cannot change
762 * the algorithm on older SoCs since those have been using
763 * fixed mac-addresses based on only using word 3 for a
764 * long time and changing a fixed mac-address with an
765 * u-boot update is not good.
766 */
767#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
768 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
769 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
770 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
771#endif
772
Hans de Goede97322c32016-07-27 17:58:06 +0200773 /* Ensure the NIC specific bytes of the mac are not all 0 */
774 if ((sid[3] & 0xffffff) == 0)
775 sid[3] |= 0x800000;
776
Hans de Goedef2219612016-06-26 13:34:42 +0200777 for (i = 0; i < 4; i++) {
778 sprintf(ethaddr, "ethernet%d", i);
779 if (!fdt_get_alias(fdt, ethaddr))
780 continue;
781
782 if (i == 0)
783 strcpy(ethaddr, "ethaddr");
784 else
785 sprintf(ethaddr, "eth%daddr", i);
786
Simon Glass00caae62017-08-03 12:22:12 -0600787 if (env_get(ethaddr))
Hans de Goedef2219612016-06-26 13:34:42 +0200788 continue;
789
790 /* Non OUI / registered MAC address */
791 mac_addr[0] = (i << 4) | 0x02;
792 mac_addr[1] = (sid[0] >> 0) & 0xff;
793 mac_addr[2] = (sid[3] >> 24) & 0xff;
794 mac_addr[3] = (sid[3] >> 16) & 0xff;
795 mac_addr[4] = (sid[3] >> 8) & 0xff;
796 mac_addr[5] = (sid[3] >> 0) & 0xff;
797
Simon Glassfd1e9592017-08-03 12:22:11 -0600798 eth_env_set_enetaddr(ethaddr, mac_addr);
Hans de Goedef2219612016-06-26 13:34:42 +0200799 }
800
Simon Glass00caae62017-08-03 12:22:12 -0600801 if (!env_get("serial#")) {
Hans de Goedef2219612016-06-26 13:34:42 +0200802 snprintf(serial_string, sizeof(serial_string),
803 "%08x%08x", sid[0], sid[3]);
804
Simon Glass382bee52017-08-03 12:22:09 -0600805 env_set("serial#", serial_string);
Hans de Goedef2219612016-06-26 13:34:42 +0200806 }
807 }
808}
809
Hans de Goedef2219612016-06-26 13:34:42 +0200810int misc_init_r(void)
811{
Maxime Ripardf4c35232017-08-23 10:08:29 +0200812 uint boot;
Jonathan Liub41d7d02014-06-14 08:59:09 +0200813
Simon Glass382bee52017-08-03 12:22:09 -0600814 env_set("fel_booted", NULL);
815 env_set("fel_scriptaddr", NULL);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200816 env_set("mmc_bootdev", NULL);
Maxime Ripardf4c35232017-08-23 10:08:29 +0200817
818 boot = sunxi_get_boot_device();
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200819 /* determine if we are running in FEL mode */
Maxime Ripardf4c35232017-08-23 10:08:29 +0200820 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass382bee52017-08-03 12:22:09 -0600821 env_set("fel_booted", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200822 parse_spl_header(SPL_ADDR);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200823 /* or if we booted from MMC, and which one */
824 } else if (boot == BOOT_DEVICE_MMC1) {
825 env_set("mmc_bootdev", "0");
826 } else if (boot == BOOT_DEVICE_MMC2) {
827 env_set("mmc_bootdev", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200828 }
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200829
Hans de Goedef2219612016-06-26 13:34:42 +0200830 setup_environment(gd->fdt_blob);
Jonathan Liub41d7d02014-06-14 08:59:09 +0200831
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800832#ifdef CONFIG_USB_ETHER
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200833 usb_ether_init();
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800834#endif
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200835
Jonathan Liub41d7d02014-06-14 08:59:09 +0200836 return 0;
837}
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200838
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200839int ft_board_setup(void *blob, bd_t *bd)
840{
Hans de Goeded75111a2016-03-22 22:51:52 +0100841 int __maybe_unused r;
842
Hans de Goedef2219612016-06-26 13:34:42 +0200843 /*
844 * Call setup_environment again in case the boot fdt has
845 * ethernet aliases the u-boot copy does not have.
846 */
847 setup_environment(blob);
848
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200849#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goeded75111a2016-03-22 22:51:52 +0100850 r = sunxi_simplefb_setup(blob);
851 if (r)
852 return r;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200853#endif
Hans de Goeded75111a2016-03-22 22:51:52 +0100854 return 0;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200855}
Andre Przywara9ea3c352017-04-26 01:32:44 +0100856
857#ifdef CONFIG_SPL_LOAD_FIT
858int board_fit_config_name_match(const char *name)
859{
Andre Przywaracff5c132018-10-25 17:23:04 +0800860 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
861 const char *cmp_str = (const char *)spl;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100862
Andre Przywara54254ba2017-04-26 01:32:50 +0100863 /* Check if there is a DT name stored in the SPL header and use that. */
Andre Przywaracff5c132018-10-25 17:23:04 +0800864 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset) {
Andre Przywara54254ba2017-04-26 01:32:50 +0100865 cmp_str += spl->dt_name_offset;
866 } else {
Andre Przywara9ea3c352017-04-26 01:32:44 +0100867#ifdef CONFIG_DEFAULT_DEVICE_TREE
Andre Przywara54254ba2017-04-26 01:32:50 +0100868 cmp_str = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100869#else
Andre Przywara54254ba2017-04-26 01:32:50 +0100870 return 0;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100871#endif
Andre Przywara54254ba2017-04-26 01:32:50 +0100872 };
Andre Przywara9ea3c352017-04-26 01:32:44 +0100873
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800874#ifdef CONFIG_PINE64_DT_SELECTION
Andre Przywara9ea3c352017-04-26 01:32:44 +0100875/* Differentiate the two Pine64 board DTs by their DRAM size. */
876 if (strstr(name, "-pine64") && strstr(cmp_str, "-pine64")) {
877 if ((gd->ram_size > 512 * 1024 * 1024))
878 return !strstr(name, "plus");
879 else
880 return !!strstr(name, "plus");
881 } else {
882 return strcmp(name, cmp_str);
883 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800884#endif
885 return strcmp(name, cmp_str);
Andre Przywara9ea3c352017-04-26 01:32:44 +0100886}
887#endif