blob: 6afea6ef42e42d6137ffc22319659512f1071ee6 [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>
Simon Glassc7694dd2019-08-01 09:46:46 -060015#include <env.h>
Simon Glassdb41d652019-12-28 10:45:07 -070016#include <hang.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070017#include <init.h>
Hans de Goedee79c7c82014-10-02 21:13:54 +020018#include <mmc.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020019#include <axp_pmic.h>
Jagan Teki237050f2018-05-07 13:03:36 +053020#include <generic-phy.h>
21#include <phy-sun4i-usb.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010022#include <asm/arch/clock.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020023#include <asm/arch/cpu.h>
Luc Verhaegen2d7a0842014-08-13 07:55:07 +020024#include <asm/arch/display.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010025#include <asm/arch/dram.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010026#include <asm/arch/gpio.h>
27#include <asm/arch/mmc.h>
Hans de Goede4a8c7c12016-07-09 09:56:56 +020028#include <asm/arch/spl.h>
Simon Glass3db71102019-11-14 12:57:16 -070029#include <u-boot/crc.h>
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020030#ifndef CONFIG_ARM64
31#include <asm/armv7.h>
32#endif
Hans de Goede4f7e01c2015-04-23 23:23:50 +020033#include <asm/gpio.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020034#include <asm/io.h>
Philipp Tomsicha740ee92018-11-25 19:22:18 +010035#include <u-boot/crc.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060036#include <env_internal.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090037#include <linux/libfdt.h>
Hans de Goedef62bfa52015-08-15 11:55:26 +020038#include <nand.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020039#include <net.h>
Maxime Ripardf4c35232017-08-23 10:08:29 +020040#include <spl.h>
Jelle van der Waa0d8382a2016-02-23 18:47:19 +010041#include <sy8106a.h>
Simon Glass5d982852017-05-17 08:23:00 -060042#include <asm/setup.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010043
Hans de Goede55410082015-02-16 17:23:25 +010044#if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD)
45/* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */
46int soft_i2c_gpio_sda;
47int soft_i2c_gpio_scl;
Hans de Goede4f7e01c2015-04-23 23:23:50 +020048
49static int soft_i2c_board_init(void)
50{
51 int ret;
52
53 soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA);
54 if (soft_i2c_gpio_sda < 0) {
55 printf("Error invalid soft i2c sda pin: '%s', err %d\n",
56 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda);
57 return soft_i2c_gpio_sda;
58 }
59 ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda");
60 if (ret) {
61 printf("Error requesting soft i2c sda pin: '%s', err %d\n",
62 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret);
63 return ret;
64 }
65
66 soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL);
67 if (soft_i2c_gpio_scl < 0) {
68 printf("Error invalid soft i2c scl pin: '%s', err %d\n",
69 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl);
70 return soft_i2c_gpio_scl;
71 }
72 ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl");
73 if (ret) {
74 printf("Error requesting soft i2c scl pin: '%s', err %d\n",
75 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret);
76 return ret;
77 }
78
79 return 0;
80}
81#else
82static int soft_i2c_board_init(void) { return 0; }
Hans de Goede55410082015-02-16 17:23:25 +010083#endif
84
Ian Campbellcba69ee2014-05-05 11:52:26 +010085DECLARE_GLOBAL_DATA_PTR;
86
Jernej Skrabecacbc7e02017-04-27 00:03:35 +020087void i2c_init_board(void)
88{
89#ifdef CONFIG_I2C0_ENABLE
90#if defined(CONFIG_MACH_SUN4I) || \
91 defined(CONFIG_MACH_SUN5I) || \
92 defined(CONFIG_MACH_SUN7I) || \
93 defined(CONFIG_MACH_SUN8I_R40)
94 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
95 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
96 clock_twi_onoff(0, 1);
97#elif defined(CONFIG_MACH_SUN6I)
98 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
99 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
100 clock_twi_onoff(0, 1);
101#elif defined(CONFIG_MACH_SUN8I)
102 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
103 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
104 clock_twi_onoff(0, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +0200105#elif defined(CONFIG_MACH_SUN50I)
106 sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
107 sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
108 clock_twi_onoff(0, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200109#endif
110#endif
111
112#ifdef CONFIG_I2C1_ENABLE
113#if defined(CONFIG_MACH_SUN4I) || \
114 defined(CONFIG_MACH_SUN7I) || \
115 defined(CONFIG_MACH_SUN8I_R40)
116 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
117 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
118 clock_twi_onoff(1, 1);
119#elif defined(CONFIG_MACH_SUN5I)
120 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
121 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
122 clock_twi_onoff(1, 1);
123#elif defined(CONFIG_MACH_SUN6I)
124 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
125 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
126 clock_twi_onoff(1, 1);
127#elif defined(CONFIG_MACH_SUN8I)
128 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
129 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
130 clock_twi_onoff(1, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +0200131#elif defined(CONFIG_MACH_SUN50I)
132 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
133 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
134 clock_twi_onoff(1, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200135#endif
136#endif
137
138#ifdef CONFIG_I2C2_ENABLE
139#if defined(CONFIG_MACH_SUN4I) || \
140 defined(CONFIG_MACH_SUN7I) || \
141 defined(CONFIG_MACH_SUN8I_R40)
142 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
143 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
144 clock_twi_onoff(2, 1);
145#elif defined(CONFIG_MACH_SUN5I)
146 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
147 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
148 clock_twi_onoff(2, 1);
149#elif defined(CONFIG_MACH_SUN6I)
150 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
151 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
152 clock_twi_onoff(2, 1);
153#elif defined(CONFIG_MACH_SUN8I)
154 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
155 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
156 clock_twi_onoff(2, 1);
Stefan Mavrodievda1ae592019-01-08 12:04:30 +0200157#elif defined(CONFIG_MACH_SUN50I)
158 sunxi_gpio_set_cfgpin(SUNXI_GPE(14), SUN50I_GPE_TWI2);
159 sunxi_gpio_set_cfgpin(SUNXI_GPE(15), SUN50I_GPE_TWI2);
160 clock_twi_onoff(2, 1);
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200161#endif
162#endif
163
164#ifdef CONFIG_I2C3_ENABLE
165#if defined(CONFIG_MACH_SUN6I)
166 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
167 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
168 clock_twi_onoff(3, 1);
169#elif defined(CONFIG_MACH_SUN7I) || \
170 defined(CONFIG_MACH_SUN8I_R40)
171 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
172 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
173 clock_twi_onoff(3, 1);
174#endif
175#endif
176
177#ifdef CONFIG_I2C4_ENABLE
178#if defined(CONFIG_MACH_SUN7I) || \
179 defined(CONFIG_MACH_SUN8I_R40)
180 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
181 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
182 clock_twi_onoff(4, 1);
183#endif
184#endif
185
186#ifdef CONFIG_R_I2C_ENABLE
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800187#ifdef CONFIG_MACH_SUN50I
188 clock_twi_onoff(5, 1);
189 sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
190 sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
191#else
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200192 clock_twi_onoff(5, 1);
193 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
194 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
195#endif
Vasily Khoruzhick31a4ac42018-11-05 20:24:30 -0800196#endif
Jernej Skrabecacbc7e02017-04-27 00:03:35 +0200197}
198
Maxime Ripardb39117c2018-01-23 21:17:03 +0100199#if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT)
200enum env_location env_get_location(enum env_operation op, int prio)
201{
202 switch (prio) {
203 case 0:
204 return ENVL_FAT;
205
206 case 1:
207 return ENVL_MMC;
208
209 default:
210 return ENVL_UNKNOWN;
211 }
212}
213#endif
214
Andre Przywaraa7ae1592019-01-29 15:54:14 +0000215#ifdef CONFIG_DM_MMC
216static void mmc_pinmux_setup(int sdc);
217#endif
218
Ian Campbellcba69ee2014-05-05 11:52:26 +0100219/* add board specific code here */
220int board_init(void)
221{
Mylène Josserandf5fd7882017-04-02 12:59:10 +0200222 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100223
224 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
225
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200226#ifndef CONFIG_ARM64
Ian Campbellcba69ee2014-05-05 11:52:26 +0100227 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
228 debug("id_pfr1: 0x%08x\n", id_pfr1);
229 /* Generic Timer Extension available? */
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200230 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
231 uint32_t freq;
232
Ian Campbellcba69ee2014-05-05 11:52:26 +0100233 debug("Setting CNTFRQ\n");
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200234
235 /*
236 * CNTFRQ is a secure register, so we will crash if we try to
237 * write this from the non-secure world (read is OK, though).
238 * In case some bootcode has already set the correct value,
239 * we avoid the risk of writing to it.
240 */
241 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
Andre Przywarae4916e82017-02-16 01:20:19 +0000242 if (freq != COUNTER_FREQUENCY) {
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200243 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
Andre Przywarae4916e82017-02-16 01:20:19 +0000244 freq, COUNTER_FREQUENCY);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200245#ifdef CONFIG_NON_SECURE
246 printf("arch timer frequency is wrong, but cannot adjust it\n");
247#else
248 asm volatile("mcr p15, 0, %0, c14, c0, 0"
Andre Przywarae4916e82017-02-16 01:20:19 +0000249 : : "r"(COUNTER_FREQUENCY));
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200250#endif
251 }
Ian Campbellcba69ee2014-05-05 11:52:26 +0100252 }
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200253#endif /* !CONFIG_ARM64 */
Ian Campbellcba69ee2014-05-05 11:52:26 +0100254
Hans de Goede2fcf0332015-04-25 17:25:14 +0200255 ret = axp_gpio_init();
256 if (ret)
257 return ret;
258
Hans de Goede9fbb0c32016-03-22 20:10:30 +0100259#ifdef CONFIG_SATAPWR
Mylène Josserandd7b560e2017-04-02 12:59:09 +0200260 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
261 gpio_request(satapwr_pin, "satapwr");
262 gpio_direction_output(satapwr_pin, 1);
Werner Böllmann8e2c2d42017-11-10 19:14:20 +0530263 /* Give attached sata device time to power-up to avoid link timeouts */
264 mdelay(500);
Hans de Goede9fbb0c32016-03-22 20:10:30 +0100265#endif
Hans de Goedefc8991c2016-03-17 13:53:03 +0100266#ifdef CONFIG_MACPWR
Mylène Josserandf5fd7882017-04-02 12:59:10 +0200267 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
268 gpio_request(macpwr_pin, "macpwr");
269 gpio_direction_output(macpwr_pin, 1);
Hans de Goedefc8991c2016-03-17 13:53:03 +0100270#endif
271
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +0200272#ifdef CONFIG_DM_I2C
273 /*
274 * Temporary workaround for enabling I2C clocks until proper sunxi DM
275 * clk, reset and pinctrl drivers land.
276 */
277 i2c_init_board();
278#endif
279
Andre Przywaraa7ae1592019-01-29 15:54:14 +0000280#ifdef CONFIG_DM_MMC
281 /*
282 * Temporary workaround for enabling MMC clocks until a sunxi DM
283 * pinctrl driver lands.
284 */
285 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
286#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
287 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
288#endif
289#endif /* CONFIG_DM_MMC */
290
Hans de Goede4f7e01c2015-04-23 23:23:50 +0200291 /* Uses dm gpio code so do this here and not in i2c_init_board() */
292 return soft_i2c_board_init();
Ian Campbellcba69ee2014-05-05 11:52:26 +0100293}
294
Andre Przywaracff5c132018-10-25 17:23:04 +0800295/*
296 * On older SoCs the SPL is actually at address zero, so using NULL as
297 * an error value does not work.
298 */
299#define INVALID_SPL_HEADER ((void *)~0UL)
300
301static struct boot_file_head * get_spl_header(uint8_t req_version)
302{
303 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
304 uint8_t spl_header_version = spl->spl_signature[3];
305
306 /* Is there really the SPL header (still) there? */
307 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
308 return INVALID_SPL_HEADER;
309
310 if (spl_header_version < req_version) {
311 printf("sunxi SPL version mismatch: expected %u, got %u\n",
312 req_version, spl_header_version);
313 return INVALID_SPL_HEADER;
314 }
315
316 return spl;
317}
318
Ian Campbellcba69ee2014-05-05 11:52:26 +0100319int dram_init(void)
320{
Andre Przywara57766102018-10-25 17:23:07 +0800321 struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
322
323 if (spl == INVALID_SPL_HEADER)
324 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
325 PHYS_SDRAM_0_SIZE);
326 else
327 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
328
329 if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
330 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100331
332 return 0;
333}
334
Boris Brezillon4ccae812016-06-15 21:09:23 +0200335#if defined(CONFIG_NAND_SUNXI)
Karol Gugalaad008292015-07-23 14:33:01 +0200336static void nand_pinmux_setup(void)
337{
338 unsigned int pin;
Hans de Goede022a99d2015-08-15 13:17:49 +0200339
340 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200341 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
342
Hans de Goede022a99d2015-08-15 13:17:49 +0200343#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
344 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200345 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200346#endif
347 /* sun4i / sun7i do have a PC23, but it is not used for nand,
348 * only sun7i has a PC24 */
349#ifdef CONFIG_MACH_SUN7I
Karol Gugalaad008292015-07-23 14:33:01 +0200350 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200351#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200352}
353
354static void nand_clock_setup(void)
355{
356 struct sunxi_ccm_reg *const ccm =
357 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede31c21472015-08-15 11:58:03 +0200358
Karol Gugalaad008292015-07-23 14:33:01 +0200359 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Miquel Raynalba1c98b2018-02-28 20:51:53 +0100360#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
361 defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
362 setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
363#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200364 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
365}
Hans de Goedef62bfa52015-08-15 11:55:26 +0200366
367void board_nand_init(void)
368{
369 nand_pinmux_setup();
370 nand_clock_setup();
Boris Brezillon4ccae812016-06-15 21:09:23 +0200371#ifndef CONFIG_SPL_BUILD
372 sunxi_nand_init();
373#endif
Hans de Goedef62bfa52015-08-15 11:55:26 +0200374}
Karol Gugalaad008292015-07-23 14:33:01 +0200375#endif
376
Masahiro Yamada4aa2ba32017-05-09 20:31:39 +0900377#ifdef CONFIG_MMC
Ian Campbelle24ea552014-05-05 14:42:31 +0100378static void mmc_pinmux_setup(int sdc)
379{
380 unsigned int pin;
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100381 __maybe_unused int pins;
Ian Campbelle24ea552014-05-05 14:42:31 +0100382
383 switch (sdc) {
384 case 0:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100385 /* SDC0: PF0-PF5 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100386 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100387 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100388 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
389 sunxi_gpio_set_drv(pin, 2);
390 }
391 break;
392
393 case 1:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100394 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS);
395
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800396#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
397 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100398 if (pins == SUNXI_GPIO_H) {
399 /* SDC1: PH22-PH-27 */
400 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
401 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
402 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
403 sunxi_gpio_set_drv(pin, 2);
404 }
405 } else {
406 /* SDC1: PG0-PG5 */
407 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
408 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
409 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
410 sunxi_gpio_set_drv(pin, 2);
411 }
412 }
413#elif defined(CONFIG_MACH_SUN5I)
414 /* SDC1: PG3-PG8 */
Hans de Goedebbff84b2014-10-03 16:44:57 +0200415 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100416 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbelle24ea552014-05-05 14:42:31 +0100417 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
418 sunxi_gpio_set_drv(pin, 2);
419 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100420#elif defined(CONFIG_MACH_SUN6I)
421 /* SDC1: PG0-PG5 */
422 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
423 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
424 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
425 sunxi_gpio_set_drv(pin, 2);
426 }
427#elif defined(CONFIG_MACH_SUN8I)
428 if (pins == SUNXI_GPIO_D) {
429 /* SDC1: PD2-PD7 */
430 for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) {
431 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1);
432 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
433 sunxi_gpio_set_drv(pin, 2);
434 }
435 } else {
436 /* SDC1: PG0-PG5 */
437 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
438 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
439 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
440 sunxi_gpio_set_drv(pin, 2);
441 }
442 }
443#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100444 break;
445
446 case 2:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100447 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS);
448
449#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
450 /* SDC2: PC6-PC11 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100451 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100452 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100453 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
454 sunxi_gpio_set_drv(pin, 2);
455 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100456#elif defined(CONFIG_MACH_SUN5I)
457 if (pins == SUNXI_GPIO_E) {
458 /* SDC2: PE4-PE9 */
459 for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) {
460 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2);
461 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
462 sunxi_gpio_set_drv(pin, 2);
463 }
464 } else {
465 /* SDC2: PC6-PC15 */
466 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
467 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
468 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
469 sunxi_gpio_set_drv(pin, 2);
470 }
471 }
472#elif defined(CONFIG_MACH_SUN6I)
473 if (pins == SUNXI_GPIO_A) {
474 /* SDC2: PA9-PA14 */
475 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
476 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2);
477 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
478 sunxi_gpio_set_drv(pin, 2);
479 }
480 } else {
481 /* SDC2: PC6-PC15, PC24 */
482 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
483 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
484 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
485 sunxi_gpio_set_drv(pin, 2);
486 }
Ian Campbelle24ea552014-05-05 14:42:31 +0100487
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100488 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
489 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
490 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
491 }
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800492#elif defined(CONFIG_MACH_SUN8I_R40)
493 /* SDC2: PC6-PC15, PC24 */
494 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); 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 }
499
500 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
501 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
502 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200503#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100504 /* SDC2: PC5-PC6, PC8-PC16 */
505 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
506 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100507 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
508 sunxi_gpio_set_drv(pin, 2);
509 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100510
511 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
512 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
513 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
514 sunxi_gpio_set_drv(pin, 2);
515 }
Icenowy Zheng42956f12018-07-21 16:20:29 +0800516#elif defined(CONFIG_MACH_SUN50I_H6)
517 /* SDC2: PC4-PC14 */
518 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
519 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
520 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
521 sunxi_gpio_set_drv(pin, 2);
522 }
Philipp Tomsich3ebb4562016-10-28 18:21:33 +0800523#elif defined(CONFIG_MACH_SUN9I)
524 /* SDC2: PC6-PC16 */
525 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
526 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
527 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
528 sunxi_gpio_set_drv(pin, 2);
529 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100530#endif
531 break;
532
533 case 3:
534 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS);
535
Chen-Yu Tsai8094a4a2016-11-30 16:28:34 +0800536#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
537 defined(CONFIG_MACH_SUN8I_R40)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100538 /* SDC3: PI4-PI9 */
539 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
540 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
541 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
542 sunxi_gpio_set_drv(pin, 2);
543 }
544#elif defined(CONFIG_MACH_SUN6I)
545 if (pins == SUNXI_GPIO_A) {
546 /* SDC3: PA9-PA14 */
547 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
548 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3);
549 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
550 sunxi_gpio_set_drv(pin, 2);
551 }
552 } else {
553 /* SDC3: PC6-PC15, PC24 */
554 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
555 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
556 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
557 sunxi_gpio_set_drv(pin, 2);
558 }
559
560 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
561 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
562 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
563 }
564#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100565 break;
566
567 default:
568 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
569 break;
570 }
571}
572
573int board_mmc_init(bd_t *bis)
574{
Hans de Goedee79c7c82014-10-02 21:13:54 +0200575 __maybe_unused struct mmc *mmc0, *mmc1;
Hans de Goedee79c7c82014-10-02 21:13:54 +0200576
Ian Campbelle24ea552014-05-05 14:42:31 +0100577 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200578 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
579 if (!mmc0)
580 return -1;
581
Hans de Goede2ccfac02014-10-02 20:43:50 +0200582#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbelle24ea552014-05-05 14:42:31 +0100583 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200584 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
585 if (!mmc1)
586 return -1;
587#endif
588
Ian Campbelle24ea552014-05-05 14:42:31 +0100589 return 0;
590}
591#endif
592
Ian Campbellcba69ee2014-05-05 11:52:26 +0100593#ifdef CONFIG_SPL_BUILD
Andre Przywara57766102018-10-25 17:23:07 +0800594
595static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
596{
597 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
598
599 if (spl == INVALID_SPL_HEADER)
600 return;
601
602 /* Promote the header version for U-Boot proper, if needed. */
603 if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
604 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
605
606 spl->dram_size = dram_size >> 20;
607}
608
Ian Campbellcba69ee2014-05-05 11:52:26 +0100609void sunxi_board_init(void)
610{
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200611 int power_failed = 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100612
Jelle van der Waa0d8382a2016-02-23 18:47:19 +0100613#ifdef CONFIG_SY8106A_POWER
614 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
615#endif
616
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800617#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800618 defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
619 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200620 power_failed = axp_init();
621
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800622#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
623 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200624 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede24289202014-06-13 22:55:51 +0200625#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200626 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
627 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800628#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200629 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200630#endif
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800631#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
632 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200633 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200634#endif
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200635
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800636#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
637 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200638 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
639#endif
640 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Chen-Yu Tsaif3c50452016-01-12 14:42:40 +0800641#if !defined(CONFIG_AXP152_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200642 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
643#endif
644#ifdef CONFIG_AXP209_POWER
645 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
646#endif
647
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800648#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
649 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800650 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
651 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800652#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800653 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
654 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800655#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200656 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
657 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
658 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
659#endif
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800660
661#ifdef CONFIG_AXP818_POWER
662 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
663 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
664 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800665#endif
666
667#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai15278cc2016-05-02 10:28:12 +0800668 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800669#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200670#endif
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000671 printf("DRAM:");
672 gd->ram_size = sunxi_dram_init();
673 printf(" %d MiB\n", (int)(gd->ram_size >> 20));
674 if (!gd->ram_size)
675 hang();
676
677 sunxi_spl_store_dram_size(gd->ram_size);
Andre Przywara57766102018-10-25 17:23:07 +0800678
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200679 /*
680 * Only clock up the CPU to full speed if we are reasonably
681 * assured it's being powered with suitable core voltage
682 */
683 if (!power_failed)
Iain Patone71b4222015-03-28 10:26:38 +0000684 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200685 else
From: Karl Palsson44c214d2018-12-19 13:00:39 +0000686 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbellcba69ee2014-05-05 11:52:26 +0100687}
688#endif
Jonathan Liub41d7d02014-06-14 08:59:09 +0200689
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100690#ifdef CONFIG_USB_GADGET
691int g_dnl_board_usb_cable_connected(void)
692{
Jagan Teki237050f2018-05-07 13:03:36 +0530693 struct udevice *dev;
694 struct phy phy;
695 int ret;
696
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +0100697 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
Jagan Teki237050f2018-05-07 13:03:36 +0530698 if (ret) {
699 pr_err("%s: Cannot find USB device\n", __func__);
700 return ret;
701 }
702
703 ret = generic_phy_get_by_name(dev, "usb", &phy);
704 if (ret) {
705 pr_err("failed to get %s USB PHY\n", dev->name);
706 return ret;
707 }
708
709 ret = generic_phy_init(&phy);
710 if (ret) {
711 pr_err("failed to init %s USB PHY\n", dev->name);
712 return ret;
713 }
714
715 ret = sun4i_usb_phy_vbus_detect(&phy);
716 if (ret == 1) {
717 pr_err("A charger is plugged into the OTG\n");
718 return -ENODEV;
719 }
720
721 return ret;
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100722}
723#endif
724
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100725#ifdef CONFIG_SERIAL_TAG
726void get_board_serial(struct tag_serialnr *serialnr)
727{
728 char *serial_string;
729 unsigned long long serial;
730
Simon Glass00caae62017-08-03 12:22:12 -0600731 serial_string = env_get("serial#");
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100732
733 if (serial_string) {
734 serial = simple_strtoull(serial_string, NULL, 16);
735
736 serialnr->high = (unsigned int) (serial >> 32);
737 serialnr->low = (unsigned int) (serial & 0xffffffff);
738 } else {
739 serialnr->high = 0;
740 serialnr->low = 0;
741 }
742}
743#endif
744
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200745/*
746 * Check the SPL header for the "sunxi" variant. If found: parse values
747 * that might have been passed by the loader ("fel" utility), and update
748 * the environment accordingly.
749 */
750static void parse_spl_header(const uint32_t spl_addr)
751{
Andre Przywaracff5c132018-10-25 17:23:04 +0800752 struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200753
Andre Przywaracff5c132018-10-25 17:23:04 +0800754 if (spl == INVALID_SPL_HEADER)
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200755 return;
Andre Przywaracff5c132018-10-25 17:23:04 +0800756
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200757 if (!spl->fel_script_address)
758 return;
759
760 if (spl->fel_uEnv_length != 0) {
761 /*
762 * data is expected in uEnv.txt compatible format, so "env
763 * import -t" the string(s) at fel_script_address right away.
764 */
Andre Przywara5a74a392016-09-05 01:32:41 +0100765 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200766 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
767 return;
768 }
769 /* otherwise assume .scr format (mkimage-type script) */
Simon Glass018f5302017-08-03 12:22:10 -0600770 env_set_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200771}
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200772
Hans de Goedef2219612016-06-26 13:34:42 +0200773/*
774 * Note this function gets called multiple times.
775 * It must not make any changes to env variables which already exist.
776 */
777static void setup_environment(const void *fdt)
Jonathan Liub41d7d02014-06-14 08:59:09 +0200778{
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100779 char serial_string[17] = { 0 };
Hans de Goedecac5b1c2014-11-26 00:04:24 +0100780 unsigned int sid[4];
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100781 uint8_t mac_addr[6];
Hans de Goedef2219612016-06-26 13:34:42 +0200782 char ethaddr[16];
783 int i, ret;
784
785 ret = sunxi_get_sid(sid);
Hans de Goede3f8ea3b2016-07-29 11:47:03 +0200786 if (ret == 0 && sid[0] != 0) {
787 /*
788 * The single words 1 - 3 of the SID have quite a few bits
789 * which are the same on many models, so we take a crc32
790 * of all 3 words, to get a more unique value.
791 *
792 * Note we only do this on newer SoCs as we cannot change
793 * the algorithm on older SoCs since those have been using
794 * fixed mac-addresses based on only using word 3 for a
795 * long time and changing a fixed mac-address with an
796 * u-boot update is not good.
797 */
798#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
799 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
800 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
801 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
802#endif
803
Hans de Goede97322c32016-07-27 17:58:06 +0200804 /* Ensure the NIC specific bytes of the mac are not all 0 */
805 if ((sid[3] & 0xffffff) == 0)
806 sid[3] |= 0x800000;
807
Hans de Goedef2219612016-06-26 13:34:42 +0200808 for (i = 0; i < 4; i++) {
809 sprintf(ethaddr, "ethernet%d", i);
810 if (!fdt_get_alias(fdt, ethaddr))
811 continue;
812
813 if (i == 0)
814 strcpy(ethaddr, "ethaddr");
815 else
816 sprintf(ethaddr, "eth%daddr", i);
817
Simon Glass00caae62017-08-03 12:22:12 -0600818 if (env_get(ethaddr))
Hans de Goedef2219612016-06-26 13:34:42 +0200819 continue;
820
821 /* Non OUI / registered MAC address */
822 mac_addr[0] = (i << 4) | 0x02;
823 mac_addr[1] = (sid[0] >> 0) & 0xff;
824 mac_addr[2] = (sid[3] >> 24) & 0xff;
825 mac_addr[3] = (sid[3] >> 16) & 0xff;
826 mac_addr[4] = (sid[3] >> 8) & 0xff;
827 mac_addr[5] = (sid[3] >> 0) & 0xff;
828
Simon Glassfd1e9592017-08-03 12:22:11 -0600829 eth_env_set_enetaddr(ethaddr, mac_addr);
Hans de Goedef2219612016-06-26 13:34:42 +0200830 }
831
Simon Glass00caae62017-08-03 12:22:12 -0600832 if (!env_get("serial#")) {
Hans de Goedef2219612016-06-26 13:34:42 +0200833 snprintf(serial_string, sizeof(serial_string),
834 "%08x%08x", sid[0], sid[3]);
835
Simon Glass382bee52017-08-03 12:22:09 -0600836 env_set("serial#", serial_string);
Hans de Goedef2219612016-06-26 13:34:42 +0200837 }
838 }
839}
840
Hans de Goedef2219612016-06-26 13:34:42 +0200841int misc_init_r(void)
842{
Maxime Ripardf4c35232017-08-23 10:08:29 +0200843 uint boot;
Jonathan Liub41d7d02014-06-14 08:59:09 +0200844
Simon Glass382bee52017-08-03 12:22:09 -0600845 env_set("fel_booted", NULL);
846 env_set("fel_scriptaddr", NULL);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200847 env_set("mmc_bootdev", NULL);
Maxime Ripardf4c35232017-08-23 10:08:29 +0200848
849 boot = sunxi_get_boot_device();
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200850 /* determine if we are running in FEL mode */
Maxime Ripardf4c35232017-08-23 10:08:29 +0200851 if (boot == BOOT_DEVICE_BOARD) {
Simon Glass382bee52017-08-03 12:22:09 -0600852 env_set("fel_booted", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200853 parse_spl_header(SPL_ADDR);
Maxime Ripardde86fc32017-08-23 10:12:22 +0200854 /* or if we booted from MMC, and which one */
855 } else if (boot == BOOT_DEVICE_MMC1) {
856 env_set("mmc_bootdev", "0");
857 } else if (boot == BOOT_DEVICE_MMC2) {
858 env_set("mmc_bootdev", "1");
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200859 }
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200860
Hans de Goedef2219612016-06-26 13:34:42 +0200861 setup_environment(gd->fdt_blob);
Jonathan Liub41d7d02014-06-14 08:59:09 +0200862
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800863#ifdef CONFIG_USB_ETHER
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200864 usb_ether_init();
Icenowy Zhenge6ee85a2017-09-28 22:16:38 +0800865#endif
Maxime Ripard90dd2f12017-09-06 22:25:03 +0200866
Jonathan Liub41d7d02014-06-14 08:59:09 +0200867 return 0;
868}
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200869
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200870int ft_board_setup(void *blob, bd_t *bd)
871{
Hans de Goeded75111a2016-03-22 22:51:52 +0100872 int __maybe_unused r;
873
Hans de Goedef2219612016-06-26 13:34:42 +0200874 /*
875 * Call setup_environment again in case the boot fdt has
876 * ethernet aliases the u-boot copy does not have.
877 */
878 setup_environment(blob);
879
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200880#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goeded75111a2016-03-22 22:51:52 +0100881 r = sunxi_simplefb_setup(blob);
882 if (r)
883 return r;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200884#endif
Hans de Goeded75111a2016-03-22 22:51:52 +0100885 return 0;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200886}
Andre Przywara9ea3c352017-04-26 01:32:44 +0100887
888#ifdef CONFIG_SPL_LOAD_FIT
889int board_fit_config_name_match(const char *name)
890{
Andre Przywaracff5c132018-10-25 17:23:04 +0800891 struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
892 const char *cmp_str = (const char *)spl;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100893
Andre Przywara54254ba2017-04-26 01:32:50 +0100894 /* Check if there is a DT name stored in the SPL header and use that. */
Andre Przywaracff5c132018-10-25 17:23:04 +0800895 if (spl != INVALID_SPL_HEADER && spl->dt_name_offset) {
Andre Przywara54254ba2017-04-26 01:32:50 +0100896 cmp_str += spl->dt_name_offset;
897 } else {
Andre Przywara9ea3c352017-04-26 01:32:44 +0100898#ifdef CONFIG_DEFAULT_DEVICE_TREE
Andre Przywara54254ba2017-04-26 01:32:50 +0100899 cmp_str = CONFIG_DEFAULT_DEVICE_TREE;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100900#else
Andre Przywara54254ba2017-04-26 01:32:50 +0100901 return 0;
Andre Przywara9ea3c352017-04-26 01:32:44 +0100902#endif
Andre Przywara54254ba2017-04-26 01:32:50 +0100903 };
Andre Przywara9ea3c352017-04-26 01:32:44 +0100904
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800905#ifdef CONFIG_PINE64_DT_SELECTION
Andre Przywara9ea3c352017-04-26 01:32:44 +0100906/* Differentiate the two Pine64 board DTs by their DRAM size. */
907 if (strstr(name, "-pine64") && strstr(cmp_str, "-pine64")) {
908 if ((gd->ram_size > 512 * 1024 * 1024))
909 return !strstr(name, "plus");
910 else
911 return !!strstr(name, "plus");
912 } else {
913 return strcmp(name, cmp_str);
914 }
Icenowy Zhengc6c2c852018-10-25 17:23:02 +0800915#endif
916 return strcmp(name, cmp_str);
Andre Przywara9ea3c352017-04-26 01:32:44 +0100917}
918#endif