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 /* The CM4 subsystem local TCM start address, refer to Reference Manual for detailed information */
20 #define FSL_MEM_M4_TCM_BEGIN 0x1FFE0000U
21 /* The CM4 subsystem local TCM end address, refer to Reference Manual for detailed information */
22 #define FSL_MEM_M4_TCM_END 0x2001FFFFU
23 
24 #define FSL_MEM_M4_TCM_OFFSET 0x220000U
25 
26 typedef enum _mem_direction
27 {
28     kMEMORY_Local2DMA = 0,
29     kMEMORY_DMA2Local,
30 } mem_direction_t;
31 
32 /*******************************************************************************
33  * API
34  ******************************************************************************/
35 #if defined(__cplusplus)
36 extern "C" {
37 #endif
38 /*!
39  * @brief Convert the memory map address.
40  *
41  * This function convert the address between system mapped address and native mapped address.
42  * There maybe offset between subsystem native address and system address for some memory,
43  * this funciton convert the address to different memory map.
44  * @param addr address need to be converted.
45  * @param direction convert direction.
46  * @return the converted address
47  */
MEMORY_ConvertMemoryMapAddress(uint32_t addr,mem_direction_t direction)48 static inline uint32_t MEMORY_ConvertMemoryMapAddress(uint32_t addr, mem_direction_t direction)
49 {
50     uint32_t dest;
51 
52     switch (direction)
53     {
54         case kMEMORY_Local2DMA:
55         {
56             if ((addr >= FSL_MEM_M4_TCM_BEGIN) && (addr <= FSL_MEM_M4_TCM_END))
57             {
58                 dest = addr + FSL_MEM_M4_TCM_OFFSET;
59             }
60             else
61             {
62                 dest = addr;
63             }
64             break;
65         }
66         case kMEMORY_DMA2Local:
67         {
68             if ((addr >= (FSL_MEM_M4_TCM_BEGIN + FSL_MEM_M4_TCM_OFFSET)) &&
69                 (addr <= (FSL_MEM_M4_TCM_END + FSL_MEM_M4_TCM_OFFSET)))
70             {
71                 dest = addr - FSL_MEM_M4_TCM_OFFSET;
72             }
73             else
74             {
75                 dest = addr;
76             }
77             break;
78         }
79         default:
80             dest = addr;
81             break;
82     }
83 
84     return dest;
85 }
86 #if defined(__cplusplus)
87 }
88 #endif /* __cplusplus */
89 #endif /* _FSL_MEMORY_H_ */
90