1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3 * Copyright (c) 2017-2021, STMicroelectronics
4 */
5
6 #include <drivers/stm32_i2c.h>
7 #include <drivers/stm32mp1_pmic.h>
8 #include <drivers/stpmic1.h>
9 #include <drivers/stpmic1_regulator.h>
10 #include <io.h>
11 #include <keep.h>
12 #include <kernel/delay.h>
13 #include <kernel/dt.h>
14 #include <kernel/boot.h>
15 #include <kernel/panic.h>
16 #include <kernel/pm.h>
17 #include <libfdt.h>
18 #include <mm/core_memprot.h>
19 #include <platform_config.h>
20 #include <stdbool.h>
21 #include <stm32_util.h>
22 #include <trace.h>
23 #include <util.h>
24
25 #define MODE_STANDBY 8
26
27 #define PMIC_I2C_TRIALS 1
28 #define PMIC_I2C_TIMEOUT_BUSY_MS 5
29
30 #define PMIC_REGU_SUPPLY_NAME_LEN 12
31
32 #define PMIC_REGU_COUNT 14
33
34 /* Expect a single PMIC instance */
35 static struct i2c_handle_s i2c_handle;
36 static uint32_t pmic_i2c_addr;
37
38 /* CPU voltage supplier if found */
39 static char cpu_supply_name[PMIC_REGU_SUPPLY_NAME_LEN];
40
stm32mp_with_pmic(void)41 bool stm32mp_with_pmic(void)
42 {
43 return i2c_handle.dt_status & DT_STATUS_OK_SEC;
44 }
45
dt_get_pmic_node(void * fdt)46 static int dt_get_pmic_node(void *fdt)
47 {
48 static int node = -FDT_ERR_BADOFFSET;
49
50 if (node == -FDT_ERR_BADOFFSET)
51 node = fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
52
53 return node;
54 }
55
dt_pmic_status(void)56 static int dt_pmic_status(void)
57 {
58 void *fdt = get_embedded_dt();
59
60 if (fdt) {
61 int node = dt_get_pmic_node(fdt);
62
63 if (node > 0)
64 return _fdt_get_status(fdt, node);
65 }
66
67 return -1;
68 }
69
stm32mp_dt_pmic_status(void)70 int stm32mp_dt_pmic_status(void)
71 {
72 return dt_pmic_status();
73 }
74
dt_pmic_is_secure(void)75 static bool dt_pmic_is_secure(void)
76 {
77 int status = dt_pmic_status();
78
79 return status == DT_STATUS_OK_SEC &&
80 i2c_handle.dt_status == DT_STATUS_OK_SEC;
81 }
82
83 /*
84 * struct regu_bo_config - Boot on configuration for a regulator
85 * @flags: Operations expected when entering a low power sequence
86 * @cfg: Boot-on configuration to apply during low power sequences
87 */
88 struct regu_bo_config {
89 uint8_t flags;
90 struct stpmic1_bo_cfg cfg;
91 };
92
93 #define REGU_BO_FLAG_ENABLE_REGU BIT(0)
94 #define REGU_BO_FLAG_SET_VOLTAGE BIT(1)
95 #define REGU_BO_FLAG_PULL_DOWN BIT(2)
96 #define REGU_BO_FLAG_MASK_RESET BIT(3)
97
98 static struct regu_bo_config *regu_bo_config;
99 static size_t regu_bo_count;
100
101 /* boot-on mandatory? if so: caller panic() on error status */
dt_get_regu_boot_on_config(void * fdt,const char * regu_name,int regu_node)102 static void dt_get_regu_boot_on_config(void *fdt, const char *regu_name,
103 int regu_node)
104 {
105 const fdt32_t *cuint = NULL;
106 struct regu_bo_config regu_cfg = { };
107 uint16_t mv = 0;
108
109 if ((!fdt_getprop(fdt, regu_node, "regulator-boot-on", NULL)) &&
110 (!fdt_getprop(fdt, regu_node, "regulator-always-on", NULL)))
111 return;
112
113 regu_cfg.flags |= REGU_BO_FLAG_ENABLE_REGU;
114 if (stpmic1_bo_enable_cfg(regu_name, ®u_cfg.cfg)) {
115 EMSG("PMIC regulator %s not supported", regu_name);
116 panic();
117 }
118
119 if (fdt_getprop(fdt, regu_node, "regulator-pull-down", NULL)) {
120 if (stpmic1_bo_pull_down_cfg(regu_name, ®u_cfg.cfg)) {
121 DMSG("No pull down mode for regu %s", regu_name);
122 panic();
123 }
124 regu_cfg.flags |= REGU_BO_FLAG_PULL_DOWN;
125 }
126
127 if (fdt_getprop(fdt, regu_node, "st,mask-reset", NULL)) {
128 if (stpmic1_bo_mask_reset_cfg(regu_name, ®u_cfg.cfg)) {
129 DMSG("No reset mode for regu %s", regu_name);
130 panic();
131 }
132 regu_cfg.flags |= REGU_BO_FLAG_MASK_RESET;
133 }
134
135 cuint = fdt_getprop(fdt, regu_node,
136 "regulator-min-microvolt", NULL);
137 if (cuint) {
138 /* DT uses microvolts and driver awaits millivolts */
139 mv = fdt32_to_cpu(*cuint) / 1000;
140
141 if (stpmic1_bo_voltage_cfg(regu_name, mv, ®u_cfg.cfg))
142 DMSG("Ignore regulator-min-microvolt for %s",
143 regu_name);
144 else
145 regu_cfg.flags |= REGU_BO_FLAG_SET_VOLTAGE;
146 }
147
148 /* Save config in the Boot On configuration list */
149 regu_bo_count++;
150 regu_bo_config = realloc(regu_bo_config,
151 regu_bo_count * sizeof(regu_cfg));
152 if (!regu_bo_config)
153 panic();
154
155 regu_bo_config[regu_bo_count - 1] = regu_cfg;
156 }
157
stm32mp_pmic_apply_boot_on_config(void)158 void stm32mp_pmic_apply_boot_on_config(void)
159 {
160 size_t i = 0;
161
162 for (i = 0; i < regu_bo_count; i++) {
163 struct regu_bo_config *regu_cfg = ®u_bo_config[i];
164
165 if (regu_cfg->flags & REGU_BO_FLAG_SET_VOLTAGE)
166 if (stpmic1_bo_voltage_unpg(®u_cfg->cfg))
167 panic();
168
169 if (regu_cfg->flags & REGU_BO_FLAG_ENABLE_REGU)
170 if (stpmic1_bo_enable_unpg(®u_cfg->cfg))
171 panic();
172
173 if (regu_cfg->flags & REGU_BO_FLAG_PULL_DOWN)
174 if (stpmic1_bo_pull_down_unpg(®u_cfg->cfg))
175 panic();
176
177 if (regu_cfg->flags & REGU_BO_FLAG_MASK_RESET)
178 if (stpmic1_bo_mask_reset_unpg(®u_cfg->cfg))
179 panic();
180 }
181 }
182
183 /*
184 * @flags: Operations expected when entering a low power sequence
185 * @voltage: Target voltage to apply during low power sequences
186 */
187 struct regu_lp_config {
188 uint8_t flags;
189 struct stpmic1_lp_cfg cfg;
190 };
191
192 #define REGU_LP_FLAG_LOAD_PWRCTRL BIT(0)
193 #define REGU_LP_FLAG_ON_IN_SUSPEND BIT(1)
194 #define REGU_LP_FLAG_OFF_IN_SUSPEND BIT(2)
195 #define REGU_LP_FLAG_SET_VOLTAGE BIT(3)
196 #define REGU_LP_FLAG_MODE_STANDBY BIT(4)
197
198 /*
199 * struct regu_lp_state - Low power configuration for regulators
200 * @name: low power state identifier string name
201 * @cfg_count: number of regulator configuration instance in @cfg
202 * @cfg: regulator configurations for low power state @name
203 */
204 struct regu_lp_state {
205 const char *name;
206 size_t cfg_count;
207 struct regu_lp_config *cfg;
208 };
209
210 enum regu_lp_state_id {
211 REGU_LP_STATE_DISK = 0,
212 REGU_LP_STATE_STANDBY,
213 REGU_LP_STATE_MEM,
214 REGU_LP_STATE_MEM_LOWVOLTAGE,
215 REGU_LP_STATE_COUNT
216 };
217
218 static struct regu_lp_state regu_lp_state[REGU_LP_STATE_COUNT] = {
219 [REGU_LP_STATE_DISK] = { .name = "standby-ddr-off", },
220 [REGU_LP_STATE_STANDBY] = { .name = "standby-ddr-sr", },
221 [REGU_LP_STATE_MEM] = { .name = "lp-stop", },
222 [REGU_LP_STATE_MEM_LOWVOLTAGE] = { .name = "lplv-stop", },
223 };
224
regu_lp_state2idx(const char * name)225 static unsigned int regu_lp_state2idx(const char *name)
226 {
227 unsigned int i = 0;
228
229 for (i = 0; i < ARRAY_SIZE(regu_lp_state); i++)
230 if (!strcmp(name, regu_lp_state[i].name))
231 return i;
232
233 panic();
234 }
235
dt_get_regu_low_power_config(void * fdt,const char * regu_name,int regu_node,const char * lp_state)236 static void dt_get_regu_low_power_config(void *fdt, const char *regu_name,
237 int regu_node, const char *lp_state)
238 {
239 unsigned int state_idx = regu_lp_state2idx(lp_state);
240 struct regu_lp_state *state = regu_lp_state + state_idx;
241 const fdt32_t *cuint = NULL;
242 int regu_state_node = 0;
243 struct regu_lp_config *regu_cfg = NULL;
244
245 state->cfg_count++;
246 state->cfg = realloc(state->cfg,
247 state->cfg_count * sizeof(*state->cfg));
248 if (!state->cfg)
249 panic();
250
251 regu_cfg = &state->cfg[state->cfg_count - 1];
252
253 memset(regu_cfg, 0, sizeof(*regu_cfg));
254
255 if (stpmic1_regu_has_lp_cfg(regu_name)) {
256 if (stpmic1_lp_cfg(regu_name, ®u_cfg->cfg)) {
257 DMSG("Cannot setup low power for regu %s", regu_name);
258 panic();
259 }
260 /*
261 * Always copy active configuration (Control register)
262 * to PWRCTRL Control register, even if regu_state_node
263 * does not exist.
264 */
265 regu_cfg->flags |= REGU_LP_FLAG_LOAD_PWRCTRL;
266 }
267
268 /* Parse regulator stte node if any */
269 regu_state_node = fdt_subnode_offset(fdt, regu_node, lp_state);
270 if (regu_state_node <= 0)
271 return;
272
273 if (fdt_getprop(fdt, regu_state_node,
274 "regulator-on-in-suspend", NULL))
275 regu_cfg->flags |= REGU_LP_FLAG_ON_IN_SUSPEND;
276
277 if (fdt_getprop(fdt, regu_state_node,
278 "regulator-off-in-suspend", NULL))
279 regu_cfg->flags |= REGU_LP_FLAG_OFF_IN_SUSPEND;
280
281 cuint = fdt_getprop(fdt, regu_state_node,
282 "regulator-suspend-microvolt", NULL);
283 if (cuint) {
284 uint32_t mv = fdt32_to_cpu(*cuint) / 1000U;
285
286 if (stpmic1_lp_voltage_cfg(regu_name, mv, ®u_cfg->cfg)) {
287 DMSG("Cannot set voltage for %s", regu_name);
288 panic();
289 }
290 regu_cfg->flags |= REGU_LP_FLAG_SET_VOLTAGE;
291 }
292
293 cuint = fdt_getprop(fdt, regu_state_node,
294 "regulator-mode", NULL);
295 if (cuint && fdt32_to_cpu(*cuint) == MODE_STANDBY)
296 regu_cfg->flags |= REGU_LP_FLAG_MODE_STANDBY;
297 }
298
299 /*
300 * int stm32mp_pmic_set_lp_config(char *lp_state)
301 *
302 * Load the low power configuration stored in regu_lp_state[].
303 */
stm32mp_pmic_apply_lp_config(const char * lp_state)304 void stm32mp_pmic_apply_lp_config(const char *lp_state)
305 {
306 unsigned int state_idx = regu_lp_state2idx(lp_state);
307 struct regu_lp_state *state = ®u_lp_state[state_idx];
308 size_t i = 0;
309
310 if (stpmic1_powerctrl_on())
311 panic();
312
313 for (i = 0; i < state->cfg_count; i++) {
314 struct stpmic1_lp_cfg *cfg = &state->cfg[i].cfg;
315
316 if ((state->cfg[i].flags & REGU_LP_FLAG_LOAD_PWRCTRL) &&
317 stpmic1_lp_load_unpg(cfg))
318 panic();
319
320 if ((state->cfg[i].flags & REGU_LP_FLAG_ON_IN_SUSPEND) &&
321 stpmic1_lp_on_off_unpg(cfg, 1))
322 panic();
323
324 if ((state->cfg[i].flags & REGU_LP_FLAG_OFF_IN_SUSPEND) &&
325 stpmic1_lp_on_off_unpg(cfg, 0))
326 panic();
327
328 if ((state->cfg[i].flags & REGU_LP_FLAG_SET_VOLTAGE) &&
329 stpmic1_lp_voltage_unpg(cfg))
330 panic();
331
332 if ((state->cfg[i].flags & REGU_LP_FLAG_MODE_STANDBY) &&
333 stpmic1_lp_mode_unpg(cfg, 1))
334 panic();
335 }
336 }
337
338 /* Return a libfdt compliant status value */
save_cpu_supply_name(void)339 static int save_cpu_supply_name(void)
340 {
341 void *fdt = NULL;
342 int node = 0;
343 const fdt32_t *cuint = NULL;
344 const char *name = NULL;
345
346 fdt = get_embedded_dt();
347 if (!fdt)
348 panic();
349
350 node = fdt_path_offset(fdt, "/cpus/cpu@0");
351 if (node < 0)
352 return -FDT_ERR_NOTFOUND;
353
354 cuint = fdt_getprop(fdt, node, "cpu-supply", NULL);
355 if (!cuint)
356 return -FDT_ERR_NOTFOUND;
357
358 node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
359 if (node < 0)
360 return -FDT_ERR_NOTFOUND;
361
362 name = fdt_get_name(fdt, node, NULL);
363 assert(strnlen(name, sizeof(cpu_supply_name)) <
364 sizeof(cpu_supply_name));
365
366 strncpy(cpu_supply_name, name, sizeof(cpu_supply_name));
367
368 return 0;
369 }
370
stm32mp_pmic_get_cpu_supply_name(void)371 const char *stm32mp_pmic_get_cpu_supply_name(void)
372 {
373 return cpu_supply_name;
374 }
375
376 /* Preallocate not that much regu references */
377 static char *nsec_access_regu_name[PMIC_REGU_COUNT];
378
stm32mp_nsec_can_access_pmic_regu(const char * name)379 bool stm32mp_nsec_can_access_pmic_regu(const char *name)
380 {
381 size_t n = 0;
382
383 for (n = 0; n < ARRAY_SIZE(nsec_access_regu_name); n++)
384 if (nsec_access_regu_name[n] &&
385 !strcmp(nsec_access_regu_name[n], name))
386 return true;
387
388 return false;
389 }
390
register_nsec_regu(const char * name_ref)391 static void register_nsec_regu(const char *name_ref)
392 {
393 size_t n = 0;
394
395 assert(!stm32mp_nsec_can_access_pmic_regu(name_ref));
396
397 for (n = 0; n < ARRAY_SIZE(nsec_access_regu_name); n++) {
398 if (!nsec_access_regu_name[n]) {
399 nsec_access_regu_name[n] = strdup(name_ref);
400
401 if (!nsec_access_regu_name[n])
402 panic();
403 break;
404 }
405 }
406
407 assert(stm32mp_nsec_can_access_pmic_regu(name_ref));
408 }
409
parse_regulator_fdt_nodes(void)410 static void parse_regulator_fdt_nodes(void)
411 {
412 int pmic_node = 0;
413 int regulators_node = 0;
414 int regu_node = 0;
415 void *fdt = NULL;
416
417 /* Expected called once */
418 assert(!regu_bo_config && !regu_bo_count);
419
420 fdt = get_embedded_dt();
421 if (!fdt)
422 panic();
423
424 pmic_node = dt_get_pmic_node(fdt);
425 if (pmic_node < 0)
426 panic();
427
428 regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");
429 if (regulators_node < 0)
430 panic();
431
432 fdt_for_each_subnode(regu_node, fdt, regulators_node) {
433 int status = _fdt_get_status(fdt, regu_node);
434 const char *regu_name = NULL;
435 size_t n = 0;
436
437 if (status == DT_STATUS_DISABLED)
438 continue;
439
440 regu_name = fdt_get_name(fdt, regu_node, NULL);
441
442 assert(stpmic1_regulator_is_valid(regu_name));
443
444 if (status & DT_STATUS_OK_NSEC)
445 register_nsec_regu(regu_name);
446
447 dt_get_regu_boot_on_config(fdt, regu_name, regu_node);
448
449 for (n = 0; n < ARRAY_SIZE(regu_lp_state); n++)
450 dt_get_regu_low_power_config(fdt, regu_name, regu_node,
451 regu_lp_state[n].name);
452 }
453
454 if (save_cpu_supply_name())
455 DMSG("No CPU supply provided");
456 }
457
458 /*
459 * Get PMIC and its I2C bus configuration from the device tree.
460 * Return 0 on success, 1 if no PMIC node found and a negative value otherwise
461 */
dt_pmic_i2c_config(struct dt_node_info * i2c_info,struct stm32_pinctrl ** pinctrl,size_t * pinctrl_count,struct stm32_i2c_init_s * init)462 static int dt_pmic_i2c_config(struct dt_node_info *i2c_info,
463 struct stm32_pinctrl **pinctrl,
464 size_t *pinctrl_count,
465 struct stm32_i2c_init_s *init)
466 {
467 int pmic_node = 0;
468 int i2c_node = 0;
469 void *fdt = NULL;
470 const fdt32_t *cuint = NULL;
471
472 fdt = get_embedded_dt();
473 if (!fdt)
474 return -FDT_ERR_NOTFOUND;
475
476 pmic_node = dt_get_pmic_node(fdt);
477 if (pmic_node < 0)
478 return 1;
479
480 cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
481 if (!cuint)
482 return -FDT_ERR_NOTFOUND;
483
484 pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1;
485 if (pmic_i2c_addr > UINT16_MAX)
486 return -FDT_ERR_BADVALUE;
487
488 i2c_node = fdt_parent_offset(fdt, pmic_node);
489 if (i2c_node < 0)
490 return -FDT_ERR_NOTFOUND;
491
492 _fdt_fill_device_info(fdt, i2c_info, i2c_node);
493 if (!i2c_info->reg)
494 return -FDT_ERR_NOTFOUND;
495
496 if (stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init,
497 pinctrl, pinctrl_count))
498 panic();
499
500 return 0;
501 }
502
503 /*
504 * PMIC and resource initialization
505 */
506
507 /* Return true if PMIC is available, false if not found, panics on errors */
initialize_pmic_i2c(void)508 static bool initialize_pmic_i2c(void)
509 {
510 int ret = 0;
511 struct dt_node_info i2c_info = { };
512 struct i2c_handle_s *i2c = &i2c_handle;
513 struct stm32_pinctrl *pinctrl = NULL;
514 size_t pin_count = 0;
515 struct stm32_i2c_init_s i2c_init = { };
516
517 ret = dt_pmic_i2c_config(&i2c_info, &pinctrl, &pin_count, &i2c_init);
518 if (ret < 0) {
519 EMSG("I2C configuration failed %d", ret);
520 panic();
521 }
522 if (ret)
523 return false;
524
525 /* Initialize PMIC I2C */
526 i2c->base.pa = i2c_info.reg;
527 i2c->base.va = (vaddr_t)phys_to_virt(i2c->base.pa, MEM_AREA_IO_SEC, 1);
528 assert(i2c->base.va);
529 i2c->dt_status = i2c_info.status;
530 i2c->clock = i2c_init.clock;
531 i2c->i2c_state = I2C_STATE_RESET;
532 i2c_init.own_address1 = pmic_i2c_addr;
533 i2c_init.analog_filter = true;
534 i2c_init.digital_filter_coef = 0;
535
536 i2c->pinctrl = pinctrl;
537 i2c->pinctrl_count = pin_count;
538
539 stm32mp_get_pmic();
540
541 ret = stm32_i2c_init(i2c, &i2c_init);
542 if (ret) {
543 EMSG("I2C init 0x%" PRIxPA ": %d", i2c_info.reg, ret);
544 panic();
545 }
546
547 if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr,
548 PMIC_I2C_TRIALS,
549 PMIC_I2C_TIMEOUT_BUSY_MS))
550 panic();
551
552 stpmic1_bind_i2c(i2c, pmic_i2c_addr);
553
554 stm32mp_put_pmic();
555
556 return true;
557 }
558
559 /*
560 * Automated suspend/resume at system suspend/resume is expected
561 * only when the PMIC is secure. If it is non secure, only atomic
562 * execution context can get/put the PMIC resources.
563 */
pmic_pm(enum pm_op op,uint32_t pm_hint __unused,const struct pm_callback_handle * pm_handle __unused)564 static TEE_Result pmic_pm(enum pm_op op, uint32_t pm_hint __unused,
565 const struct pm_callback_handle *pm_handle __unused)
566 {
567 if (op == PM_OP_SUSPEND)
568 stm32_i2c_suspend(&i2c_handle);
569 else
570 stm32_i2c_resume(&i2c_handle);
571
572 return TEE_SUCCESS;
573 }
574 DECLARE_KEEP_PAGER(pmic_pm);
575
576 /* stm32mp_get/put_pmic allows secure atomic sequences to use non secure PMIC */
stm32mp_get_pmic(void)577 void stm32mp_get_pmic(void)
578 {
579 stm32_i2c_resume(&i2c_handle);
580 }
581
stm32mp_put_pmic(void)582 void stm32mp_put_pmic(void)
583 {
584 stm32_i2c_suspend(&i2c_handle);
585 }
586
register_non_secure_pmic(void)587 static void register_non_secure_pmic(void)
588 {
589 size_t n = 0;
590
591 /* Allow this function to be called when STPMIC1 not used */
592 if (!i2c_handle.base.pa)
593 return;
594
595 for (n = 0; n < i2c_handle.pinctrl_count; n++)
596 stm32mp_register_non_secure_gpio(i2c_handle.pinctrl[n].bank,
597 i2c_handle.pinctrl[n].pin);
598
599 stm32mp_register_non_secure_periph_iomem(i2c_handle.base.pa);
600 }
601
register_secure_pmic(void)602 static void register_secure_pmic(void)
603 {
604 size_t n = 0;
605
606 for (n = 0; n < i2c_handle.pinctrl_count; n++)
607 stm32mp_register_secure_gpio(i2c_handle.pinctrl[n].bank,
608 i2c_handle.pinctrl[n].pin);
609
610 stm32mp_register_secure_periph_iomem(i2c_handle.base.pa);
611 register_pm_driver_cb(pmic_pm, NULL, "stm32mp1-pmic");
612 }
613
initialize_pmic(void)614 static TEE_Result initialize_pmic(void)
615 {
616 unsigned long pmic_version = 0;
617
618 if (!initialize_pmic_i2c()) {
619 DMSG("No PMIC");
620 register_non_secure_pmic();
621 return TEE_SUCCESS;
622 }
623
624 stm32mp_get_pmic();
625
626 if (stpmic1_get_version(&pmic_version))
627 panic("Failed to access PMIC");
628
629 DMSG("PMIC version = 0x%02lx", pmic_version);
630 stpmic1_dump_regulators();
631
632 if (dt_pmic_is_secure())
633 register_secure_pmic();
634 else
635 register_non_secure_pmic();
636
637 parse_regulator_fdt_nodes();
638
639 stm32mp_put_pmic();
640
641 return TEE_SUCCESS;
642 }
643 driver_init(initialize_pmic);
644