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