stm32mp_ddr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #include <drivers/delay_timer.h>
  8. #include <drivers/st/stm32mp_ddr.h>
  9. #include <drivers/st/stm32mp_ddrctrl_regs.h>
  10. #include <drivers/st/stm32mp_pmic.h>
  11. #include <lib/mmio.h>
  12. #include <platform_def.h>
  13. #define INVALID_OFFSET 0xFFU
  14. static uintptr_t get_base_addr(const struct stm32mp_ddr_priv *priv, enum stm32mp_ddr_base_type base)
  15. {
  16. if (base == DDRPHY_BASE) {
  17. return (uintptr_t)priv->phy;
  18. } else {
  19. return (uintptr_t)priv->ctl;
  20. }
  21. }
  22. void stm32mp_ddr_set_reg(const struct stm32mp_ddr_priv *priv, enum stm32mp_ddr_reg_type type,
  23. const void *param, const struct stm32mp_ddr_reg_info *ddr_registers)
  24. {
  25. unsigned int i;
  26. unsigned int value;
  27. enum stm32mp_ddr_base_type base = ddr_registers[type].base;
  28. uintptr_t base_addr = get_base_addr(priv, base);
  29. const struct stm32mp_ddr_reg_desc *desc = ddr_registers[type].desc;
  30. VERBOSE("init %s\n", ddr_registers[type].name);
  31. for (i = 0; i < ddr_registers[type].size; i++) {
  32. uintptr_t ptr = base_addr + desc[i].offset;
  33. if (desc[i].par_offset == INVALID_OFFSET) {
  34. ERROR("invalid parameter offset for %s", desc[i].name);
  35. panic();
  36. } else {
  37. value = *((uint32_t *)((uintptr_t)param +
  38. desc[i].par_offset));
  39. mmio_write_32(ptr, value);
  40. }
  41. }
  42. }
  43. /* Start quasi dynamic register update */
  44. void stm32mp_ddr_start_sw_done(struct stm32mp_ddrctl *ctl)
  45. {
  46. mmio_clrbits_32((uintptr_t)&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
  47. VERBOSE("[0x%lx] swctl = 0x%x\n",
  48. (uintptr_t)&ctl->swctl, mmio_read_32((uintptr_t)&ctl->swctl));
  49. }
  50. /* Wait quasi dynamic register update */
  51. void stm32mp_ddr_wait_sw_done_ack(struct stm32mp_ddrctl *ctl)
  52. {
  53. uint64_t timeout;
  54. uint32_t swstat;
  55. mmio_setbits_32((uintptr_t)&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
  56. VERBOSE("[0x%lx] swctl = 0x%x\n",
  57. (uintptr_t)&ctl->swctl, mmio_read_32((uintptr_t)&ctl->swctl));
  58. timeout = timeout_init_us(TIMEOUT_US_1S);
  59. do {
  60. swstat = mmio_read_32((uintptr_t)&ctl->swstat);
  61. VERBOSE("[0x%lx] swstat = 0x%x ",
  62. (uintptr_t)&ctl->swstat, swstat);
  63. if (timeout_elapsed(timeout)) {
  64. panic();
  65. }
  66. } while ((swstat & DDRCTRL_SWSTAT_SW_DONE_ACK) == 0U);
  67. VERBOSE("[0x%lx] swstat = 0x%x\n",
  68. (uintptr_t)&ctl->swstat, swstat);
  69. }
  70. void stm32mp_ddr_enable_axi_port(struct stm32mp_ddrctl *ctl)
  71. {
  72. /* Enable uMCTL2 AXI port 0 */
  73. mmio_setbits_32((uintptr_t)&ctl->pctrl_0, DDRCTRL_PCTRL_N_PORT_EN);
  74. VERBOSE("[0x%lx] pctrl_0 = 0x%x\n", (uintptr_t)&ctl->pctrl_0,
  75. mmio_read_32((uintptr_t)&ctl->pctrl_0));
  76. #if STM32MP_DDR_DUAL_AXI_PORT
  77. /* Enable uMCTL2 AXI port 1 */
  78. mmio_setbits_32((uintptr_t)&ctl->pctrl_1, DDRCTRL_PCTRL_N_PORT_EN);
  79. VERBOSE("[0x%lx] pctrl_1 = 0x%x\n", (uintptr_t)&ctl->pctrl_1,
  80. mmio_read_32((uintptr_t)&ctl->pctrl_1));
  81. #endif
  82. }
  83. int stm32mp_board_ddr_power_init(enum ddr_type ddr_type)
  84. {
  85. if (dt_pmic_status() > 0) {
  86. return pmic_ddr_power_init(ddr_type);
  87. }
  88. return 0;
  89. }