1*79629b1aSNicolas Le Bayon /*
2*79629b1aSNicolas Le Bayon * Copyright (C) 2021-2024, STMicroelectronics - All Rights Reserved
3*79629b1aSNicolas Le Bayon *
4*79629b1aSNicolas Le Bayon * SPDX-License-Identifier: BSD-3-Clause
5*79629b1aSNicolas Le Bayon */
6*79629b1aSNicolas Le Bayon
7*79629b1aSNicolas Le Bayon #include <stdio.h>
8*79629b1aSNicolas Le Bayon
9*79629b1aSNicolas Le Bayon #include <common/debug.h>
10*79629b1aSNicolas Le Bayon
11*79629b1aSNicolas Le Bayon #include <ddrphy_phyinit.h>
12*79629b1aSNicolas Le Bayon
13*79629b1aSNicolas Le Bayon #include <lib/mmio.h>
14*79629b1aSNicolas Le Bayon
15*79629b1aSNicolas Le Bayon #include <platform_def.h>
16*79629b1aSNicolas Le Bayon
17*79629b1aSNicolas Le Bayon /*
18*79629b1aSNicolas Le Bayon * Writes local memory content into the SRAM via APB interface.
19*79629b1aSNicolas Le Bayon *
20*79629b1aSNicolas Le Bayon * This function issued APB writes commands to SRAM address based on values
21*79629b1aSNicolas Le Bayon * stored in a local PhyInit array that contains consolidated IMEM and DMEM
22*79629b1aSNicolas Le Bayon * data.
23*79629b1aSNicolas Le Bayon * @param[in] mem[] Local memory array.
24*79629b1aSNicolas Le Bayon * @param[in] mem_offset offset index. if provided, skips to the offset index
25*79629b1aSNicolas Le Bayon * from the local array and issues APB commands from mem_offset to mem_size.
26*79629b1aSNicolas Le Bayon * @param[in] mem_size size of the memroy (in mem array index)
27*79629b1aSNicolas Le Bayon * @returns void
28*79629b1aSNicolas Le Bayon */
ddrphy_phyinit_writeoutmem(uint32_t * mem,uint32_t mem_offset,uint32_t mem_size)29*79629b1aSNicolas Le Bayon void ddrphy_phyinit_writeoutmem(uint32_t *mem, uint32_t mem_offset, uint32_t mem_size)
30*79629b1aSNicolas Le Bayon {
31*79629b1aSNicolas Le Bayon uint32_t index;
32*79629b1aSNicolas Le Bayon
33*79629b1aSNicolas Le Bayon /*
34*79629b1aSNicolas Le Bayon * 1. Enable access to the internal CSRs by setting the MicroContMuxSel CSR to 0.
35*79629b1aSNicolas Le Bayon * This allows the memory controller unrestricted access to the configuration CSRs.
36*79629b1aSNicolas Le Bayon */
37*79629b1aSNicolas Le Bayon mmio_write_16((uintptr_t)(DDRPHYC_BASE + (4U * (TAPBONLY | CSR_MICROCONTMUXSEL_ADDR))),
38*79629b1aSNicolas Le Bayon 0x0U);
39*79629b1aSNicolas Le Bayon
40*79629b1aSNicolas Le Bayon for (index = 0U; index < mem_size / sizeof(uint32_t); index++) {
41*79629b1aSNicolas Le Bayon uint32_t data = mem[index];
42*79629b1aSNicolas Le Bayon
43*79629b1aSNicolas Le Bayon mmio_write_16((uintptr_t)(DDRPHYC_BASE + (4U * ((index * 2) + mem_offset))),
44*79629b1aSNicolas Le Bayon data & 0xFFFFU);
45*79629b1aSNicolas Le Bayon mmio_write_16((uintptr_t)(DDRPHYC_BASE + (4U * ((index * 2) + 1 + mem_offset))),
46*79629b1aSNicolas Le Bayon (data >> 16) & 0xFFFFU);
47*79629b1aSNicolas Le Bayon }
48*79629b1aSNicolas Le Bayon
49*79629b1aSNicolas Le Bayon /*
50*79629b1aSNicolas Le Bayon * 2. Isolate the APB access from the internal CSRs by setting the MicroContMuxSel CSR to 1.
51*79629b1aSNicolas Le Bayon * This allows the firmware unrestricted access to the configuration CSRs.
52*79629b1aSNicolas Le Bayon */
53*79629b1aSNicolas Le Bayon mmio_write_16((uintptr_t)(DDRPHYC_BASE + (4U * (TAPBONLY | CSR_MICROCONTMUXSEL_ADDR))),
54*79629b1aSNicolas Le Bayon 0x1U);
55*79629b1aSNicolas Le Bayon }
56*79629b1aSNicolas Le Bayon
57*79629b1aSNicolas Le Bayon /* Similar function for message block */
ddrphy_phyinit_writeoutmsgblk(uint16_t * mem,uint32_t mem_offset,uint32_t mem_size)58*79629b1aSNicolas Le Bayon void ddrphy_phyinit_writeoutmsgblk(uint16_t *mem, uint32_t mem_offset, uint32_t mem_size)
59*79629b1aSNicolas Le Bayon {
60*79629b1aSNicolas Le Bayon uint32_t index;
61*79629b1aSNicolas Le Bayon
62*79629b1aSNicolas Le Bayon /*
63*79629b1aSNicolas Le Bayon * 1. Enable access to the internal CSRs by setting the MicroContMuxSel CSR to 0.
64*79629b1aSNicolas Le Bayon * This allows the memory controller unrestricted access to the configuration CSRs.
65*79629b1aSNicolas Le Bayon */
66*79629b1aSNicolas Le Bayon mmio_write_16((uintptr_t)(DDRPHYC_BASE + (4U * (TAPBONLY | CSR_MICROCONTMUXSEL_ADDR))),
67*79629b1aSNicolas Le Bayon 0x0U);
68*79629b1aSNicolas Le Bayon
69*79629b1aSNicolas Le Bayon for (index = 0U; index < mem_size / sizeof(uint16_t); index++) {
70*79629b1aSNicolas Le Bayon mmio_write_16((uintptr_t)(DDRPHYC_BASE + (4U * (index + mem_offset))), mem[index]);
71*79629b1aSNicolas Le Bayon }
72*79629b1aSNicolas Le Bayon
73*79629b1aSNicolas Le Bayon /*
74*79629b1aSNicolas Le Bayon * 2. Isolate the APB access from the internal CSRs by setting the MicroContMuxSel CSR to 1.
75*79629b1aSNicolas Le Bayon * This allows the firmware unrestricted access to the configuration CSRs.
76*79629b1aSNicolas Le Bayon */
77*79629b1aSNicolas Le Bayon mmio_write_16((uintptr_t)(DDRPHYC_BASE + (4U * (TAPBONLY | CSR_MICROCONTMUXSEL_ADDR))),
78*79629b1aSNicolas Le Bayon 0x1U);
79*79629b1aSNicolas Le Bayon }
80