sunxi_power.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <common/debug.h>
  9. #include <common/fdt_wrappers.h>
  10. #include <drivers/allwinner/axp.h>
  11. #include <drivers/allwinner/sunxi_rsb.h>
  12. #include <libfdt.h>
  13. #include <lib/mmio.h>
  14. #include <sunxi_cpucfg.h>
  15. #include <sunxi_def.h>
  16. #include <sunxi_mmap.h>
  17. #include <sunxi_private.h>
  18. #define AXP805_HW_ADDR 0x745
  19. #define AXP805_RT_ADDR 0x3a
  20. static enum pmic_type {
  21. UNKNOWN,
  22. AXP805,
  23. } pmic;
  24. int axp_read(uint8_t reg)
  25. {
  26. return rsb_read(AXP805_RT_ADDR, reg);
  27. }
  28. int axp_write(uint8_t reg, uint8_t val)
  29. {
  30. return rsb_write(AXP805_RT_ADDR, reg, val);
  31. }
  32. static int rsb_init(void)
  33. {
  34. int ret;
  35. ret = rsb_init_controller();
  36. if (ret)
  37. return ret;
  38. /* Switch to the recommended 3 MHz bus clock. */
  39. ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
  40. if (ret)
  41. return ret;
  42. /* Initiate an I2C transaction to switch the PMIC to RSB mode. */
  43. ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8);
  44. if (ret)
  45. return ret;
  46. /* Associate the 8-bit runtime address with the 12-bit bus address. */
  47. ret = rsb_assign_runtime_address(AXP805_HW_ADDR, AXP805_RT_ADDR);
  48. if (ret)
  49. return ret;
  50. return axp_check_id();
  51. }
  52. int sunxi_pmic_setup(uint16_t socid, const void *fdt)
  53. {
  54. int node, ret;
  55. node = fdt_node_offset_by_compatible(fdt, 0, "allwinner,sun8i-a23-rsb");
  56. if ((node < 0) || !fdt_node_is_enabled(fdt, node)) {
  57. return -ENODEV;
  58. }
  59. INFO("PMIC: Probing AXP805 on RSB\n");
  60. ret = sunxi_init_platform_r_twi(socid, true);
  61. if (ret)
  62. return ret;
  63. ret = rsb_init();
  64. if (ret)
  65. return ret;
  66. /* Switch the AXP805 to master/single-PMIC mode. */
  67. ret = axp_write(0xff, 0x0);
  68. if (ret)
  69. return ret;
  70. pmic = AXP805;
  71. axp_setup_regulators(fdt);
  72. /* Switch the PMIC back to I2C mode. */
  73. ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C);
  74. if (ret)
  75. return ret;
  76. return 0;
  77. }
  78. void sunxi_power_down(void)
  79. {
  80. switch (pmic) {
  81. case AXP805:
  82. /* (Re-)init RSB in case the rich OS has disabled it. */
  83. sunxi_init_platform_r_twi(SUNXI_SOC_H6, true);
  84. rsb_init();
  85. axp_power_off();
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. void sunxi_cpu_power_off_self(void)
  92. {
  93. u_register_t mpidr = read_mpidr();
  94. unsigned int core = MPIDR_AFFLVL0_VAL(mpidr);
  95. /* Enable the CPUIDLE hardware (only really needs to be done once). */
  96. mmio_write_32(SUNXI_CPUIDLE_EN_REG, 0x16aa0000);
  97. mmio_write_32(SUNXI_CPUIDLE_EN_REG, 0xaa160001);
  98. /* Trigger power off for this core. */
  99. mmio_write_32(SUNXI_CORE_CLOSE_REG, BIT_32(core));
  100. }