blob: 4c0ecdbb60dbd35cf49098e0cafe94609f99d81c [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 if (!initialize_pmic_i2c()) {
274 VERBOSE("No PMIC\n");
275 return;
276 }
Yann Gautierbb836ee2018-07-16 17:55:07 +0200277
Etienne Carriere67b106a2019-12-02 10:10:08 +0100278 register_pmic_shared_peripherals();
279
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100280 if (dt_pmic_configure_boot_on_regulators() < 0) {
281 panic();
282 };
283}
284
285#if DEBUG
286void print_pmic_info_and_debug(void)
287{
288 unsigned long pmic_version;
289
Yann Gautierf3928f62019-02-14 11:15:03 +0100290 if (stpmic1_get_version(&pmic_version) != 0) {
291 ERROR("Failed to access PMIC\n");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200292 panic();
293 }
294
Yann Gautierf3928f62019-02-14 11:15:03 +0100295 INFO("PMIC version = 0x%02lx\n", pmic_version);
296 stpmic1_dump_regulators();
Yann Gautierbb836ee2018-07-16 17:55:07 +0200297}
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100298#endif
Yann Gautierbb836ee2018-07-16 17:55:07 +0200299
300int pmic_ddr_power_init(enum ddr_type ddr_type)
301{
302 bool buck3_at_1v8 = false;
303 uint8_t read_val;
304 int status;
305
306 switch (ddr_type) {
307 case STM32MP_DDR3:
308 /* Set LDO3 to sync mode */
Yann Gautiera45433b2019-01-16 18:31:00 +0100309 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200310 if (status != 0) {
311 return status;
312 }
313
Yann Gautiera45433b2019-01-16 18:31:00 +0100314 read_val &= ~STPMIC1_LDO3_MODE;
315 read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
316 read_val |= STPMIC1_LDO3_DDR_SEL <<
317 STPMIC1_LDO12356_OUTPUT_SHIFT;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200318
Yann Gautiera45433b2019-01-16 18:31:00 +0100319 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200320 if (status != 0) {
321 return status;
322 }
323
Yann Gautiera45433b2019-01-16 18:31:00 +0100324 status = stpmic1_regulator_voltage_set("buck2", 1350);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200325 if (status != 0) {
326 return status;
327 }
328
Yann Gautiera45433b2019-01-16 18:31:00 +0100329 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200330 if (status != 0) {
331 return status;
332 }
333
Yann Gautiera45433b2019-01-16 18:31:00 +0100334 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200335
Yann Gautiera45433b2019-01-16 18:31:00 +0100336 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200337 if (status != 0) {
338 return status;
339 }
340
Yann Gautiera45433b2019-01-16 18:31:00 +0100341 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200342
Yann Gautiera45433b2019-01-16 18:31:00 +0100343 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200344 if (status != 0) {
345 return status;
346 }
347
Yann Gautiera45433b2019-01-16 18:31:00 +0100348 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200349 break;
350
351 case STM32MP_LPDDR2:
Yann Gautier917a00c2019-04-16 16:20:58 +0200352 case STM32MP_LPDDR3:
Yann Gautierbb836ee2018-07-16 17:55:07 +0200353 /*
354 * Set LDO3 to 1.8V
355 * Set LDO3 to bypass mode if BUCK3 = 1.8V
356 * Set LDO3 to normal mode if BUCK3 != 1.8V
357 */
Yann Gautiera45433b2019-01-16 18:31:00 +0100358 status = stpmic1_register_read(BUCK3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200359 if (status != 0) {
360 return status;
361 }
362
Yann Gautiera45433b2019-01-16 18:31:00 +0100363 if ((read_val & STPMIC1_BUCK3_1V8) == STPMIC1_BUCK3_1V8) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200364 buck3_at_1v8 = true;
365 }
366
Yann Gautiera45433b2019-01-16 18:31:00 +0100367 status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200368 if (status != 0) {
369 return status;
370 }
371
Yann Gautiera45433b2019-01-16 18:31:00 +0100372 read_val &= ~STPMIC1_LDO3_MODE;
373 read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
374 read_val |= STPMIC1_LDO3_1800000;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200375 if (buck3_at_1v8) {
Yann Gautiera45433b2019-01-16 18:31:00 +0100376 read_val |= STPMIC1_LDO3_MODE;
Yann Gautierbb836ee2018-07-16 17:55:07 +0200377 }
378
Yann Gautiera45433b2019-01-16 18:31:00 +0100379 status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200380 if (status != 0) {
381 return status;
382 }
383
Yann Gautiera45433b2019-01-16 18:31:00 +0100384 status = stpmic1_regulator_voltage_set("buck2", 1200);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200385 if (status != 0) {
386 return status;
387 }
388
Yann Gautiera45433b2019-01-16 18:31:00 +0100389 status = stpmic1_regulator_enable("ldo3");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200390 if (status != 0) {
391 return status;
392 }
393
Yann Gautiera45433b2019-01-16 18:31:00 +0100394 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200395
Yann Gautiera45433b2019-01-16 18:31:00 +0100396 status = stpmic1_regulator_enable("buck2");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200397 if (status != 0) {
398 return status;
399 }
400
Yann Gautiera45433b2019-01-16 18:31:00 +0100401 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200402
Yann Gautiera45433b2019-01-16 18:31:00 +0100403 status = stpmic1_regulator_enable("vref_ddr");
Yann Gautierbb836ee2018-07-16 17:55:07 +0200404 if (status != 0) {
405 return status;
406 }
407
Yann Gautiera45433b2019-01-16 18:31:00 +0100408 mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautierbb836ee2018-07-16 17:55:07 +0200409 break;
410
411 default:
412 break;
413 };
414
415 return 0;
416}