mt_mcdi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2021, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <cdefs.h>
  7. #include <common/debug.h>
  8. #include <lib/mmio.h>
  9. #include <lib/utils_def.h>
  10. #include <mt_mcdi.h>
  11. /* Read/Write */
  12. #define APMCU_MCUPM_MBOX_AP_READY U(0)
  13. #define APMCU_MCUPM_MBOX_RESERVED_1 U(1)
  14. #define APMCU_MCUPM_MBOX_RESERVED_2 U(2)
  15. #define APMCU_MCUPM_MBOX_RESERVED_3 U(3)
  16. #define APMCU_MCUPM_MBOX_PWR_CTRL_EN U(4)
  17. #define APMCU_MCUPM_MBOX_L3_CACHE_MODE U(5)
  18. #define APMCU_MCUPM_MBOX_BUCK_MODE U(6)
  19. #define APMCU_MCUPM_MBOX_ARMPLL_MODE U(7)
  20. /* Read only */
  21. #define APMCU_MCUPM_MBOX_TASK_STA U(8)
  22. #define APMCU_MCUPM_MBOX_RESERVED_9 U(9)
  23. #define APMCU_MCUPM_MBOX_RESERVED_10 U(10)
  24. #define APMCU_MCUPM_MBOX_RESERVED_11 U(11)
  25. /* CPC mode - Read/Write */
  26. #define APMCU_MCUPM_MBOX_WAKEUP_CPU U(12)
  27. /* Mbox Slot: APMCU_MCUPM_MBOX_PWR_CTRL_EN */
  28. #define MCUPM_MCUSYS_CTRL BIT(0)
  29. #define MCUPM_BUCK_CTRL BIT(1)
  30. #define MCUPM_ARMPLL_CTRL BIT(2)
  31. #define MCUPM_CM_CTRL BIT(3)
  32. #define MCUPM_PWR_CTRL_MASK GENMASK(3, 0)
  33. /* Mbox Slot: APMCU_MCUPM_MBOX_BUCK_MODE */
  34. #define MCUPM_BUCK_NORMAL_MODE U(0) /* default */
  35. #define MCUPM_BUCK_LP_MODE U(1)
  36. #define MCUPM_BUCK_OFF_MODE U(2)
  37. #define NF_MCUPM_BUCK_MODE U(3)
  38. /* Mbox Slot: APMCU_MCUPM_MBOX_ARMPLL_MODE */
  39. #define MCUPM_ARMPLL_ON U(0) /* default */
  40. #define MCUPM_ARMPLL_GATING U(1)
  41. #define MCUPM_ARMPLL_OFF U(2)
  42. #define NF_MCUPM_ARMPLL_MODE U(3)
  43. /* Mbox Slot: APMCU_MCUPM_MBOX_TASK_STA */
  44. #define MCUPM_TASK_UNINIT U(0)
  45. #define MCUPM_TASK_INIT U(1)
  46. #define MCUPM_TASK_INIT_FINISH U(2)
  47. #define MCUPM_TASK_WAIT U(3)
  48. #define MCUPM_TASK_RUN U(4)
  49. #define MCUPM_TASK_PAUSE U(5)
  50. #define SSPM_MBOX_3_BASE U(0x10420000)
  51. #define MCDI_NOT_INIT U(0)
  52. #define MCDI_INIT_1 U(1)
  53. #define MCDI_INIT_2 U(2)
  54. #define MCDI_INIT_DONE U(3)
  55. static int mcdi_init_status __section(".tzfw_coherent_mem");
  56. static inline uint32_t mcdi_mbox_read(uint32_t id)
  57. {
  58. return mmio_read_32(SSPM_MBOX_3_BASE + (id << 2));
  59. }
  60. static inline void mcdi_mbox_write(uint32_t id, uint32_t val)
  61. {
  62. mmio_write_32(SSPM_MBOX_3_BASE + (id << 2), val);
  63. }
  64. static void mtk_mcupm_pwr_ctrl_setting(uint32_t dev)
  65. {
  66. mcdi_mbox_write(APMCU_MCUPM_MBOX_PWR_CTRL_EN, dev);
  67. }
  68. static void mtk_set_mcupm_pll_mode(uint32_t mode)
  69. {
  70. if (mode < NF_MCUPM_ARMPLL_MODE) {
  71. mcdi_mbox_write(APMCU_MCUPM_MBOX_ARMPLL_MODE, mode);
  72. }
  73. }
  74. static void mtk_set_mcupm_buck_mode(uint32_t mode)
  75. {
  76. if (mode < NF_MCUPM_BUCK_MODE) {
  77. mcdi_mbox_write(APMCU_MCUPM_MBOX_BUCK_MODE, mode);
  78. }
  79. }
  80. static int mtk_mcupm_is_ready(void)
  81. {
  82. unsigned int sta = mcdi_mbox_read(APMCU_MCUPM_MBOX_TASK_STA);
  83. return ((sta == MCUPM_TASK_WAIT) || (sta == MCUPM_TASK_INIT_FINISH));
  84. }
  85. static int mcdi_init_1(void)
  86. {
  87. unsigned int sta = mcdi_mbox_read(APMCU_MCUPM_MBOX_TASK_STA);
  88. if (sta != MCUPM_TASK_INIT) {
  89. return -1;
  90. }
  91. mtk_set_mcupm_pll_mode(MCUPM_ARMPLL_OFF);
  92. mtk_set_mcupm_buck_mode(MCUPM_BUCK_OFF_MODE);
  93. mtk_mcupm_pwr_ctrl_setting(
  94. MCUPM_MCUSYS_CTRL |
  95. MCUPM_BUCK_CTRL |
  96. MCUPM_ARMPLL_CTRL);
  97. mcdi_mbox_write(APMCU_MCUPM_MBOX_AP_READY, 1);
  98. return 0;
  99. }
  100. static int mcdi_init_2(void)
  101. {
  102. return mtk_mcupm_is_ready() ? 0 : -1;
  103. }
  104. int mcdi_try_init(void)
  105. {
  106. if (mcdi_init_status == MCDI_INIT_DONE) {
  107. return 0;
  108. }
  109. if (mcdi_init_status == MCDI_NOT_INIT) {
  110. mcdi_init_status = MCDI_INIT_1;
  111. }
  112. if (mcdi_init_status == MCDI_INIT_1 && mcdi_init_1() == 0) {
  113. mcdi_init_status = MCDI_INIT_2;
  114. }
  115. if (mcdi_init_status == MCDI_INIT_2 && mcdi_init_2() == 0) {
  116. mcdi_init_status = MCDI_INIT_DONE;
  117. }
  118. INFO("mcdi ready for mcusys-off-idle and system suspend\n");
  119. return (mcdi_init_status == MCDI_INIT_DONE) ? 0 : mcdi_init_status;
  120. }