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