1 /*
2  * Copyright 2021 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _FSL_MEMORY_H_
8 #define _FSL_MEMORY_H_
9 
10 #include "fsl_common.h"
11 
12 /*******************************************************************************
13  * Definitions
14  ******************************************************************************/
15 /* Component ID definition, used by tools. */
16 #ifndef FSL_COMPONENT_ID
17 #define FSL_COMPONENT_ID "platform.drivers.memory"
18 #endif
19 
20 /*! @name Driver version */
21 /*@{*/
22 /*! @brief MEMORY driver version 2.0.0. */
23 #define FSL_MEMORY_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
24 /*@}*/
25 
26 #if (defined(MIMX8UD7_dsp0_SERIES) || defined(MIMX8UD5_dsp0_SERIES) || defined(MIMX8UD3_dsp0_SERIES) || \
27      defined(MIMX8US5_dsp0_SERIES) || defined(MIMX8US3_dsp0_SERIES))
28 /* The DSP subsystem dRAM start address, refer to Reference Manual for detailed information */
29 #define FSL_MEM_DSP_TCM_BEGIN 0x00800000U
30 /* The DSP subsystem dRAM end address, refer to Reference Manual for detailed information */
31 #define FSL_MEM_DSP_TCM_END 0x008BFFFFU
32 
33 /* The SSRAM P0-P6 start address for RTD CM33 */
34 #define FSL_MEM_M33_SSRAM06_BEGIN 0x20000000U
35 /* The SSRAM P7 start address for RTD CM33 */
36 #define FSL_MEM_M33_SSRAM7_BEGIN 0x0FFC0000U
37 /* The SSRAM end address for RTD CM33 */
38 #define FSL_MEM_M33_SSRAM06_END 0x2007FFFFU
39 #define FSL_MEM_M33_SSRAM7_END  0x0FFFFFFFU
40 
41 /* This offset allows the DSP SSRAM to be mapped to the M33 system address space, M33 address = DSP address + offset */
42 #define FSL_MEM_DSP_OFFSET(x) ((x >= 0x00880000U) ? 0x0F740000U : 0x1F800000U)
43 /* This offset allows the DSP SSRAM to be mapped to the DSP system address space, DSP address = M33 address - offset */
44 #define FSL_MEM_M33_OFFSET(x) ((x < 0x20000000U) ? 0x0F740000U : 0x1F800000U)
45 #else
46 #error "Device is not supported by this driver!"
47 #endif
48 
49 typedef enum _mem_direction
50 {
51     kMEMORY_Local2DMA = 0,
52     kMEMORY_DMA2Local,
53 } mem_direction_t;
54 
55 /*******************************************************************************
56  * API
57  ******************************************************************************/
58 #if defined(__cplusplus)
59 extern "C" {
60 #endif
61 /*!
62  * @brief Convert the memory map address.
63  *
64  * This function convert the address between system mapped address and native mapped address.
65  * There maybe offset between subsystem native address and system address for some memory,
66  * this funciton convert the address to different memory map.
67  * @param addr address need to be converted.
68  * @param direction convert direction.
69  * @return the converted address
70  */
MEMORY_ConvertMemoryMapAddress(uint32_t addr,mem_direction_t direction)71 static inline uint32_t MEMORY_ConvertMemoryMapAddress(uint32_t addr, mem_direction_t direction)
72 {
73     uint32_t dest;
74 
75     switch (direction)
76     {
77         case kMEMORY_Local2DMA:
78         {
79             if ((addr >= FSL_MEM_DSP_TCM_BEGIN) && (addr <= FSL_MEM_DSP_TCM_END))
80             {
81                 dest = addr + FSL_MEM_DSP_OFFSET(addr);
82             }
83             else
84             {
85                 dest = addr;
86             }
87             break;
88         }
89         case kMEMORY_DMA2Local:
90         {
91             addr &= 0xEFFFFFFF; /* Convert to non-secure address. */
92             if (((addr >= FSL_MEM_M33_SSRAM06_BEGIN) && (addr <= FSL_MEM_M33_SSRAM06_END)) ||
93                 ((addr >= FSL_MEM_M33_SSRAM7_BEGIN) && (addr <= FSL_MEM_M33_SSRAM7_END)))
94             {
95                 dest = addr - FSL_MEM_M33_OFFSET(addr);
96             }
97             else
98             {
99                 dest = addr;
100             }
101             break;
102         }
103         default:
104             dest = addr;
105             break;
106     }
107 
108     return dest;
109 }
110 #if defined(__cplusplus)
111 }
112 #endif /* __cplusplus */
113 #endif /* _FSL_MEMORY_H_ */
114