sunxi: Properly check for SATAPWR and MACPWR
[platform/kernel/u-boot.git] / board / sunxi / board.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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.
11  */
12
13 #include <common.h>
14 #include <dm.h>
15 #include <env.h>
16 #include <hang.h>
17 #include <image.h>
18 #include <init.h>
19 #include <log.h>
20 #include <mmc.h>
21 #include <axp_pmic.h>
22 #include <generic-phy.h>
23 #include <phy-sun4i-usb.h>
24 #include <asm/arch/clock.h>
25 #include <asm/arch/cpu.h>
26 #include <asm/arch/display.h>
27 #include <asm/arch/dram.h>
28 #include <asm/arch/gpio.h>
29 #include <asm/arch/mmc.h>
30 #include <asm/arch/prcm.h>
31 #include <asm/arch/spl.h>
32 #include <linux/delay.h>
33 #include <u-boot/crc.h>
34 #ifndef CONFIG_ARM64
35 #include <asm/armv7.h>
36 #endif
37 #include <asm/gpio.h>
38 #include <asm/io.h>
39 #include <u-boot/crc.h>
40 #include <env_internal.h>
41 #include <linux/libfdt.h>
42 #include <fdt_support.h>
43 #include <nand.h>
44 #include <net.h>
45 #include <spl.h>
46 #include <sy8106a.h>
47 #include <asm/setup.h>
48
49 #if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD)
50 /* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */
51 int soft_i2c_gpio_sda;
52 int soft_i2c_gpio_scl;
53
54 static int soft_i2c_board_init(void)
55 {
56         int ret;
57
58         soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA);
59         if (soft_i2c_gpio_sda < 0) {
60                 printf("Error invalid soft i2c sda pin: '%s', err %d\n",
61                        CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda);
62                 return soft_i2c_gpio_sda;
63         }
64         ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda");
65         if (ret) {
66                 printf("Error requesting soft i2c sda pin: '%s', err %d\n",
67                        CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret);
68                 return ret;
69         }
70
71         soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL);
72         if (soft_i2c_gpio_scl < 0) {
73                 printf("Error invalid soft i2c scl pin: '%s', err %d\n",
74                        CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl);
75                 return soft_i2c_gpio_scl;
76         }
77         ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl");
78         if (ret) {
79                 printf("Error requesting soft i2c scl pin: '%s', err %d\n",
80                        CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret);
81                 return ret;
82         }
83
84         return 0;
85 }
86 #else
87 static int soft_i2c_board_init(void) { return 0; }
88 #endif
89
90 DECLARE_GLOBAL_DATA_PTR;
91
92 void i2c_init_board(void)
93 {
94 #ifdef CONFIG_I2C0_ENABLE
95 #if defined(CONFIG_MACH_SUN4I) || \
96     defined(CONFIG_MACH_SUN5I) || \
97     defined(CONFIG_MACH_SUN7I) || \
98     defined(CONFIG_MACH_SUN8I_R40)
99         sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0);
100         sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0);
101         clock_twi_onoff(0, 1);
102 #elif defined(CONFIG_MACH_SUN6I)
103         sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0);
104         sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0);
105         clock_twi_onoff(0, 1);
106 #elif defined(CONFIG_MACH_SUN8I_V3S)
107         sunxi_gpio_set_cfgpin(SUNXI_GPB(6), SUN8I_V3S_GPB_TWI0);
108         sunxi_gpio_set_cfgpin(SUNXI_GPB(7), SUN8I_V3S_GPB_TWI0);
109         clock_twi_onoff(0, 1);
110 #elif defined(CONFIG_MACH_SUN8I)
111         sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0);
112         sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0);
113         clock_twi_onoff(0, 1);
114 #elif defined(CONFIG_MACH_SUN50I)
115         sunxi_gpio_set_cfgpin(SUNXI_GPH(0), SUN50I_GPH_TWI0);
116         sunxi_gpio_set_cfgpin(SUNXI_GPH(1), SUN50I_GPH_TWI0);
117         clock_twi_onoff(0, 1);
118 #endif
119 #endif
120
121 #ifdef CONFIG_I2C1_ENABLE
122 #if defined(CONFIG_MACH_SUN4I) || \
123     defined(CONFIG_MACH_SUN7I) || \
124     defined(CONFIG_MACH_SUN8I_R40)
125         sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1);
126         sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1);
127         clock_twi_onoff(1, 1);
128 #elif defined(CONFIG_MACH_SUN5I)
129         sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1);
130         sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1);
131         clock_twi_onoff(1, 1);
132 #elif defined(CONFIG_MACH_SUN6I)
133         sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1);
134         sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1);
135         clock_twi_onoff(1, 1);
136 #elif defined(CONFIG_MACH_SUN8I)
137         sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1);
138         sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1);
139         clock_twi_onoff(1, 1);
140 #elif defined(CONFIG_MACH_SUN50I)
141         sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN50I_GPH_TWI1);
142         sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
143         clock_twi_onoff(1, 1);
144 #endif
145 #endif
146
147 #ifdef CONFIG_I2C2_ENABLE
148 #if defined(CONFIG_MACH_SUN4I) || \
149     defined(CONFIG_MACH_SUN7I) || \
150     defined(CONFIG_MACH_SUN8I_R40)
151         sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2);
152         sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2);
153         clock_twi_onoff(2, 1);
154 #elif defined(CONFIG_MACH_SUN5I)
155         sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2);
156         sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2);
157         clock_twi_onoff(2, 1);
158 #elif defined(CONFIG_MACH_SUN6I)
159         sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2);
160         sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2);
161         clock_twi_onoff(2, 1);
162 #elif defined(CONFIG_MACH_SUN8I)
163         sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2);
164         sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2);
165         clock_twi_onoff(2, 1);
166 #elif defined(CONFIG_MACH_SUN50I)
167         sunxi_gpio_set_cfgpin(SUNXI_GPE(14), SUN50I_GPE_TWI2);
168         sunxi_gpio_set_cfgpin(SUNXI_GPE(15), SUN50I_GPE_TWI2);
169         clock_twi_onoff(2, 1);
170 #endif
171 #endif
172
173 #ifdef CONFIG_I2C3_ENABLE
174 #if defined(CONFIG_MACH_SUN6I)
175         sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3);
176         sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3);
177         clock_twi_onoff(3, 1);
178 #elif defined(CONFIG_MACH_SUN7I) || \
179       defined(CONFIG_MACH_SUN8I_R40)
180         sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3);
181         sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3);
182         clock_twi_onoff(3, 1);
183 #endif
184 #endif
185
186 #ifdef CONFIG_I2C4_ENABLE
187 #if defined(CONFIG_MACH_SUN7I) || \
188     defined(CONFIG_MACH_SUN8I_R40)
189         sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4);
190         sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4);
191         clock_twi_onoff(4, 1);
192 #endif
193 #endif
194
195 #ifdef CONFIG_R_I2C_ENABLE
196 #ifdef CONFIG_MACH_SUN50I
197         clock_twi_onoff(5, 1);
198         sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
199         sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
200 #else
201         clock_twi_onoff(5, 1);
202         sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI);
203         sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI);
204 #endif
205 #endif
206 }
207
208 #if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT)
209 enum env_location env_get_location(enum env_operation op, int prio)
210 {
211         switch (prio) {
212         case 0:
213                 return ENVL_FAT;
214
215         case 1:
216                 return ENVL_MMC;
217
218         default:
219                 return ENVL_UNKNOWN;
220         }
221 }
222 #endif
223
224 #ifdef CONFIG_DM_MMC
225 static void mmc_pinmux_setup(int sdc);
226 #endif
227
228 /* add board specific code here */
229 int board_init(void)
230 {
231         __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin;
232
233         gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100);
234
235 #ifndef CONFIG_ARM64
236         asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
237         debug("id_pfr1: 0x%08x\n", id_pfr1);
238         /* Generic Timer Extension available? */
239         if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) {
240                 uint32_t freq;
241
242                 debug("Setting CNTFRQ\n");
243
244                 /*
245                  * CNTFRQ is a secure register, so we will crash if we try to
246                  * write this from the non-secure world (read is OK, though).
247                  * In case some bootcode has already set the correct value,
248                  * we avoid the risk of writing to it.
249                  */
250                 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq));
251                 if (freq != COUNTER_FREQUENCY) {
252                         debug("arch timer frequency is %d Hz, should be %d, fixing ...\n",
253                               freq, COUNTER_FREQUENCY);
254 #ifdef CONFIG_NON_SECURE
255                         printf("arch timer frequency is wrong, but cannot adjust it\n");
256 #else
257                         asm volatile("mcr p15, 0, %0, c14, c0, 0"
258                                      : : "r"(COUNTER_FREQUENCY));
259 #endif
260                 }
261         }
262 #endif /* !CONFIG_ARM64 */
263
264         ret = axp_gpio_init();
265         if (ret)
266                 return ret;
267
268         /* strcmp() would look better, but doesn't get optimised away. */
269         if (CONFIG_SATAPWR[0]) {
270                 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR);
271                 if (satapwr_pin >= 0) {
272                         gpio_request(satapwr_pin, "satapwr");
273                         gpio_direction_output(satapwr_pin, 1);
274
275                         /*
276                          * Give the attached SATA device time to power-up
277                          * to avoid link timeouts
278                          */
279                         mdelay(500);
280                 }
281         }
282
283         if (CONFIG_MACPWR[0]) {
284                 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR);
285                 if (macpwr_pin >= 0) {
286                         gpio_request(macpwr_pin, "macpwr");
287                         gpio_direction_output(macpwr_pin, 1);
288                 }
289         }
290
291 #ifdef CONFIG_DM_I2C
292         /*
293          * Temporary workaround for enabling I2C clocks until proper sunxi DM
294          * clk, reset and pinctrl drivers land.
295          */
296         i2c_init_board();
297 #endif
298
299 #ifdef CONFIG_DM_MMC
300         /*
301          * Temporary workaround for enabling MMC clocks until a sunxi DM
302          * pinctrl driver lands.
303          */
304         mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
305 #if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
306         mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
307 #endif
308 #endif  /* CONFIG_DM_MMC */
309
310         /* Uses dm gpio code so do this here and not in i2c_init_board() */
311         return soft_i2c_board_init();
312 }
313
314 /*
315  * On older SoCs the SPL is actually at address zero, so using NULL as
316  * an error value does not work.
317  */
318 #define INVALID_SPL_HEADER ((void *)~0UL)
319
320 static struct boot_file_head * get_spl_header(uint8_t req_version)
321 {
322         struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
323         uint8_t spl_header_version = spl->spl_signature[3];
324
325         /* Is there really the SPL header (still) there? */
326         if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
327                 return INVALID_SPL_HEADER;
328
329         if (spl_header_version < req_version) {
330                 printf("sunxi SPL version mismatch: expected %u, got %u\n",
331                        req_version, spl_header_version);
332                 return INVALID_SPL_HEADER;
333         }
334
335         return spl;
336 }
337
338 static const char *get_spl_dt_name(void)
339 {
340         struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
341
342         /* Check if there is a DT name stored in the SPL header. */
343         if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
344                 return (char *)spl + spl->dt_name_offset;
345
346         return NULL;
347 }
348
349 int dram_init(void)
350 {
351         struct boot_file_head *spl = get_spl_header(SPL_DRAM_HEADER_VERSION);
352
353         if (spl == INVALID_SPL_HEADER)
354                 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0,
355                                             PHYS_SDRAM_0_SIZE);
356         else
357                 gd->ram_size = (phys_addr_t)spl->dram_size << 20;
358
359         if (gd->ram_size > CONFIG_SUNXI_DRAM_MAX_SIZE)
360                 gd->ram_size = CONFIG_SUNXI_DRAM_MAX_SIZE;
361
362         return 0;
363 }
364
365 #if defined(CONFIG_NAND_SUNXI)
366 static void nand_pinmux_setup(void)
367 {
368         unsigned int pin;
369
370         for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++)
371                 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
372
373 #if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I
374         for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++)
375                 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND);
376 #endif
377         /* sun4i / sun7i do have a PC23, but it is not used for nand,
378          * only sun7i has a PC24 */
379 #ifdef CONFIG_MACH_SUN7I
380         sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND);
381 #endif
382 }
383
384 static void nand_clock_setup(void)
385 {
386         struct sunxi_ccm_reg *const ccm =
387                 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
388
389         setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0));
390 #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I || \
391     defined CONFIG_MACH_SUN9I || defined CONFIG_MACH_SUN50I
392         setbits_le32(&ccm->ahb_reset0_cfg, (1 << AHB_GATE_OFFSET_NAND0));
393 #endif
394         setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1);
395 }
396
397 void board_nand_init(void)
398 {
399         nand_pinmux_setup();
400         nand_clock_setup();
401 #ifndef CONFIG_SPL_BUILD
402         sunxi_nand_init();
403 #endif
404 }
405 #endif
406
407 #ifdef CONFIG_MMC
408 static void mmc_pinmux_setup(int sdc)
409 {
410         unsigned int pin;
411         __maybe_unused int pins;
412
413         switch (sdc) {
414         case 0:
415                 /* SDC0: PF0-PF5 */
416                 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) {
417                         sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0);
418                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
419                         sunxi_gpio_set_drv(pin, 2);
420                 }
421                 break;
422
423         case 1:
424                 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS);
425
426 #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
427     defined(CONFIG_MACH_SUN8I_R40)
428                 if (pins == SUNXI_GPIO_H) {
429                         /* SDC1: PH22-PH-27 */
430                         for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) {
431                                 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_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, SUN4I_GPG_SDC1);
439                                 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
440                                 sunxi_gpio_set_drv(pin, 2);
441                         }
442                 }
443 #elif defined(CONFIG_MACH_SUN5I)
444                 /* SDC1: PG3-PG8 */
445                 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) {
446                         sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1);
447                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
448                         sunxi_gpio_set_drv(pin, 2);
449                 }
450 #elif defined(CONFIG_MACH_SUN6I)
451                 /* SDC1: PG0-PG5 */
452                 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
453                         sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1);
454                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
455                         sunxi_gpio_set_drv(pin, 2);
456                 }
457 #elif defined(CONFIG_MACH_SUN8I)
458                 if (pins == SUNXI_GPIO_D) {
459                         /* SDC1: PD2-PD7 */
460                         for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) {
461                                 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1);
462                                 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
463                                 sunxi_gpio_set_drv(pin, 2);
464                         }
465                 } else {
466                         /* SDC1: PG0-PG5 */
467                         for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) {
468                                 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1);
469                                 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
470                                 sunxi_gpio_set_drv(pin, 2);
471                         }
472                 }
473 #endif
474                 break;
475
476         case 2:
477                 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS);
478
479 #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
480                 /* SDC2: PC6-PC11 */
481                 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) {
482                         sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
483                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
484                         sunxi_gpio_set_drv(pin, 2);
485                 }
486 #elif defined(CONFIG_MACH_SUN5I)
487                 if (pins == SUNXI_GPIO_E) {
488                         /* SDC2: PE4-PE9 */
489                         for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) {
490                                 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2);
491                                 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
492                                 sunxi_gpio_set_drv(pin, 2);
493                         }
494                 } else {
495                         /* SDC2: PC6-PC15 */
496                         for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
497                                 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
498                                 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
499                                 sunxi_gpio_set_drv(pin, 2);
500                         }
501                 }
502 #elif defined(CONFIG_MACH_SUN6I)
503                 if (pins == SUNXI_GPIO_A) {
504                         /* SDC2: PA9-PA14 */
505                         for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
506                                 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2);
507                                 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
508                                 sunxi_gpio_set_drv(pin, 2);
509                         }
510                 } else {
511                         /* SDC2: PC6-PC15, PC24 */
512                         for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
513                                 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
514                                 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
515                                 sunxi_gpio_set_drv(pin, 2);
516                         }
517
518                         sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
519                         sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
520                         sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
521                 }
522 #elif defined(CONFIG_MACH_SUN8I_R40)
523                 /* SDC2: PC6-PC15, PC24 */
524                 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
525                         sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
526                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
527                         sunxi_gpio_set_drv(pin, 2);
528                 }
529
530                 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2);
531                 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
532                 sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
533 #elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I)
534                 /* SDC2: PC5-PC6, PC8-PC16 */
535                 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) {
536                         sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
537                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
538                         sunxi_gpio_set_drv(pin, 2);
539                 }
540
541                 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) {
542                         sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
543                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
544                         sunxi_gpio_set_drv(pin, 2);
545                 }
546 #elif defined(CONFIG_MACH_SUN50I_H6)
547                 /* SDC2: PC4-PC14 */
548                 for (pin = SUNXI_GPC(4); pin <= SUNXI_GPC(14); pin++) {
549                         sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
550                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
551                         sunxi_gpio_set_drv(pin, 2);
552                 }
553 #elif defined(CONFIG_MACH_SUN9I)
554                 /* SDC2: PC6-PC16 */
555                 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) {
556                         sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2);
557                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
558                         sunxi_gpio_set_drv(pin, 2);
559                 }
560 #endif
561                 break;
562
563         case 3:
564                 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS);
565
566 #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \
567     defined(CONFIG_MACH_SUN8I_R40)
568                 /* SDC3: PI4-PI9 */
569                 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) {
570                         sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3);
571                         sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
572                         sunxi_gpio_set_drv(pin, 2);
573                 }
574 #elif defined(CONFIG_MACH_SUN6I)
575                 if (pins == SUNXI_GPIO_A) {
576                         /* SDC3: PA9-PA14 */
577                         for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) {
578                                 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3);
579                                 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
580                                 sunxi_gpio_set_drv(pin, 2);
581                         }
582                 } else {
583                         /* SDC3: PC6-PC15, PC24 */
584                         for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) {
585                                 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3);
586                                 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP);
587                                 sunxi_gpio_set_drv(pin, 2);
588                         }
589
590                         sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3);
591                         sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP);
592                         sunxi_gpio_set_drv(SUNXI_GPC(24), 2);
593                 }
594 #endif
595                 break;
596
597         default:
598                 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc);
599                 break;
600         }
601 }
602
603 int board_mmc_init(struct bd_info *bis)
604 {
605         __maybe_unused struct mmc *mmc0, *mmc1;
606
607         mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT);
608         mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT);
609         if (!mmc0)
610                 return -1;
611
612 #if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
613         mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA);
614         mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA);
615         if (!mmc1)
616                 return -1;
617 #endif
618
619         return 0;
620 }
621 #endif
622
623 #ifdef CONFIG_SPL_BUILD
624
625 static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
626 {
627         struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
628
629         if (spl == INVALID_SPL_HEADER)
630                 return;
631
632         /* Promote the header version for U-Boot proper, if needed. */
633         if (spl->spl_signature[3] < SPL_DRAM_HEADER_VERSION)
634                 spl->spl_signature[3] = SPL_DRAM_HEADER_VERSION;
635
636         spl->dram_size = dram_size >> 20;
637 }
638
639 void sunxi_board_init(void)
640 {
641         int power_failed = 0;
642
643 #ifdef CONFIG_SY8106A_POWER
644         power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT);
645 #endif
646
647 #if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
648         defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
649         defined CONFIG_AXP818_POWER
650         power_failed = axp_init();
651
652 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
653         defined CONFIG_AXP818_POWER
654         power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
655 #endif
656         power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT);
657         power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT);
658 #if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER)
659         power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT);
660 #endif
661 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
662         defined CONFIG_AXP818_POWER
663         power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT);
664 #endif
665
666 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
667         defined CONFIG_AXP818_POWER
668         power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT);
669 #endif
670         power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT);
671 #if !defined(CONFIG_AXP152_POWER)
672         power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT);
673 #endif
674 #ifdef CONFIG_AXP209_POWER
675         power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT);
676 #endif
677
678 #if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \
679         defined(CONFIG_AXP818_POWER)
680         power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT);
681         power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT);
682 #if !defined CONFIG_AXP809_POWER
683         power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT);
684         power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT);
685 #endif
686         power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT);
687         power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT);
688         power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT);
689 #endif
690
691 #ifdef CONFIG_AXP818_POWER
692         power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT);
693         power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT);
694         power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT);
695 #endif
696
697 #if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
698         power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON));
699 #endif
700 #endif
701         printf("DRAM:");
702         gd->ram_size = sunxi_dram_init();
703         printf(" %d MiB\n", (int)(gd->ram_size >> 20));
704         if (!gd->ram_size)
705                 hang();
706
707         sunxi_spl_store_dram_size(gd->ram_size);
708
709         /*
710          * Only clock up the CPU to full speed if we are reasonably
711          * assured it's being powered with suitable core voltage
712          */
713         if (!power_failed)
714                 clock_set_pll1(CONFIG_SYS_CLK_FREQ);
715         else
716                 printf("Failed to set core voltage! Can't set CPU frequency\n");
717 }
718 #endif
719
720 #ifdef CONFIG_USB_GADGET
721 int g_dnl_board_usb_cable_connected(void)
722 {
723         struct udevice *dev;
724         struct phy phy;
725         int ret;
726
727         ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
728         if (ret) {
729                 pr_err("%s: Cannot find USB device\n", __func__);
730                 return ret;
731         }
732
733         ret = generic_phy_get_by_name(dev, "usb", &phy);
734         if (ret) {
735                 pr_err("failed to get %s USB PHY\n", dev->name);
736                 return ret;
737         }
738
739         ret = generic_phy_init(&phy);
740         if (ret) {
741                 pr_debug("failed to init %s USB PHY\n", dev->name);
742                 return ret;
743         }
744
745         ret = sun4i_usb_phy_vbus_detect(&phy);
746         if (ret == 1) {
747                 pr_err("A charger is plugged into the OTG\n");
748                 return -ENODEV;
749         }
750
751         return ret;
752 }
753 #endif
754
755 #ifdef CONFIG_SERIAL_TAG
756 void get_board_serial(struct tag_serialnr *serialnr)
757 {
758         char *serial_string;
759         unsigned long long serial;
760
761         serial_string = env_get("serial#");
762
763         if (serial_string) {
764                 serial = simple_strtoull(serial_string, NULL, 16);
765
766                 serialnr->high = (unsigned int) (serial >> 32);
767                 serialnr->low = (unsigned int) (serial & 0xffffffff);
768         } else {
769                 serialnr->high = 0;
770                 serialnr->low = 0;
771         }
772 }
773 #endif
774
775 /*
776  * Check the SPL header for the "sunxi" variant. If found: parse values
777  * that might have been passed by the loader ("fel" utility), and update
778  * the environment accordingly.
779  */
780 static void parse_spl_header(const uint32_t spl_addr)
781 {
782         struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
783
784         if (spl == INVALID_SPL_HEADER)
785                 return;
786
787         if (!spl->fel_script_address)
788                 return;
789
790         if (spl->fel_uEnv_length != 0) {
791                 /*
792                  * data is expected in uEnv.txt compatible format, so "env
793                  * import -t" the string(s) at fel_script_address right away.
794                  */
795                 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
796                           spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
797                 return;
798         }
799         /* otherwise assume .scr format (mkimage-type script) */
800         env_set_hex("fel_scriptaddr", spl->fel_script_address);
801 }
802
803 static bool get_unique_sid(unsigned int *sid)
804 {
805         if (sunxi_get_sid(sid) != 0)
806                 return false;
807
808         if (!sid[0])
809                 return false;
810
811         /*
812          * The single words 1 - 3 of the SID have quite a few bits
813          * which are the same on many models, so we take a crc32
814          * of all 3 words, to get a more unique value.
815          *
816          * Note we only do this on newer SoCs as we cannot change
817          * the algorithm on older SoCs since those have been using
818          * fixed mac-addresses based on only using word 3 for a
819          * long time and changing a fixed mac-address with an
820          * u-boot update is not good.
821          */
822 #if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
823     !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
824     !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
825         sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
826 #endif
827
828         /* Ensure the NIC specific bytes of the mac are not all 0 */
829         if ((sid[3] & 0xffffff) == 0)
830                 sid[3] |= 0x800000;
831
832         return true;
833 }
834
835 /*
836  * Note this function gets called multiple times.
837  * It must not make any changes to env variables which already exist.
838  */
839 static void setup_environment(const void *fdt)
840 {
841         char serial_string[17] = { 0 };
842         unsigned int sid[4];
843         uint8_t mac_addr[6];
844         char ethaddr[16];
845         int i;
846
847         if (!get_unique_sid(sid))
848                 return;
849
850         for (i = 0; i < 4; i++) {
851                 sprintf(ethaddr, "ethernet%d", i);
852                 if (!fdt_get_alias(fdt, ethaddr))
853                         continue;
854
855                 if (i == 0)
856                         strcpy(ethaddr, "ethaddr");
857                 else
858                         sprintf(ethaddr, "eth%daddr", i);
859
860                 if (env_get(ethaddr))
861                         continue;
862
863                 /* Non OUI / registered MAC address */
864                 mac_addr[0] = (i << 4) | 0x02;
865                 mac_addr[1] = (sid[0] >>  0) & 0xff;
866                 mac_addr[2] = (sid[3] >> 24) & 0xff;
867                 mac_addr[3] = (sid[3] >> 16) & 0xff;
868                 mac_addr[4] = (sid[3] >>  8) & 0xff;
869                 mac_addr[5] = (sid[3] >>  0) & 0xff;
870
871                 eth_env_set_enetaddr(ethaddr, mac_addr);
872         }
873
874         if (!env_get("serial#")) {
875                 snprintf(serial_string, sizeof(serial_string),
876                         "%08x%08x", sid[0], sid[3]);
877
878                 env_set("serial#", serial_string);
879         }
880 }
881
882 int misc_init_r(void)
883 {
884         const char *spl_dt_name;
885         uint boot;
886
887         env_set("fel_booted", NULL);
888         env_set("fel_scriptaddr", NULL);
889         env_set("mmc_bootdev", NULL);
890
891         boot = sunxi_get_boot_device();
892         /* determine if we are running in FEL mode */
893         if (boot == BOOT_DEVICE_BOARD) {
894                 env_set("fel_booted", "1");
895                 parse_spl_header(SPL_ADDR);
896         /* or if we booted from MMC, and which one */
897         } else if (boot == BOOT_DEVICE_MMC1) {
898                 env_set("mmc_bootdev", "0");
899         } else if (boot == BOOT_DEVICE_MMC2) {
900                 env_set("mmc_bootdev", "1");
901         }
902
903         /* Set fdtfile to match the FIT configuration chosen in SPL. */
904         spl_dt_name = get_spl_dt_name();
905         if (spl_dt_name) {
906                 char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
907                 char str[64];
908
909                 snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
910                 env_set("fdtfile", str);
911         }
912
913         setup_environment(gd->fdt_blob);
914
915         return 0;
916 }
917
918 int board_late_init(void)
919 {
920 #ifdef CONFIG_USB_ETHER
921         usb_ether_init();
922 #endif
923
924         return 0;
925 }
926
927 static void bluetooth_dt_fixup(void *blob)
928 {
929         /* Some devices ship with a Bluetooth controller default address.
930          * Set a valid address through the device tree.
931          */
932         uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
933         unsigned int sid[4];
934         int i;
935
936         if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
937                 return;
938
939         if (eth_env_get_enetaddr("bdaddr", tmp)) {
940                 /* Convert between the binary formats of the corresponding stacks */
941                 for (i = 0; i < ETH_ALEN; ++i)
942                         bdaddr[i] = tmp[ETH_ALEN - i - 1];
943         } else {
944                 if (!get_unique_sid(sid))
945                         return;
946
947                 bdaddr[0] = ((sid[3] >>  0) & 0xff) ^ 1;
948                 bdaddr[1] = (sid[3] >>  8) & 0xff;
949                 bdaddr[2] = (sid[3] >> 16) & 0xff;
950                 bdaddr[3] = (sid[3] >> 24) & 0xff;
951                 bdaddr[4] = (sid[0] >>  0) & 0xff;
952                 bdaddr[5] = 0x02;
953         }
954
955         do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
956                            "local-bd-address", bdaddr, ETH_ALEN, 1);
957 }
958
959 int ft_board_setup(void *blob, struct bd_info *bd)
960 {
961         int __maybe_unused r;
962
963         /*
964          * Call setup_environment again in case the boot fdt has
965          * ethernet aliases the u-boot copy does not have.
966          */
967         setup_environment(blob);
968
969         bluetooth_dt_fixup(blob);
970
971 #ifdef CONFIG_VIDEO_DT_SIMPLEFB
972         r = sunxi_simplefb_setup(blob);
973         if (r)
974                 return r;
975 #endif
976         return 0;
977 }
978
979 #ifdef CONFIG_SPL_LOAD_FIT
980
981 static void set_spl_dt_name(const char *name)
982 {
983         struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
984
985         if (spl == INVALID_SPL_HEADER)
986                 return;
987
988         /* Promote the header version for U-Boot proper, if needed. */
989         if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
990                 spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
991
992         strcpy((char *)&spl->string_pool, name);
993         spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
994 }
995
996 int board_fit_config_name_match(const char *name)
997 {
998         const char *best_dt_name = get_spl_dt_name();
999         int ret;
1000
1001 #ifdef CONFIG_DEFAULT_DEVICE_TREE
1002         if (best_dt_name == NULL)
1003                 best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
1004 #endif
1005
1006         if (best_dt_name == NULL) {
1007                 /* No DT name was provided, so accept the first config. */
1008                 return 0;
1009         }
1010 #ifdef CONFIG_PINE64_DT_SELECTION
1011         if (strstr(best_dt_name, "-pine64-plus")) {
1012                 /* Differentiate the Pine A64 boards by their DRAM size. */
1013                 if ((gd->ram_size == 512 * 1024 * 1024))
1014                         best_dt_name = "sun50i-a64-pine64";
1015         }
1016 #endif
1017 #ifdef CONFIG_PINEPHONE_DT_SELECTION
1018         if (strstr(best_dt_name, "-pinephone")) {
1019                 /* Differentiate the PinePhone revisions by GPIO inputs. */
1020                 prcm_apb0_enable(PRCM_APB0_GATE_PIO);
1021                 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
1022                 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
1023                 udelay(100);
1024
1025                 /* PL6 is pulled low by the modem on v1.2. */
1026                 if (gpio_get_value(SUNXI_GPL(6)) == 0)
1027                         best_dt_name = "sun50i-a64-pinephone-1.2";
1028                 else
1029                         best_dt_name = "sun50i-a64-pinephone-1.1";
1030
1031                 sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
1032                 sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
1033                 prcm_apb0_disable(PRCM_APB0_GATE_PIO);
1034         }
1035 #endif
1036
1037         ret = strcmp(name, best_dt_name);
1038
1039         /*
1040          * If one of the FIT configurations matches the most accurate DT name,
1041          * update the SPL header to provide that DT name to U-Boot proper.
1042          */
1043         if (ret == 0)
1044                 set_spl_dt_name(best_dt_name);
1045
1046         return ret;
1047 }
1048 #endif