blob: 52f712f108398115bc3f3b7233c3eb9fc8ffb12f [file] [log] [blame]
Yann Gautierbb836ee2018-07-16 17:55:07 +02001/*
Yann Gautiera45433b2019-01-16 18:31:00 +01002 * Copyright (c) 2017-2019, STMicroelectronics - All Rights Reserved
Yann Gautierbb836ee2018-07-16 17:55:07 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautierbb836ee2018-07-16 17:55:07 +02007#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <stdbool.h>
9
Yann Gautierbb836ee2018-07-16 17:55:07 +020010#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
Yann Gautierbb836ee2018-07-16 17:55:07 +020012#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013
14#include <common/debug.h>
15#include <drivers/delay_timer.h>
Yann Gautiera45433b2019-01-16 18:31:00 +010016#include <drivers/st/stm32mp_pmic.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <drivers/st/stm32_gpio.h>
Yann Gautiera45433b2019-01-16 18:31:00 +010018#include <drivers/st/stpmic1.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000019#include <lib/mmio.h>
20#include <lib/utils_def.h>
21
Yann Gautierbb836ee2018-07-16 17:55:07 +020022/* 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 Gautiera45433b2019-01-16 18:31:00 +010029#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 Gautierbb836ee2018-07-16 17:55:07 +020034
Yann Gautiera45433b2019-01-16 18:31:00 +010035#define STPMIC1_BUCK_OUTPUT_SHIFT 2
36#define STPMIC1_BUCK3_1V8 (39U << STPMIC1_BUCK_OUTPUT_SHIFT)
Yann Gautierbb836ee2018-07-16 17:55:07 +020037
Yann Gautiera45433b2019-01-16 18:31:00 +010038#define STPMIC1_DEFAULT_START_UP_DELAY_MS 1
Yann Gautierbb836ee2018-07-16 17:55:07 +020039
40static struct i2c_handle_s i2c_handle;
41static uint32_t pmic_i2c_addr;
42
43static int dt_get_pmic_node(void *fdt)
44{
Yann Gautiera45433b2019-01-16 18:31:00 +010045 return fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
Yann Gautierbb836ee2018-07-16 17:55:07 +020046}
47
48bool 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 Gautier038bff22019-01-17 19:17:47 +010063 return fdt_get_status(node);
Yann Gautierbb836ee2018-07-16 17:55:07 +020064}
65
66static 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
104int 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 Gautiera45433b2019-01-16 18:31:00 +0100140 if (stpmic1_is_regulator_enabled(node_name) == 0U) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200141 int status;
142
Yann Gautiera45433b2019-01-16 18:31:00 +0100143 status = stpmic1_regulator_voltage_set(node_name,
144 voltage);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200145 if (status != 0) {
146 return status;
147 }
148
Yann Gautiera45433b2019-01-16 18:31:00 +0100149 status = stpmic1_regulator_enable(node_name);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200150 if (status != 0) {
151 return status;
152 }
153 }
154 }
155
156 return 0;
157}
158
159void 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 Gautiera2e2a302019-02-14 11:13:39 +0100169 if (stm32mp_clk_enable((uint32_t)i2c_info.clock) < 0) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200170 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 Gautiera45433b2019-01-16 18:31:00 +0100206 stpmic1_bind_i2c(&i2c_handle, (uint16_t)pmic_i2c_addr);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200207}
208
209void initialize_pmic(void)
210{
211 int status;
212 uint8_t read_val;
213
214 initialize_pmic_i2c();
215
Yann Gautiera45433b2019-01-16 18:31:00 +0100216 status = stpmic1_register_read(VERSION_STATUS_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200217 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 Gautiera45433b2019-01-16 18:31:00 +0100224 status = stpmic1_register_update(MASK_RESET_BUCK_REG,
Yann Gautierbb836ee2018-07-16 17:55:07 +0200225 MASK_RESET_BUCK3,
226 MASK_RESET_BUCK3);
227 if (status != 0) {
228 panic();
229 }
230}
231
232int 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 Gautiera45433b2019-01-16 18:31:00 +0100241 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200242 if (status != 0) {
243 return status;
244 }
245
Yann Gautiera45433b2019-01-16 18:31:00 +0100246 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 Gautierbb836ee2018-07-16 17:55:07 +0200250
Yann Gautiera45433b2019-01-16 18:31:00 +0100251 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200252 if (status != 0) {
253 return status;
254 }
255
Yann Gautiera45433b2019-01-16 18:31:00 +0100256 status = stpmic1_regulator_voltage_set("buck2", 1350);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200257 if (status != 0) {
258 return status;
259 }
260
Yann Gautiera45433b2019-01-16 18:31:00 +0100261 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200262 if (status != 0) {
263 return status;
264 }
265
Yann Gautiera45433b2019-01-16 18:31:00 +0100266 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200267
Yann Gautiera45433b2019-01-16 18:31:00 +0100268 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200269 if (status != 0) {
270 return status;
271 }
272
Yann Gautiera45433b2019-01-16 18:31:00 +0100273 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200274
Yann Gautiera45433b2019-01-16 18:31:00 +0100275 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200276 if (status != 0) {
277 return status;
278 }
279
Yann Gautiera45433b2019-01-16 18:31:00 +0100280 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200281 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 Gautiera45433b2019-01-16 18:31:00 +0100289 status = stpmic1_register_read(BUCK3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200290 if (status != 0) {
291 return status;
292 }
293
Yann Gautiera45433b2019-01-16 18:31:00 +0100294 if ((read_val & STPMIC1_BUCK3_1V8) == STPMIC1_BUCK3_1V8) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200295 buck3_at_1v8 = true;
296 }
297
Yann Gautiera45433b2019-01-16 18:31:00 +0100298 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200299 if (status != 0) {
300 return status;
301 }
302
Yann Gautiera45433b2019-01-16 18:31:00 +0100303 read_val &= ~STPMIC1_LDO3_MODE;
304 read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
305 read_val |= STPMIC1_LDO3_1800000;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200306 if (buck3_at_1v8) {
Yann Gautiera45433b2019-01-16 18:31:00 +0100307 read_val |= STPMIC1_LDO3_MODE;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200308 }
309
Yann Gautiera45433b2019-01-16 18:31:00 +0100310 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200311 if (status != 0) {
312 return status;
313 }
314
Yann Gautiera45433b2019-01-16 18:31:00 +0100315 status = stpmic1_regulator_voltage_set("buck2", 1200);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200316 if (status != 0) {
317 return status;
318 }
319
Yann Gautiera45433b2019-01-16 18:31:00 +0100320 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200321 if (status != 0) {
322 return status;
323 }
324
Yann Gautiera45433b2019-01-16 18:31:00 +0100325 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200326
Yann Gautiera45433b2019-01-16 18:31:00 +0100327 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200328 if (status != 0) {
329 return status;
330 }
331
Yann Gautiera45433b2019-01-16 18:31:00 +0100332 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200333
Yann Gautiera45433b2019-01-16 18:31:00 +0100334 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200335 if (status != 0) {
336 return status;
337 }
338
Yann Gautiera45433b2019-01-16 18:31:00 +0100339 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200340 break;
341
342 default:
343 break;
344 };
345
346 return 0;
347}