mt_spm_pmic_wrap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2023, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stddef.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <common/debug.h>
  10. #include <lib/mmio.h>
  11. #include <plat/common/platform.h>
  12. #include <lib/pm/mtk_pm.h>
  13. #include "mt_spm.h"
  14. #include "mt_spm_internal.h"
  15. #include "mt_spm_pmic_wrap.h"
  16. #include "mt_spm_reg.h"
  17. #include <platform_def.h>
  18. /* BIT operation */
  19. #define _BITS_(h, l, v) ((GENMASK(h, l) & ((v) << (l))))
  20. /* PMIC_WRAP */
  21. #define VCORE_BASE_UV (40000) /* PMIC MT6359 */
  22. #define VOLT_TO_PMIC_VAL(volt) (((volt) - VCORE_BASE_UV + 625 - 1) / 625)
  23. #define NR_PMIC_WRAP_CMD (NR_IDX_ALL)
  24. #define SPM_DATA_SHIFT (16)
  25. #define BUCK_VGPU11_ELR0 (0x15B4)
  26. #define TOP_SPI_CON0 (0x0456)
  27. #define BUCK_TOP_CON1 (0x1443) /* PMIC MT6315 */
  28. #define TOP_CON (0x0013) /* PMIC MT6315 */
  29. #define TOP_DIG_WPK (0x03a9)
  30. #define TOP_CON_LOCK (0x03a8)
  31. #define TOP_CLK_CON0 (0x0134) /* PMIC MT6359*/
  32. struct pmic_wrap_cmd {
  33. uint32_t cmd_addr;
  34. uint32_t 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. uint32_t cmd_addr;
  42. uint32_t 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, /* invalid setting for init */
  49. .addr = {{0, 0} },
  50. .set[PMIC_WRAP_PHASE_ALLINONE] = {
  51. ._[CMD_0] = {BUCK_VGPU11_ELR0, _BITS_(6, 0, VOLT_TO_PMIC_VAL(75000)),},
  52. ._[CMD_1] = {BUCK_VGPU11_ELR0, _BITS_(6, 0, VOLT_TO_PMIC_VAL(65000)),},
  53. ._[CMD_2] = {BUCK_VGPU11_ELR0, _BITS_(6, 0, VOLT_TO_PMIC_VAL(60000)),},
  54. ._[CMD_3] = {BUCK_VGPU11_ELR0, _BITS_(6, 0, VOLT_TO_PMIC_VAL(55000)),},
  55. ._[CMD_4] = {TOP_SPI_CON0, _BITS_(0, 0, 1),},
  56. ._[CMD_5] = {TOP_SPI_CON0, _BITS_(0, 0, 0),},
  57. ._[CMD_6] = {BUCK_TOP_CON1, 0x0,}, /* MT6315-3: VMD NO LP */
  58. ._[CMD_7] = {BUCK_TOP_CON1, 0xF,}, /* MT6315-3: VMD LP */
  59. ._[CMD_8] = {TOP_CON, 0x3,}, /* MT6315-3: PMIC NO LP */
  60. ._[CMD_9] = {TOP_CON, 0x0,}, /* MT6315-3: PMIC LP */
  61. ._[CMD_10] = {TOP_DIG_WPK, 0x63,}, /* MT6315-2: PMIC_CON_DIG_WPK */
  62. ._[CMD_11] = {TOP_CON_LOCK, 0x15,}, /* MT6315-2: PMIC_CON_UNLOCK */
  63. ._[CMD_12] = {TOP_DIG_WPK, 0x0,}, /* MT6315-2: PMIC_CON_DIG_WPK */
  64. ._[CMD_13] = {TOP_CON_LOCK, 0x0,}, /* MT6315-2: PMIC_CON_LOCK */
  65. ._[CMD_14] = {TOP_CLK_CON0, 0x0040,}, /* MT6359: 6359_LDO_SW_SEL_H */
  66. ._[CMD_15] = {TOP_CLK_CON0, 0x0000,}, /* MT6359: 6359_LDO_SW_SEL_L */
  67. .nr_idx = NR_IDX_ALL,
  68. },
  69. };
  70. void _mt_spm_pmic_table_init(void)
  71. {
  72. struct pmic_wrap_cmd pwrap_cmd_default[NR_PMIC_WRAP_CMD] = {
  73. { (uint32_t)SPM_DVFS_CMD0, (uint32_t)SPM_DVFS_CMD0, },
  74. { (uint32_t)SPM_DVFS_CMD1, (uint32_t)SPM_DVFS_CMD1, },
  75. { (uint32_t)SPM_DVFS_CMD2, (uint32_t)SPM_DVFS_CMD2, },
  76. { (uint32_t)SPM_DVFS_CMD3, (uint32_t)SPM_DVFS_CMD3, },
  77. { (uint32_t)SPM_DVFS_CMD4, (uint32_t)SPM_DVFS_CMD4, },
  78. { (uint32_t)SPM_DVFS_CMD5, (uint32_t)SPM_DVFS_CMD5, },
  79. { (uint32_t)SPM_DVFS_CMD6, (uint32_t)SPM_DVFS_CMD6, },
  80. { (uint32_t)SPM_DVFS_CMD7, (uint32_t)SPM_DVFS_CMD7, },
  81. { (uint32_t)SPM_DVFS_CMD8, (uint32_t)SPM_DVFS_CMD8, },
  82. { (uint32_t)SPM_DVFS_CMD9, (uint32_t)SPM_DVFS_CMD9, },
  83. { (uint32_t)SPM_DVFS_CMD10, (uint32_t)SPM_DVFS_CMD10, },
  84. { (uint32_t)SPM_DVFS_CMD11, (uint32_t)SPM_DVFS_CMD11, },
  85. { (uint32_t)SPM_DVFS_CMD12, (uint32_t)SPM_DVFS_CMD12, },
  86. { (uint32_t)SPM_DVFS_CMD13, (uint32_t)SPM_DVFS_CMD13, },
  87. { (uint32_t)SPM_DVFS_CMD14, (uint32_t)SPM_DVFS_CMD14, },
  88. { (uint32_t)SPM_DVFS_CMD15, (uint32_t)SPM_DVFS_CMD15, },
  89. };
  90. memcpy(pw.addr, pwrap_cmd_default, sizeof(pwrap_cmd_default));
  91. }
  92. void mt_spm_pmic_wrap_set_phase(enum pmic_wrap_phase_id phase)
  93. {
  94. int idx;
  95. if ((phase >= NR_PMIC_WRAP_PHASE) || (pw.phase == phase)) {
  96. return;
  97. }
  98. if (pw.addr[0].cmd_addr == 0) {
  99. _mt_spm_pmic_table_init();
  100. }
  101. pw.phase = phase;
  102. mmio_write_32(POWERON_CONFIG_EN, SPM_REGWR_CFG_KEY | BCLK_CG_EN_LSB);
  103. for (idx = 0; idx < pw.set[phase].nr_idx; idx++) {
  104. mmio_write_32(pw.addr[idx].cmd_addr,
  105. (pw.set[phase]._[idx].cmd_addr << SPM_DATA_SHIFT) |
  106. (pw.set[phase]._[idx].cmd_wdata));
  107. }
  108. }
  109. void mt_spm_pmic_wrap_set_cmd(enum pmic_wrap_phase_id phase, unsigned int idx,
  110. unsigned int cmd_wdata)
  111. {
  112. /* just set wdata value */
  113. if ((phase >= NR_PMIC_WRAP_PHASE) || (idx >= pw.set[phase].nr_idx)) {
  114. return;
  115. }
  116. pw.set[phase]._[idx].cmd_wdata = cmd_wdata;
  117. mmio_write_32(POWERON_CONFIG_EN, SPM_REGWR_CFG_KEY | BCLK_CG_EN_LSB);
  118. if (pw.phase == phase) {
  119. mmio_write_32(pw.addr[idx].cmd_addr,
  120. (pw.set[phase]._[idx].cmd_addr << SPM_DATA_SHIFT) | cmd_wdata);
  121. }
  122. }
  123. uint64_t mt_spm_pmic_wrap_get_cmd(enum pmic_wrap_phase_id phase, unsigned int idx)
  124. {
  125. /* just get wdata value */
  126. if ((phase >= NR_PMIC_WRAP_PHASE) || (idx >= pw.set[phase].nr_idx)) {
  127. return 0;
  128. }
  129. return pw.set[phase]._[idx].cmd_wdata;
  130. }