Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018, STMicroelectronics - All Rights Reserved |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <debug.h> |
| 8 | #include <delay_timer.h> |
| 9 | #include <errno.h> |
| 10 | #include <libfdt.h> |
| 11 | #include <mmio.h> |
| 12 | #include <mmio.h> |
| 13 | #include <platform_def.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <stm32_gpio.h> |
| 16 | #include <stm32mp1_clk.h> |
| 17 | #include <stm32mp1_dt.h> |
| 18 | #include <stm32mp1_pmic.h> |
| 19 | #include <stpmu1.h> |
| 20 | #include <utils_def.h> |
| 21 | |
| 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 | |
| 29 | #define STPMU1_LDO12356_OUTPUT_MASK (uint8_t)(GENMASK(6, 2)) |
| 30 | #define STPMU1_LDO12356_OUTPUT_SHIFT 2 |
| 31 | #define STPMU1_LDO3_MODE (uint8_t)(BIT(7)) |
| 32 | #define STPMU1_LDO3_DDR_SEL 31U |
| 33 | #define STPMU1_LDO3_1800000 (9U << STPMU1_LDO12356_OUTPUT_SHIFT) |
| 34 | |
| 35 | #define STPMU1_BUCK_OUTPUT_SHIFT 2 |
| 36 | #define STPMU1_BUCK3_1V8 (39U << STPMU1_BUCK_OUTPUT_SHIFT) |
| 37 | |
| 38 | #define STPMU1_DEFAULT_START_UP_DELAY_MS 1 |
| 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 | { |
| 45 | return fdt_node_offset_by_compatible(fdt, -1, "st,stpmu1"); |
| 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 | |
| 63 | return fdt_check_status(node); |
| 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 | |
| 140 | if (stpmu1_is_regulator_enabled(node_name) == 0U) { |
| 141 | int status; |
| 142 | |
| 143 | status = stpmu1_regulator_voltage_set(node_name, |
| 144 | voltage); |
| 145 | if (status != 0) { |
| 146 | return status; |
| 147 | } |
| 148 | |
| 149 | status = stpmu1_regulator_enable(node_name); |
| 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 | |
| 169 | if (stm32mp1_clk_enable((uint32_t)i2c_info.clock) < 0) { |
| 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 | |
| 206 | stpmu1_bind_i2c(&i2c_handle, (uint16_t)pmic_i2c_addr); |
| 207 | } |
| 208 | |
| 209 | void initialize_pmic(void) |
| 210 | { |
| 211 | int status; |
| 212 | uint8_t read_val; |
| 213 | |
| 214 | initialize_pmic_i2c(); |
| 215 | |
| 216 | status = stpmu1_register_read(VERSION_STATUS_REG, &read_val); |
| 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 */ |
| 224 | status = stpmu1_register_update(MASK_RESET_BUCK_REG, |
| 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 */ |
| 241 | status = stpmu1_register_read(LDO3_CONTROL_REG, &read_val); |
| 242 | if (status != 0) { |
| 243 | return status; |
| 244 | } |
| 245 | |
| 246 | read_val &= ~STPMU1_LDO3_MODE; |
| 247 | read_val &= ~STPMU1_LDO12356_OUTPUT_MASK; |
| 248 | read_val |= STPMU1_LDO3_DDR_SEL << STPMU1_LDO12356_OUTPUT_SHIFT; |
| 249 | |
| 250 | status = stpmu1_register_write(LDO3_CONTROL_REG, read_val); |
| 251 | if (status != 0) { |
| 252 | return status; |
| 253 | } |
| 254 | |
| 255 | status = stpmu1_regulator_voltage_set("buck2", 1350); |
| 256 | if (status != 0) { |
| 257 | return status; |
| 258 | } |
| 259 | |
| 260 | status = stpmu1_regulator_enable("buck2"); |
| 261 | if (status != 0) { |
| 262 | return status; |
| 263 | } |
| 264 | |
| 265 | mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS); |
| 266 | |
| 267 | status = stpmu1_regulator_enable("vref_ddr"); |
| 268 | if (status != 0) { |
| 269 | return status; |
| 270 | } |
| 271 | |
| 272 | mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS); |
| 273 | |
| 274 | status = stpmu1_regulator_enable("ldo3"); |
| 275 | if (status != 0) { |
| 276 | return status; |
| 277 | } |
| 278 | |
| 279 | mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS); |
| 280 | break; |
| 281 | |
| 282 | case STM32MP_LPDDR2: |
| 283 | /* |
| 284 | * Set LDO3 to 1.8V |
| 285 | * Set LDO3 to bypass mode if BUCK3 = 1.8V |
| 286 | * Set LDO3 to normal mode if BUCK3 != 1.8V |
| 287 | */ |
| 288 | status = stpmu1_register_read(BUCK3_CONTROL_REG, &read_val); |
| 289 | if (status != 0) { |
| 290 | return status; |
| 291 | } |
| 292 | |
| 293 | if ((read_val & STPMU1_BUCK3_1V8) == STPMU1_BUCK3_1V8) { |
| 294 | buck3_at_1v8 = true; |
| 295 | } |
| 296 | |
| 297 | status = stpmu1_register_read(LDO3_CONTROL_REG, &read_val); |
| 298 | if (status != 0) { |
| 299 | return status; |
| 300 | } |
| 301 | |
| 302 | read_val &= ~STPMU1_LDO3_MODE; |
| 303 | read_val &= ~STPMU1_LDO12356_OUTPUT_MASK; |
| 304 | read_val |= STPMU1_LDO3_1800000; |
| 305 | if (buck3_at_1v8) { |
| 306 | read_val |= STPMU1_LDO3_MODE; |
| 307 | } |
| 308 | |
| 309 | status = stpmu1_register_write(LDO3_CONTROL_REG, read_val); |
| 310 | if (status != 0) { |
| 311 | return status; |
| 312 | } |
| 313 | |
| 314 | status = stpmu1_regulator_voltage_set("buck2", 1200); |
| 315 | if (status != 0) { |
| 316 | return status; |
| 317 | } |
| 318 | |
| 319 | status = stpmu1_regulator_enable("ldo3"); |
| 320 | if (status != 0) { |
| 321 | return status; |
| 322 | } |
| 323 | |
| 324 | mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS); |
| 325 | |
| 326 | status = stpmu1_regulator_enable("buck2"); |
| 327 | if (status != 0) { |
| 328 | return status; |
| 329 | } |
| 330 | |
| 331 | mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS); |
| 332 | |
| 333 | status = stpmu1_regulator_enable("vref_ddr"); |
| 334 | if (status != 0) { |
| 335 | return status; |
| 336 | } |
| 337 | |
| 338 | mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS); |
| 339 | break; |
| 340 | |
| 341 | default: |
| 342 | break; |
| 343 | }; |
| 344 | |
| 345 | return 0; |
| 346 | } |