blob: be410a1f4962474921ae5397c5418aa315052836 [file] [log] [blame]
Yann Gautierbb836ee2018-07-16 17:55:07 +02001/*
Benjamin Gaignard53ee7d32020-02-24 13:57:40 +01002 * Copyright (c) 2017-2021, 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
Yann Gautierbb836ee2018-07-16 17:55:07 +02009#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
Yann Gautierbb836ee2018-07-16 17:55:07 +020011#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <common/debug.h>
14#include <drivers/delay_timer.h>
Yann Gautierf3928f62019-02-14 11:15:03 +010015#include <drivers/st/stm32_i2c.h>
Yann Gautiera45433b2019-01-16 18:31:00 +010016#include <drivers/st/stm32mp_pmic.h>
Yann Gautiera45433b2019-01-16 18:31:00 +010017#include <drivers/st/stpmic1.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <lib/mmio.h>
19#include <lib/utils_def.h>
20
Yann Gautiera45433b2019-01-16 18:31:00 +010021#define STPMIC1_LDO12356_OUTPUT_MASK (uint8_t)(GENMASK(6, 2))
22#define STPMIC1_LDO12356_OUTPUT_SHIFT 2
23#define STPMIC1_LDO3_MODE (uint8_t)(BIT(7))
24#define STPMIC1_LDO3_DDR_SEL 31U
25#define STPMIC1_LDO3_1800000 (9U << STPMIC1_LDO12356_OUTPUT_SHIFT)
Yann Gautierbb836ee2018-07-16 17:55:07 +020026
Yann Gautiera45433b2019-01-16 18:31:00 +010027#define STPMIC1_BUCK_OUTPUT_SHIFT 2
28#define STPMIC1_BUCK3_1V8 (39U << STPMIC1_BUCK_OUTPUT_SHIFT)
Yann Gautierbb836ee2018-07-16 17:55:07 +020029
Yann Gautiera45433b2019-01-16 18:31:00 +010030#define STPMIC1_DEFAULT_START_UP_DELAY_MS 1
Yann Gautierbb836ee2018-07-16 17:55:07 +020031
32static struct i2c_handle_s i2c_handle;
33static uint32_t pmic_i2c_addr;
34
35static int dt_get_pmic_node(void *fdt)
36{
Yann Gautiera45433b2019-01-16 18:31:00 +010037 return fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
Yann Gautierbb836ee2018-07-16 17:55:07 +020038}
39
Yann Gautierf3928f62019-02-14 11:15:03 +010040int dt_pmic_status(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +020041{
42 int node;
43 void *fdt;
44
45 if (fdt_get_address(&fdt) == 0) {
Yann Gautierf3928f62019-02-14 11:15:03 +010046 return -ENOENT;
Yann Gautierbb836ee2018-07-16 17:55:07 +020047 }
48
49 node = dt_get_pmic_node(fdt);
Yann Gautierf3928f62019-02-14 11:15:03 +010050 if (node <= 0) {
51 return -FDT_ERR_NOTFOUND;
Yann Gautierbb836ee2018-07-16 17:55:07 +020052 }
53
Yann Gautier038bff22019-01-17 19:17:47 +010054 return fdt_get_status(node);
Yann Gautierbb836ee2018-07-16 17:55:07 +020055}
56
Etienne Carriere67b106a2019-12-02 10:10:08 +010057static bool dt_pmic_is_secure(void)
58{
59 int status = dt_pmic_status();
60
61 return (status >= 0) &&
62 (status == DT_SECURE) &&
63 (i2c_handle.dt_status == DT_SECURE);
64}
65
Yann Gautierf3928f62019-02-14 11:15:03 +010066/*
67 * Get PMIC and its I2C bus configuration from the device tree.
68 * Return 0 on success, negative on error, 1 if no PMIC node is found.
69 */
70static int dt_pmic_i2c_config(struct dt_node_info *i2c_info,
71 struct stm32_i2c_init_s *init)
Yann Gautierbb836ee2018-07-16 17:55:07 +020072{
73 int pmic_node, i2c_node;
74 void *fdt;
75 const fdt32_t *cuint;
76
77 if (fdt_get_address(&fdt) == 0) {
78 return -ENOENT;
79 }
80
81 pmic_node = dt_get_pmic_node(fdt);
82 if (pmic_node < 0) {
Yann Gautierf3928f62019-02-14 11:15:03 +010083 return 1;
Yann Gautierbb836ee2018-07-16 17:55:07 +020084 }
85
86 cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
87 if (cuint == NULL) {
88 return -FDT_ERR_NOTFOUND;
89 }
90
91 pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1;
92 if (pmic_i2c_addr > UINT16_MAX) {
93 return -EINVAL;
94 }
95
96 i2c_node = fdt_parent_offset(fdt, pmic_node);
97 if (i2c_node < 0) {
98 return -FDT_ERR_NOTFOUND;
99 }
100
101 dt_fill_device_info(i2c_info, i2c_node);
102 if (i2c_info->base == 0U) {
103 return -FDT_ERR_NOTFOUND;
104 }
105
Yann Gautierf3928f62019-02-14 11:15:03 +0100106 return stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200107}
108
Yann Gautierf3928f62019-02-14 11:15:03 +0100109int dt_pmic_configure_boot_on_regulators(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +0200110{
111 int pmic_node, regulators_node, regulator_node;
112 void *fdt;
113
114 if (fdt_get_address(&fdt) == 0) {
115 return -ENOENT;
116 }
117
118 pmic_node = dt_get_pmic_node(fdt);
119 if (pmic_node < 0) {
120 return -FDT_ERR_NOTFOUND;
121 }
122
123 regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");
Nicolas Le Bayon72ceb352020-03-10 18:18:45 +0100124 if (regulators_node < 0) {
125 return -ENOENT;
126 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200127
128 fdt_for_each_subnode(regulator_node, fdt, regulators_node) {
129 const fdt32_t *cuint;
Yann Gautierf3928f62019-02-14 11:15:03 +0100130 const char *node_name = fdt_get_name(fdt, regulator_node, NULL);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200131 uint16_t voltage;
Yann Gautierf3928f62019-02-14 11:15:03 +0100132 int status;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200133
Yann Gautierf3928f62019-02-14 11:15:03 +0100134#if defined(IMAGE_BL2)
135 if ((fdt_getprop(fdt, regulator_node, "regulator-boot-on",
136 NULL) == NULL) &&
137 (fdt_getprop(fdt, regulator_node, "regulator-always-on",
138 NULL) == NULL)) {
139#else
Yann Gautierbb836ee2018-07-16 17:55:07 +0200140 if (fdt_getprop(fdt, regulator_node, "regulator-boot-on",
141 NULL) == NULL) {
Yann Gautierf3928f62019-02-14 11:15:03 +0100142#endif
Yann Gautierbb836ee2018-07-16 17:55:07 +0200143 continue;
144 }
145
Yann Gautierf3928f62019-02-14 11:15:03 +0100146 if (fdt_getprop(fdt, regulator_node, "regulator-pull-down",
147 NULL) != NULL) {
148
149 status = stpmic1_regulator_pull_down_set(node_name);
150 if (status != 0) {
151 return status;
152 }
153 }
154
155 if (fdt_getprop(fdt, regulator_node, "st,mask-reset",
156 NULL) != NULL) {
157
158 status = stpmic1_regulator_mask_reset_set(node_name);
159 if (status != 0) {
160 return status;
161 }
162 }
163
Yann Gautierbb836ee2018-07-16 17:55:07 +0200164 cuint = fdt_getprop(fdt, regulator_node,
165 "regulator-min-microvolt", NULL);
166 if (cuint == NULL) {
167 continue;
168 }
169
170 /* DT uses microvolts, whereas driver awaits millivolts */
171 voltage = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200172
Yann Gautierf3928f62019-02-14 11:15:03 +0100173 status = stpmic1_regulator_voltage_set(node_name, voltage);
174 if (status != 0) {
175 return status;
176 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200177
Yann Gautierf3928f62019-02-14 11:15:03 +0100178 if (stpmic1_is_regulator_enabled(node_name) == 0U) {
Yann Gautiera45433b2019-01-16 18:31:00 +0100179 status = stpmic1_regulator_enable(node_name);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200180 if (status != 0) {
181 return status;
182 }
183 }
184 }
185
186 return 0;
187}
188
Yann Gautierf3928f62019-02-14 11:15:03 +0100189bool initialize_pmic_i2c(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +0200190{
191 int ret;
192 struct dt_node_info i2c_info;
Yann Gautierf3928f62019-02-14 11:15:03 +0100193 struct i2c_handle_s *i2c = &i2c_handle;
194 struct stm32_i2c_init_s i2c_init;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200195
Yann Gautierf3928f62019-02-14 11:15:03 +0100196 ret = dt_pmic_i2c_config(&i2c_info, &i2c_init);
197 if (ret < 0) {
198 ERROR("I2C configuration failed %d\n", ret);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200199 panic();
200 }
201
Yann Gautierf3928f62019-02-14 11:15:03 +0100202 if (ret != 0) {
203 return false;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200204 }
205
206 /* Initialize PMIC I2C */
Yann Gautierf3928f62019-02-14 11:15:03 +0100207 i2c->i2c_base_addr = i2c_info.base;
208 i2c->dt_status = i2c_info.status;
209 i2c->clock = i2c_info.clock;
Benjamin Gaignard53ee7d32020-02-24 13:57:40 +0100210 i2c->i2c_state = I2C_STATE_RESET;
Yann Gautierf3928f62019-02-14 11:15:03 +0100211 i2c_init.own_address1 = pmic_i2c_addr;
212 i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT;
213 i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE;
214 i2c_init.own_address2 = 0;
215 i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK;
216 i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE;
217 i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE;
218 i2c_init.analog_filter = 1;
219 i2c_init.digital_filter_coef = 0;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200220
Yann Gautierf3928f62019-02-14 11:15:03 +0100221 ret = stm32_i2c_init(i2c, &i2c_init);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200222 if (ret != 0) {
223 ERROR("Cannot initialize I2C %x (%d)\n",
Yann Gautierf3928f62019-02-14 11:15:03 +0100224 i2c->i2c_base_addr, ret);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200225 panic();
226 }
227
Yann Gautierf3928f62019-02-14 11:15:03 +0100228 if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, 1,
229 I2C_TIMEOUT_BUSY_MS)) {
230 ERROR("I2C device not ready\n");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200231 panic();
232 }
233
Yann Gautierf3928f62019-02-14 11:15:03 +0100234 stpmic1_bind_i2c(i2c, (uint16_t)pmic_i2c_addr);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200235
Yann Gautierf3928f62019-02-14 11:15:03 +0100236 return true;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200237}
238
Etienne Carriere67b106a2019-12-02 10:10:08 +0100239static void register_pmic_shared_peripherals(void)
240{
241 uintptr_t i2c_base = i2c_handle.i2c_base_addr;
242
243 if (dt_pmic_is_secure()) {
244 stm32mp_register_secure_periph_iomem(i2c_base);
245 } else {
246 if (i2c_base != 0U) {
247 stm32mp_register_non_secure_periph_iomem(i2c_base);
248 }
249 }
250}
251
Yann Gautierbb836ee2018-07-16 17:55:07 +0200252void initialize_pmic(void)
253{
Yann Gautierf3928f62019-02-14 11:15:03 +0100254 unsigned long pmic_version;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200255
Yann Gautierf3928f62019-02-14 11:15:03 +0100256 if (!initialize_pmic_i2c()) {
257 VERBOSE("No PMIC\n");
258 return;
259 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200260
Etienne Carriere67b106a2019-12-02 10:10:08 +0100261 register_pmic_shared_peripherals();
262
Yann Gautierf3928f62019-02-14 11:15:03 +0100263 if (stpmic1_get_version(&pmic_version) != 0) {
264 ERROR("Failed to access PMIC\n");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200265 panic();
266 }
267
Yann Gautierf3928f62019-02-14 11:15:03 +0100268 INFO("PMIC version = 0x%02lx\n", pmic_version);
269 stpmic1_dump_regulators();
Yann Gautierbb836ee2018-07-16 17:55:07 +0200270
Yann Gautierf3928f62019-02-14 11:15:03 +0100271#if defined(IMAGE_BL2)
272 if (dt_pmic_configure_boot_on_regulators() != 0) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200273 panic();
Yann Gautierf3928f62019-02-14 11:15:03 +0100274 };
275#endif
Yann Gautierbb836ee2018-07-16 17:55:07 +0200276}
277
278int pmic_ddr_power_init(enum ddr_type ddr_type)
279{
280 bool buck3_at_1v8 = false;
281 uint8_t read_val;
282 int status;
283
284 switch (ddr_type) {
285 case STM32MP_DDR3:
286 /* Set LDO3 to sync mode */
Yann Gautiera45433b2019-01-16 18:31:00 +0100287 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200288 if (status != 0) {
289 return status;
290 }
291
Yann Gautiera45433b2019-01-16 18:31:00 +0100292 read_val &= ~STPMIC1_LDO3_MODE;
293 read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
294 read_val |= STPMIC1_LDO3_DDR_SEL <<
295 STPMIC1_LDO12356_OUTPUT_SHIFT;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200296
Yann Gautiera45433b2019-01-16 18:31:00 +0100297 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200298 if (status != 0) {
299 return status;
300 }
301
Yann Gautiera45433b2019-01-16 18:31:00 +0100302 status = stpmic1_regulator_voltage_set("buck2", 1350);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200303 if (status != 0) {
304 return status;
305 }
306
Yann Gautiera45433b2019-01-16 18:31:00 +0100307 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200308 if (status != 0) {
309 return status;
310 }
311
Yann Gautiera45433b2019-01-16 18:31:00 +0100312 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200313
Yann Gautiera45433b2019-01-16 18:31:00 +0100314 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200315 if (status != 0) {
316 return status;
317 }
318
Yann Gautiera45433b2019-01-16 18:31:00 +0100319 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200320
Yann Gautiera45433b2019-01-16 18:31:00 +0100321 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200322 if (status != 0) {
323 return status;
324 }
325
Yann Gautiera45433b2019-01-16 18:31:00 +0100326 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200327 break;
328
329 case STM32MP_LPDDR2:
Yann Gautier917a00c2019-04-16 16:20:58 +0200330 case STM32MP_LPDDR3:
Yann Gautierbb836ee2018-07-16 17:55:07 +0200331 /*
332 * Set LDO3 to 1.8V
333 * Set LDO3 to bypass mode if BUCK3 = 1.8V
334 * Set LDO3 to normal mode if BUCK3 != 1.8V
335 */
Yann Gautiera45433b2019-01-16 18:31:00 +0100336 status = stpmic1_register_read(BUCK3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200337 if (status != 0) {
338 return status;
339 }
340
Yann Gautiera45433b2019-01-16 18:31:00 +0100341 if ((read_val & STPMIC1_BUCK3_1V8) == STPMIC1_BUCK3_1V8) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200342 buck3_at_1v8 = true;
343 }
344
Yann Gautiera45433b2019-01-16 18:31:00 +0100345 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200346 if (status != 0) {
347 return status;
348 }
349
Yann Gautiera45433b2019-01-16 18:31:00 +0100350 read_val &= ~STPMIC1_LDO3_MODE;
351 read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
352 read_val |= STPMIC1_LDO3_1800000;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200353 if (buck3_at_1v8) {
Yann Gautiera45433b2019-01-16 18:31:00 +0100354 read_val |= STPMIC1_LDO3_MODE;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200355 }
356
Yann Gautiera45433b2019-01-16 18:31:00 +0100357 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200358 if (status != 0) {
359 return status;
360 }
361
Yann Gautiera45433b2019-01-16 18:31:00 +0100362 status = stpmic1_regulator_voltage_set("buck2", 1200);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200363 if (status != 0) {
364 return status;
365 }
366
Yann Gautiera45433b2019-01-16 18:31:00 +0100367 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200368 if (status != 0) {
369 return status;
370 }
371
Yann Gautiera45433b2019-01-16 18:31:00 +0100372 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200373
Yann Gautiera45433b2019-01-16 18:31:00 +0100374 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200375 if (status != 0) {
376 return status;
377 }
378
Yann Gautiera45433b2019-01-16 18:31:00 +0100379 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200380
Yann Gautiera45433b2019-01-16 18:31:00 +0100381 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200382 if (status != 0) {
383 return status;
384 }
385
Yann Gautiera45433b2019-01-16 18:31:00 +0100386 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200387 break;
388
389 default:
390 break;
391 };
392
393 return 0;
394}