board.c 27 KB

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