blob: 03d719009acffa2d7c83fdcef16228a48ec2bcd3 [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
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <common/debug.h>
10#include <drivers/delay_timer.h>
Yann Gautierf3928f62019-02-14 11:15:03 +010011#include <drivers/st/stm32_i2c.h>
Yann Gautiera45433b2019-01-16 18:31:00 +010012#include <drivers/st/stm32mp_pmic.h>
Yann Gautiera45433b2019-01-16 18:31:00 +010013#include <drivers/st/stpmic1.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <lib/mmio.h>
15#include <lib/utils_def.h>
Nicolas Le Bayonf5188ee2019-09-19 11:24:50 +020016#include <libfdt.h>
17
18#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000019
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010020#define PMIC_NODE_NOT_FOUND 1
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{
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010037 static int node = -FDT_ERR_BADOFFSET;
38
39 if (node == -FDT_ERR_BADOFFSET) {
40 node = fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
41 }
42
43 return node;
Yann Gautierbb836ee2018-07-16 17:55:07 +020044}
45
Yann Gautierf3928f62019-02-14 11:15:03 +010046int dt_pmic_status(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +020047{
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010048 static int status = -FDT_ERR_BADVALUE;
Yann Gautierbb836ee2018-07-16 17:55:07 +020049 int node;
50 void *fdt;
51
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010052 if (status != -FDT_ERR_BADVALUE) {
53 return status;
54 }
55
Yann Gautierbb836ee2018-07-16 17:55:07 +020056 if (fdt_get_address(&fdt) == 0) {
Yann Gautierf3928f62019-02-14 11:15:03 +010057 return -ENOENT;
Yann Gautierbb836ee2018-07-16 17:55:07 +020058 }
59
60 node = dt_get_pmic_node(fdt);
Yann Gautierf3928f62019-02-14 11:15:03 +010061 if (node <= 0) {
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010062 status = -FDT_ERR_NOTFOUND;
63
64 return status;
Yann Gautierbb836ee2018-07-16 17:55:07 +020065 }
66
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010067 status = (int)fdt_get_status(node);
68
69 return status;
Yann Gautierbb836ee2018-07-16 17:55:07 +020070}
71
Etienne Carriere67b106a2019-12-02 10:10:08 +010072static bool dt_pmic_is_secure(void)
73{
74 int status = dt_pmic_status();
75
76 return (status >= 0) &&
77 (status == DT_SECURE) &&
78 (i2c_handle.dt_status == DT_SECURE);
79}
80
Yann Gautierf3928f62019-02-14 11:15:03 +010081/*
82 * Get PMIC and its I2C bus configuration from the device tree.
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010083 * Return 0 on success, negative on error, 1 if no PMIC node is defined.
Yann Gautierf3928f62019-02-14 11:15:03 +010084 */
85static int dt_pmic_i2c_config(struct dt_node_info *i2c_info,
86 struct stm32_i2c_init_s *init)
Yann Gautierbb836ee2018-07-16 17:55:07 +020087{
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010088 static int i2c_node = -FDT_ERR_NOTFOUND;
Yann Gautierbb836ee2018-07-16 17:55:07 +020089 void *fdt;
Yann Gautierbb836ee2018-07-16 17:55:07 +020090
91 if (fdt_get_address(&fdt) == 0) {
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010092 return -FDT_ERR_NOTFOUND;
Yann Gautierbb836ee2018-07-16 17:55:07 +020093 }
94
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010095 if (i2c_node == -FDT_ERR_NOTFOUND) {
96 int pmic_node;
97 const fdt32_t *cuint;
Yann Gautierbb836ee2018-07-16 17:55:07 +020098
Nicolas Le Bayon342865a2019-11-15 15:56:06 +010099 pmic_node = dt_get_pmic_node(fdt);
100 if (pmic_node < 0) {
101 return PMIC_NODE_NOT_FOUND;
102 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200103
Nicolas Le Bayon342865a2019-11-15 15:56:06 +0100104 cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
105 if (cuint == NULL) {
106 return -FDT_ERR_NOTFOUND;
107 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200108
Nicolas Le Bayon342865a2019-11-15 15:56:06 +0100109 pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1;
110 if (pmic_i2c_addr > UINT16_MAX) {
111 return -FDT_ERR_BADVALUE;
112 }
113
114 i2c_node = fdt_parent_offset(fdt, pmic_node);
115 if (i2c_node < 0) {
116 return -FDT_ERR_NOTFOUND;
117 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200118 }
119
120 dt_fill_device_info(i2c_info, i2c_node);
121 if (i2c_info->base == 0U) {
122 return -FDT_ERR_NOTFOUND;
123 }
124
Yann Gautierf3928f62019-02-14 11:15:03 +0100125 return stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200126}
127
Yann Gautierf3928f62019-02-14 11:15:03 +0100128int dt_pmic_configure_boot_on_regulators(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +0200129{
130 int pmic_node, regulators_node, regulator_node;
131 void *fdt;
132
133 if (fdt_get_address(&fdt) == 0) {
134 return -ENOENT;
135 }
136
137 pmic_node = dt_get_pmic_node(fdt);
138 if (pmic_node < 0) {
139 return -FDT_ERR_NOTFOUND;
140 }
141
142 regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");
Nicolas Le Bayon72ceb352020-03-10 18:18:45 +0100143 if (regulators_node < 0) {
144 return -ENOENT;
145 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200146
147 fdt_for_each_subnode(regulator_node, fdt, regulators_node) {
148 const fdt32_t *cuint;
Yann Gautierf3928f62019-02-14 11:15:03 +0100149 const char *node_name = fdt_get_name(fdt, regulator_node, NULL);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200150 uint16_t voltage;
Yann Gautierf3928f62019-02-14 11:15:03 +0100151 int status;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200152
Yann Gautierf3928f62019-02-14 11:15:03 +0100153#if defined(IMAGE_BL2)
154 if ((fdt_getprop(fdt, regulator_node, "regulator-boot-on",
155 NULL) == NULL) &&
156 (fdt_getprop(fdt, regulator_node, "regulator-always-on",
157 NULL) == NULL)) {
158#else
Yann Gautierbb836ee2018-07-16 17:55:07 +0200159 if (fdt_getprop(fdt, regulator_node, "regulator-boot-on",
160 NULL) == NULL) {
Yann Gautierf3928f62019-02-14 11:15:03 +0100161#endif
Yann Gautierbb836ee2018-07-16 17:55:07 +0200162 continue;
163 }
164
Yann Gautierf3928f62019-02-14 11:15:03 +0100165 if (fdt_getprop(fdt, regulator_node, "regulator-pull-down",
166 NULL) != NULL) {
167
168 status = stpmic1_regulator_pull_down_set(node_name);
169 if (status != 0) {
170 return status;
171 }
172 }
173
174 if (fdt_getprop(fdt, regulator_node, "st,mask-reset",
175 NULL) != NULL) {
176
177 status = stpmic1_regulator_mask_reset_set(node_name);
178 if (status != 0) {
179 return status;
180 }
181 }
182
Yann Gautierbb836ee2018-07-16 17:55:07 +0200183 cuint = fdt_getprop(fdt, regulator_node,
184 "regulator-min-microvolt", NULL);
185 if (cuint == NULL) {
186 continue;
187 }
188
189 /* DT uses microvolts, whereas driver awaits millivolts */
190 voltage = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200191
Yann Gautierf3928f62019-02-14 11:15:03 +0100192 status = stpmic1_regulator_voltage_set(node_name, voltage);
193 if (status != 0) {
194 return status;
195 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200196
Nicolas Le Bayonf5188ee2019-09-19 11:24:50 +0200197 if (!stpmic1_is_regulator_enabled(node_name)) {
Yann Gautiera45433b2019-01-16 18:31:00 +0100198 status = stpmic1_regulator_enable(node_name);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200199 if (status != 0) {
200 return status;
201 }
202 }
203 }
204
205 return 0;
206}
207
Yann Gautierf3928f62019-02-14 11:15:03 +0100208bool initialize_pmic_i2c(void)
Yann Gautierbb836ee2018-07-16 17:55:07 +0200209{
210 int ret;
211 struct dt_node_info i2c_info;
Yann Gautierf3928f62019-02-14 11:15:03 +0100212 struct i2c_handle_s *i2c = &i2c_handle;
213 struct stm32_i2c_init_s i2c_init;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200214
Yann Gautierf3928f62019-02-14 11:15:03 +0100215 ret = dt_pmic_i2c_config(&i2c_info, &i2c_init);
216 if (ret < 0) {
217 ERROR("I2C configuration failed %d\n", ret);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200218 panic();
219 }
220
Yann Gautierf3928f62019-02-14 11:15:03 +0100221 if (ret != 0) {
222 return false;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200223 }
224
225 /* Initialize PMIC I2C */
Yann Gautierf3928f62019-02-14 11:15:03 +0100226 i2c->i2c_base_addr = i2c_info.base;
227 i2c->dt_status = i2c_info.status;
228 i2c->clock = i2c_info.clock;
Benjamin Gaignard53ee7d32020-02-24 13:57:40 +0100229 i2c->i2c_state = I2C_STATE_RESET;
Yann Gautierf3928f62019-02-14 11:15:03 +0100230 i2c_init.own_address1 = pmic_i2c_addr;
231 i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT;
232 i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE;
233 i2c_init.own_address2 = 0;
234 i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK;
235 i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE;
236 i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE;
237 i2c_init.analog_filter = 1;
238 i2c_init.digital_filter_coef = 0;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200239
Yann Gautierf3928f62019-02-14 11:15:03 +0100240 ret = stm32_i2c_init(i2c, &i2c_init);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200241 if (ret != 0) {
242 ERROR("Cannot initialize I2C %x (%d)\n",
Yann Gautierf3928f62019-02-14 11:15:03 +0100243 i2c->i2c_base_addr, ret);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200244 panic();
245 }
246
Yann Gautierf3928f62019-02-14 11:15:03 +0100247 if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, 1,
248 I2C_TIMEOUT_BUSY_MS)) {
249 ERROR("I2C device not ready\n");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200250 panic();
251 }
252
Yann Gautierf3928f62019-02-14 11:15:03 +0100253 stpmic1_bind_i2c(i2c, (uint16_t)pmic_i2c_addr);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200254
Yann Gautierf3928f62019-02-14 11:15:03 +0100255 return true;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200256}
257
Etienne Carriere67b106a2019-12-02 10:10:08 +0100258static void register_pmic_shared_peripherals(void)
259{
260 uintptr_t i2c_base = i2c_handle.i2c_base_addr;
261
262 if (dt_pmic_is_secure()) {
263 stm32mp_register_secure_periph_iomem(i2c_base);
264 } else {
265 if (i2c_base != 0U) {
266 stm32mp_register_non_secure_periph_iomem(i2c_base);
267 }
268 }
269}
270
Yann Gautierbb836ee2018-07-16 17:55:07 +0200271void initialize_pmic(void)
272{
Yann Gautierf3928f62019-02-14 11:15:03 +0100273 unsigned long pmic_version;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200274
Yann Gautierf3928f62019-02-14 11:15:03 +0100275 if (!initialize_pmic_i2c()) {
276 VERBOSE("No PMIC\n");
277 return;
278 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200279
Etienne Carriere67b106a2019-12-02 10:10:08 +0100280 register_pmic_shared_peripherals();
281
Yann Gautierf3928f62019-02-14 11:15:03 +0100282 if (stpmic1_get_version(&pmic_version) != 0) {
283 ERROR("Failed to access PMIC\n");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200284 panic();
285 }
286
Yann Gautierf3928f62019-02-14 11:15:03 +0100287 INFO("PMIC version = 0x%02lx\n", pmic_version);
288 stpmic1_dump_regulators();
Yann Gautierbb836ee2018-07-16 17:55:07 +0200289
Yann Gautierf3928f62019-02-14 11:15:03 +0100290#if defined(IMAGE_BL2)
291 if (dt_pmic_configure_boot_on_regulators() != 0) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200292 panic();
Yann Gautierf3928f62019-02-14 11:15:03 +0100293 };
294#endif
Yann Gautierbb836ee2018-07-16 17:55:07 +0200295}
296
297int pmic_ddr_power_init(enum ddr_type ddr_type)
298{
299 bool buck3_at_1v8 = false;
300 uint8_t read_val;
301 int status;
302
303 switch (ddr_type) {
304 case STM32MP_DDR3:
305 /* Set LDO3 to sync mode */
Yann Gautiera45433b2019-01-16 18:31:00 +0100306 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200307 if (status != 0) {
308 return status;
309 }
310
Yann Gautiera45433b2019-01-16 18:31:00 +0100311 read_val &= ~STPMIC1_LDO3_MODE;
312 read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
313 read_val |= STPMIC1_LDO3_DDR_SEL <<
314 STPMIC1_LDO12356_OUTPUT_SHIFT;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200315
Yann Gautiera45433b2019-01-16 18:31:00 +0100316 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200317 if (status != 0) {
318 return status;
319 }
320
Yann Gautiera45433b2019-01-16 18:31:00 +0100321 status = stpmic1_regulator_voltage_set("buck2", 1350);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200322 if (status != 0) {
323 return status;
324 }
325
Yann Gautiera45433b2019-01-16 18:31:00 +0100326 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200327 if (status != 0) {
328 return status;
329 }
330
Yann Gautiera45433b2019-01-16 18:31:00 +0100331 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200332
Yann Gautiera45433b2019-01-16 18:31:00 +0100333 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200334 if (status != 0) {
335 return status;
336 }
337
Yann Gautiera45433b2019-01-16 18:31:00 +0100338 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200339
Yann Gautiera45433b2019-01-16 18:31:00 +0100340 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200341 if (status != 0) {
342 return status;
343 }
344
Yann Gautiera45433b2019-01-16 18:31:00 +0100345 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200346 break;
347
348 case STM32MP_LPDDR2:
Yann Gautier917a00c2019-04-16 16:20:58 +0200349 case STM32MP_LPDDR3:
Yann Gautierbb836ee2018-07-16 17:55:07 +0200350 /*
351 * Set LDO3 to 1.8V
352 * Set LDO3 to bypass mode if BUCK3 = 1.8V
353 * Set LDO3 to normal mode if BUCK3 != 1.8V
354 */
Yann Gautiera45433b2019-01-16 18:31:00 +0100355 status = stpmic1_register_read(BUCK3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200356 if (status != 0) {
357 return status;
358 }
359
Yann Gautiera45433b2019-01-16 18:31:00 +0100360 if ((read_val & STPMIC1_BUCK3_1V8) == STPMIC1_BUCK3_1V8) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200361 buck3_at_1v8 = true;
362 }
363
Yann Gautiera45433b2019-01-16 18:31:00 +0100364 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200365 if (status != 0) {
366 return status;
367 }
368
Yann Gautiera45433b2019-01-16 18:31:00 +0100369 read_val &= ~STPMIC1_LDO3_MODE;
370 read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
371 read_val |= STPMIC1_LDO3_1800000;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200372 if (buck3_at_1v8) {
Yann Gautiera45433b2019-01-16 18:31:00 +0100373 read_val |= STPMIC1_LDO3_MODE;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200374 }
375
Yann Gautiera45433b2019-01-16 18:31:00 +0100376 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200377 if (status != 0) {
378 return status;
379 }
380
Yann Gautiera45433b2019-01-16 18:31:00 +0100381 status = stpmic1_regulator_voltage_set("buck2", 1200);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200382 if (status != 0) {
383 return status;
384 }
385
Yann Gautiera45433b2019-01-16 18:31:00 +0100386 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200387 if (status != 0) {
388 return status;
389 }
390
Yann Gautiera45433b2019-01-16 18:31:00 +0100391 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200392
Yann Gautiera45433b2019-01-16 18:31:00 +0100393 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200394 if (status != 0) {
395 return status;
396 }
397
Yann Gautiera45433b2019-01-16 18:31:00 +0100398 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200399
Yann Gautiera45433b2019-01-16 18:31:00 +0100400 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200401 if (status != 0) {
402 return status;
403 }
404
Yann Gautiera45433b2019-01-16 18:31:00 +0100405 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200406 break;
407
408 default:
409 break;
410 };
411
412 return 0;
413}