blob: 98c8dcf71001b3007ac78c42b5635bc9584f9247 [file] [log] [blame]
Yann Gautier9aea69e2018-07-24 17:13:36 +02001/*
Yann Gautiera2e2a302019-02-14 11:13:39 +01002 * Copyright (c) 2018-2019, STMicroelectronics - All Rights Reserved
Yann Gautier9aea69e2018-07-24 17:13:36 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Etienne Carrieref02647a2019-12-08 08:14:40 +01007#include <errno.h>
Yann Gautier9aea69e2018-07-24 17:13:36 +02008#include <limits.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
Yann Gautier9aea69e2018-07-24 17:13:36 +020010#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
12#include <common/bl_common.h>
13#include <common/debug.h>
Yann Gautier33b1be42019-02-14 09:17:55 +010014#include <drivers/delay_timer.h>
Yann Gautiera2e2a302019-02-14 11:13:39 +010015#include <drivers/st/stm32mp_reset.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <lib/mmio.h>
17#include <lib/utils_def.h>
Yann Gautier9aea69e2018-07-24 17:13:36 +020018
Yann Gautier33b1be42019-02-14 09:17:55 +010019static uint32_t id2reg_offset(unsigned int reset_id)
20{
21 return ((reset_id & GENMASK(31, 5)) >> 5) * sizeof(uint32_t);
22}
23
24static uint8_t id2reg_bit_pos(unsigned int reset_id)
25{
26 return (uint8_t)(reset_id & GENMASK(4, 0));
27}
Yann Gautier9aea69e2018-07-24 17:13:36 +020028
Etienne Carrieref02647a2019-12-08 08:14:40 +010029int stm32mp_reset_assert(uint32_t id, unsigned int to_us)
Yann Gautier9aea69e2018-07-24 17:13:36 +020030{
Yann Gautier33b1be42019-02-14 09:17:55 +010031 uint32_t offset = id2reg_offset(id);
32 uint32_t bitmsk = BIT(id2reg_bit_pos(id));
Yann Gautier3d78a2e2019-02-14 11:01:20 +010033 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +020034
Yann Gautier33b1be42019-02-14 09:17:55 +010035 mmio_write_32(rcc_base + offset, bitmsk);
36
Etienne Carrieref02647a2019-12-08 08:14:40 +010037 if (to_us != 0U) {
38 uint64_t timeout_ref = timeout_init_us(to_us);
39
40 while ((mmio_read_32(rcc_base + offset) & bitmsk) == 0U) {
41 if (timeout_elapsed(timeout_ref)) {
42 return -ETIMEDOUT;
43 }
Yann Gautier33b1be42019-02-14 09:17:55 +010044 }
Yann Gautier9aea69e2018-07-24 17:13:36 +020045 }
Etienne Carrieref02647a2019-12-08 08:14:40 +010046
47 return 0;
Yann Gautier9aea69e2018-07-24 17:13:36 +020048}
49
Etienne Carrieref02647a2019-12-08 08:14:40 +010050int stm32mp_reset_deassert(uint32_t id, unsigned int to_us)
Yann Gautier9aea69e2018-07-24 17:13:36 +020051{
Yann Gautier33b1be42019-02-14 09:17:55 +010052 uint32_t offset = id2reg_offset(id) + RCC_RSTCLRR_OFFSET;
53 uint32_t bitmsk = BIT(id2reg_bit_pos(id));
Yann Gautier3d78a2e2019-02-14 11:01:20 +010054 uintptr_t rcc_base = stm32mp_rcc_base();
Yann Gautier9aea69e2018-07-24 17:13:36 +020055
Yann Gautier33b1be42019-02-14 09:17:55 +010056 mmio_write_32(rcc_base + offset, bitmsk);
57
Etienne Carrieref02647a2019-12-08 08:14:40 +010058 if (to_us != 0U) {
59 uint64_t timeout_ref = timeout_init_us(to_us);
60
61 while ((mmio_read_32(rcc_base + offset) & bitmsk) != 0U) {
62 if (timeout_elapsed(timeout_ref)) {
63 return -ETIMEDOUT;
64 }
Yann Gautier33b1be42019-02-14 09:17:55 +010065 }
Yann Gautier9aea69e2018-07-24 17:13:36 +020066 }
Etienne Carrieref02647a2019-12-08 08:14:40 +010067
68 return 0;
Yann Gautier9aea69e2018-07-24 17:13:36 +020069}