blob: c5bdfc07d3a1457dc570c3948e451ad5410ae357 [file] [log] [blame]
Yann Gautierbb836ee2018-07-16 17:55:07 +02001/*
2 * Copyright (c) 2017-2018, STMicroelectronics - All Rights Reserved
3 *
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>
16#include <drivers/st/stm32_gpio.h>
17#include <drivers/st/stm32mp1_clk.h>
18#include <drivers/st/stm32mp1_pmic.h>
19#include <drivers/st/stpmu1.h>
20#include <lib/mmio.h>
21#include <lib/utils_def.h>
22
Yann Gautierbb836ee2018-07-16 17:55:07 +020023/* I2C Timing hard-coded value, for I2C clock source is HSI at 64MHz */
24#define I2C_TIMING 0x10D07DB5
25
26#define I2C_TIMEOUT 0xFFFFF
27
28#define MASK_RESET_BUCK3 BIT(2)
29
30#define STPMU1_LDO12356_OUTPUT_MASK (uint8_t)(GENMASK(6, 2))
31#define STPMU1_LDO12356_OUTPUT_SHIFT 2
32#define STPMU1_LDO3_MODE (uint8_t)(BIT(7))
33#define STPMU1_LDO3_DDR_SEL 31U
34#define STPMU1_LDO3_1800000 (9U << STPMU1_LDO12356_OUTPUT_SHIFT)
35
36#define STPMU1_BUCK_OUTPUT_SHIFT 2
37#define STPMU1_BUCK3_1V8 (39U << STPMU1_BUCK_OUTPUT_SHIFT)
38
39#define STPMU1_DEFAULT_START_UP_DELAY_MS 1
40
41static struct i2c_handle_s i2c_handle;
42static uint32_t pmic_i2c_addr;
43
44static int dt_get_pmic_node(void *fdt)
45{
46 return fdt_node_offset_by_compatible(fdt, -1, "st,stpmu1");
47}
48
49bool dt_check_pmic(void)
50{
51 int node;
52 void *fdt;
53
54 if (fdt_get_address(&fdt) == 0) {
55 return false;
56 }
57
58 node = dt_get_pmic_node(fdt);
59 if (node < 0) {
60 VERBOSE("%s: No PMIC node found in DT\n", __func__);
61 return false;
62 }
63
64 return fdt_check_status(node);
65}
66
67static int dt_pmic_i2c_config(struct dt_node_info *i2c_info)
68{
69 int pmic_node, i2c_node;
70 void *fdt;
71 const fdt32_t *cuint;
72
73 if (fdt_get_address(&fdt) == 0) {
74 return -ENOENT;
75 }
76
77 pmic_node = dt_get_pmic_node(fdt);
78 if (pmic_node < 0) {
79 return -FDT_ERR_NOTFOUND;
80 }
81
82 cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
83 if (cuint == NULL) {
84 return -FDT_ERR_NOTFOUND;
85 }
86
87 pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1;
88 if (pmic_i2c_addr > UINT16_MAX) {
89 return -EINVAL;
90 }
91
92 i2c_node = fdt_parent_offset(fdt, pmic_node);
93 if (i2c_node < 0) {
94 return -FDT_ERR_NOTFOUND;
95 }
96
97 dt_fill_device_info(i2c_info, i2c_node);
98 if (i2c_info->base == 0U) {
99 return -FDT_ERR_NOTFOUND;
100 }
101
102 return dt_set_pinctrl_config(i2c_node);
103}
104
105int dt_pmic_enable_boot_on_regulators(void)
106{
107 int pmic_node, regulators_node, regulator_node;
108 void *fdt;
109
110 if (fdt_get_address(&fdt) == 0) {
111 return -ENOENT;
112 }
113
114 pmic_node = dt_get_pmic_node(fdt);
115 if (pmic_node < 0) {
116 return -FDT_ERR_NOTFOUND;
117 }
118
119 regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");
120
121 fdt_for_each_subnode(regulator_node, fdt, regulators_node) {
122 const fdt32_t *cuint;
123 const char *node_name;
124 uint16_t voltage;
125
126 if (fdt_getprop(fdt, regulator_node, "regulator-boot-on",
127 NULL) == NULL) {
128 continue;
129 }
130
131 cuint = fdt_getprop(fdt, regulator_node,
132 "regulator-min-microvolt", NULL);
133 if (cuint == NULL) {
134 continue;
135 }
136
137 /* DT uses microvolts, whereas driver awaits millivolts */
138 voltage = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U);
139 node_name = fdt_get_name(fdt, regulator_node, NULL);
140
141 if (stpmu1_is_regulator_enabled(node_name) == 0U) {
142 int status;
143
144 status = stpmu1_regulator_voltage_set(node_name,
145 voltage);
146 if (status != 0) {
147 return status;
148 }
149
150 status = stpmu1_regulator_enable(node_name);
151 if (status != 0) {
152 return status;
153 }
154 }
155 }
156
157 return 0;
158}
159
160void initialize_pmic_i2c(void)
161{
162 int ret;
163 struct dt_node_info i2c_info;
164
165 if (dt_pmic_i2c_config(&i2c_info) != 0) {
166 ERROR("I2C configuration failed\n");
167 panic();
168 }
169
170 if (stm32mp1_clk_enable((uint32_t)i2c_info.clock) < 0) {
171 ERROR("I2C clock enable failed\n");
172 panic();
173 }
174
175 /* Initialize PMIC I2C */
176 i2c_handle.i2c_base_addr = i2c_info.base;
177 i2c_handle.i2c_init.timing = I2C_TIMING;
178 i2c_handle.i2c_init.own_address1 = pmic_i2c_addr;
179 i2c_handle.i2c_init.addressing_mode = I2C_ADDRESSINGMODE_7BIT;
180 i2c_handle.i2c_init.dual_address_mode = I2C_DUALADDRESS_DISABLE;
181 i2c_handle.i2c_init.own_address2 = 0;
182 i2c_handle.i2c_init.own_address2_masks = I2C_OAR2_OA2NOMASK;
183 i2c_handle.i2c_init.general_call_mode = I2C_GENERALCALL_DISABLE;
184 i2c_handle.i2c_init.no_stretch_mode = I2C_NOSTRETCH_DISABLE;
185
186 ret = stm32_i2c_init(&i2c_handle);
187 if (ret != 0) {
188 ERROR("Cannot initialize I2C %x (%d)\n",
189 i2c_handle.i2c_base_addr, ret);
190 panic();
191 }
192
193 ret = stm32_i2c_config_analog_filter(&i2c_handle,
194 I2C_ANALOGFILTER_ENABLE);
195 if (ret != 0) {
196 ERROR("Cannot initialize I2C analog filter (%d)\n", ret);
197 panic();
198 }
199
200 ret = stm32_i2c_is_device_ready(&i2c_handle, (uint16_t)pmic_i2c_addr, 1,
201 I2C_TIMEOUT);
202 if (ret != 0) {
203 ERROR("I2C device not ready (%d)\n", ret);
204 panic();
205 }
206
207 stpmu1_bind_i2c(&i2c_handle, (uint16_t)pmic_i2c_addr);
208}
209
210void initialize_pmic(void)
211{
212 int status;
213 uint8_t read_val;
214
215 initialize_pmic_i2c();
216
217 status = stpmu1_register_read(VERSION_STATUS_REG, &read_val);
218 if (status != 0) {
219 panic();
220 }
221
222 INFO("PMIC version = 0x%x\n", read_val);
223
224 /* Keep VDD on during the reset cycle */
225 status = stpmu1_register_update(MASK_RESET_BUCK_REG,
226 MASK_RESET_BUCK3,
227 MASK_RESET_BUCK3);
228 if (status != 0) {
229 panic();
230 }
231}
232
233int pmic_ddr_power_init(enum ddr_type ddr_type)
234{
235 bool buck3_at_1v8 = false;
236 uint8_t read_val;
237 int status;
238
239 switch (ddr_type) {
240 case STM32MP_DDR3:
241 /* Set LDO3 to sync mode */
242 status = stpmu1_register_read(LDO3_CONTROL_REG, &read_val);
243 if (status != 0) {
244 return status;
245 }
246
247 read_val &= ~STPMU1_LDO3_MODE;
248 read_val &= ~STPMU1_LDO12356_OUTPUT_MASK;
249 read_val |= STPMU1_LDO3_DDR_SEL << STPMU1_LDO12356_OUTPUT_SHIFT;
250
251 status = stpmu1_register_write(LDO3_CONTROL_REG, read_val);
252 if (status != 0) {
253 return status;
254 }
255
256 status = stpmu1_regulator_voltage_set("buck2", 1350);
257 if (status != 0) {
258 return status;
259 }
260
261 status = stpmu1_regulator_enable("buck2");
262 if (status != 0) {
263 return status;
264 }
265
266 mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS);
267
268 status = stpmu1_regulator_enable("vref_ddr");
269 if (status != 0) {
270 return status;
271 }
272
273 mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS);
274
275 status = stpmu1_regulator_enable("ldo3");
276 if (status != 0) {
277 return status;
278 }
279
280 mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS);
281 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 */
289 status = stpmu1_register_read(BUCK3_CONTROL_REG, &read_val);
290 if (status != 0) {
291 return status;
292 }
293
294 if ((read_val & STPMU1_BUCK3_1V8) == STPMU1_BUCK3_1V8) {
295 buck3_at_1v8 = true;
296 }
297
298 status = stpmu1_register_read(LDO3_CONTROL_REG, &read_val);
299 if (status != 0) {
300 return status;
301 }
302
303 read_val &= ~STPMU1_LDO3_MODE;
304 read_val &= ~STPMU1_LDO12356_OUTPUT_MASK;
305 read_val |= STPMU1_LDO3_1800000;
306 if (buck3_at_1v8) {
307 read_val |= STPMU1_LDO3_MODE;
308 }
309
310 status = stpmu1_register_write(LDO3_CONTROL_REG, read_val);
311 if (status != 0) {
312 return status;
313 }
314
315 status = stpmu1_regulator_voltage_set("buck2", 1200);
316 if (status != 0) {
317 return status;
318 }
319
320 status = stpmu1_regulator_enable("ldo3");
321 if (status != 0) {
322 return status;
323 }
324
325 mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS);
326
327 status = stpmu1_regulator_enable("buck2");
328 if (status != 0) {
329 return status;
330 }
331
332 mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS);
333
334 status = stpmu1_regulator_enable("vref_ddr");
335 if (status != 0) {
336 return status;
337 }
338
339 mdelay(STPMU1_DEFAULT_START_UP_DELAY_MS);
340 break;
341
342 default:
343 break;
344 };
345
346 return 0;
347}