1 /* 2 * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net> 3 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 4 * 5 * (C) Copyright 2007-2011 6 * Allwinner Technology Co., Ltd. <www.allwinnertech.com> 7 * Tom Cubie <tangliang@allwinnertech.com> 8 * 9 * Some board init for the Allwinner A10-evb board. 10 * 11 * SPDX-License-Identifier: GPL-2.0+ 12 */ 13 14 #include <common.h> 15 #include <mmc.h> 16 #include <axp_pmic.h> 17 #include <asm/arch/clock.h> 18 #include <asm/arch/cpu.h> 19 #include <asm/arch/display.h> 20 #include <asm/arch/dram.h> 21 #include <asm/arch/gpio.h> 22 #include <asm/arch/mmc.h> 23 #include <asm/arch/spl.h> 24 #include <asm/arch/usb_phy.h> 25 #ifndef CONFIG_ARM64 26 #include <asm/armv7.h> 27 #endif 28 #include <asm/gpio.h> 29 #include <asm/io.h> 30 #include <crc.h> 31 #include <environment.h> 32 #include <libfdt.h> 33 #include <nand.h> 34 #include <net.h> 35 #include <spl.h> 36 #include <sy8106a.h> 37 #include <asm/setup.h> 38 39 #if defined CONFIG_VIDEO_LCD_PANEL_I2C && !(defined CONFIG_SPL_BUILD) 40 /* So that we can use pin names in Kconfig and sunxi_name_to_gpio() */ 41 int soft_i2c_gpio_sda; 42 int soft_i2c_gpio_scl; 43 44 static int soft_i2c_board_init(void) 45 { 46 int ret; 47 48 soft_i2c_gpio_sda = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SDA); 49 if (soft_i2c_gpio_sda < 0) { 50 printf("Error invalid soft i2c sda pin: '%s', err %d\n", 51 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, soft_i2c_gpio_sda); 52 return soft_i2c_gpio_sda; 53 } 54 ret = gpio_request(soft_i2c_gpio_sda, "soft-i2c-sda"); 55 if (ret) { 56 printf("Error requesting soft i2c sda pin: '%s', err %d\n", 57 CONFIG_VIDEO_LCD_PANEL_I2C_SDA, ret); 58 return ret; 59 } 60 61 soft_i2c_gpio_scl = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_PANEL_I2C_SCL); 62 if (soft_i2c_gpio_scl < 0) { 63 printf("Error invalid soft i2c scl pin: '%s', err %d\n", 64 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, soft_i2c_gpio_scl); 65 return soft_i2c_gpio_scl; 66 } 67 ret = gpio_request(soft_i2c_gpio_scl, "soft-i2c-scl"); 68 if (ret) { 69 printf("Error requesting soft i2c scl pin: '%s', err %d\n", 70 CONFIG_VIDEO_LCD_PANEL_I2C_SCL, ret); 71 return ret; 72 } 73 74 return 0; 75 } 76 #else 77 static int soft_i2c_board_init(void) { return 0; } 78 #endif 79 80 DECLARE_GLOBAL_DATA_PTR; 81 82 void i2c_init_board(void) 83 { 84 #ifdef CONFIG_I2C0_ENABLE 85 #if defined(CONFIG_MACH_SUN4I) || \ 86 defined(CONFIG_MACH_SUN5I) || \ 87 defined(CONFIG_MACH_SUN7I) || \ 88 defined(CONFIG_MACH_SUN8I_R40) 89 sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN4I_GPB_TWI0); 90 sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN4I_GPB_TWI0); 91 clock_twi_onoff(0, 1); 92 #elif defined(CONFIG_MACH_SUN6I) 93 sunxi_gpio_set_cfgpin(SUNXI_GPH(14), SUN6I_GPH_TWI0); 94 sunxi_gpio_set_cfgpin(SUNXI_GPH(15), SUN6I_GPH_TWI0); 95 clock_twi_onoff(0, 1); 96 #elif defined(CONFIG_MACH_SUN8I) 97 sunxi_gpio_set_cfgpin(SUNXI_GPH(2), SUN8I_GPH_TWI0); 98 sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN8I_GPH_TWI0); 99 clock_twi_onoff(0, 1); 100 #endif 101 #endif 102 103 #ifdef CONFIG_I2C1_ENABLE 104 #if defined(CONFIG_MACH_SUN4I) || \ 105 defined(CONFIG_MACH_SUN7I) || \ 106 defined(CONFIG_MACH_SUN8I_R40) 107 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN4I_GPB_TWI1); 108 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN4I_GPB_TWI1); 109 clock_twi_onoff(1, 1); 110 #elif defined(CONFIG_MACH_SUN5I) 111 sunxi_gpio_set_cfgpin(SUNXI_GPB(15), SUN5I_GPB_TWI1); 112 sunxi_gpio_set_cfgpin(SUNXI_GPB(16), SUN5I_GPB_TWI1); 113 clock_twi_onoff(1, 1); 114 #elif defined(CONFIG_MACH_SUN6I) 115 sunxi_gpio_set_cfgpin(SUNXI_GPH(16), SUN6I_GPH_TWI1); 116 sunxi_gpio_set_cfgpin(SUNXI_GPH(17), SUN6I_GPH_TWI1); 117 clock_twi_onoff(1, 1); 118 #elif defined(CONFIG_MACH_SUN8I) 119 sunxi_gpio_set_cfgpin(SUNXI_GPH(4), SUN8I_GPH_TWI1); 120 sunxi_gpio_set_cfgpin(SUNXI_GPH(5), SUN8I_GPH_TWI1); 121 clock_twi_onoff(1, 1); 122 #endif 123 #endif 124 125 #ifdef CONFIG_I2C2_ENABLE 126 #if defined(CONFIG_MACH_SUN4I) || \ 127 defined(CONFIG_MACH_SUN7I) || \ 128 defined(CONFIG_MACH_SUN8I_R40) 129 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN4I_GPB_TWI2); 130 sunxi_gpio_set_cfgpin(SUNXI_GPB(21), SUN4I_GPB_TWI2); 131 clock_twi_onoff(2, 1); 132 #elif defined(CONFIG_MACH_SUN5I) 133 sunxi_gpio_set_cfgpin(SUNXI_GPB(17), SUN5I_GPB_TWI2); 134 sunxi_gpio_set_cfgpin(SUNXI_GPB(18), SUN5I_GPB_TWI2); 135 clock_twi_onoff(2, 1); 136 #elif defined(CONFIG_MACH_SUN6I) 137 sunxi_gpio_set_cfgpin(SUNXI_GPH(18), SUN6I_GPH_TWI2); 138 sunxi_gpio_set_cfgpin(SUNXI_GPH(19), SUN6I_GPH_TWI2); 139 clock_twi_onoff(2, 1); 140 #elif defined(CONFIG_MACH_SUN8I) 141 sunxi_gpio_set_cfgpin(SUNXI_GPE(12), SUN8I_GPE_TWI2); 142 sunxi_gpio_set_cfgpin(SUNXI_GPE(13), SUN8I_GPE_TWI2); 143 clock_twi_onoff(2, 1); 144 #endif 145 #endif 146 147 #ifdef CONFIG_I2C3_ENABLE 148 #if defined(CONFIG_MACH_SUN6I) 149 sunxi_gpio_set_cfgpin(SUNXI_GPG(10), SUN6I_GPG_TWI3); 150 sunxi_gpio_set_cfgpin(SUNXI_GPG(11), SUN6I_GPG_TWI3); 151 clock_twi_onoff(3, 1); 152 #elif defined(CONFIG_MACH_SUN7I) || \ 153 defined(CONFIG_MACH_SUN8I_R40) 154 sunxi_gpio_set_cfgpin(SUNXI_GPI(0), SUN7I_GPI_TWI3); 155 sunxi_gpio_set_cfgpin(SUNXI_GPI(1), SUN7I_GPI_TWI3); 156 clock_twi_onoff(3, 1); 157 #endif 158 #endif 159 160 #ifdef CONFIG_I2C4_ENABLE 161 #if defined(CONFIG_MACH_SUN7I) || \ 162 defined(CONFIG_MACH_SUN8I_R40) 163 sunxi_gpio_set_cfgpin(SUNXI_GPI(2), SUN7I_GPI_TWI4); 164 sunxi_gpio_set_cfgpin(SUNXI_GPI(3), SUN7I_GPI_TWI4); 165 clock_twi_onoff(4, 1); 166 #endif 167 #endif 168 169 #ifdef CONFIG_R_I2C_ENABLE 170 clock_twi_onoff(5, 1); 171 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_H3_GPL_R_TWI); 172 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_H3_GPL_R_TWI); 173 #endif 174 } 175 176 #if defined(CONFIG_ENV_IS_IN_MMC) && defined(CONFIG_ENV_IS_IN_FAT) 177 enum env_location env_get_location(enum env_operation op, int prio) 178 { 179 switch (prio) { 180 case 0: 181 return ENVL_FAT; 182 183 case 1: 184 return ENVL_MMC; 185 186 default: 187 return ENVL_UNKNOWN; 188 } 189 } 190 #endif 191 192 /* add board specific code here */ 193 int board_init(void) 194 { 195 __maybe_unused int id_pfr1, ret, satapwr_pin, macpwr_pin; 196 197 gd->bd->bi_boot_params = (PHYS_SDRAM_0 + 0x100); 198 199 #ifndef CONFIG_ARM64 200 asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1)); 201 debug("id_pfr1: 0x%08x\n", id_pfr1); 202 /* Generic Timer Extension available? */ 203 if ((id_pfr1 >> CPUID_ARM_GENTIMER_SHIFT) & 0xf) { 204 uint32_t freq; 205 206 debug("Setting CNTFRQ\n"); 207 208 /* 209 * CNTFRQ is a secure register, so we will crash if we try to 210 * write this from the non-secure world (read is OK, though). 211 * In case some bootcode has already set the correct value, 212 * we avoid the risk of writing to it. 213 */ 214 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r"(freq)); 215 if (freq != COUNTER_FREQUENCY) { 216 debug("arch timer frequency is %d Hz, should be %d, fixing ...\n", 217 freq, COUNTER_FREQUENCY); 218 #ifdef CONFIG_NON_SECURE 219 printf("arch timer frequency is wrong, but cannot adjust it\n"); 220 #else 221 asm volatile("mcr p15, 0, %0, c14, c0, 0" 222 : : "r"(COUNTER_FREQUENCY)); 223 #endif 224 } 225 } 226 #endif /* !CONFIG_ARM64 */ 227 228 ret = axp_gpio_init(); 229 if (ret) 230 return ret; 231 232 #ifdef CONFIG_SATAPWR 233 satapwr_pin = sunxi_name_to_gpio(CONFIG_SATAPWR); 234 gpio_request(satapwr_pin, "satapwr"); 235 gpio_direction_output(satapwr_pin, 1); 236 /* Give attached sata device time to power-up to avoid link timeouts */ 237 mdelay(500); 238 #endif 239 #ifdef CONFIG_MACPWR 240 macpwr_pin = sunxi_name_to_gpio(CONFIG_MACPWR); 241 gpio_request(macpwr_pin, "macpwr"); 242 gpio_direction_output(macpwr_pin, 1); 243 #endif 244 245 #ifdef CONFIG_DM_I2C 246 /* 247 * Temporary workaround for enabling I2C clocks until proper sunxi DM 248 * clk, reset and pinctrl drivers land. 249 */ 250 i2c_init_board(); 251 #endif 252 253 /* Uses dm gpio code so do this here and not in i2c_init_board() */ 254 return soft_i2c_board_init(); 255 } 256 257 int dram_init(void) 258 { 259 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_0, PHYS_SDRAM_0_SIZE); 260 261 return 0; 262 } 263 264 #if defined(CONFIG_NAND_SUNXI) 265 static void nand_pinmux_setup(void) 266 { 267 unsigned int pin; 268 269 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(19); pin++) 270 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND); 271 272 #if defined CONFIG_MACH_SUN4I || defined CONFIG_MACH_SUN7I 273 for (pin = SUNXI_GPC(20); pin <= SUNXI_GPC(22); pin++) 274 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_NAND); 275 #endif 276 /* sun4i / sun7i do have a PC23, but it is not used for nand, 277 * only sun7i has a PC24 */ 278 #ifdef CONFIG_MACH_SUN7I 279 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_NAND); 280 #endif 281 } 282 283 static void nand_clock_setup(void) 284 { 285 struct sunxi_ccm_reg *const ccm = 286 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; 287 288 setbits_le32(&ccm->ahb_gate0, (CLK_GATE_OPEN << AHB_GATE_OFFSET_NAND0)); 289 #ifdef CONFIG_MACH_SUN9I 290 setbits_le32(&ccm->ahb_gate1, (1 << AHB_GATE_OFFSET_DMA)); 291 #else 292 setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_DMA)); 293 #endif 294 setbits_le32(&ccm->nand0_clk_cfg, CCM_NAND_CTRL_ENABLE | AHB_DIV_1); 295 } 296 297 void board_nand_init(void) 298 { 299 nand_pinmux_setup(); 300 nand_clock_setup(); 301 #ifndef CONFIG_SPL_BUILD 302 sunxi_nand_init(); 303 #endif 304 } 305 #endif 306 307 #ifdef CONFIG_MMC 308 static void mmc_pinmux_setup(int sdc) 309 { 310 unsigned int pin; 311 __maybe_unused int pins; 312 313 switch (sdc) { 314 case 0: 315 /* SDC0: PF0-PF5 */ 316 for (pin = SUNXI_GPF(0); pin <= SUNXI_GPF(5); pin++) { 317 sunxi_gpio_set_cfgpin(pin, SUNXI_GPF_SDC0); 318 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 319 sunxi_gpio_set_drv(pin, 2); 320 } 321 break; 322 323 case 1: 324 pins = sunxi_name_to_gpio_bank(CONFIG_MMC1_PINS); 325 326 #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \ 327 defined(CONFIG_MACH_SUN8I_R40) 328 if (pins == SUNXI_GPIO_H) { 329 /* SDC1: PH22-PH-27 */ 330 for (pin = SUNXI_GPH(22); pin <= SUNXI_GPH(27); pin++) { 331 sunxi_gpio_set_cfgpin(pin, SUN4I_GPH_SDC1); 332 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 333 sunxi_gpio_set_drv(pin, 2); 334 } 335 } else { 336 /* SDC1: PG0-PG5 */ 337 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) { 338 sunxi_gpio_set_cfgpin(pin, SUN4I_GPG_SDC1); 339 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 340 sunxi_gpio_set_drv(pin, 2); 341 } 342 } 343 #elif defined(CONFIG_MACH_SUN5I) 344 /* SDC1: PG3-PG8 */ 345 for (pin = SUNXI_GPG(3); pin <= SUNXI_GPG(8); pin++) { 346 sunxi_gpio_set_cfgpin(pin, SUN5I_GPG_SDC1); 347 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 348 sunxi_gpio_set_drv(pin, 2); 349 } 350 #elif defined(CONFIG_MACH_SUN6I) 351 /* SDC1: PG0-PG5 */ 352 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) { 353 sunxi_gpio_set_cfgpin(pin, SUN6I_GPG_SDC1); 354 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 355 sunxi_gpio_set_drv(pin, 2); 356 } 357 #elif defined(CONFIG_MACH_SUN8I) 358 if (pins == SUNXI_GPIO_D) { 359 /* SDC1: PD2-PD7 */ 360 for (pin = SUNXI_GPD(2); pin <= SUNXI_GPD(7); pin++) { 361 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD_SDC1); 362 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 363 sunxi_gpio_set_drv(pin, 2); 364 } 365 } else { 366 /* SDC1: PG0-PG5 */ 367 for (pin = SUNXI_GPG(0); pin <= SUNXI_GPG(5); pin++) { 368 sunxi_gpio_set_cfgpin(pin, SUN8I_GPG_SDC1); 369 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 370 sunxi_gpio_set_drv(pin, 2); 371 } 372 } 373 #endif 374 break; 375 376 case 2: 377 pins = sunxi_name_to_gpio_bank(CONFIG_MMC2_PINS); 378 379 #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) 380 /* SDC2: PC6-PC11 */ 381 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(11); pin++) { 382 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2); 383 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 384 sunxi_gpio_set_drv(pin, 2); 385 } 386 #elif defined(CONFIG_MACH_SUN5I) 387 if (pins == SUNXI_GPIO_E) { 388 /* SDC2: PE4-PE9 */ 389 for (pin = SUNXI_GPE(4); pin <= SUNXI_GPD(9); pin++) { 390 sunxi_gpio_set_cfgpin(pin, SUN5I_GPE_SDC2); 391 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 392 sunxi_gpio_set_drv(pin, 2); 393 } 394 } else { 395 /* SDC2: PC6-PC15 */ 396 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) { 397 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2); 398 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 399 sunxi_gpio_set_drv(pin, 2); 400 } 401 } 402 #elif defined(CONFIG_MACH_SUN6I) 403 if (pins == SUNXI_GPIO_A) { 404 /* SDC2: PA9-PA14 */ 405 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) { 406 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC2); 407 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 408 sunxi_gpio_set_drv(pin, 2); 409 } 410 } else { 411 /* SDC2: PC6-PC15, PC24 */ 412 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) { 413 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2); 414 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 415 sunxi_gpio_set_drv(pin, 2); 416 } 417 418 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2); 419 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP); 420 sunxi_gpio_set_drv(SUNXI_GPC(24), 2); 421 } 422 #elif defined(CONFIG_MACH_SUN8I_R40) 423 /* SDC2: PC6-PC15, PC24 */ 424 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) { 425 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2); 426 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 427 sunxi_gpio_set_drv(pin, 2); 428 } 429 430 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUNXI_GPC_SDC2); 431 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP); 432 sunxi_gpio_set_drv(SUNXI_GPC(24), 2); 433 #elif defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I) 434 /* SDC2: PC5-PC6, PC8-PC16 */ 435 for (pin = SUNXI_GPC(5); pin <= SUNXI_GPC(6); pin++) { 436 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2); 437 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 438 sunxi_gpio_set_drv(pin, 2); 439 } 440 441 for (pin = SUNXI_GPC(8); pin <= SUNXI_GPC(16); pin++) { 442 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2); 443 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 444 sunxi_gpio_set_drv(pin, 2); 445 } 446 #elif defined(CONFIG_MACH_SUN9I) 447 /* SDC2: PC6-PC16 */ 448 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(16); pin++) { 449 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SDC2); 450 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 451 sunxi_gpio_set_drv(pin, 2); 452 } 453 #endif 454 break; 455 456 case 3: 457 pins = sunxi_name_to_gpio_bank(CONFIG_MMC3_PINS); 458 459 #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) || \ 460 defined(CONFIG_MACH_SUN8I_R40) 461 /* SDC3: PI4-PI9 */ 462 for (pin = SUNXI_GPI(4); pin <= SUNXI_GPI(9); pin++) { 463 sunxi_gpio_set_cfgpin(pin, SUNXI_GPI_SDC3); 464 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 465 sunxi_gpio_set_drv(pin, 2); 466 } 467 #elif defined(CONFIG_MACH_SUN6I) 468 if (pins == SUNXI_GPIO_A) { 469 /* SDC3: PA9-PA14 */ 470 for (pin = SUNXI_GPA(9); pin <= SUNXI_GPA(14); pin++) { 471 sunxi_gpio_set_cfgpin(pin, SUN6I_GPA_SDC3); 472 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 473 sunxi_gpio_set_drv(pin, 2); 474 } 475 } else { 476 /* SDC3: PC6-PC15, PC24 */ 477 for (pin = SUNXI_GPC(6); pin <= SUNXI_GPC(15); pin++) { 478 sunxi_gpio_set_cfgpin(pin, SUN6I_GPC_SDC3); 479 sunxi_gpio_set_pull(pin, SUNXI_GPIO_PULL_UP); 480 sunxi_gpio_set_drv(pin, 2); 481 } 482 483 sunxi_gpio_set_cfgpin(SUNXI_GPC(24), SUN6I_GPC_SDC3); 484 sunxi_gpio_set_pull(SUNXI_GPC(24), SUNXI_GPIO_PULL_UP); 485 sunxi_gpio_set_drv(SUNXI_GPC(24), 2); 486 } 487 #endif 488 break; 489 490 default: 491 printf("sunxi: invalid MMC slot %d for pinmux setup\n", sdc); 492 break; 493 } 494 } 495 496 int board_mmc_init(bd_t *bis) 497 { 498 __maybe_unused struct mmc *mmc0, *mmc1; 499 __maybe_unused char buf[512]; 500 501 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT); 502 mmc0 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT); 503 if (!mmc0) 504 return -1; 505 506 #if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1 507 mmc_pinmux_setup(CONFIG_MMC_SUNXI_SLOT_EXTRA); 508 mmc1 = sunxi_mmc_init(CONFIG_MMC_SUNXI_SLOT_EXTRA); 509 if (!mmc1) 510 return -1; 511 #endif 512 513 return 0; 514 } 515 #endif 516 517 #ifdef CONFIG_SPL_BUILD 518 void sunxi_board_init(void) 519 { 520 int power_failed = 0; 521 522 #ifdef CONFIG_SY8106A_POWER 523 power_failed = sy8106a_set_vout1(CONFIG_SY8106A_VOUT1_VOLT); 524 #endif 525 526 #if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \ 527 defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \ 528 defined CONFIG_AXP818_POWER 529 power_failed = axp_init(); 530 531 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \ 532 defined CONFIG_AXP818_POWER 533 power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT); 534 #endif 535 power_failed |= axp_set_dcdc2(CONFIG_AXP_DCDC2_VOLT); 536 power_failed |= axp_set_dcdc3(CONFIG_AXP_DCDC3_VOLT); 537 #if !defined(CONFIG_AXP209_POWER) && !defined(CONFIG_AXP818_POWER) 538 power_failed |= axp_set_dcdc4(CONFIG_AXP_DCDC4_VOLT); 539 #endif 540 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \ 541 defined CONFIG_AXP818_POWER 542 power_failed |= axp_set_dcdc5(CONFIG_AXP_DCDC5_VOLT); 543 #endif 544 545 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \ 546 defined CONFIG_AXP818_POWER 547 power_failed |= axp_set_aldo1(CONFIG_AXP_ALDO1_VOLT); 548 #endif 549 power_failed |= axp_set_aldo2(CONFIG_AXP_ALDO2_VOLT); 550 #if !defined(CONFIG_AXP152_POWER) 551 power_failed |= axp_set_aldo3(CONFIG_AXP_ALDO3_VOLT); 552 #endif 553 #ifdef CONFIG_AXP209_POWER 554 power_failed |= axp_set_aldo4(CONFIG_AXP_ALDO4_VOLT); 555 #endif 556 557 #if defined(CONFIG_AXP221_POWER) || defined(CONFIG_AXP809_POWER) || \ 558 defined(CONFIG_AXP818_POWER) 559 power_failed |= axp_set_dldo(1, CONFIG_AXP_DLDO1_VOLT); 560 power_failed |= axp_set_dldo(2, CONFIG_AXP_DLDO2_VOLT); 561 #if !defined CONFIG_AXP809_POWER 562 power_failed |= axp_set_dldo(3, CONFIG_AXP_DLDO3_VOLT); 563 power_failed |= axp_set_dldo(4, CONFIG_AXP_DLDO4_VOLT); 564 #endif 565 power_failed |= axp_set_eldo(1, CONFIG_AXP_ELDO1_VOLT); 566 power_failed |= axp_set_eldo(2, CONFIG_AXP_ELDO2_VOLT); 567 power_failed |= axp_set_eldo(3, CONFIG_AXP_ELDO3_VOLT); 568 #endif 569 570 #ifdef CONFIG_AXP818_POWER 571 power_failed |= axp_set_fldo(1, CONFIG_AXP_FLDO1_VOLT); 572 power_failed |= axp_set_fldo(2, CONFIG_AXP_FLDO2_VOLT); 573 power_failed |= axp_set_fldo(3, CONFIG_AXP_FLDO3_VOLT); 574 #endif 575 576 #if defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER 577 power_failed |= axp_set_sw(IS_ENABLED(CONFIG_AXP_SW_ON)); 578 #endif 579 #endif 580 printf("DRAM:"); 581 gd->ram_size = sunxi_dram_init(); 582 printf(" %d MiB\n", (int)(gd->ram_size >> 20)); 583 if (!gd->ram_size) 584 hang(); 585 586 /* 587 * Only clock up the CPU to full speed if we are reasonably 588 * assured it's being powered with suitable core voltage 589 */ 590 if (!power_failed) 591 clock_set_pll1(CONFIG_SYS_CLK_FREQ); 592 else 593 printf("Failed to set core voltage! Can't set CPU frequency\n"); 594 } 595 #endif 596 597 #ifdef CONFIG_USB_GADGET 598 int g_dnl_board_usb_cable_connected(void) 599 { 600 return sunxi_usb_phy_vbus_detect(0); 601 } 602 #endif 603 604 #ifdef CONFIG_SERIAL_TAG 605 void get_board_serial(struct tag_serialnr *serialnr) 606 { 607 char *serial_string; 608 unsigned long long serial; 609 610 serial_string = env_get("serial#"); 611 612 if (serial_string) { 613 serial = simple_strtoull(serial_string, NULL, 16); 614 615 serialnr->high = (unsigned int) (serial >> 32); 616 serialnr->low = (unsigned int) (serial & 0xffffffff); 617 } else { 618 serialnr->high = 0; 619 serialnr->low = 0; 620 } 621 } 622 #endif 623 624 /* 625 * Check the SPL header for the "sunxi" variant. If found: parse values 626 * that might have been passed by the loader ("fel" utility), and update 627 * the environment accordingly. 628 */ 629 static void parse_spl_header(const uint32_t spl_addr) 630 { 631 struct boot_file_head *spl = (void *)(ulong)spl_addr; 632 if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0) 633 return; /* signature mismatch, no usable header */ 634 635 uint8_t spl_header_version = spl->spl_signature[3]; 636 if (spl_header_version != SPL_HEADER_VERSION) { 637 printf("sunxi SPL version mismatch: expected %u, got %u\n", 638 SPL_HEADER_VERSION, spl_header_version); 639 return; 640 } 641 if (!spl->fel_script_address) 642 return; 643 644 if (spl->fel_uEnv_length != 0) { 645 /* 646 * data is expected in uEnv.txt compatible format, so "env 647 * import -t" the string(s) at fel_script_address right away. 648 */ 649 himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address, 650 spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL); 651 return; 652 } 653 /* otherwise assume .scr format (mkimage-type script) */ 654 env_set_hex("fel_scriptaddr", spl->fel_script_address); 655 } 656 657 /* 658 * Note this function gets called multiple times. 659 * It must not make any changes to env variables which already exist. 660 */ 661 static void setup_environment(const void *fdt) 662 { 663 char serial_string[17] = { 0 }; 664 unsigned int sid[4]; 665 uint8_t mac_addr[6]; 666 char ethaddr[16]; 667 int i, ret; 668 669 ret = sunxi_get_sid(sid); 670 if (ret == 0 && sid[0] != 0) { 671 /* 672 * The single words 1 - 3 of the SID have quite a few bits 673 * which are the same on many models, so we take a crc32 674 * of all 3 words, to get a more unique value. 675 * 676 * Note we only do this on newer SoCs as we cannot change 677 * the algorithm on older SoCs since those have been using 678 * fixed mac-addresses based on only using word 3 for a 679 * long time and changing a fixed mac-address with an 680 * u-boot update is not good. 681 */ 682 #if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \ 683 !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \ 684 !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33) 685 sid[3] = crc32(0, (unsigned char *)&sid[1], 12); 686 #endif 687 688 /* Ensure the NIC specific bytes of the mac are not all 0 */ 689 if ((sid[3] & 0xffffff) == 0) 690 sid[3] |= 0x800000; 691 692 for (i = 0; i < 4; i++) { 693 sprintf(ethaddr, "ethernet%d", i); 694 if (!fdt_get_alias(fdt, ethaddr)) 695 continue; 696 697 if (i == 0) 698 strcpy(ethaddr, "ethaddr"); 699 else 700 sprintf(ethaddr, "eth%daddr", i); 701 702 if (env_get(ethaddr)) 703 continue; 704 705 /* Non OUI / registered MAC address */ 706 mac_addr[0] = (i << 4) | 0x02; 707 mac_addr[1] = (sid[0] >> 0) & 0xff; 708 mac_addr[2] = (sid[3] >> 24) & 0xff; 709 mac_addr[3] = (sid[3] >> 16) & 0xff; 710 mac_addr[4] = (sid[3] >> 8) & 0xff; 711 mac_addr[5] = (sid[3] >> 0) & 0xff; 712 713 eth_env_set_enetaddr(ethaddr, mac_addr); 714 } 715 716 if (!env_get("serial#")) { 717 snprintf(serial_string, sizeof(serial_string), 718 "%08x%08x", sid[0], sid[3]); 719 720 env_set("serial#", serial_string); 721 } 722 } 723 } 724 725 int misc_init_r(void) 726 { 727 __maybe_unused int ret; 728 uint boot; 729 730 env_set("fel_booted", NULL); 731 env_set("fel_scriptaddr", NULL); 732 env_set("mmc_bootdev", NULL); 733 734 boot = sunxi_get_boot_device(); 735 /* determine if we are running in FEL mode */ 736 if (boot == BOOT_DEVICE_BOARD) { 737 env_set("fel_booted", "1"); 738 parse_spl_header(SPL_ADDR); 739 /* or if we booted from MMC, and which one */ 740 } else if (boot == BOOT_DEVICE_MMC1) { 741 env_set("mmc_bootdev", "0"); 742 } else if (boot == BOOT_DEVICE_MMC2) { 743 env_set("mmc_bootdev", "1"); 744 } 745 746 setup_environment(gd->fdt_blob); 747 748 #ifndef CONFIG_MACH_SUN9I 749 ret = sunxi_usb_phy_probe(); 750 if (ret) 751 return ret; 752 #endif 753 754 #ifdef CONFIG_USB_ETHER 755 usb_ether_init(); 756 #endif 757 758 return 0; 759 } 760 761 int ft_board_setup(void *blob, bd_t *bd) 762 { 763 int __maybe_unused r; 764 765 /* 766 * Call setup_environment again in case the boot fdt has 767 * ethernet aliases the u-boot copy does not have. 768 */ 769 setup_environment(blob); 770 771 #ifdef CONFIG_VIDEO_DT_SIMPLEFB 772 r = sunxi_simplefb_setup(blob); 773 if (r) 774 return r; 775 #endif 776 return 0; 777 } 778 779 #ifdef CONFIG_SPL_LOAD_FIT 780 int board_fit_config_name_match(const char *name) 781 { 782 struct boot_file_head *spl = (void *)(ulong)SPL_ADDR; 783 const char *cmp_str = (void *)(ulong)SPL_ADDR; 784 785 /* Check if there is a DT name stored in the SPL header and use that. */ 786 if (spl->dt_name_offset) { 787 cmp_str += spl->dt_name_offset; 788 } else { 789 #ifdef CONFIG_DEFAULT_DEVICE_TREE 790 cmp_str = CONFIG_DEFAULT_DEVICE_TREE; 791 #else 792 return 0; 793 #endif 794 }; 795 796 /* Differentiate the two Pine64 board DTs by their DRAM size. */ 797 if (strstr(name, "-pine64") && strstr(cmp_str, "-pine64")) { 798 if ((gd->ram_size > 512 * 1024 * 1024)) 799 return !strstr(name, "plus"); 800 else 801 return !!strstr(name, "plus"); 802 } else { 803 return strcmp(name, cmp_str); 804 } 805 } 806 #endif 807