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