Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, STMicroelectronics - All Rights Reserved |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 7 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <stdbool.h> |
| 9 | |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 10 | #include <libfdt.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 12 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <common/debug.h> |
| 15 | #include <drivers/delay_timer.h> |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 16 | #include <drivers/st/stm32mp_pmic.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 17 | #include <drivers/st/stm32_gpio.h> |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 18 | #include <drivers/st/stpmic1.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 19 | #include <lib/mmio.h> |
| 20 | #include <lib/utils_def.h> |
| 21 | |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 22 | /* I2C Timing hard-coded value, for I2C clock source is HSI at 64MHz */ |
| 23 | #define I2C_TIMING 0x10D07DB5 |
| 24 | |
| 25 | #define I2C_TIMEOUT 0xFFFFF |
| 26 | |
| 27 | #define MASK_RESET_BUCK3 BIT(2) |
| 28 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 29 | #define STPMIC1_LDO12356_OUTPUT_MASK (uint8_t)(GENMASK(6, 2)) |
| 30 | #define STPMIC1_LDO12356_OUTPUT_SHIFT 2 |
| 31 | #define STPMIC1_LDO3_MODE (uint8_t)(BIT(7)) |
| 32 | #define STPMIC1_LDO3_DDR_SEL 31U |
| 33 | #define STPMIC1_LDO3_1800000 (9U << STPMIC1_LDO12356_OUTPUT_SHIFT) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 34 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 35 | #define STPMIC1_BUCK_OUTPUT_SHIFT 2 |
| 36 | #define STPMIC1_BUCK3_1V8 (39U << STPMIC1_BUCK_OUTPUT_SHIFT) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 37 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 38 | #define STPMIC1_DEFAULT_START_UP_DELAY_MS 1 |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 39 | |
| 40 | static struct i2c_handle_s i2c_handle; |
| 41 | static uint32_t pmic_i2c_addr; |
| 42 | |
| 43 | static int dt_get_pmic_node(void *fdt) |
| 44 | { |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 45 | return fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1"); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | bool dt_check_pmic(void) |
| 49 | { |
| 50 | int node; |
| 51 | void *fdt; |
| 52 | |
| 53 | if (fdt_get_address(&fdt) == 0) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | node = dt_get_pmic_node(fdt); |
| 58 | if (node < 0) { |
| 59 | VERBOSE("%s: No PMIC node found in DT\n", __func__); |
| 60 | return false; |
| 61 | } |
| 62 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 63 | return fdt_get_status(node); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int dt_pmic_i2c_config(struct dt_node_info *i2c_info) |
| 67 | { |
| 68 | int pmic_node, i2c_node; |
| 69 | void *fdt; |
| 70 | const fdt32_t *cuint; |
| 71 | |
| 72 | if (fdt_get_address(&fdt) == 0) { |
| 73 | return -ENOENT; |
| 74 | } |
| 75 | |
| 76 | pmic_node = dt_get_pmic_node(fdt); |
| 77 | if (pmic_node < 0) { |
| 78 | return -FDT_ERR_NOTFOUND; |
| 79 | } |
| 80 | |
| 81 | cuint = fdt_getprop(fdt, pmic_node, "reg", NULL); |
| 82 | if (cuint == NULL) { |
| 83 | return -FDT_ERR_NOTFOUND; |
| 84 | } |
| 85 | |
| 86 | pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1; |
| 87 | if (pmic_i2c_addr > UINT16_MAX) { |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
| 91 | i2c_node = fdt_parent_offset(fdt, pmic_node); |
| 92 | if (i2c_node < 0) { |
| 93 | return -FDT_ERR_NOTFOUND; |
| 94 | } |
| 95 | |
| 96 | dt_fill_device_info(i2c_info, i2c_node); |
| 97 | if (i2c_info->base == 0U) { |
| 98 | return -FDT_ERR_NOTFOUND; |
| 99 | } |
| 100 | |
| 101 | return dt_set_pinctrl_config(i2c_node); |
| 102 | } |
| 103 | |
| 104 | int dt_pmic_enable_boot_on_regulators(void) |
| 105 | { |
| 106 | int pmic_node, regulators_node, regulator_node; |
| 107 | void *fdt; |
| 108 | |
| 109 | if (fdt_get_address(&fdt) == 0) { |
| 110 | return -ENOENT; |
| 111 | } |
| 112 | |
| 113 | pmic_node = dt_get_pmic_node(fdt); |
| 114 | if (pmic_node < 0) { |
| 115 | return -FDT_ERR_NOTFOUND; |
| 116 | } |
| 117 | |
| 118 | regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators"); |
| 119 | |
| 120 | fdt_for_each_subnode(regulator_node, fdt, regulators_node) { |
| 121 | const fdt32_t *cuint; |
| 122 | const char *node_name; |
| 123 | uint16_t voltage; |
| 124 | |
| 125 | if (fdt_getprop(fdt, regulator_node, "regulator-boot-on", |
| 126 | NULL) == NULL) { |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | cuint = fdt_getprop(fdt, regulator_node, |
| 131 | "regulator-min-microvolt", NULL); |
| 132 | if (cuint == NULL) { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | /* DT uses microvolts, whereas driver awaits millivolts */ |
| 137 | voltage = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U); |
| 138 | node_name = fdt_get_name(fdt, regulator_node, NULL); |
| 139 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 140 | if (stpmic1_is_regulator_enabled(node_name) == 0U) { |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 141 | int status; |
| 142 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 143 | status = stpmic1_regulator_voltage_set(node_name, |
| 144 | voltage); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 145 | if (status != 0) { |
| 146 | return status; |
| 147 | } |
| 148 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 149 | status = stpmic1_regulator_enable(node_name); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 150 | if (status != 0) { |
| 151 | return status; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | void initialize_pmic_i2c(void) |
| 160 | { |
| 161 | int ret; |
| 162 | struct dt_node_info i2c_info; |
| 163 | |
| 164 | if (dt_pmic_i2c_config(&i2c_info) != 0) { |
| 165 | ERROR("I2C configuration failed\n"); |
| 166 | panic(); |
| 167 | } |
| 168 | |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 169 | if (stm32mp_clk_enable((uint32_t)i2c_info.clock) < 0) { |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 170 | ERROR("I2C clock enable failed\n"); |
| 171 | panic(); |
| 172 | } |
| 173 | |
| 174 | /* Initialize PMIC I2C */ |
| 175 | i2c_handle.i2c_base_addr = i2c_info.base; |
| 176 | i2c_handle.i2c_init.timing = I2C_TIMING; |
| 177 | i2c_handle.i2c_init.own_address1 = pmic_i2c_addr; |
| 178 | i2c_handle.i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT; |
| 179 | i2c_handle.i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE; |
| 180 | i2c_handle.i2c_init.own_address2 = 0; |
| 181 | i2c_handle.i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK; |
| 182 | i2c_handle.i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE; |
| 183 | i2c_handle.i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE; |
| 184 | |
| 185 | ret = stm32_i2c_init(&i2c_handle); |
| 186 | if (ret != 0) { |
| 187 | ERROR("Cannot initialize I2C %x (%d)\n", |
| 188 | i2c_handle.i2c_base_addr, ret); |
| 189 | panic(); |
| 190 | } |
| 191 | |
| 192 | ret = stm32_i2c_config_analog_filter(&i2c_handle, |
| 193 | I2C_ANALOGFILTER_ENABLE); |
| 194 | if (ret != 0) { |
| 195 | ERROR("Cannot initialize I2C analog filter (%d)\n", ret); |
| 196 | panic(); |
| 197 | } |
| 198 | |
| 199 | ret = stm32_i2c_is_device_ready(&i2c_handle, (uint16_t)pmic_i2c_addr, 1, |
| 200 | I2C_TIMEOUT); |
| 201 | if (ret != 0) { |
| 202 | ERROR("I2C device not ready (%d)\n", ret); |
| 203 | panic(); |
| 204 | } |
| 205 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 206 | stpmic1_bind_i2c(&i2c_handle, (uint16_t)pmic_i2c_addr); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void initialize_pmic(void) |
| 210 | { |
| 211 | int status; |
| 212 | uint8_t read_val; |
| 213 | |
| 214 | initialize_pmic_i2c(); |
| 215 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 216 | status = stpmic1_register_read(VERSION_STATUS_REG, &read_val); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 217 | if (status != 0) { |
| 218 | panic(); |
| 219 | } |
| 220 | |
| 221 | INFO("PMIC version = 0x%x\n", read_val); |
| 222 | |
| 223 | /* Keep VDD on during the reset cycle */ |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 224 | status = stpmic1_register_update(MASK_RESET_BUCK_REG, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 225 | MASK_RESET_BUCK3, |
| 226 | MASK_RESET_BUCK3); |
| 227 | if (status != 0) { |
| 228 | panic(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | int pmic_ddr_power_init(enum ddr_type ddr_type) |
| 233 | { |
| 234 | bool buck3_at_1v8 = false; |
| 235 | uint8_t read_val; |
| 236 | int status; |
| 237 | |
| 238 | switch (ddr_type) { |
| 239 | case STM32MP_DDR3: |
| 240 | /* Set LDO3 to sync mode */ |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 241 | status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 242 | if (status != 0) { |
| 243 | return status; |
| 244 | } |
| 245 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 246 | read_val &= ~STPMIC1_LDO3_MODE; |
| 247 | read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK; |
| 248 | read_val |= STPMIC1_LDO3_DDR_SEL << |
| 249 | STPMIC1_LDO12356_OUTPUT_SHIFT; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 250 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 251 | status = stpmic1_register_write(LDO3_CONTROL_REG, read_val); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 252 | if (status != 0) { |
| 253 | return status; |
| 254 | } |
| 255 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 256 | status = stpmic1_regulator_voltage_set("buck2", 1350); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 257 | if (status != 0) { |
| 258 | return status; |
| 259 | } |
| 260 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 261 | status = stpmic1_regulator_enable("buck2"); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 262 | if (status != 0) { |
| 263 | return status; |
| 264 | } |
| 265 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 266 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 267 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 268 | status = stpmic1_regulator_enable("vref_ddr"); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 269 | if (status != 0) { |
| 270 | return status; |
| 271 | } |
| 272 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 273 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 274 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 275 | status = stpmic1_regulator_enable("ldo3"); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 276 | if (status != 0) { |
| 277 | return status; |
| 278 | } |
| 279 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 280 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 281 | break; |
| 282 | |
| 283 | case STM32MP_LPDDR2: |
| 284 | /* |
| 285 | * Set LDO3 to 1.8V |
| 286 | * Set LDO3 to bypass mode if BUCK3 = 1.8V |
| 287 | * Set LDO3 to normal mode if BUCK3 != 1.8V |
| 288 | */ |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 289 | status = stpmic1_register_read(BUCK3_CONTROL_REG, &read_val); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 290 | if (status != 0) { |
| 291 | return status; |
| 292 | } |
| 293 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 294 | if ((read_val & STPMIC1_BUCK3_1V8) == STPMIC1_BUCK3_1V8) { |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 295 | buck3_at_1v8 = true; |
| 296 | } |
| 297 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 298 | status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 299 | if (status != 0) { |
| 300 | return status; |
| 301 | } |
| 302 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 303 | read_val &= ~STPMIC1_LDO3_MODE; |
| 304 | read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK; |
| 305 | read_val |= STPMIC1_LDO3_1800000; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 306 | if (buck3_at_1v8) { |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 307 | read_val |= STPMIC1_LDO3_MODE; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 310 | status = stpmic1_register_write(LDO3_CONTROL_REG, read_val); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 311 | if (status != 0) { |
| 312 | return status; |
| 313 | } |
| 314 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 315 | status = stpmic1_regulator_voltage_set("buck2", 1200); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 316 | if (status != 0) { |
| 317 | return status; |
| 318 | } |
| 319 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 320 | status = stpmic1_regulator_enable("ldo3"); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 321 | if (status != 0) { |
| 322 | return status; |
| 323 | } |
| 324 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 325 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 326 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 327 | status = stpmic1_regulator_enable("buck2"); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 328 | if (status != 0) { |
| 329 | return status; |
| 330 | } |
| 331 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 332 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 333 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 334 | status = stpmic1_regulator_enable("vref_ddr"); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 335 | if (status != 0) { |
| 336 | return status; |
| 337 | } |
| 338 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 339 | mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 340 | break; |
| 341 | |
| 342 | default: |
| 343 | break; |
| 344 | }; |
| 345 | |
| 346 | return 0; |
| 347 | } |