stm32_iwdg.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2017-2021, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <libfdt.h>
  10. #include <platform_def.h>
  11. #include <arch_helpers.h>
  12. #include <common/debug.h>
  13. #include <drivers/arm/gicv2.h>
  14. #include <drivers/clk.h>
  15. #include <drivers/delay_timer.h>
  16. #include <drivers/st/stm32_iwdg.h>
  17. #include <drivers/st/stm32mp_clkfunc.h>
  18. #include <lib/mmio.h>
  19. #include <lib/utils.h>
  20. #include <plat/common/platform.h>
  21. /* IWDG registers offsets */
  22. #define IWDG_KR_OFFSET 0x00U
  23. /* Registers values */
  24. #define IWDG_KR_RELOAD_KEY 0xAAAA
  25. struct stm32_iwdg_instance {
  26. uintptr_t base;
  27. unsigned long clock;
  28. uint8_t flags;
  29. int num_irq;
  30. };
  31. static struct stm32_iwdg_instance stm32_iwdg[IWDG_MAX_INSTANCE];
  32. static int stm32_iwdg_get_dt_node(struct dt_node_info *info, int offset)
  33. {
  34. int node;
  35. node = dt_get_node(info, offset, DT_IWDG_COMPAT);
  36. if (node < 0) {
  37. if (offset == -1) {
  38. VERBOSE("%s: No IDWG found\n", __func__);
  39. }
  40. return -FDT_ERR_NOTFOUND;
  41. }
  42. return node;
  43. }
  44. void stm32_iwdg_refresh(void)
  45. {
  46. uint8_t i;
  47. for (i = 0U; i < IWDG_MAX_INSTANCE; i++) {
  48. struct stm32_iwdg_instance *iwdg = &stm32_iwdg[i];
  49. /* 0x00000000 is not a valid address for IWDG peripherals */
  50. if (iwdg->base != 0U) {
  51. clk_enable(iwdg->clock);
  52. mmio_write_32(iwdg->base + IWDG_KR_OFFSET,
  53. IWDG_KR_RELOAD_KEY);
  54. clk_disable(iwdg->clock);
  55. }
  56. }
  57. }
  58. int stm32_iwdg_init(void)
  59. {
  60. int node = -1;
  61. struct dt_node_info dt_info;
  62. void *fdt;
  63. uint32_t __unused count = 0;
  64. if (fdt_get_address(&fdt) == 0) {
  65. panic();
  66. }
  67. for (node = stm32_iwdg_get_dt_node(&dt_info, node);
  68. node != -FDT_ERR_NOTFOUND;
  69. node = stm32_iwdg_get_dt_node(&dt_info, node)) {
  70. struct stm32_iwdg_instance *iwdg;
  71. uint32_t hw_init;
  72. uint32_t idx;
  73. count++;
  74. idx = stm32_iwdg_get_instance(dt_info.base);
  75. iwdg = &stm32_iwdg[idx];
  76. iwdg->base = dt_info.base;
  77. iwdg->clock = (unsigned long)dt_info.clock;
  78. /* DT can specify low power cases */
  79. if (fdt_getprop(fdt, node, "stm32,enable-on-stop", NULL) ==
  80. NULL) {
  81. iwdg->flags |= IWDG_DISABLE_ON_STOP;
  82. }
  83. if (fdt_getprop(fdt, node, "stm32,enable-on-standby", NULL) ==
  84. NULL) {
  85. iwdg->flags |= IWDG_DISABLE_ON_STANDBY;
  86. }
  87. /* Explicit list of supported bit flags */
  88. hw_init = stm32_iwdg_get_otp_config(idx);
  89. if ((hw_init & IWDG_HW_ENABLED) != 0) {
  90. if (dt_info.status == DT_DISABLED) {
  91. ERROR("OTP enabled but iwdg%u DT-disabled\n",
  92. idx + 1U);
  93. panic();
  94. }
  95. iwdg->flags |= IWDG_HW_ENABLED;
  96. }
  97. if (dt_info.status == DT_DISABLED) {
  98. zeromem((void *)iwdg,
  99. sizeof(struct stm32_iwdg_instance));
  100. continue;
  101. }
  102. if ((hw_init & IWDG_DISABLE_ON_STOP) != 0) {
  103. iwdg->flags |= IWDG_DISABLE_ON_STOP;
  104. }
  105. if ((hw_init & IWDG_DISABLE_ON_STANDBY) != 0) {
  106. iwdg->flags |= IWDG_DISABLE_ON_STANDBY;
  107. }
  108. VERBOSE("IWDG%u found, %ssecure\n", idx + 1U,
  109. ((dt_info.status & DT_NON_SECURE) != 0) ?
  110. "non-" : "");
  111. if ((dt_info.status & DT_NON_SECURE) != 0) {
  112. stm32mp_register_non_secure_periph_iomem(iwdg->base);
  113. } else {
  114. stm32mp_register_secure_periph_iomem(iwdg->base);
  115. }
  116. #if defined(IMAGE_BL2)
  117. if (stm32_iwdg_shadow_update(idx, iwdg->flags) != BSEC_OK) {
  118. return -1;
  119. }
  120. #endif
  121. }
  122. VERBOSE("%u IWDG instance%s found\n", count, (count > 1U) ? "s" : "");
  123. return 0;
  124. }