sunxi_power.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io>
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <errno.h>
  8. #include <platform_def.h>
  9. #include <common/debug.h>
  10. #include <drivers/allwinner/axp.h>
  11. #include <drivers/allwinner/sunxi_rsb.h>
  12. #include <lib/mmio.h>
  13. #include <core_off_arisc.h>
  14. #include <sunxi_def.h>
  15. #include <sunxi_mmap.h>
  16. #include <sunxi_private.h>
  17. static enum pmic_type {
  18. UNKNOWN,
  19. GENERIC_H5,
  20. GENERIC_A64,
  21. REF_DESIGN_H5, /* regulators controlled by GPIO pins on port L */
  22. AXP803_RSB, /* PMIC connected via RSB on most A64 boards */
  23. } pmic;
  24. #define AXP803_HW_ADDR 0x3a3
  25. #define AXP803_RT_ADDR 0x2d
  26. /*
  27. * On boards without a proper PMIC we struggle to turn off the system properly.
  28. * Try to turn off as much off the system as we can, to reduce power
  29. * consumption. This should be entered with only one core running and SMP
  30. * disabled.
  31. * This function only cares about peripherals.
  32. */
  33. static void sunxi_turn_off_soc(uint16_t socid)
  34. {
  35. int i;
  36. /** Turn off most peripherals, most importantly DRAM users. **/
  37. /* Keep DRAM controller running for now. */
  38. mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, ~BIT_32(14));
  39. mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, ~BIT_32(14));
  40. /* Contains msgbox (bit 21) and spinlock (bit 22) */
  41. mmio_write_32(SUNXI_CCU_BASE + 0x2c4, 0);
  42. mmio_write_32(SUNXI_CCU_BASE + 0x64, 0);
  43. mmio_write_32(SUNXI_CCU_BASE + 0x2c8, 0);
  44. /* Keep PIO controller running for now. */
  45. mmio_clrbits_32(SUNXI_CCU_BASE + 0x68, ~(BIT_32(5)));
  46. mmio_write_32(SUNXI_CCU_BASE + 0x2d0, 0);
  47. /* Contains UART0 (bit 16) */
  48. mmio_write_32(SUNXI_CCU_BASE + 0x2d8, 0);
  49. mmio_write_32(SUNXI_CCU_BASE + 0x6c, 0);
  50. mmio_write_32(SUNXI_CCU_BASE + 0x70, 0);
  51. /** Turn off DRAM controller. **/
  52. mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, BIT_32(14));
  53. mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, BIT_32(14));
  54. /** Migrate CPU and bus clocks away from the PLLs. **/
  55. /* AHB1: use OSC24M/1, APB1 = AHB1 / 2 */
  56. mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x1000);
  57. /* APB2: use OSC24M */
  58. mmio_write_32(SUNXI_CCU_BASE + 0x58, 0x1000000);
  59. /* AHB2: use AHB1 clock */
  60. mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0);
  61. /* CPU: use OSC24M */
  62. mmio_write_32(SUNXI_CCU_BASE + 0x50, 0x10000);
  63. /** Turn off PLLs. **/
  64. for (i = 0; i < 6; i++)
  65. mmio_clrbits_32(SUNXI_CCU_BASE + i * 8, BIT(31));
  66. switch (socid) {
  67. case SUNXI_SOC_H5:
  68. mmio_clrbits_32(SUNXI_CCU_BASE + 0x44, BIT(31));
  69. break;
  70. case SUNXI_SOC_A64:
  71. mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c, BIT(31));
  72. mmio_clrbits_32(SUNXI_CCU_BASE + 0x4c, BIT(31));
  73. break;
  74. }
  75. }
  76. static int rsb_init(void)
  77. {
  78. int ret;
  79. ret = rsb_init_controller();
  80. if (ret)
  81. return ret;
  82. /* Switch to the recommended 3 MHz bus clock. */
  83. ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
  84. if (ret)
  85. return ret;
  86. /* Initiate an I2C transaction to switch the PMIC to RSB mode. */
  87. ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8);
  88. if (ret)
  89. return ret;
  90. /* Associate the 8-bit runtime address with the 12-bit bus address. */
  91. ret = rsb_assign_runtime_address(AXP803_HW_ADDR,
  92. AXP803_RT_ADDR);
  93. if (ret)
  94. return ret;
  95. return axp_check_id();
  96. }
  97. int axp_read(uint8_t reg)
  98. {
  99. return rsb_read(AXP803_RT_ADDR, reg);
  100. }
  101. int axp_write(uint8_t reg, uint8_t val)
  102. {
  103. return rsb_write(AXP803_RT_ADDR, reg, val);
  104. }
  105. int sunxi_pmic_setup(uint16_t socid, const void *fdt)
  106. {
  107. int ret;
  108. switch (socid) {
  109. case SUNXI_SOC_H5:
  110. NOTICE("PMIC: Assuming H5 reference regulator design\n");
  111. pmic = REF_DESIGN_H5;
  112. break;
  113. case SUNXI_SOC_A64:
  114. pmic = GENERIC_A64;
  115. INFO("PMIC: Probing AXP803 on RSB\n");
  116. ret = sunxi_init_platform_r_twi(socid, true);
  117. if (ret)
  118. return ret;
  119. ret = rsb_init();
  120. if (ret)
  121. return ret;
  122. pmic = AXP803_RSB;
  123. axp_setup_regulators(fdt);
  124. /* Switch the PMIC back to I2C mode. */
  125. ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C);
  126. if (ret)
  127. return ret;
  128. break;
  129. default:
  130. return -ENODEV;
  131. }
  132. return 0;
  133. }
  134. void sunxi_power_down(void)
  135. {
  136. switch (pmic) {
  137. case GENERIC_H5:
  138. /* Turn off as many peripherals and clocks as we can. */
  139. sunxi_turn_off_soc(SUNXI_SOC_H5);
  140. /* Turn off the pin controller now. */
  141. mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
  142. break;
  143. case GENERIC_A64:
  144. /* Turn off as many peripherals and clocks as we can. */
  145. sunxi_turn_off_soc(SUNXI_SOC_A64);
  146. /* Turn off the pin controller now. */
  147. mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
  148. break;
  149. case REF_DESIGN_H5:
  150. sunxi_turn_off_soc(SUNXI_SOC_H5);
  151. /*
  152. * Switch PL pins to power off the board:
  153. * - PL5 (VCC_IO) -> high
  154. * - PL8 (PWR-STB = CPU power supply) -> low
  155. * - PL9 (PWR-DRAM) ->low
  156. * - PL10 (power LED) -> low
  157. * Note: Clearing PL8 will reset the board, so keep it up.
  158. */
  159. sunxi_set_gpio_out('L', 5, 1);
  160. sunxi_set_gpio_out('L', 9, 0);
  161. sunxi_set_gpio_out('L', 10, 0);
  162. /* Turn off pin controller now. */
  163. mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
  164. break;
  165. case AXP803_RSB:
  166. /* (Re-)init RSB in case the rich OS has disabled it. */
  167. sunxi_init_platform_r_twi(SUNXI_SOC_A64, true);
  168. rsb_init();
  169. axp_power_off();
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. /* This lock synchronises access to the arisc management processor. */
  176. static DEFINE_BAKERY_LOCK(arisc_lock);
  177. /*
  178. * If we are supposed to turn ourself off, tell the arisc SCP to do that
  179. * work for us. Without any SCPI provider running there, we place some
  180. * OpenRISC code into SRAM, put the address of that into the reset vector
  181. * and release the arisc reset line. The SCP will wait for the core to enter
  182. * WFI, then execute that code and pull the line up again.
  183. * The code expects the core mask to be patched into the first instruction.
  184. */
  185. void sunxi_cpu_power_off_self(void)
  186. {
  187. u_register_t mpidr = read_mpidr();
  188. unsigned int core = MPIDR_AFFLVL0_VAL(mpidr);
  189. uintptr_t arisc_reset_vec = SUNXI_SRAM_A2_BASE + 0x100;
  190. uint32_t *code = arisc_core_off;
  191. do {
  192. bakery_lock_get(&arisc_lock);
  193. /* Wait until the arisc is in reset state. */
  194. if (!(mmio_read_32(SUNXI_R_CPUCFG_BASE) & BIT(0)))
  195. break;
  196. bakery_lock_release(&arisc_lock);
  197. } while (1);
  198. /* Patch up the code to feed in an input parameter. */
  199. code[0] = (code[0] & ~0xffff) | BIT_32(core);
  200. clean_dcache_range((uintptr_t)code, sizeof(arisc_core_off));
  201. /*
  202. * The OpenRISC unconditional branch has opcode 0, the branch offset
  203. * is in the lower 26 bits, containing the distance to the target,
  204. * in instruction granularity (32 bits).
  205. */
  206. mmio_write_32(arisc_reset_vec, ((uintptr_t)code - arisc_reset_vec) / 4);
  207. /* De-assert the arisc reset line to let it run. */
  208. mmio_setbits_32(SUNXI_R_CPUCFG_BASE, BIT(0));
  209. /*
  210. * We release the lock here, although the arisc is still busy.
  211. * But as long as it runs, the reset line is high, so other users
  212. * won't leave the loop above.
  213. * Once it has finished, the code is supposed to clear the reset line,
  214. * to signal this to other users.
  215. */
  216. bakery_lock_release(&arisc_lock);
  217. }