Patrick Delaunay | 918e9c3 | 2020-05-25 12:19:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2020, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Patrick Delaunay | d1a4b09 | 2020-05-25 12:19:46 +0200 | [diff] [blame] | 8 | #include <asm/io.h> |
Patrick Delaunay | 918e9c3 | 2020-05-25 12:19:43 +0200 | [diff] [blame] | 9 | #include <asm/arch/ddr.h> |
| 10 | #include <linux/bitops.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <power/pmic.h> |
| 13 | #include <power/stpmic1.h> |
| 14 | |
| 15 | int board_ddr_power_init(enum ddr_type ddr_type) |
| 16 | { |
| 17 | struct udevice *dev; |
| 18 | bool buck3_at_1800000v = false; |
| 19 | int ret; |
| 20 | u32 buck2; |
| 21 | |
| 22 | ret = uclass_get_device_by_driver(UCLASS_PMIC, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame^] | 23 | DM_DRIVER_GET(pmic_stpmic1), &dev); |
Patrick Delaunay | 918e9c3 | 2020-05-25 12:19:43 +0200 | [diff] [blame] | 24 | if (ret) |
| 25 | /* No PMIC on board */ |
| 26 | return 0; |
| 27 | |
| 28 | switch (ddr_type) { |
| 29 | case STM32MP_DDR3: |
| 30 | /* VTT = Set LDO3 to sync mode */ |
| 31 | ret = pmic_reg_read(dev, STPMIC1_LDOX_MAIN_CR(STPMIC1_LDO3)); |
| 32 | if (ret < 0) |
| 33 | return ret; |
| 34 | |
| 35 | ret &= ~STPMIC1_LDO3_MODE; |
| 36 | ret &= ~STPMIC1_LDO12356_VOUT_MASK; |
| 37 | ret |= STPMIC1_LDO_VOUT(STPMIC1_LDO3_DDR_SEL); |
| 38 | |
| 39 | ret = pmic_reg_write(dev, STPMIC1_LDOX_MAIN_CR(STPMIC1_LDO3), |
| 40 | ret); |
| 41 | if (ret < 0) |
| 42 | return ret; |
| 43 | |
| 44 | /* VDD_DDR = Set BUCK2 to 1.35V */ |
| 45 | ret = pmic_clrsetbits(dev, |
| 46 | STPMIC1_BUCKX_MAIN_CR(STPMIC1_BUCK2), |
| 47 | STPMIC1_BUCK_VOUT_MASK, |
| 48 | STPMIC1_BUCK2_1350000V); |
| 49 | if (ret < 0) |
| 50 | return ret; |
| 51 | |
| 52 | /* Enable VDD_DDR = BUCK2 */ |
| 53 | ret = pmic_clrsetbits(dev, |
| 54 | STPMIC1_BUCKX_MAIN_CR(STPMIC1_BUCK2), |
| 55 | STPMIC1_BUCK_ENA, STPMIC1_BUCK_ENA); |
| 56 | if (ret < 0) |
| 57 | return ret; |
| 58 | |
| 59 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
| 60 | |
| 61 | /* Enable VREF */ |
| 62 | ret = pmic_clrsetbits(dev, STPMIC1_REFDDR_MAIN_CR, |
| 63 | STPMIC1_VREF_ENA, STPMIC1_VREF_ENA); |
| 64 | if (ret < 0) |
| 65 | return ret; |
| 66 | |
| 67 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
| 68 | |
| 69 | /* Enable VTT = LDO3 */ |
| 70 | ret = pmic_clrsetbits(dev, |
| 71 | STPMIC1_LDOX_MAIN_CR(STPMIC1_LDO3), |
| 72 | STPMIC1_LDO_ENA, STPMIC1_LDO_ENA); |
| 73 | if (ret < 0) |
| 74 | return ret; |
| 75 | |
| 76 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
| 77 | |
| 78 | break; |
| 79 | |
| 80 | case STM32MP_LPDDR2_16: |
| 81 | case STM32MP_LPDDR2_32: |
| 82 | case STM32MP_LPDDR3_16: |
| 83 | case STM32MP_LPDDR3_32: |
| 84 | /* |
| 85 | * configure VDD_DDR1 = LDO3 |
| 86 | * Set LDO3 to 1.8V |
| 87 | * + bypass mode if BUCK3 = 1.8V |
| 88 | * + normal mode if BUCK3 != 1.8V |
| 89 | */ |
| 90 | ret = pmic_reg_read(dev, |
| 91 | STPMIC1_BUCKX_MAIN_CR(STPMIC1_BUCK3)); |
| 92 | if (ret < 0) |
| 93 | return ret; |
| 94 | |
| 95 | if ((ret & STPMIC1_BUCK3_1800000V) == STPMIC1_BUCK3_1800000V) |
| 96 | buck3_at_1800000v = true; |
| 97 | |
| 98 | ret = pmic_reg_read(dev, STPMIC1_LDOX_MAIN_CR(STPMIC1_LDO3)); |
| 99 | if (ret < 0) |
| 100 | return ret; |
| 101 | |
| 102 | ret &= ~STPMIC1_LDO3_MODE; |
| 103 | ret &= ~STPMIC1_LDO12356_VOUT_MASK; |
| 104 | ret |= STPMIC1_LDO3_1800000; |
| 105 | if (buck3_at_1800000v) |
| 106 | ret |= STPMIC1_LDO3_MODE; |
| 107 | |
| 108 | ret = pmic_reg_write(dev, STPMIC1_LDOX_MAIN_CR(STPMIC1_LDO3), |
| 109 | ret); |
| 110 | if (ret < 0) |
| 111 | return ret; |
| 112 | |
| 113 | /* VDD_DDR2 : Set BUCK2 to 1.2V (16bits) or 1.25V (32 bits)*/ |
| 114 | switch (ddr_type) { |
| 115 | case STM32MP_LPDDR2_32: |
| 116 | case STM32MP_LPDDR3_32: |
| 117 | buck2 = STPMIC1_BUCK2_1250000V; |
| 118 | break; |
| 119 | default: |
| 120 | case STM32MP_LPDDR2_16: |
| 121 | case STM32MP_LPDDR3_16: |
| 122 | buck2 = STPMIC1_BUCK2_1200000V; |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | ret = pmic_clrsetbits(dev, |
| 127 | STPMIC1_BUCKX_MAIN_CR(STPMIC1_BUCK2), |
| 128 | STPMIC1_BUCK_VOUT_MASK, |
| 129 | buck2); |
| 130 | if (ret < 0) |
| 131 | return ret; |
| 132 | |
| 133 | /* Enable VDD_DDR1 = LDO3 */ |
| 134 | ret = pmic_clrsetbits(dev, STPMIC1_LDOX_MAIN_CR(STPMIC1_LDO3), |
| 135 | STPMIC1_LDO_ENA, STPMIC1_LDO_ENA); |
| 136 | if (ret < 0) |
| 137 | return ret; |
| 138 | |
| 139 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
| 140 | |
| 141 | /* Enable VDD_DDR2 =BUCK2 */ |
| 142 | ret = pmic_clrsetbits(dev, |
| 143 | STPMIC1_BUCKX_MAIN_CR(STPMIC1_BUCK2), |
| 144 | STPMIC1_BUCK_ENA, STPMIC1_BUCK_ENA); |
| 145 | if (ret < 0) |
| 146 | return ret; |
| 147 | |
| 148 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
| 149 | |
| 150 | /* Enable VREF */ |
| 151 | ret = pmic_clrsetbits(dev, STPMIC1_REFDDR_MAIN_CR, |
| 152 | STPMIC1_VREF_ENA, STPMIC1_VREF_ENA); |
| 153 | if (ret < 0) |
| 154 | return ret; |
| 155 | |
| 156 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
| 157 | |
| 158 | break; |
| 159 | |
| 160 | default: |
| 161 | break; |
| 162 | }; |
| 163 | |
| 164 | return 0; |
| 165 | } |
Patrick Delaunay | d1a4b09 | 2020-05-25 12:19:46 +0200 | [diff] [blame] | 166 | |
Patrick Delaunay | 2f23832 | 2020-05-25 12:19:47 +0200 | [diff] [blame] | 167 | static int stmpic_buck1_set(struct udevice *dev, u32 voltage_mv) |
| 168 | { |
| 169 | u32 value; |
| 170 | |
| 171 | /* VDDCORE= STMPCI1 BUCK1 ramp=+25mV, 5 => 725mV, 36 => 1500mV */ |
| 172 | value = ((voltage_mv - 725) / 25) + 5; |
| 173 | if (value < 5) |
| 174 | value = 5; |
| 175 | if (value > 36) |
| 176 | value = 36; |
| 177 | |
| 178 | return pmic_clrsetbits(dev, |
| 179 | STPMIC1_BUCKX_MAIN_CR(STPMIC1_BUCK1), |
| 180 | STPMIC1_BUCK_VOUT_MASK, |
| 181 | STPMIC1_BUCK_VOUT(value)); |
| 182 | } |
| 183 | |
Patrick Delaunay | d1a4b09 | 2020-05-25 12:19:46 +0200 | [diff] [blame] | 184 | /* early init of PMIC */ |
Patrick Delaunay | 2f23832 | 2020-05-25 12:19:47 +0200 | [diff] [blame] | 185 | void stpmic1_init(u32 voltage_mv) |
Patrick Delaunay | d1a4b09 | 2020-05-25 12:19:46 +0200 | [diff] [blame] | 186 | { |
| 187 | struct udevice *dev; |
| 188 | |
| 189 | if (uclass_get_device_by_driver(UCLASS_PMIC, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame^] | 190 | DM_DRIVER_GET(pmic_stpmic1), &dev)) |
Patrick Delaunay | d1a4b09 | 2020-05-25 12:19:46 +0200 | [diff] [blame] | 191 | return; |
| 192 | |
Patrick Delaunay | 2f23832 | 2020-05-25 12:19:47 +0200 | [diff] [blame] | 193 | /* update VDDCORE = BUCK1 */ |
| 194 | if (voltage_mv) |
| 195 | stmpic_buck1_set(dev, voltage_mv); |
| 196 | |
Patrick Delaunay | d1a4b09 | 2020-05-25 12:19:46 +0200 | [diff] [blame] | 197 | /* Keep vdd on during the reset cycle */ |
| 198 | pmic_clrsetbits(dev, |
| 199 | STPMIC1_BUCKS_MRST_CR, |
| 200 | STPMIC1_MRST_BUCK(STPMIC1_BUCK3), |
| 201 | STPMIC1_MRST_BUCK(STPMIC1_BUCK3)); |
| 202 | |
| 203 | /* Check if debug is enabled to program PMIC according to the bit */ |
| 204 | if (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_DEBUG_ON) { |
| 205 | printf("Keep debug unit ON\n"); |
| 206 | |
| 207 | pmic_clrsetbits(dev, STPMIC1_BUCKS_MRST_CR, |
| 208 | STPMIC1_MRST_BUCK_DEBUG, |
| 209 | STPMIC1_MRST_BUCK_DEBUG); |
| 210 | |
| 211 | if (STPMIC1_MRST_LDO_DEBUG) |
| 212 | pmic_clrsetbits(dev, STPMIC1_LDOS_MRST_CR, |
| 213 | STPMIC1_MRST_LDO_DEBUG, |
| 214 | STPMIC1_MRST_LDO_DEBUG); |
| 215 | } |
| 216 | } |