blob: 320958af288b16c1c0b1930a1d0e768a26abbf7a [file] [log] [blame]
Ian Campbell6efe3692014-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 Goede63deaa82014-10-02 21:13:54 +020015#include <mmc.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020016#include <axp_pmic.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010017#include <asm/arch/clock.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020018#include <asm/arch/cpu.h>
Luc Verhaegen4869a8c2014-08-13 07:55:07 +020019#include <asm/arch/display.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010020#include <asm/arch/dram.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010021#include <asm/arch/gpio.h>
22#include <asm/arch/mmc.h>
Hans de Goedea146c502016-07-09 09:56:56 +020023#include <asm/arch/spl.h>
Hans de Goede26a90052015-04-27 15:05:10 +020024#include <asm/arch/usb_phy.h>
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020025#ifndef CONFIG_ARM64
26#include <asm/armv7.h>
27#endif
Hans de Goeded9d05652015-04-23 23:23:50 +020028#include <asm/gpio.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020029#include <asm/io.h>
Hans de Goedea146c502016-07-09 09:56:56 +020030#include <environment.h>
Hans de Goededa0ff7c2016-06-26 13:34:42 +020031#include <libfdt.h>
Hans de Goede5ed52f62015-08-15 11:55:26 +020032#include <nand.h>
Jonathan Liuabc1aae2014-06-14 08:59:09 +020033#include <net.h>
Jelle van der Waa3f3a3092016-02-23 18:47:19 +010034#include <sy8106a.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010035
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010036#if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD)
37/* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */
38int soft_i2c_gpio_sda;
39int soft_i2c_gpio_scl;
Hans de Goeded9d05652015-04-23 23:23:50 +020040
41static int soft_i2c_board_init(void)
42{
43 int ret;
44
45 soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA);
46 if (soft_i2c_gpio_sda < 0) {
47 printf("Error invalid soft i2c sda pin: '%s', err %d\n",
48 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda);
49 return soft_i2c_gpio_sda;
50 }
51 ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda");
52 if (ret) {
53 printf("Error requesting soft i2c sda pin: '%s', err %d\n",
54 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret);
55 return ret;
56 }
57
58 soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL);
59 if (soft_i2c_gpio_scl < 0) {
60 printf("Error invalid soft i2c scl pin: '%s', err %d\n",
61 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl);
62 return soft_i2c_gpio_scl;
63 }
64 ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl");
65 if (ret) {
66 printf("Error requesting soft i2c scl pin: '%s', err %d\n",
67 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret);
68 return ret;
69 }
70
71 return 0;
72}
73#else
74static int soft_i2c_board_init(void) { return 0; }
Hans de Goedea5b4cfe2015-02-16 17:23:25 +010075#endif
76
Ian Campbell6efe3692014-05-05 11:52:26 +010077DECLARE_GLOBAL_DATA_PTR;
78
79/* add board specific code here */
80int board_init(void)
81{
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020082 __maybe_unused int id_pfr1, ret;
Ian Campbell6efe3692014-05-05 11:52:26 +010083
84 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
85
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020086#ifndef CONFIG_ARM64
Ian Campbell6efe3692014-05-05 11:52:26 +010087 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
88 debug("id_pfr1: 0x%08x\n", id_pfr1);
89 /* Generic Timer Extension available? */
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020090 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
91 uint32_t freq;
92
Ian Campbell6efe3692014-05-05 11:52:26 +010093 debug("Setting CNTFRQ\n");
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +020094
95 /*
96 * CNTFRQ is a secure register, so we will crash if we try to
97 * write this from the non-secure world (read is OK, though).
98 * In case some bootcode has already set the correct value,
99 * we avoid the risk of writing to it.
100 */
101 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
102 if (freq != CONFIG_TIMER_CLK_FREQ) {
103 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
104 freq, CONFIG_TIMER_CLK_FREQ);
105#ifdef CONFIG_NON_SECURE
106 printf("arch timer frequency is wrong, but cannot adjust it\n");
107#else
108 asm volatile("mcr p15, 0, %0, c14, c0, 0"
109 : : "r"(CONFIG_TIMER_CLK_FREQ));
110#endif
111 }
Ian Campbell6efe3692014-05-05 11:52:26 +0100112 }
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200113#endif /* !CONFIG_ARM64 */
Ian Campbell6efe3692014-05-05 11:52:26 +0100114
Hans de Goede3ae1d132015-04-25 17:25:14 +0200115 ret = axp_gpio_init();
116 if (ret)
117 return ret;
118
Hans de Goede9c34c3e2016-03-22 20:10:30 +0100119#ifdef CONFIG_SATAPWR
120 gpio_request(CONFIG_SATAPWR, "satapwr");
121 gpio_direction_output(CONFIG_SATAPWR, 1);
122#endif
Hans de Goede42cbbe32016-03-17 13:53:03 +0100123#ifdef CONFIG_MACPWR
124 gpio_request(CONFIG_MACPWR, "macpwr");
125 gpio_direction_output(CONFIG_MACPWR, 1);
126#endif
127
Hans de Goeded9d05652015-04-23 23:23:50 +0200128 /* Uses dm gpio code so do this here and not in i2c_init_board() */
129 return soft_i2c_board_init();
Ian Campbell6efe3692014-05-05 11:52:26 +0100130}
131
132int dram_init(void)
133{
134 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0, PHYS_SDRAM_0_SIZE);
135
136 return 0;
137}
138
Hans de Goede3ce35f92015-08-16 14:48:22 +0200139#if defined(CONFIG_NAND_SUNXI) && defined(CONFIG_SPL_BUILD)
Karol Gugala7bea8932015-07-23 14:33:01 +0200140static void nand_pinmux_setup(void)
141{
142 unsigned int pin;
Karol Gugala7bea8932015-07-23 14:33:01 +0200143
Hans de Goeded2236782015-08-15 13:17:49 +0200144 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
Karol Gugala7bea8932015-07-23 14:33:01 +0200145 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
146
Hans de Goeded2236782015-08-15 13:17:49 +0200147#if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
148 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
149 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
150#endif
151 /* sun4i / sun7i do have a PC23, but it is not used for nand,
152 * only sun7i has a PC24 */
153#ifdef CONFIG_MACH_SUN7I
Karol Gugala7bea8932015-07-23 14:33:01 +0200154 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
Hans de Goeded2236782015-08-15 13:17:49 +0200155#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200156}
157
158static void nand_clock_setup(void)
159{
160 struct sunxi_ccm_reg *const ccm =
161 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goedee5561a82015-08-15 11:58:03 +0200162
Karol Gugala7bea8932015-07-23 14:33:01 +0200163 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
Hans de Goedee5561a82015-08-15 11:58:03 +0200164#ifdef CONFIG_MACH_SUN9I
165 setbits_le32(&ccm->ahb_gate1, (1 << AHB_GATE_OFFSET_DMA));
166#else
167 setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_DMA));
168#endif
Karol Gugala7bea8932015-07-23 14:33:01 +0200169 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
170}
Hans de Goede5ed52f62015-08-15 11:55:26 +0200171
172void board_nand_init(void)
173{
174 nand_pinmux_setup();
175 nand_clock_setup();
176}
Karol Gugala7bea8932015-07-23 14:33:01 +0200177#endif
178
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100179#ifdef CONFIG_GENERIC_MMC
180static void mmc_pinmux_setup(int sdc)
181{
182 unsigned int pin;
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100183 __maybe_unused int pins;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100184
185 switch (sdc) {
186 case 0:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100187 /* SDC0: PF0-PF5 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100188 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100189 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100190 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
191 sunxi_gpio_set_drv(pin, 2);
192 }
193 break;
194
195 case 1:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100196 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS);
197
198#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
199 if (pins == SUNXI_GPIO_H) {
200 /* SDC1: PH22-PH-27 */
201 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
202 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1);
203 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
204 sunxi_gpio_set_drv(pin, 2);
205 }
206 } else {
207 /* SDC1: PG0-PG5 */
208 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
209 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1);
210 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
211 sunxi_gpio_set_drv(pin, 2);
212 }
213 }
214#elif defined(CONFIG_MACH_SUN5I)
215 /* SDC1: PG3-PG8 */
Hans de Goede4dccfd42014-10-03 16:44:57 +0200216 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100217 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100218 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
219 sunxi_gpio_set_drv(pin, 2);
220 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100221#elif defined(CONFIG_MACH_SUN6I)
222 /* SDC1: PG0-PG5 */
223 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
224 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
225 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
226 sunxi_gpio_set_drv(pin, 2);
227 }
228#elif defined(CONFIG_MACH_SUN8I)
229 if (pins == SUNXI_GPIO_D) {
230 /* SDC1: PD2-PD7 */
231 for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) {
232 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1);
233 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
234 sunxi_gpio_set_drv(pin, 2);
235 }
236 } else {
237 /* SDC1: PG0-PG5 */
238 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
239 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
240 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
241 sunxi_gpio_set_drv(pin, 2);
242 }
243 }
244#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100245 break;
246
247 case 2:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100248 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS);
249
250#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
251 /* SDC2: PC6-PC11 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100252 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
Paul Kocialkowskiae358a42015-03-22 18:12:22 +0100253 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100254 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
255 sunxi_gpio_set_drv(pin, 2);
256 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100257#elif defined(CONFIG_MACH_SUN5I)
258 if (pins == SUNXI_GPIO_E) {
259 /* SDC2: PE4-PE9 */
260 for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) {
261 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2);
262 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
263 sunxi_gpio_set_drv(pin, 2);
264 }
265 } else {
266 /* SDC2: PC6-PC15 */
267 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
268 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
269 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
270 sunxi_gpio_set_drv(pin, 2);
271 }
272 }
273#elif defined(CONFIG_MACH_SUN6I)
274 if (pins == SUNXI_GPIO_A) {
275 /* SDC2: PA9-PA14 */
276 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
277 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2);
278 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
279 sunxi_gpio_set_drv(pin, 2);
280 }
281 } else {
282 /* SDC2: PC6-PC15, PC24 */
283 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
284 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
285 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
286 sunxi_gpio_set_drv(pin, 2);
287 }
288
289 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
290 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
291 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
292 }
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200293#elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100294 /* SDC2: PC5-PC6, PC8-PC16 */
295 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
296 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
297 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
298 sunxi_gpio_set_drv(pin, 2);
299 }
300
301 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
302 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
303 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
304 sunxi_gpio_set_drv(pin, 2);
305 }
306#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100307 break;
308
309 case 3:
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100310 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS);
311
312#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
313 /* SDC3: PI4-PI9 */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100314 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100315 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100316 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
317 sunxi_gpio_set_drv(pin, 2);
318 }
Paul Kocialkowskid390d8c2015-03-22 18:12:23 +0100319#elif defined(CONFIG_MACH_SUN6I)
320 if (pins == SUNXI_GPIO_A) {
321 /* SDC3: PA9-PA14 */
322 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
323 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3);
324 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
325 sunxi_gpio_set_drv(pin, 2);
326 }
327 } else {
328 /* SDC3: PC6-PC15, PC24 */
329 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
330 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
331 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
332 sunxi_gpio_set_drv(pin, 2);
333 }
334
335 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
336 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
337 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
338 }
339#endif
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100340 break;
341
342 default:
343 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
344 break;
345 }
346}
347
348int board_mmc_init(bd_t *bis)
349{
Hans de Goede63deaa82014-10-02 21:13:54 +0200350 __maybe_unused struct mmc *mmc0, *mmc1;
351 __maybe_unused char buf[512];
352
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100353 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
Hans de Goede63deaa82014-10-02 21:13:54 +0200354 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
355 if (!mmc0)
356 return -1;
357
Hans de Goedeaf593e42014-10-02 20:43:50 +0200358#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100359 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
Hans de Goede63deaa82014-10-02 21:13:54 +0200360 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
361 if (!mmc1)
362 return -1;
363#endif
364
Daniel Kochmański25d9a6d2015-05-29 17:21:00 +0200365#if !defined(CONFIG_SPL_BUILD) && CONFIG_MMC_SUNXI_SLOT_EXTRA == 2
Hans de Goede63deaa82014-10-02 21:13:54 +0200366 /*
Daniel Kochmański25d9a6d2015-05-29 17:21:00 +0200367 * On systems with an emmc (mmc2), figure out if we are booting from
368 * the emmc and if we are make it "mmc dev 0" so that boot.scr, etc.
369 * are searched there first. Note we only do this for u-boot proper,
370 * not for the SPL, see spl_boot_device().
Hans de Goede63deaa82014-10-02 21:13:54 +0200371 */
Daniel Kochmański25d9a6d2015-05-29 17:21:00 +0200372 if (!sunxi_mmc_has_egon_boot_signature(mmc0) &&
373 sunxi_mmc_has_egon_boot_signature(mmc1)) {
374 /* Booting from emmc / mmc2, swap */
Simon Glass2f26fff2016-02-29 15:25:51 -0700375 mmc0->block_dev.devnum = 1;
376 mmc1->block_dev.devnum = 0;
Daniel Kochmański25d9a6d2015-05-29 17:21:00 +0200377 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100378#endif
379
380 return 0;
381}
382#endif
383
Hans de Goede3352b222014-06-13 22:55:49 +0200384void i2c_init_board(void)
385{
Paul Kocialkowski0a3ec0a2015-04-10 23:09:52 +0200386#ifdef CONFIG_I2C0_ENABLE
387#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I)
388 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
389 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
390 clock_twi_onoff(0, 1);
391#elif defined(CONFIG_MACH_SUN6I)
392 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
393 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
394 clock_twi_onoff(0, 1);
395#elif defined(CONFIG_MACH_SUN8I)
396 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
397 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
Hans de Goede3352b222014-06-13 22:55:49 +0200398 clock_twi_onoff(0, 1);
Paul Kocialkowski0a3ec0a2015-04-10 23:09:52 +0200399#endif
400#endif
401
402#ifdef CONFIG_I2C1_ENABLE
403#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
404 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
405 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
406 clock_twi_onoff(1, 1);
407#elif defined(CONFIG_MACH_SUN5I)
408 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
409 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
410 clock_twi_onoff(1, 1);
411#elif defined(CONFIG_MACH_SUN6I)
412 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
413 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
414 clock_twi_onoff(1, 1);
415#elif defined(CONFIG_MACH_SUN8I)
416 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
417 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
418 clock_twi_onoff(1, 1);
419#endif
420#endif
421
422#ifdef CONFIG_I2C2_ENABLE
423#if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
424 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
425 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
426 clock_twi_onoff(2, 1);
427#elif defined(CONFIG_MACH_SUN5I)
428 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
429 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
430 clock_twi_onoff(2, 1);
431#elif defined(CONFIG_MACH_SUN6I)
432 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
433 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
434 clock_twi_onoff(2, 1);
435#elif defined(CONFIG_MACH_SUN8I)
436 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
437 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
438 clock_twi_onoff(2, 1);
439#endif
440#endif
441
442#ifdef CONFIG_I2C3_ENABLE
443#if defined(CONFIG_MACH_SUN6I)
444 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
445 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
446 clock_twi_onoff(3, 1);
447#elif defined(CONFIG_MACH_SUN7I)
448 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
449 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
450 clock_twi_onoff(3, 1);
451#endif
452#endif
453
454#ifdef CONFIG_I2C4_ENABLE
455#if defined(CONFIG_MACH_SUN7I)
456 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
457 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
458 clock_twi_onoff(4, 1);
459#endif
460#endif
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100461
462#ifdef CONFIG_R_I2C_ENABLE
463 clock_twi_onoff(5, 1);
464 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
465 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
466#endif
Hans de Goede3352b222014-06-13 22:55:49 +0200467}
468
Ian Campbell6efe3692014-05-05 11:52:26 +0100469#ifdef CONFIG_SPL_BUILD
470void sunxi_board_init(void)
471{
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200472 int power_failed = 0;
Ian Campbell6efe3692014-05-05 11:52:26 +0100473 unsigned long ramsize;
474
Jelle van der Waa3f3a3092016-02-23 18:47:19 +0100475#ifdef CONFIG_SY8106A_POWER
476 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
477#endif
478
vishnupatekar1895dfd2015-11-29 01:07:22 +0800479#if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800480 defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
481 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200482 power_failed = axp_init();
483
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800484#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
485 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200486 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Hans de Goede1f247362014-06-13 22:55:51 +0200487#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200488 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
489 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
vishnupatekar1895dfd2015-11-29 01:07:22 +0800490#if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200491 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200492#endif
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800493#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
494 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200495 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
Oliver Schinagld3a558d2013-07-26 12:56:58 +0200496#endif
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200497
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800498#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
499 defined CONFIG_AXP818_POWER
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200500 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
501#endif
502 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
Chen-Yu Tsaic05aa392016-01-12 14:42:40 +0800503#if !defined(CONFIG_AXP152_POWER)
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200504 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
505#endif
506#ifdef CONFIG_AXP209_POWER
507 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
508#endif
509
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800510#if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
511 defined(CONFIG_AXP818_POWER)
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800512 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
513 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800514#if !defined CONFIG_AXP809_POWER
Chen-Yu Tsai2e6911f2016-01-12 14:42:37 +0800515 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
516 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800517#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200518 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
519 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
520 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
521#endif
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800522
523#ifdef CONFIG_AXP818_POWER
524 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
525 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
526 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
Chen-Yu Tsaif1e66e72016-05-02 10:28:15 +0800527#endif
528
529#if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
Chen-Yu Tsai0e3efd32016-05-02 10:28:12 +0800530 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
Chen-Yu Tsaid028fba2016-03-30 00:26:48 +0800531#endif
Hans de Goeded9ee84b2015-10-03 15:18:33 +0200532#endif
Ian Campbell6efe3692014-05-05 11:52:26 +0100533 printf("DRAM:");
534 ramsize = sunxi_dram_init();
Hans de Goede42ad8c52016-06-26 13:56:01 +0200535 printf(" %d MiB\n", (int)(ramsize >> 20));
Ian Campbell6efe3692014-05-05 11:52:26 +0100536 if (!ramsize)
537 hang();
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200538
539 /*
540 * Only clock up the CPU to full speed if we are reasonably
541 * assured it's being powered with suitable core voltage
542 */
543 if (!power_failed)
Iain Paton630df142015-03-28 10:26:38 +0000544 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
Henrik Nordstromaa382ad2014-06-13 22:55:50 +0200545 else
546 printf("Failed to set core voltage! Can't set CPU frequency\n");
Ian Campbell6efe3692014-05-05 11:52:26 +0100547}
548#endif
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200549
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100550#ifdef CONFIG_USB_GADGET
551int g_dnl_board_usb_cable_connected(void)
552{
Paul Kocialkowski61c73ee2015-05-16 19:52:10 +0200553 return sunxi_usb_phy_vbus_detect(0);
Paul Kocialkowskidbbccaf2015-03-22 18:07:13 +0100554}
555#endif
556
Paul Kocialkowski99ae0f62015-03-28 18:35:36 +0100557#ifdef CONFIG_SERIAL_TAG
558void get_board_serial(struct tag_serialnr *serialnr)
559{
560 char *serial_string;
561 unsigned long long serial;
562
563 serial_string = getenv("serial#");
564
565 if (serial_string) {
566 serial = simple_strtoull(serial_string, NULL, 16);
567
568 serialnr->high = (unsigned int) (serial >> 32);
569 serialnr->low = (unsigned int) (serial & 0xffffffff);
570 } else {
571 serialnr->high = 0;
572 serialnr->low = 0;
573 }
574}
575#endif
576
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200577/*
578 * Check the SPL header for the "sunxi" variant. If found: parse values
579 * that might have been passed by the loader ("fel" utility), and update
580 * the environment accordingly.
581 */
582static void parse_spl_header(const uint32_t spl_addr)
583{
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200584 struct boot_file_head *spl = (void *)(ulong)spl_addr;
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200585 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
586 return; /* signature mismatch, no usable header */
587
588 uint8_t spl_header_version = spl->spl_signature[3];
589 if (spl_header_version != SPL_HEADER_VERSION) {
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200590 printf("sunxi SPL version mismatch: expected %u, got %u\n",
591 SPL_HEADER_VERSION, spl_header_version);
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200592 return;
593 }
594 if (!spl->fel_script_address)
595 return;
596
597 if (spl->fel_uEnv_length != 0) {
598 /*
599 * data is expected in uEnv.txt compatible format, so "env
600 * import -t" the string(s) at fel_script_address right away.
601 */
602 himport_r(&env_htab, (char *)spl->fel_script_address,
603 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
604 return;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200605 }
Bernhard Nortmanne9bbbe82016-06-09 07:37:35 +0200606 /* otherwise assume .scr format (mkimage-type script) */
607 setenv_hex("fel_scriptaddr", spl->fel_script_address);
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200608}
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200609
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200610/*
611 * Note this function gets called multiple times.
612 * It must not make any changes to env variables which already exist.
613 */
614static void setup_environment(const void *fdt)
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200615{
Paul Kocialkowski92935942015-03-28 18:35:35 +0100616 char serial_string[17] = { 0 };
Hans de Goede11d70982014-11-26 00:04:24 +0100617 unsigned int sid[4];
Paul Kocialkowski92935942015-03-28 18:35:35 +0100618 uint8_t mac_addr[6];
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200619 char ethaddr[16];
620 int i, ret;
Bernhard Nortmannead498a2015-09-17 18:52:52 +0200621
Paul Kocialkowski92935942015-03-28 18:35:35 +0100622 ret = sunxi_get_sid(sid);
623 if (ret == 0 && sid[0] != 0 && sid[3] != 0) {
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200624 for (i = 0; i < 4; i++) {
625 sprintf(ethaddr, "ethernet%d", i);
626 if (!fdt_get_alias(fdt, ethaddr))
627 continue;
628
629 if (i == 0)
630 strcpy(ethaddr, "ethaddr");
631 else
632 sprintf(ethaddr, "eth%daddr", i);
633
634 if (getenv(ethaddr))
635 continue;
636
Paul Kocialkowski92935942015-03-28 18:35:35 +0100637 /* Non OUI / registered MAC address */
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200638 mac_addr[0] = (i << 4) | 0x02;
Paul Kocialkowski92935942015-03-28 18:35:35 +0100639 mac_addr[1] = (sid[0] >> 0) & 0xff;
640 mac_addr[2] = (sid[3] >> 24) & 0xff;
641 mac_addr[3] = (sid[3] >> 16) & 0xff;
642 mac_addr[4] = (sid[3] >> 8) & 0xff;
643 mac_addr[5] = (sid[3] >> 0) & 0xff;
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200644
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200645 eth_setenv_enetaddr(ethaddr, mac_addr);
Paul Kocialkowski92935942015-03-28 18:35:35 +0100646 }
647
648 if (!getenv("serial#")) {
649 snprintf(serial_string, sizeof(serial_string),
650 "%08x%08x", sid[0], sid[3]);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200651
Paul Kocialkowski92935942015-03-28 18:35:35 +0100652 setenv("serial#", serial_string);
653 }
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200654 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200655}
656
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200657int misc_init_r(void)
658{
659 __maybe_unused int ret;
660
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200661 setenv("fel_booted", NULL);
662 setenv("fel_scriptaddr", NULL);
663 /* determine if we are running in FEL mode */
664 if (!is_boot0_magic(SPL_ADDR + 4)) { /* eGON.BT0 */
665 setenv("fel_booted", "1");
666 parse_spl_header(SPL_ADDR);
667 }
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200668
669 setup_environment(gd->fdt_blob);
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200670
Hans de Goede7bfe2bb2015-01-13 19:25:06 +0100671#ifndef CONFIG_MACH_SUN9I
Hans de Goede1168e092015-04-27 16:50:04 +0200672 ret = sunxi_usb_phy_probe();
673 if (ret)
674 return ret;
Hans de Goede7bfe2bb2015-01-13 19:25:06 +0100675#endif
Hans de Goedeea059bf2015-06-17 15:49:26 +0200676 sunxi_musb_board_init();
677
Jonathan Liuabc1aae2014-06-14 08:59:09 +0200678 return 0;
679}
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200680
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200681int ft_board_setup(void *blob, bd_t *bd)
682{
Hans de Goede48a234a2016-03-22 22:51:52 +0100683 int __maybe_unused r;
684
Hans de Goededa0ff7c2016-06-26 13:34:42 +0200685 /*
686 * Call setup_environment again in case the boot fdt has
687 * ethernet aliases the u-boot copy does not have.
688 */
689 setup_environment(blob);
690
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200691#ifdef CONFIG_VIDEO_DT_SIMPLEFB
Hans de Goede48a234a2016-03-22 22:51:52 +0100692 r = sunxi_simplefb_setup(blob);
693 if (r)
694 return r;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200695#endif
Hans de Goede48a234a2016-03-22 22:51:52 +0100696 return 0;
Luc Verhaegen4869a8c2014-08-13 07:55:07 +0200697}