sunxi_common.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <common/debug.h>
  8. #include <lib/mmio.h>
  9. #include <lib/smccc.h>
  10. #include <lib/xlat_tables/xlat_tables_v2.h>
  11. #include <services/arm_arch_svc.h>
  12. #include <sunxi_def.h>
  13. #include <sunxi_mmap.h>
  14. #include <sunxi_private.h>
  15. static const mmap_region_t sunxi_mmap[MAX_STATIC_MMAP_REGIONS + 1] = {
  16. MAP_REGION_FLAT(SUNXI_SRAM_BASE, SUNXI_SRAM_SIZE,
  17. MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER),
  18. MAP_REGION_FLAT(SUNXI_DEV_BASE, SUNXI_DEV_SIZE,
  19. MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER),
  20. MAP_REGION(PRELOADED_BL33_BASE, SUNXI_BL33_VIRT_BASE,
  21. SUNXI_DRAM_MAP_SIZE, MT_RW_DATA | MT_NS),
  22. {},
  23. };
  24. unsigned int plat_get_syscnt_freq2(void)
  25. {
  26. return SUNXI_OSC24M_CLK_IN_HZ;
  27. }
  28. void sunxi_configure_mmu_el3(int flags)
  29. {
  30. mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
  31. BL_CODE_END - BL_CODE_BASE,
  32. MT_CODE | MT_SECURE);
  33. mmap_add_region(BL_CODE_END, BL_CODE_END,
  34. BL_END - BL_CODE_END,
  35. MT_RW_DATA | MT_SECURE);
  36. #if SEPARATE_CODE_AND_RODATA
  37. mmap_add_region(BL_RO_DATA_BASE, BL_RO_DATA_BASE,
  38. BL_RO_DATA_END - BL_RO_DATA_BASE,
  39. MT_RO_DATA | MT_SECURE);
  40. #endif
  41. #if SEPARATE_NOBITS_REGION
  42. mmap_add_region(BL_NOBITS_BASE, BL_NOBITS_BASE,
  43. BL_NOBITS_END - BL_NOBITS_BASE,
  44. MT_RW_DATA | MT_SECURE);
  45. #endif
  46. #if USE_COHERENT_MEM
  47. mmap_add_region(BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_BASE,
  48. BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
  49. MT_DEVICE | MT_RW | MT_SECURE | MT_EXECUTE_NEVER);
  50. #endif
  51. mmap_add(sunxi_mmap);
  52. init_xlat_tables();
  53. enable_mmu_el3(0);
  54. }
  55. #define SRAM_VER_REG (SUNXI_SYSCON_BASE + 0x24)
  56. uint16_t sunxi_read_soc_id(void)
  57. {
  58. uint32_t reg = mmio_read_32(SRAM_VER_REG);
  59. /* Set bit 15 to prepare for the SOCID read. */
  60. mmio_write_32(SRAM_VER_REG, reg | BIT(15));
  61. reg = mmio_read_32(SRAM_VER_REG);
  62. /* deactivate the SOCID access again */
  63. mmio_write_32(SRAM_VER_REG, reg & ~BIT(15));
  64. return reg >> 16;
  65. }
  66. /*
  67. * Configure a given pin to the GPIO-OUT function and sets its level.
  68. * The port is given as a capital letter, the pin is the number within
  69. * this port group.
  70. * So to set pin PC7 to high, use: sunxi_set_gpio_out('C', 7, true);
  71. */
  72. void sunxi_set_gpio_out(char port, int pin, bool level_high)
  73. {
  74. uintptr_t port_base;
  75. if (port < 'A' || port > 'L')
  76. return;
  77. if (port == 'L')
  78. port_base = SUNXI_R_PIO_BASE;
  79. else
  80. port_base = SUNXI_PIO_BASE + (port - 'A') * 0x24;
  81. /* Set the new level first before configuring the pin. */
  82. if (level_high)
  83. mmio_setbits_32(port_base + 0x10, BIT(pin));
  84. else
  85. mmio_clrbits_32(port_base + 0x10, BIT(pin));
  86. /* configure pin as GPIO out (4(3) bits per pin, 1: GPIO out */
  87. mmio_clrsetbits_32(port_base + (pin / 8) * 4,
  88. 0x7 << ((pin % 8) * 4),
  89. 0x1 << ((pin % 8) * 4));
  90. }
  91. int sunxi_init_platform_r_twi(uint16_t socid, bool use_rsb)
  92. {
  93. uint32_t pin_func = 0x77;
  94. uint32_t device_bit;
  95. unsigned int reset_offset = 0xb0;
  96. switch (socid) {
  97. case SUNXI_SOC_H5:
  98. if (use_rsb)
  99. return -ENODEV;
  100. pin_func = 0x22;
  101. device_bit = BIT(6);
  102. break;
  103. case SUNXI_SOC_H6:
  104. case SUNXI_SOC_H616:
  105. pin_func = use_rsb ? 0x22 : 0x33;
  106. device_bit = BIT(16);
  107. reset_offset = use_rsb ? 0x1bc : 0x19c;
  108. break;
  109. case SUNXI_SOC_A64:
  110. pin_func = use_rsb ? 0x22 : 0x33;
  111. device_bit = use_rsb ? BIT(3) : BIT(6);
  112. break;
  113. default:
  114. INFO("R_I2C/RSB on Allwinner 0x%x SoC not supported\n", socid);
  115. return -ENODEV;
  116. }
  117. /* un-gate R_PIO clock */
  118. if (socid != SUNXI_SOC_H6 && socid != SUNXI_SOC_H616)
  119. mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, BIT(0));
  120. /* switch pins PL0 and PL1 to the desired function */
  121. mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x00, 0xffU, pin_func);
  122. /* level 2 drive strength */
  123. mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x14, 0x0fU, 0xaU);
  124. /* set both pins to pull-up */
  125. mmio_clrsetbits_32(SUNXI_R_PIO_BASE + 0x1c, 0x0fU, 0x5U);
  126. /* un-gate clock */
  127. if (socid != SUNXI_SOC_H6 && socid != SUNXI_SOC_H616)
  128. mmio_setbits_32(SUNXI_R_PRCM_BASE + 0x28, device_bit);
  129. else
  130. mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, BIT(0));
  131. /* assert, then de-assert reset of I2C/RSB controller */
  132. mmio_clrbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit);
  133. mmio_setbits_32(SUNXI_R_PRCM_BASE + reset_offset, device_bit);
  134. return 0;
  135. }
  136. int32_t plat_is_smccc_feature_available(u_register_t fid)
  137. {
  138. switch (fid) {
  139. case SMCCC_ARCH_SOC_ID:
  140. return SMC_ARCH_CALL_SUCCESS;
  141. default:
  142. return SMC_ARCH_CALL_NOT_SUPPORTED;
  143. }
  144. }
  145. int32_t plat_get_soc_version(void)
  146. {
  147. int32_t ret;
  148. ret = SOC_ID_SET_JEP_106(JEDEC_ALLWINNER_BKID, JEDEC_ALLWINNER_MFID);
  149. return ret | (sunxi_read_soc_id() & SOC_ID_IMPL_DEF_MASK);
  150. }
  151. int32_t plat_get_soc_revision(void)
  152. {
  153. uint32_t reg = mmio_read_32(SRAM_VER_REG);
  154. return reg & SUNXI_VER_BITS_MASK;
  155. }