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