stm32mp2_reset.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <stdbool.h>
  8. #include <common/debug.h>
  9. #include <drivers/delay_timer.h>
  10. #include <drivers/st/stm32mp_reset.h>
  11. #include <lib/mmio.h>
  12. #include <lib/utils_def.h>
  13. #include <platform_def.h>
  14. static uint32_t id2reg_offset(unsigned int reset_id)
  15. {
  16. return ((reset_id & GENMASK(31, 5)) >> 5) * sizeof(uint32_t);
  17. }
  18. static uint8_t id2reg_bit_pos(unsigned int reset_id)
  19. {
  20. return (uint8_t)(reset_id & GENMASK(4, 0));
  21. }
  22. static int reset_toggle(uint32_t id, unsigned int to_us, bool reset_status)
  23. {
  24. uint32_t offset = id2reg_offset(id);
  25. uint32_t bitmsk = BIT(id2reg_bit_pos(id));
  26. uint32_t bit_check;
  27. uintptr_t rcc_base = stm32mp_rcc_base();
  28. if (reset_status) {
  29. mmio_setbits_32(rcc_base + offset, bitmsk);
  30. bit_check = bitmsk;
  31. } else {
  32. mmio_clrbits_32(rcc_base + offset, bitmsk);
  33. bit_check = 0U;
  34. }
  35. if (to_us != 0U) {
  36. uint64_t timeout_ref = timeout_init_us(to_us);
  37. while ((mmio_read_32(rcc_base + offset) & bitmsk) != bit_check) {
  38. if (timeout_elapsed(timeout_ref)) {
  39. return -ETIMEDOUT;
  40. }
  41. }
  42. }
  43. return 0;
  44. }
  45. int stm32mp_reset_assert(uint32_t id, unsigned int to_us)
  46. {
  47. return reset_toggle(id, to_us, true);
  48. }
  49. int stm32mp_reset_deassert(uint32_t id, unsigned int to_us)
  50. {
  51. return reset_toggle(id, to_us, false);
  52. }
  53. void __dead2 stm32mp_system_reset(void)
  54. {
  55. uintptr_t rcc_base = stm32mp_rcc_base();
  56. mmio_setbits_32(rcc_base + RCC_GRSTCSETR, RCC_GRSTCSETR_SYSRST);
  57. /* Loop in case system reset is not immediately caught */
  58. while (true) {
  59. wfi();
  60. }
  61. }