1 /*
2  * Copyright 2019 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 /* The DSP subsystem local TCM start address, refer to Reference Manual for detailed information */
20 #define FSL_MEM_DSP_TCM_BEGIN 0x00800000U
21 /* The DSP subsystem local TCM end address, refer to Reference Manual for detailed information */
22 #define FSL_MEM_DSP_TCM_END 0x00CFFFFFU
23 
24 /* The alias start address for CM33 subsystem */
25 #define FSL_MEM_M33_ALIAS_BEGIN 0x20000000U
26 /* The alias end address for CM33 subsystem */
27 #define FSL_MEM_M33_ALIAS_END 0x204FFFFFU
28 /*
29   This alias allows the DSP TCM to be mapped to the CM33 system address space without the need
30   to enable and configure SMMU.
31   DSP TCM addresses 0x0080_0000 to 0x00CF_FFFF are mapped to the CM33 system address space 0x2000_0000 to 0x204F_FFFF
32 */
33 #define FSL_MEM_M33_ALIAS_OFFSET 0x1F800000U
34 
35 #if (defined(CPU_MIMXRT595SFAWC_dsp) || defined(CPU_MIMXRT595SFFOC_dsp))
36 /* System level TCM memory address = CM33 subsystem local TCM address + FSL_FEATURE_TCM_OFFSET */
37 #define FSL_MEM_DSP_TCM_OFFSET 0x1F800000U
38 #else
39 #error "Device is not supported by this driver!"
40 #endif
41 
42 typedef enum _mem_direction
43 {
44     kMEMORY_Local2DMA = 0,
45     kMEMORY_DMA2Local,
46 } mem_direction_t;
47 
48 /*******************************************************************************
49  * API
50  ******************************************************************************/
51 #if defined(__cplusplus)
52 extern "C" {
53 #endif
54 /*!
55  * @brief Convert the memory map address.
56  *
57  * This function convert the address between system mapped address and native mapped address.
58  * There maybe offset between subsystem native address and system address for some memory,
59  * this funciton convert the address to different memory map.
60  * @param addr address need to be converted.
61  * @param direction convert direction.
62  * @return the converted address
63  */
MEMORY_ConvertMemoryMapAddress(uint32_t addr,mem_direction_t direction)64 static inline uint32_t MEMORY_ConvertMemoryMapAddress(uint32_t addr, mem_direction_t direction)
65 {
66     uint32_t dest;
67 
68     switch (direction)
69     {
70         case kMEMORY_Local2DMA:
71         {
72             if ((addr >= FSL_MEM_DSP_TCM_BEGIN) && (addr <= FSL_MEM_DSP_TCM_END))
73             {
74                 dest = addr + FSL_MEM_DSP_TCM_OFFSET;
75             }
76             else if ((addr >= FSL_MEM_M33_ALIAS_BEGIN) && (addr <= FSL_MEM_M33_ALIAS_END))
77             {
78                 dest = addr - FSL_MEM_M33_ALIAS_OFFSET;
79             }
80             else
81             {
82                 dest = addr;
83             }
84             break;
85         }
86         case kMEMORY_DMA2Local:
87         {
88             if ((addr >= (FSL_MEM_DSP_TCM_BEGIN + FSL_MEM_DSP_TCM_OFFSET)) &&
89                 (addr <= (FSL_MEM_DSP_TCM_END + FSL_MEM_DSP_TCM_OFFSET)))
90             {
91                 dest = addr - FSL_MEM_DSP_TCM_OFFSET;
92             }
93             else if (addr <= (FSL_MEM_M33_ALIAS_END - FSL_MEM_M33_ALIAS_OFFSET))
94             {
95                 dest = addr + FSL_MEM_M33_ALIAS_OFFSET;
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