mt_spm_pmic_wrap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2020, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <string.h>
  7. #include <common/debug.h>
  8. #include <lib/mmio.h>
  9. #include <mt_spm.h>
  10. #include <mt_spm_internal.h>
  11. #include <mt_spm_pmic_wrap.h>
  12. #include <mt_spm_reg.h>
  13. #include <plat_pm.h>
  14. #include <platform_def.h>
  15. /* PMIC_WRAP MT6359 */
  16. #define VCORE_BASE_UV 40000
  17. #define VOLT_TO_PMIC_VAL(volt) (((volt) - VCORE_BASE_UV + 625 - 1) / 625)
  18. #define PMIC_VAL_TO_VOLT(pmic) (((pmic) * 625) + VCORE_BASE_UV)
  19. #define NR_PMIC_WRAP_CMD (NR_IDX_ALL)
  20. #define SPM_DATA_SHIFT 16
  21. #define BUCK_VGPU11_ELR0 0x15B4
  22. #define TOP_SPI_CON0 0x0456
  23. #define BUCK_TOP_CON1 0x1443
  24. #define TOP_CON 0x0013
  25. #define TOP_DIG_WPK 0x03a9
  26. #define TOP_CON_LOCK 0x03a8
  27. #define TOP_CLK_CON0 0x0134
  28. struct pmic_wrap_cmd {
  29. unsigned long cmd_addr;
  30. unsigned long cmd_wdata;
  31. };
  32. struct pmic_wrap_setting {
  33. enum pmic_wrap_phase_id phase;
  34. struct pmic_wrap_cmd addr[NR_PMIC_WRAP_CMD];
  35. struct {
  36. struct {
  37. unsigned long cmd_addr;
  38. unsigned long cmd_wdata;
  39. } _[NR_PMIC_WRAP_CMD];
  40. const int nr_idx;
  41. } set[NR_PMIC_WRAP_PHASE];
  42. };
  43. static struct pmic_wrap_setting pw = {
  44. .phase = NR_PMIC_WRAP_PHASE, /* invalid setting for init */
  45. .addr = { {0UL, 0UL} },
  46. .set[PMIC_WRAP_PHASE_ALLINONE] = {
  47. ._[CMD_0] = {BUCK_VGPU11_ELR0, VOLT_TO_PMIC_VAL(72500),},
  48. ._[CMD_1] = {BUCK_VGPU11_ELR0, VOLT_TO_PMIC_VAL(65000),},
  49. ._[CMD_2] = {BUCK_VGPU11_ELR0, VOLT_TO_PMIC_VAL(60000),},
  50. ._[CMD_3] = {BUCK_VGPU11_ELR0, VOLT_TO_PMIC_VAL(57500),},
  51. ._[CMD_4] = {TOP_SPI_CON0, 0x1,},
  52. ._[CMD_5] = {TOP_SPI_CON0, 0x0,},
  53. ._[CMD_6] = {BUCK_TOP_CON1, 0x0,},
  54. ._[CMD_7] = {BUCK_TOP_CON1, 0xf,},
  55. ._[CMD_8] = {TOP_CON, 0x3,},
  56. ._[CMD_9] = {TOP_CON, 0x0,},
  57. ._[CMD_10] = {TOP_DIG_WPK, 0x63,},
  58. ._[CMD_11] = {TOP_CON_LOCK, 0x15,},
  59. ._[CMD_12] = {TOP_DIG_WPK, 0x0,},
  60. ._[CMD_13] = {TOP_CON_LOCK, 0x0,},
  61. ._[CMD_14] = {TOP_CLK_CON0, 0x40,},
  62. ._[CMD_15] = {TOP_CLK_CON0, 0x0,},
  63. .nr_idx = NR_IDX_ALL,
  64. },
  65. };
  66. void _mt_spm_pmic_table_init(void)
  67. {
  68. struct pmic_wrap_cmd pwrap_cmd_default[NR_PMIC_WRAP_CMD] = {
  69. {(uint32_t)SPM_DVFS_CMD0, (uint32_t)SPM_DVFS_CMD0,},
  70. {(uint32_t)SPM_DVFS_CMD1, (uint32_t)SPM_DVFS_CMD1,},
  71. {(uint32_t)SPM_DVFS_CMD2, (uint32_t)SPM_DVFS_CMD2,},
  72. {(uint32_t)SPM_DVFS_CMD3, (uint32_t)SPM_DVFS_CMD3,},
  73. {(uint32_t)SPM_DVFS_CMD4, (uint32_t)SPM_DVFS_CMD4,},
  74. {(uint32_t)SPM_DVFS_CMD5, (uint32_t)SPM_DVFS_CMD5,},
  75. {(uint32_t)SPM_DVFS_CMD6, (uint32_t)SPM_DVFS_CMD6,},
  76. {(uint32_t)SPM_DVFS_CMD7, (uint32_t)SPM_DVFS_CMD7,},
  77. {(uint32_t)SPM_DVFS_CMD8, (uint32_t)SPM_DVFS_CMD8,},
  78. {(uint32_t)SPM_DVFS_CMD9, (uint32_t)SPM_DVFS_CMD9,},
  79. {(uint32_t)SPM_DVFS_CMD10, (uint32_t)SPM_DVFS_CMD10,},
  80. {(uint32_t)SPM_DVFS_CMD11, (uint32_t)SPM_DVFS_CMD11,},
  81. {(uint32_t)SPM_DVFS_CMD12, (uint32_t)SPM_DVFS_CMD12,},
  82. {(uint32_t)SPM_DVFS_CMD13, (uint32_t)SPM_DVFS_CMD13,},
  83. {(uint32_t)SPM_DVFS_CMD14, (uint32_t)SPM_DVFS_CMD14,},
  84. {(uint32_t)SPM_DVFS_CMD15, (uint32_t)SPM_DVFS_CMD15,},
  85. };
  86. memcpy(pw.addr, pwrap_cmd_default, sizeof(pwrap_cmd_default));
  87. }
  88. void mt_spm_pmic_wrap_set_phase(enum pmic_wrap_phase_id phase)
  89. {
  90. uint32_t idx, addr, data;
  91. if (phase >= NR_PMIC_WRAP_PHASE) {
  92. return;
  93. }
  94. if (pw.phase == phase) {
  95. return;
  96. }
  97. if (pw.addr[0].cmd_addr == 0UL) {
  98. _mt_spm_pmic_table_init();
  99. }
  100. pw.phase = phase;
  101. mmio_write_32(POWERON_CONFIG_EN, SPM_REGWR_CFG_KEY | BCLK_CG_EN_LSB);
  102. for (idx = 0U; idx < pw.set[phase].nr_idx; idx++) {
  103. addr = pw.set[phase]._[idx].cmd_addr << SPM_DATA_SHIFT;
  104. data = pw.set[phase]._[idx].cmd_wdata;
  105. mmio_write_32(pw.addr[idx].cmd_addr, addr | data);
  106. }
  107. }
  108. void mt_spm_pmic_wrap_set_cmd(enum pmic_wrap_phase_id phase, uint32_t idx,
  109. uint32_t cmd_wdata)
  110. {
  111. uint32_t addr;
  112. if (phase >= NR_PMIC_WRAP_PHASE) {
  113. return;
  114. }
  115. if (idx >= pw.set[phase].nr_idx) {
  116. return;
  117. }
  118. pw.set[phase]._[idx].cmd_wdata = cmd_wdata;
  119. mmio_write_32(POWERON_CONFIG_EN, SPM_REGWR_CFG_KEY | BCLK_CG_EN_LSB);
  120. if (pw.phase == phase) {
  121. addr = pw.set[phase]._[idx].cmd_addr << SPM_DATA_SHIFT;
  122. mmio_write_32(pw.addr[idx].cmd_addr, addr | cmd_wdata);
  123. }
  124. }
  125. uint64_t mt_spm_pmic_wrap_get_cmd(enum pmic_wrap_phase_id phase, uint32_t idx)
  126. {
  127. if (phase >= NR_PMIC_WRAP_PHASE) {
  128. return 0UL;
  129. }
  130. if (idx >= pw.set[phase].nr_idx) {
  131. return 0UL;
  132. }
  133. return pw.set[phase]._[idx].cmd_wdata;
  134. }