spm_pmic_wrap.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #include <lib/mmio.h>
  8. #include <platform_def.h>
  9. #include <spm.h>
  10. #include <spm_pmic_wrap.h>
  11. #include <lib/libc/string.h>
  12. #define SLEEP_REG_MD_SPM_DVFS_CMD20 (SLEEP_REG_MD_BASE + 0x010)
  13. #define SLEEP_REG_MD_SPM_DVFS_CMD21 (SLEEP_REG_MD_BASE + 0x014)
  14. #define SLEEP_REG_MD_SPM_DVFS_CMD22 (SLEEP_REG_MD_BASE + 0x018)
  15. #define SLEEP_REG_MD_SPM_DVFS_CMD23 (SLEEP_REG_MD_BASE + 0x01C)
  16. /* PMIC_WRAP -> PMIC MT6358 */
  17. #define VCORE_BASE_UV 50000
  18. #define VOLT_TO_PMIC_VAL(volt) (((volt) - VCORE_BASE_UV + 625 - 1) / 625)
  19. #define PMIC_VAL_TO_VOLT(pmic) (((pmic) * 625) + VCORE_BASE_UV)
  20. #define DEFAULT_VOLT_VSRAM (100000)
  21. #define DEFAULT_VOLT_VCORE (100000)
  22. #define NR_PMIC_WRAP_CMD (NR_IDX_ALL)
  23. #define MAX_RETRY_COUNT (100)
  24. #define SPM_DATA_SHIFT (16)
  25. #define BUCK_VCORE_ELR0 0x14AA
  26. #define BUCK_VPROC12_CON0 0x1408
  27. #define BUCK_VPROC11_CON0 0x1388
  28. #define TOP_SPI_CON0 0x044C
  29. #define LDO_VSRAM_PROC12_CON0 0x1B88
  30. #define LDO_VSRAM_PROC11_CON0 0x1B46
  31. #define BUCK_VMODEM_ELR0 0x15A6
  32. struct pmic_wrap_cmd {
  33. unsigned long cmd_addr;
  34. unsigned long cmd_wdata;
  35. };
  36. struct pmic_wrap_setting {
  37. enum pmic_wrap_phase_id phase;
  38. struct pmic_wrap_cmd addr[NR_PMIC_WRAP_CMD];
  39. struct {
  40. struct {
  41. unsigned long cmd_addr;
  42. unsigned long cmd_wdata;
  43. } _[NR_PMIC_WRAP_CMD];
  44. const int nr_idx;
  45. } set[NR_PMIC_WRAP_PHASE];
  46. };
  47. static struct pmic_wrap_setting pw = {
  48. .phase = NR_PMIC_WRAP_PHASE,
  49. .addr = {{0, 0} },
  50. .set[PMIC_WRAP_PHASE_ALLINONE] = {
  51. ._[CMD_0] = {BUCK_VCORE_ELR0, VOLT_TO_PMIC_VAL(70000),},
  52. ._[CMD_1] = {BUCK_VCORE_ELR0, VOLT_TO_PMIC_VAL(80000),},
  53. ._[CMD_2] = {BUCK_VPROC12_CON0, 0x3,},
  54. ._[CMD_3] = {BUCK_VPROC12_CON0, 0x1,},
  55. ._[CMD_4] = {BUCK_VPROC11_CON0, 0x3,},
  56. ._[CMD_5] = {BUCK_VPROC11_CON0, 0x1,},
  57. ._[CMD_6] = {TOP_SPI_CON0, 0x1,},
  58. ._[CMD_7] = {TOP_SPI_CON0, 0x0,},
  59. ._[CMD_8] = {BUCK_VPROC12_CON0, 0x0,},
  60. ._[CMD_9] = {BUCK_VPROC12_CON0, 0x1,},
  61. ._[CMD_10] = {BUCK_VPROC11_CON0, 0x0,},
  62. ._[CMD_11] = {BUCK_VPROC11_CON0, 0x1,},
  63. ._[CMD_12] = {LDO_VSRAM_PROC12_CON0, 0x0,},
  64. ._[CMD_13] = {LDO_VSRAM_PROC12_CON0, 0x1,},
  65. ._[CMD_14] = {LDO_VSRAM_PROC11_CON0, 0x0,},
  66. ._[CMD_15] = {LDO_VSRAM_PROC11_CON0, 0x1,},
  67. ._[CMD_20] = {BUCK_VMODEM_ELR0, VOLT_TO_PMIC_VAL(55000),},
  68. ._[CMD_21] = {BUCK_VCORE_ELR0, VOLT_TO_PMIC_VAL(60000),},
  69. ._[CMD_22] = {LDO_VSRAM_PROC11_CON0, 0x3,},
  70. ._[CMD_23] = {LDO_VSRAM_PROC11_CON0, 0x1,},
  71. .nr_idx = NR_IDX_ALL
  72. }
  73. };
  74. void _mt_spm_pmic_table_init(void)
  75. {
  76. struct pmic_wrap_cmd pwrap_cmd_default[NR_PMIC_WRAP_CMD] = {
  77. {(uint32_t)SPM_DVFS_CMD0, (uint32_t)SPM_DVFS_CMD0,},
  78. {(uint32_t)SPM_DVFS_CMD1, (uint32_t)SPM_DVFS_CMD1,},
  79. {(uint32_t)SPM_DVFS_CMD2, (uint32_t)SPM_DVFS_CMD2,},
  80. {(uint32_t)SPM_DVFS_CMD3, (uint32_t)SPM_DVFS_CMD3,},
  81. {(uint32_t)SPM_DVFS_CMD4, (uint32_t)SPM_DVFS_CMD4,},
  82. {(uint32_t)SPM_DVFS_CMD5, (uint32_t)SPM_DVFS_CMD5,},
  83. {(uint32_t)SPM_DVFS_CMD6, (uint32_t)SPM_DVFS_CMD6,},
  84. {(uint32_t)SPM_DVFS_CMD7, (uint32_t)SPM_DVFS_CMD7,},
  85. {(uint32_t)SPM_DVFS_CMD8, (uint32_t)SPM_DVFS_CMD8,},
  86. {(uint32_t)SPM_DVFS_CMD9, (uint32_t)SPM_DVFS_CMD9,},
  87. {(uint32_t)SPM_DVFS_CMD10, (uint32_t)SPM_DVFS_CMD10,},
  88. {(uint32_t)SPM_DVFS_CMD11, (uint32_t)SPM_DVFS_CMD11,},
  89. {(uint32_t)SPM_DVFS_CMD12, (uint32_t)SPM_DVFS_CMD12,},
  90. {(uint32_t)SPM_DVFS_CMD13, (uint32_t)SPM_DVFS_CMD13,},
  91. {(uint32_t)SPM_DVFS_CMD14, (uint32_t)SPM_DVFS_CMD14,},
  92. {(uint32_t)SPM_DVFS_CMD15, (uint32_t)SPM_DVFS_CMD15,},
  93. {(uint32_t)SLEEP_REG_MD_SPM_DVFS_CMD20,
  94. (uint32_t)SLEEP_REG_MD_SPM_DVFS_CMD20,},
  95. {(uint32_t)SLEEP_REG_MD_SPM_DVFS_CMD21,
  96. (uint32_t)SLEEP_REG_MD_SPM_DVFS_CMD21,},
  97. {(uint32_t)SLEEP_REG_MD_SPM_DVFS_CMD22,
  98. (uint32_t)SLEEP_REG_MD_SPM_DVFS_CMD22,},
  99. {(uint32_t)SLEEP_REG_MD_SPM_DVFS_CMD23,
  100. (uint32_t)SLEEP_REG_MD_SPM_DVFS_CMD23,}
  101. };
  102. memcpy(pw.addr, pwrap_cmd_default, sizeof(pwrap_cmd_default));
  103. }
  104. void mt_spm_pmic_wrap_set_phase(enum pmic_wrap_phase_id phase)
  105. {
  106. uint32_t idx, addr, data;
  107. if (phase >= NR_PMIC_WRAP_PHASE)
  108. return;
  109. if (pw.phase == phase)
  110. return;
  111. if (pw.addr[0].cmd_addr == 0)
  112. _mt_spm_pmic_table_init();
  113. pw.phase = phase;
  114. mmio_write_32(POWERON_CONFIG_EN, SPM_REGWR_CFG_KEY |
  115. BCLK_CG_EN_LSB | MD_BCLK_CG_EN_LSB);
  116. for (idx = 0; idx < pw.set[phase].nr_idx; idx++) {
  117. addr = pw.set[phase]._[idx].cmd_addr << SPM_DATA_SHIFT;
  118. data = pw.set[phase]._[idx].cmd_wdata;
  119. mmio_write_32(pw.addr[idx].cmd_addr, addr | data);
  120. }
  121. }
  122. void mt_spm_pmic_wrap_set_cmd(enum pmic_wrap_phase_id phase, uint32_t idx,
  123. uint32_t cmd_wdata)
  124. {
  125. uint32_t addr;
  126. if (phase >= NR_PMIC_WRAP_PHASE)
  127. return;
  128. if (idx >= pw.set[phase].nr_idx)
  129. return;
  130. pw.set[phase]._[idx].cmd_wdata = cmd_wdata;
  131. mmio_write_32(POWERON_CONFIG_EN, SPM_REGWR_CFG_KEY |
  132. BCLK_CG_EN_LSB | MD_BCLK_CG_EN_LSB);
  133. if (pw.phase == phase) {
  134. addr = pw.set[phase]._[idx].cmd_addr << SPM_DATA_SHIFT;
  135. mmio_write_32(pw.addr[idx].cmd_addr, addr | cmd_wdata);
  136. }
  137. }
  138. uint64_t mt_spm_pmic_wrap_get_cmd(enum pmic_wrap_phase_id phase, uint32_t idx)
  139. {
  140. if (phase >= NR_PMIC_WRAP_PHASE)
  141. return 0;
  142. if (idx >= pw.set[phase].nr_idx)
  143. return 0;
  144. return pw.set[phase]._[idx].cmd_wdata;
  145. }