blob: 53656383d512199338dcdc1d4fdc4f7f939c9b61 [file] [log] [blame]
Ian Campbellcba69ee2014-05-05 11:52:26 +01001/*
2 * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net>
3 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
4 *
5 * (C) Copyright 2007-2011
6 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
7 * Tom Cubie <tangliang@allwinnertech.com>
8 *
9 * Some board init for the Allwinner A10-evb board.
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 */
13
14#include <common.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>
Ian Campbellcba69ee2014-05-05 11:52:26 +010017#include <asm/arch/clock.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020018#include <asm/arch/cpu.h>
Luc Verhaegen2d7a0842014-08-13 07:55:07 +020019#include <asm/arch/display.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010020#include <asm/arch/dram.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010021#include <asm/arch/gpio.h>
22#include <asm/arch/mmc.h>
Hans de Goede4a8c7c12016-07-09 09:56:56 +020023#include <asm/arch/spl.h>
Hans de Goede2aacc422015-04-27 15:05:10 +020024#include <asm/arch/usb_phy.h>
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020025#ifndef CONFIG_ARM64
26#include <asm/armv7.h>
27#endif
Hans de Goede4f7e01c2015-04-23 23:23:50 +020028#include <asm/gpio.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020029#include <asm/io.h>
Hans de Goede3f8ea3b2016-07-29 11:47:03 +020030#include <crc.h>
Hans de Goede4a8c7c12016-07-09 09:56:56 +020031#include <environment.h>
Hans de Goedef2219612016-06-26 13:34:42 +020032#include <libfdt.h>
Hans de Goedef62bfa52015-08-15 11:55:26 +020033#include <nand.h>
Jonathan Liub41d7d02014-06-14 08:59:09 +020034#include <net.h>
Jelle van der Waa0d8382a2016-02-23 18:47:19 +010035#include <sy8106a.h>
Ian Campbellcba69ee2014-05-05 11:52:26 +010036
Hans de Goede55410082015-02-16 17:23:25 +010037#if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD)
38/* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */
39int soft_i2c_gpio_sda;
40int soft_i2c_gpio_scl;
Hans de Goede4f7e01c2015-04-23 23:23:50 +020041
42static int soft_i2c_board_init(void)
43{
44 int ret;
45
46 soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA);
47 if (soft_i2c_gpio_sda < 0) {
48 printf("Error invalid soft i2c sda pin: '%s', err %d\n",
49 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda);
50 return soft_i2c_gpio_sda;
51 }
52 ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda");
53 if (ret) {
54 printf("Error requesting soft i2c sda pin: '%s', err %d\n",
55 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret);
56 return ret;
57 }
58
59 soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL);
60 if (soft_i2c_gpio_scl < 0) {
61 printf("Error invalid soft i2c scl pin: '%s', err %d\n",
62 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl);
63 return soft_i2c_gpio_scl;
64 }
65 ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl");
66 if (ret) {
67 printf("Error requesting soft i2c scl pin: '%s', err %d\n",
68 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret);
69 return ret;
70 }
71
72 return 0;
73}
74#else
75static int soft_i2c_board_init(void) { return 0; }
Hans de Goede55410082015-02-16 17:23:25 +010076#endif
77
Ian Campbellcba69ee2014-05-05 11:52:26 +010078DECLARE_GLOBAL_DATA_PTR;
79
80/* add board specific code here */
81int board_init(void)
82{
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020083 __maybe_unused int id_pfr1, ret;
Ian Campbellcba69ee2014-05-05 11:52:26 +010084
85 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
86
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020087#ifndef CONFIG_ARM64
Ian Campbellcba69ee2014-05-05 11:52:26 +010088 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
89 debug("id_pfr1: 0x%08x\n", id_pfr1);
90 /* Generic Timer Extension available? */
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020091 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
92 uint32_t freq;
93
Ian Campbellcba69ee2014-05-05 11:52:26 +010094 debug("Setting CNTFRQ\n");
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +020095
96 /*
97 * CNTFRQ is a secure register, so we will crash if we try to
98 * write this from the non-secure world (read is OK, though).
99 * In case some bootcode has already set the correct value,
100 * we avoid the risk of writing to it.
101 */
102 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
103 if (freq != CONFIG_TIMER_CLK_FREQ) {
104 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
105 freq, CONFIG_TIMER_CLK_FREQ);
106#ifdef CONFIG_NON_SECURE
107 printf("arch timer frequency is wrong, but cannot adjust it\n");
108#else
109 asm volatile("mcr p15, 0, %0, c14, c0, 0"
110 : : "r"(CONFIG_TIMER_CLK_FREQ));
111#endif
112 }
Ian Campbellcba69ee2014-05-05 11:52:26 +0100113 }
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200114#endif /* !CONFIG_ARM64 */
Ian Campbellcba69ee2014-05-05 11:52:26 +0100115
Hans de Goede2fcf0332015-04-25 17:25:14 +0200116 ret = axp_gpio_init();
117 if (ret)
118 return ret;
119
Hans de Goede9fbb0c32016-03-22 20:10:30 +0100120#ifdef CONFIG_SATAPWR
121 gpio_request(CONFIG_SATAPWR, "satapwr");
122 gpio_direction_output(CONFIG_SATAPWR, 1);
123#endif
Hans de Goedefc8991c2016-03-17 13:53:03 +0100124#ifdef CONFIG_MACPWR
125 gpio_request(CONFIG_MACPWR, "macpwr");
126 gpio_direction_output(CONFIG_MACPWR, 1);
127#endif
128
Hans de Goede4f7e01c2015-04-23 23:23:50 +0200129 /* Uses dm gpio code so do this here and not in i2c_init_board() */
130 return soft_i2c_board_init();
Ian Campbellcba69ee2014-05-05 11:52:26 +0100131}
132
133int dram_init(void)
134{
135 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0, PHYS_SDRAM_0_SIZE);
136
137 return 0;
138}
139
Boris Brezillon4ccae812016-06-15 21:09:23 +0200140#if defined(CONFIG_NAND_SUNXI)
Karol Gugalaad008292015-07-23 14:33:01 +0200141static void nand_pinmux_setup(void)
142{
143 unsigned int pin;
Hans de Goede022a99d2015-08-15 13:17:49 +0200144
145 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200146 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
147
Hans de Goede022a99d2015-08-15 13:17:49 +0200148#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
149 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
Karol Gugalaad008292015-07-23 14:33:01 +0200150 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200151#endif
152 /* sun4i / sun7i do have a PC23, but it is not used for nand,
153 * only sun7i has a PC24 */
154#ifdef CONFIG_MACH_SUN7I
Karol Gugalaad008292015-07-23 14:33:01 +0200155 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goede022a99d2015-08-15 13:17:49 +0200156#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200157}
158
159static void nand_clock_setup(void)
160{
161 struct sunxi_ccm_reg *const ccm =
162 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede31c21472015-08-15 11:58:03 +0200163
Karol Gugalaad008292015-07-23 14:33:01 +0200164 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Hans de Goede31c21472015-08-15 11:58:03 +0200165#ifdef CONFIG_MACH_SUN9I
166 setbits_le32(&ccm->ahb_gate1, (1 << AHB_GATE_OFFSET_DMA));
167#else
168 setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_DMA));
169#endif
Karol Gugalaad008292015-07-23 14:33:01 +0200170 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
171}
Hans de Goedef62bfa52015-08-15 11:55:26 +0200172
173void board_nand_init(void)
174{
175 nand_pinmux_setup();
176 nand_clock_setup();
Boris Brezillon4ccae812016-06-15 21:09:23 +0200177#ifndef CONFIG_SPL_BUILD
178 sunxi_nand_init();
179#endif
Hans de Goedef62bfa52015-08-15 11:55:26 +0200180}
Karol Gugalaad008292015-07-23 14:33:01 +0200181#endif
182
Ian Campbelle24ea552014-05-05 14:42:31 +0100183#ifdef CONFIG_GENERIC_MMC
184static void mmc_pinmux_setup(int sdc)
185{
186 unsigned int pin;
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100187 __maybe_unused int pins;
Ian Campbelle24ea552014-05-05 14:42:31 +0100188
189 switch (sdc) {
190 case 0:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100191 /* SDC0: PF0-PF5 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100192 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100193 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100194 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
195 sunxi_gpio_set_drv(pin, 2);
196 }
197 break;
198
199 case 1:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100200 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS);
201
202#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
203 if (pins == SUNXI_GPIO_H) {
204 /* SDC1: PH22-PH-27 */
205 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
206 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
207 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
208 sunxi_gpio_set_drv(pin, 2);
209 }
210 } else {
211 /* SDC1: PG0-PG5 */
212 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
213 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
214 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
215 sunxi_gpio_set_drv(pin, 2);
216 }
217 }
218#elif defined(CONFIG_MACH_SUN5I)
219 /* SDC1: PG3-PG8 */
Hans de Goedebbff84b2014-10-03 16:44:57 +0200220 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100221 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbelle24ea552014-05-05 14:42:31 +0100222 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
223 sunxi_gpio_set_drv(pin, 2);
224 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100225#elif defined(CONFIG_MACH_SUN6I)
226 /* SDC1: PG0-PG5 */
227 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
228 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
229 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
230 sunxi_gpio_set_drv(pin, 2);
231 }
232#elif defined(CONFIG_MACH_SUN8I)
233 if (pins == SUNXI_GPIO_D) {
234 /* SDC1: PD2-PD7 */
235 for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) {
236 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1);
237 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
238 sunxi_gpio_set_drv(pin, 2);
239 }
240 } else {
241 /* SDC1: PG0-PG5 */
242 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
243 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
244 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
245 sunxi_gpio_set_drv(pin, 2);
246 }
247 }
248#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100249 break;
250
251 case 2:
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100252 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS);
253
254#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
255 /* SDC2: PC6-PC11 */
Ian Campbelle24ea552014-05-05 14:42:31 +0100256 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowski487b3272015-03-22 18:12:22 +0100257 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100258 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
259 sunxi_gpio_set_drv(pin, 2);
260 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100261#elif defined(CONFIG_MACH_SUN5I)
262 if (pins == SUNXI_GPIO_E) {
263 /* SDC2: PE4-PE9 */
264 for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) {
265 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2);
266 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
267 sunxi_gpio_set_drv(pin, 2);
268 }
269 } else {
270 /* SDC2: PC6-PC15 */
271 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
272 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
273 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
274 sunxi_gpio_set_drv(pin, 2);
275 }
276 }
277#elif defined(CONFIG_MACH_SUN6I)
278 if (pins == SUNXI_GPIO_A) {
279 /* SDC2: PA9-PA14 */
280 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
281 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2);
282 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
283 sunxi_gpio_set_drv(pin, 2);
284 }
285 } else {
286 /* SDC2: PC6-PC15, PC24 */
287 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
288 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
289 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
290 sunxi_gpio_set_drv(pin, 2);
291 }
Ian Campbelle24ea552014-05-05 14:42:31 +0100292
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100293 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
294 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
295 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
296 }
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200297#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100298 /* SDC2: PC5-PC6, PC8-PC16 */
299 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
300 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbelle24ea552014-05-05 14:42:31 +0100301 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
302 sunxi_gpio_set_drv(pin, 2);
303 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100304
305 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
306 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
307 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
308 sunxi_gpio_set_drv(pin, 2);
309 }
Philipp Tomsich3ebb4562016-10-28 18:21:33 +0800310#elif defined(CONFIG_MACH_SUN9I)
311 /* SDC2: PC6-PC16 */
312 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
313 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
314 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
315 sunxi_gpio_set_drv(pin, 2);
316 }
Paul Kocialkowski8deacca2015-03-22 18:12:23 +0100317#endif
318 break;
319
320 case 3:
321 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS);
322
323#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
324 /* SDC3: PI4-PI9 */
325 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
326 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
327 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
328 sunxi_gpio_set_drv(pin, 2);
329 }
330#elif defined(CONFIG_MACH_SUN6I)
331 if (pins == SUNXI_GPIO_A) {
332 /* SDC3: PA9-PA14 */
333 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
334 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3);
335 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
336 sunxi_gpio_set_drv(pin, 2);
337 }
338 } else {
339 /* SDC3: PC6-PC15, PC24 */
340 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
341 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
342 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
343 sunxi_gpio_set_drv(pin, 2);
344 }
345
346 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
347 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
348 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
349 }
350#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100351 break;
352
353 default:
354 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
355 break;
356 }
357}
358
359int board_mmc_init(bd_t *bis)
360{
Hans de Goedee79c7c82014-10-02 21:13:54 +0200361 __maybe_unused struct mmc *mmc0, *mmc1;
362 __maybe_unused char buf[512];
363
Ian Campbelle24ea552014-05-05 14:42:31 +0100364 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200365 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
366 if (!mmc0)
367 return -1;
368
Hans de Goede2ccfac02014-10-02 20:43:50 +0200369#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbelle24ea552014-05-05 14:42:31 +0100370 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goedee79c7c82014-10-02 21:13:54 +0200371 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
372 if (!mmc1)
373 return -1;
374#endif
375
Daniel Kochmańskibf5b9b12015-05-29 17:21:00 +0200376#if !defined(CONFIG_SPL_BUILD) && CONFIG_MMC_SUNXI_SLOT_EXTRA == 2
Hans de Goedee79c7c82014-10-02 21:13:54 +0200377 /*
Daniel Kochmańskibf5b9b12015-05-29 17:21:00 +0200378 * On systems with an emmc (mmc2), figure out if we are booting from
379 * the emmc and if we are make it "mmc dev 0" so that boot.scr, etc.
380 * are searched there first. Note we only do this for u-boot proper,
381 * not for the SPL, see spl_boot_device().
Hans de Goedee79c7c82014-10-02 21:13:54 +0200382 */
Hans de Goedeef36d9a2016-07-09 15:31:47 +0200383 if (readb(SPL_ADDR + 0x28) == SUNXI_BOOTED_FROM_MMC2) {
Daniel Kochmańskibf5b9b12015-05-29 17:21:00 +0200384 /* Booting from emmc / mmc2, swap */
Simon Glassbcce53d2016-02-29 15:25:51 -0700385 mmc0->block_dev.devnum = 1;
386 mmc1->block_dev.devnum = 0;
Daniel Kochmańskibf5b9b12015-05-29 17:21:00 +0200387 }
Ian Campbelle24ea552014-05-05 14:42:31 +0100388#endif
389
390 return 0;
391}
392#endif
393
Hans de Goede66203772014-06-13 22:55:49 +0200394void i2c_init_board(void)
395{
Paul Kocialkowski6c739c52015-04-10 23:09:52 +0200396#ifdef CONFIG_I2C0_ENABLE
397#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I)
398 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
399 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
Hans de Goede66203772014-06-13 22:55:49 +0200400 clock_twi_onoff(0, 1);
Paul Kocialkowski6c739c52015-04-10 23:09:52 +0200401#elif defined(CONFIG_MACH_SUN6I)
402 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
403 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
404 clock_twi_onoff(0, 1);
405#elif defined(CONFIG_MACH_SUN8I)
406 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
407 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
408 clock_twi_onoff(0, 1);
409#endif
410#endif
411
412#ifdef CONFIG_I2C1_ENABLE
413#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
414 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
415 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
416 clock_twi_onoff(1, 1);
417#elif defined(CONFIG_MACH_SUN5I)
418 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
419 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
420 clock_twi_onoff(1, 1);
421#elif defined(CONFIG_MACH_SUN6I)
422 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
423 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
424 clock_twi_onoff(1, 1);
425#elif defined(CONFIG_MACH_SUN8I)
426 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
427 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
428 clock_twi_onoff(1, 1);
429#endif
430#endif
431
432#ifdef CONFIG_I2C2_ENABLE
433#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
434 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
435 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
436 clock_twi_onoff(2, 1);
437#elif defined(CONFIG_MACH_SUN5I)
438 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
439 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
440 clock_twi_onoff(2, 1);
441#elif defined(CONFIG_MACH_SUN6I)
442 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
443 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
444 clock_twi_onoff(2, 1);
445#elif defined(CONFIG_MACH_SUN8I)
446 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
447 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
448 clock_twi_onoff(2, 1);
449#endif
450#endif
451
452#ifdef CONFIG_I2C3_ENABLE
453#if defined(CONFIG_MACH_SUN6I)
454 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
455 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
456 clock_twi_onoff(3, 1);
457#elif defined(CONFIG_MACH_SUN7I)
458 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
459 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
460 clock_twi_onoff(3, 1);
461#endif
462#endif
463
464#ifdef CONFIG_I2C4_ENABLE
465#if defined(CONFIG_MACH_SUN7I)
466 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
467 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
468 clock_twi_onoff(4, 1);
469#endif
470#endif
Jelle van der Waa9d082682016-01-14 14:06:26 +0100471
472#ifdef CONFIG_R_I2C_ENABLE
473 clock_twi_onoff(5, 1);
474 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
475 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
476#endif
Hans de Goede66203772014-06-13 22:55:49 +0200477}
478
Ian Campbellcba69ee2014-05-05 11:52:26 +0100479#ifdef CONFIG_SPL_BUILD
480void sunxi_board_init(void)
481{
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200482 int power_failed = 0;
Ian Campbellcba69ee2014-05-05 11:52:26 +0100483 unsigned long ramsize;
484
Jelle van der Waa0d8382a2016-02-23 18:47:19 +0100485#ifdef CONFIG_SY8106A_POWER
486 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
487#endif
488
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800489#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800490 defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
491 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200492 power_failed = axp_init();
493
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800494#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
495 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200496 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede24289202014-06-13 22:55:51 +0200497#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200498 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
499 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
vishnupatekar95ab8fe2015-11-29 01:07:22 +0800500#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200501 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200502#endif
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800503#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
504 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200505 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagl5c7f10f2013-07-26 12:56:58 +0200506#endif
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200507
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800508#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
509 defined CONFIG_AXP818_POWER
Hans de Goede6944aff2015-10-03 15:18:33 +0200510 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
511#endif
512 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Chen-Yu Tsaif3c50452016-01-12 14:42:40 +0800513#if !defined(CONFIG_AXP152_POWER)
Hans de Goede6944aff2015-10-03 15:18:33 +0200514 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
515#endif
516#ifdef CONFIG_AXP209_POWER
517 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
518#endif
519
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800520#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
521 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800522 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
523 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800524#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai3517a272016-01-12 14:42:37 +0800525 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
526 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800527#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200528 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
529 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
530 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
531#endif
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800532
533#ifdef CONFIG_AXP818_POWER
534 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
535 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
536 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsai795857d2016-05-02 10:28:15 +0800537#endif
538
539#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai15278cc2016-05-02 10:28:12 +0800540 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsai38491d92016-03-30 00:26:48 +0800541#endif
Hans de Goede6944aff2015-10-03 15:18:33 +0200542#endif
Ian Campbellcba69ee2014-05-05 11:52:26 +0100543 printf("DRAM:");
544 ramsize = sunxi_dram_init();
Hans de Goedecd8b35d2016-06-26 13:56:01 +0200545 printf(" %d MiB\n", (int)(ramsize >> 20));
Ian Campbellcba69ee2014-05-05 11:52:26 +0100546 if (!ramsize)
547 hang();
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200548
549 /*
550 * Only clock up the CPU to full speed if we are reasonably
551 * assured it's being powered with suitable core voltage
552 */
553 if (!power_failed)
Iain Patone71b4222015-03-28 10:26:38 +0000554 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
Henrik Nordstrom14bc66b2014-06-13 22:55:50 +0200555 else
556 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbellcba69ee2014-05-05 11:52:26 +0100557}
558#endif
Jonathan Liub41d7d02014-06-14 08:59:09 +0200559
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100560#ifdef CONFIG_USB_GADGET
561int g_dnl_board_usb_cable_connected(void)
562{
Paul Kocialkowski5bfdca02015-05-16 19:52:10 +0200563 return sunxi_usb_phy_vbus_detect(0);
Paul Kocialkowskif1df7582015-03-22 18:07:13 +0100564}
565#endif
566
Paul Kocialkowski9f852212015-03-28 18:35:36 +0100567#ifdef CONFIG_SERIAL_TAG
568void get_board_serial(struct tag_serialnr *serialnr)
569{
570 char *serial_string;
571 unsigned long long serial;
572
573 serial_string = getenv("serial#");
574
575 if (serial_string) {
576 serial = simple_strtoull(serial_string, NULL, 16);
577
578 serialnr->high = (unsigned int) (serial >> 32);
579 serialnr->low = (unsigned int) (serial & 0xffffffff);
580 } else {
581 serialnr->high = 0;
582 serialnr->low = 0;
583 }
584}
585#endif
586
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200587/*
588 * Check the SPL header for the "sunxi" variant. If found: parse values
589 * that might have been passed by the loader ("fel" utility), and update
590 * the environment accordingly.
591 */
592static void parse_spl_header(const uint32_t spl_addr)
593{
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200594 struct boot_file_head *spl = (void *)(ulong)spl_addr;
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200595 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
596 return; /* signature mismatch, no usable header */
597
598 uint8_t spl_header_version = spl->spl_signature[3];
599 if (spl_header_version != SPL_HEADER_VERSION) {
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200600 printf("sunxi SPL version mismatch: expected %u, got %u\n",
601 SPL_HEADER_VERSION, spl_header_version);
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200602 return;
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200603 }
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200604 if (!spl->fel_script_address)
605 return;
606
607 if (spl->fel_uEnv_length != 0) {
608 /*
609 * data is expected in uEnv.txt compatible format, so "env
610 * import -t" the string(s) at fel_script_address right away.
611 */
Andre Przywara5a74a392016-09-05 01:32:41 +0100612 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
Bernhard Nortmann320e0572016-06-09 07:37:35 +0200613 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
614 return;
615 }
616 /* otherwise assume .scr format (mkimage-type script) */
617 setenv_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200618}
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200619
Hans de Goedef2219612016-06-26 13:34:42 +0200620/*
621 * Note this function gets called multiple times.
622 * It must not make any changes to env variables which already exist.
623 */
624static void setup_environment(const void *fdt)
Jonathan Liub41d7d02014-06-14 08:59:09 +0200625{
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100626 char serial_string[17] = { 0 };
Hans de Goedecac5b1c2014-11-26 00:04:24 +0100627 unsigned int sid[4];
Paul Kocialkowski8c816572015-03-28 18:35:35 +0100628 uint8_t mac_addr[6];
Hans de Goedef2219612016-06-26 13:34:42 +0200629 char ethaddr[16];
630 int i, ret;
631
632 ret = sunxi_get_sid(sid);
Hans de Goede3f8ea3b2016-07-29 11:47:03 +0200633 if (ret == 0 && sid[0] != 0) {
634 /*
635 * The single words 1 - 3 of the SID have quite a few bits
636 * which are the same on many models, so we take a crc32
637 * of all 3 words, to get a more unique value.
638 *
639 * Note we only do this on newer SoCs as we cannot change
640 * the algorithm on older SoCs since those have been using
641 * fixed mac-addresses based on only using word 3 for a
642 * long time and changing a fixed mac-address with an
643 * u-boot update is not good.
644 */
645#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
646 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
647 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
648 sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
649#endif
650
Hans de Goede97322c32016-07-27 17:58:06 +0200651 /* Ensure the NIC specific bytes of the mac are not all 0 */
652 if ((sid[3] & 0xffffff) == 0)
653 sid[3] |= 0x800000;
654
Hans de Goedef2219612016-06-26 13:34:42 +0200655 for (i = 0; i < 4; i++) {
656 sprintf(ethaddr, "ethernet%d", i);
657 if (!fdt_get_alias(fdt, ethaddr))
658 continue;
659
660 if (i == 0)
661 strcpy(ethaddr, "ethaddr");
662 else
663 sprintf(ethaddr, "eth%daddr", i);
664
665 if (getenv(ethaddr))
666 continue;
667
668 /* Non OUI / registered MAC address */
669 mac_addr[0] = (i << 4) | 0x02;
670 mac_addr[1] = (sid[0] >> 0) & 0xff;
671 mac_addr[2] = (sid[3] >> 24) & 0xff;
672 mac_addr[3] = (sid[3] >> 16) & 0xff;
673 mac_addr[4] = (sid[3] >> 8) & 0xff;
674 mac_addr[5] = (sid[3] >> 0) & 0xff;
675
676 eth_setenv_enetaddr(ethaddr, mac_addr);
677 }
678
679 if (!getenv("serial#")) {
680 snprintf(serial_string, sizeof(serial_string),
681 "%08x%08x", sid[0], sid[3]);
682
683 setenv("serial#", serial_string);
684 }
685 }
686}
687
Hans de Goedef2219612016-06-26 13:34:42 +0200688int misc_init_r(void)
689{
690 __maybe_unused int ret;
Jonathan Liub41d7d02014-06-14 08:59:09 +0200691
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200692 setenv("fel_booted", NULL);
693 setenv("fel_scriptaddr", NULL);
694 /* determine if we are running in FEL mode */
695 if (!is_boot0_magic(SPL_ADDR + 4)) { /* eGON.BT0 */
696 setenv("fel_booted", "1");
697 parse_spl_header(SPL_ADDR);
698 }
Bernhard Nortmannaf654d12015-09-17 18:52:52 +0200699
Hans de Goedef2219612016-06-26 13:34:42 +0200700 setup_environment(gd->fdt_blob);
Jonathan Liub41d7d02014-06-14 08:59:09 +0200701
Hans de Goede1871a8c2015-01-13 19:25:06 +0100702#ifndef CONFIG_MACH_SUN9I
Hans de Goedee13afee2015-04-27 16:50:04 +0200703 ret = sunxi_usb_phy_probe();
704 if (ret)
705 return ret;
Hans de Goede1871a8c2015-01-13 19:25:06 +0100706#endif
Hans de Goeded42faf32015-06-17 15:49:26 +0200707 sunxi_musb_board_init();
708
Jonathan Liub41d7d02014-06-14 08:59:09 +0200709 return 0;
710}
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200711
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200712int ft_board_setup(void *blob, bd_t *bd)
713{
Hans de Goeded75111a2016-03-22 22:51:52 +0100714 int __maybe_unused r;
715
Hans de Goedef2219612016-06-26 13:34:42 +0200716 /*
717 * Call setup_environment again in case the boot fdt has
718 * ethernet aliases the u-boot copy does not have.
719 */
720 setup_environment(blob);
721
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200722#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goeded75111a2016-03-22 22:51:52 +0100723 r = sunxi_simplefb_setup(blob);
724 if (r)
725 return r;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200726#endif
Hans de Goeded75111a2016-03-22 22:51:52 +0100727 return 0;
Luc Verhaegen2d7a0842014-08-13 07:55:07 +0200728}