1 /*
2  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016, 2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __USB_MISC_H__
10 #define __USB_MISC_H__
11 
12 /*******************************************************************************
13  * Definitions
14  ******************************************************************************/
15 
16 /*! @brief Define big endian */
17 #define USB_BIG_ENDIAN (0U)
18 /*! @brief Define little endian */
19 #define USB_LITTLE_ENDIAN (1U)
20 
21 /*! @brief Define current endian */
22 #ifndef ENDIANNESS
23 #define ENDIANNESS USB_LITTLE_ENDIAN
24 #endif
25 /*! @brief Define default timeout value */
26 #if (defined(USE_RTOS) && (USE_RTOS > 0))
27 #define USB_OSA_WAIT_TIMEOUT (osaWaitForever_c)
28 #else
29 #define USB_OSA_WAIT_TIMEOUT (0U)
30 #endif /* (defined(USE_RTOS) && (USE_RTOS > 0)) */
31 
32 /*! @brief Define USB printf */
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif /* __cplusplus */
36 
37 extern int DbgConsole_Printf(const char *fmt_s, ...);
38 
39 #if defined(__cplusplus)
40 }
41 #endif /* __cplusplus */
42 
43 /*! @brief Definition to select sdk or toolchain printf, scanf. */
44 #ifndef SDK_DEBUGCONSOLE
45 #define SDK_DEBUGCONSOLE 1U
46 #endif
47 
48 /*! @brief 0U: DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN, 1U: DEBUGCONSOLE_REDIRECT_TO_SDK, 2U: DEBUGCONSOLE_DISABLE*/
49 #if defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE == 0U)
50 #define usb_echo printf
51 #elif defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE == 1U)
52 #define usb_echo DbgConsole_Printf
53 #elif defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE == 2U)
USB_DbgConsole_Disabled(void)54 static inline int USB_DbgConsole_Disabled(void)
55 {
56     return -1;
57 }
58 #define usb_echo(...) USB_DbgConsole_Disabled()
59 #endif
60 
61 #if defined(__ICCARM__)
62 
63 #ifndef STRUCT_PACKED
64 #define STRUCT_PACKED __packed
65 #endif
66 
67 #ifndef STRUCT_UNPACKED
68 #define STRUCT_UNPACKED
69 #endif
70 
71 #elif defined(__GNUC__)
72 
73 #ifndef STRUCT_PACKED
74 #define STRUCT_PACKED
75 #endif
76 
77 #ifndef STRUCT_UNPACKED
78 #define STRUCT_UNPACKED __attribute__((__packed__))
79 #endif
80 
81 #elif defined(__CC_ARM) || (defined(__ARMCC_VERSION))
82 
83 #ifndef STRUCT_PACKED
84 #define STRUCT_PACKED _Pragma("pack(1U)")
85 #endif
86 
87 #ifndef STRUCT_UNPACKED
88 #define STRUCT_UNPACKED _Pragma("pack()")
89 #endif
90 
91 #elif (defined(__DSC__) || defined(__CW__))
92 
93 #ifndef STRUCT_PACKED
94 #define STRUCT_PACKED
95 #endif
96 
97 #ifndef STRUCT_UNPACKED
98 #define STRUCT_UNPACKED __attribute__((packed))
99 #endif
100 #endif
101 
102 #define USB_SHORT_GET_LOW(x)  (((uint16_t)x) & 0xFFU)
103 #define USB_SHORT_GET_HIGH(x) ((uint8_t)(((uint16_t)x) >> 8U) & 0xFFU)
104 
105 #define USB_LONG_GET_BYTE0(x) ((uint8_t)(((uint32_t)(x))) & 0xFFU)
106 #define USB_LONG_GET_BYTE1(x) ((uint8_t)(((uint32_t)(x)) >> 8U) & 0xFFU)
107 #define USB_LONG_GET_BYTE2(x) ((uint8_t)(((uint32_t)(x)) >> 16U) & 0xFFU)
108 #define USB_LONG_GET_BYTE3(x) ((uint8_t)(((uint32_t)(x)) >> 24U) & 0xFFU)
109 
110 #define USB_MEM4_ALIGN_MASK (0x03U)
111 
112 /* accessory macro */
113 #define USB_MEM4_ALIGN(n)  ((n + 3U) & (0xFFFFFFFCu))
114 #define USB_MEM32_ALIGN(n) ((n + 31U) & (0xFFFFFFE0u))
115 #define USB_MEM64_ALIGN(n) ((n + 63U) & (0xFFFFFFC0u))
116 
117 /* big/little endian */
118 #define SWAP2BYTE_CONST(n) ((((n)&0x00FFU) << 8U) | (((n)&0xFF00U) >> 8U))
119 #define SWAP4BYTE_CONST(n) \
120     ((((n)&0x000000FFU) << 24U) | (((n)&0x0000FF00U) << 8U) | (((n)&0x00FF0000U) >> 8U) | (((n)&0xFF000000U) >> 24U))
121 
122 #define USB_ASSIGN_VALUE_ADDRESS_LONG_BY_BYTE(n, m)      \
123     {                                                    \
124         *((uint8_t *)&(n))     = *((uint8_t *)&(m));     \
125         *((uint8_t *)&(n) + 1) = *((uint8_t *)&(m) + 1); \
126         *((uint8_t *)&(n) + 2) = *((uint8_t *)&(m) + 2); \
127         *((uint8_t *)&(n) + 3) = *((uint8_t *)&(m) + 3); \
128     }
129 
130 #define USB_ASSIGN_VALUE_ADDRESS_SHORT_BY_BYTE(n, m)     \
131     {                                                    \
132         *((uint8_t *)&(n))     = *((uint8_t *)&(m));     \
133         *((uint8_t *)&(n) + 1) = *((uint8_t *)&(m) + 1); \
134     }
135 
136 #define USB_ASSIGN_MACRO_VALUE_ADDRESS_LONG_BY_BYTE(n, m) \
137     {                                                     \
138         *((uint8_t *)&(n))     = (uint8_t)m;              \
139         *((uint8_t *)&(n) + 1) = (uint8_t)(m >> 8);       \
140         *((uint8_t *)&(n) + 2) = (uint8_t)(m >> 16);      \
141         *((uint8_t *)&(n) + 3) = (uint8_t)(m >> 24);      \
142     }
143 
144 #define USB_ASSIGN_MACRO_VALUE_ADDRESS_SHORT_BY_BYTE(n, m) \
145     {                                                      \
146         *((uint8_t *)&(n))     = (uint8_t)m;               \
147         *((uint8_t *)&(n) + 1) = (uint8_t)(m >> 8);        \
148     }
149 
150 #if (ENDIANNESS == USB_BIG_ENDIAN)
151 
152 #define USB_SHORT_TO_LITTLE_ENDIAN(n)   SWAP2BYTE_CONST(n)
153 #define USB_LONG_TO_LITTLE_ENDIAN(n)    SWAP4BYTE_CONST(n)
154 #define USB_SHORT_FROM_LITTLE_ENDIAN(n) SWAP2BYTE_CONST(n)
155 #define USB_LONG_FROM_LITTLE_ENDIAN(n)  SWAP2BYTE_CONST(n)
156 
157 #define USB_SHORT_TO_BIG_ENDIAN(n)   (n)
158 #define USB_LONG_TO_BIG_ENDIAN(n)    (n)
159 #define USB_SHORT_FROM_BIG_ENDIAN(n) (n)
160 #define USB_LONG_FROM_BIG_ENDIAN(n)  (n)
161 
162 #define USB_LONG_TO_LITTLE_ENDIAN_ADDRESS(n, m)             \
163     {                                                       \
164         m[3] = (uint8_t)((((uint32_t)(n)) >> 24U) & 0xFFU); \
165         m[2] = (uint8_t)((((uint32_t)(n)) >> 16U) & 0xFFU); \
166         m[1] = (uint8_t)((((uint32_t)(n)) >> 8U) & 0xFFU);  \
167         m[0] = (uint8_t)(((uint32_t)(n)) & 0xFFU);          \
168     }
169 
170 #define USB_LONG_FROM_LITTLE_ENDIAN_ADDRESS(n)                                                     \
171     ((uint32_t)((((uint32_t)n[3]) << 24U) | (((uint32_t)n[2]) << 16U) | (((uint32_t)n[1]) << 8U) | \
172                 (((uint32_t)n[0]) << 0U)))
173 
174 #define USB_LONG_TO_BIG_ENDIAN_ADDRESS(n, m)       \
175     {                                              \
176         m[0] = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
177         m[1] = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
178         m[2] = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
179         m[3] = (((uint32_t)(n)) & 0xFFU);          \
180     }
181 
182 #define USB_LONG_FROM_BIG_ENDIAN_ADDRESS(n)                                                        \
183     ((uint32_t)((((uint32_t)n[0]) << 24U) | (((uint32_t)n[1]) << 16U) | (((uint32_t)n[2]) << 8U) | \
184                 (((uint32_t)n[3]) << 0U)))
185 
186 #define USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(n, m)  \
187     {                                             \
188         m[1] = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
189         m[0] = (((uint16_t)(n)) & 0xFFU);         \
190     }
191 
192 #define USB_SHORT_FROM_LITTLE_ENDIAN_ADDRESS(n) ((uint16_t)((((uint16_t)n[1]) << 8U) | (((uint16_t)n[0]) << 0U)))
193 
194 #define USB_SHORT_TO_BIG_ENDIAN_ADDRESS(n, m)     \
195     {                                             \
196         m[0] = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
197         m[1] = (((uint16_t)(n)) & 0xFFU);         \
198     }
199 
200 #define USB_SHORT_FROM_BIG_ENDIAN_ADDRESS(n) ((uint16_t)((((uint16_t)n[0]) << 8U) | (((uint16_t)n[1]) << 0U)))
201 
202 #define USB_LONG_TO_LITTLE_ENDIAN_DATA(n, m)                         \
203     {                                                                \
204         *((uint8_t *)&(m) + 3) = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
205         *((uint8_t *)&(m) + 2) = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
206         *((uint8_t *)&(m) + 1) = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
207         *((uint8_t *)&(m) + 0) = (((uint32_t)(n)) & 0xFFU);          \
208     }
209 
210 #define USB_LONG_FROM_LITTLE_ENDIAN_DATA(n)                                                                 \
211     ((uint32_t)(((uint32_t)(*((uint8_t *)&(n) + 3)) << 24U) | ((uint32_t)(*((uint8_t *)&(n) + 2)) << 16U) | \
212                 ((uint32_t)(*((uint8_t *)&(n) + 1)) << 8U) | ((uint32_t)(*((uint8_t *)&(n))) << 0U)))
213 
214 #define USB_SHORT_TO_LITTLE_ENDIAN_DATA(n, m)                       \
215     {                                                               \
216         *((uint8_t *)&(m) + 1) = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
217         *((uint8_t *)&(m))     = ((((uint16_t)(n))) & 0xFFU);       \
218     }
219 
220 #define USB_SHORT_FROM_LITTLE_ENDIAN_DATA(n) \
221     ((uint16_t)((uint16_t)(*((uint8_t *)&(n) + 1)) << 8U) | ((uint16_t)(*((uint8_t *)&(n)))))
222 
223 #else
224 
225 #define USB_SHORT_TO_LITTLE_ENDIAN(n)   (n)
226 #define USB_LONG_TO_LITTLE_ENDIAN(n)    (n)
227 #define USB_SHORT_FROM_LITTLE_ENDIAN(n) (n)
228 #define USB_LONG_FROM_LITTLE_ENDIAN(n)  (n)
229 
230 #define USB_SHORT_TO_BIG_ENDIAN(n)   SWAP2BYTE_CONST(n)
231 #define USB_LONG_TO_BIG_ENDIAN(n)    SWAP4BYTE_CONST(n)
232 #define USB_SHORT_FROM_BIG_ENDIAN(n) SWAP2BYTE_CONST(n)
233 #define USB_LONG_FROM_BIG_ENDIAN(n)  SWAP4BYTE_CONST(n)
234 
235 #define USB_LONG_TO_LITTLE_ENDIAN_ADDRESS(n, m)             \
236     {                                                       \
237         m[3] = (uint8_t)((((uint32_t)(n)) >> 24U) & 0xFFU); \
238         m[2] = (uint8_t)((((uint32_t)(n)) >> 16U) & 0xFFU); \
239         m[1] = (uint8_t)((((uint32_t)(n)) >> 8U) & 0xFFU);  \
240         m[0] = (uint8_t)(((uint32_t)(n)) & 0xFFU);          \
241     }
242 
243 #define USB_LONG_FROM_LITTLE_ENDIAN_ADDRESS(n)                                                     \
244     ((uint32_t)((((uint32_t)n[3]) << 24U) | (((uint32_t)n[2]) << 16U) | (((uint32_t)n[1]) << 8U) | \
245                 (((uint32_t)n[0]) << 0U)))
246 
247 #define USB_LONG_TO_BIG_ENDIAN_ADDRESS(n, m)       \
248     {                                              \
249         m[0] = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
250         m[1] = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
251         m[2] = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
252         m[3] = (((uint32_t)(n)) & 0xFFU);          \
253     }
254 
255 #define USB_LONG_FROM_BIG_ENDIAN_ADDRESS(n)                                                        \
256     ((uint32_t)((((uint32_t)n[0]) << 24U) | (((uint32_t)n[1]) << 16U) | (((uint32_t)n[2]) << 8U) | \
257                 (((uint32_t)n[3]) << 0U)))
258 
259 #define USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(n, m)  \
260     {                                             \
261         m[1] = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
262         m[0] = (((uint16_t)(n)) & 0xFFU);         \
263     }
264 
265 #define USB_SHORT_FROM_LITTLE_ENDIAN_ADDRESS(n) ((uint16_t)((((uint16_t)n[1]) << 8U) | (((uint16_t)n[0]) << 0U)))
266 
267 #define USB_SHORT_TO_BIG_ENDIAN_ADDRESS(n, m)     \
268     {                                             \
269         m[0] = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
270         m[1] = (((uint16_t)(n)) & 0xFFU);         \
271     }
272 
273 #define USB_SHORT_FROM_BIG_ENDIAN_ADDRESS(n) ((uint16_t)((((uint16_t)n[0]) << 8U) | (((uint16_t)n[1]) << 0U)))
274 
275 #define USB_LONG_TO_LITTLE_ENDIAN_DATA(n, m)                         \
276     {                                                                \
277         *((uint8_t *)&(m) + 3) = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
278         *((uint8_t *)&(m) + 2) = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
279         *((uint8_t *)&(m) + 1) = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
280         *((uint8_t *)&(m) + 0) = (((uint32_t)(n)) & 0xFFU);          \
281     }
282 
283 #define USB_LONG_FROM_LITTLE_ENDIAN_DATA(n)                                                                 \
284     ((uint32_t)(((uint32_t)(*((uint8_t *)&(n) + 3)) << 24U) | ((uint32_t)(*((uint8_t *)&(n) + 2)) << 16U) | \
285                 ((uint32_t)(*((uint8_t *)&(n) + 1)) << 8U) | ((uint32_t)(*((uint8_t *)&(n))) << 0U)))
286 
287 #define USB_SHORT_TO_LITTLE_ENDIAN_DATA(n, m)                       \
288     {                                                               \
289         *((uint8_t *)&(m) + 1) = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
290         *((uint8_t *)&(m))     = ((((uint16_t)(n))) & 0xFFU);       \
291     }
292 
293 #define USB_SHORT_FROM_LITTLE_ENDIAN_DATA(n) \
294     ((uint16_t)(((uint16_t)(*(((uint8_t *)&(n)) + 1)) << 8U) | ((uint16_t)(*((uint8_t *)&(n))))))
295 
296 #endif
297 
298 /*
299  * The following MACROs (USB_GLOBAL, USB_BDT, USB_RAM_ADDRESS_ALIGNMENT, etc) are only used for USB device stack.
300  * The USB device global variables are put into the section m_usb_global and m_usb_bdt
301  * by using the MACRO USB_GLOBAL and USB_BDT. In this way, the USB device
302  * global variables can be linked into USB dedicated RAM by USB_STACK_USE_DEDICATED_RAM.
303  * The MACRO USB_STACK_USE_DEDICATED_RAM is used to decide the USB stack uses dedicated RAM or not. The value of
304  * the macro can be set as 0, USB_STACK_DEDICATED_RAM_TYPE_BDT_GLOBAL, or USB_STACK_DEDICATED_RAM_TYPE_BDT.
305  * The MACRO USB_STACK_DEDICATED_RAM_TYPE_BDT_GLOBAL means USB device global variables, including USB_BDT and
306  * USB_GLOBAL, are put into the USB dedicated RAM. This feature can only be enabled when the USB dedicated RAM
307  * is not less than 2K Bytes.
308  * The MACRO USB_STACK_DEDICATED_RAM_TYPE_BDT means USB device global variables, only including USB_BDT, are put
309  * into the USB dedicated RAM, the USB_GLOBAL will be put into .bss section. This feature is used for some SOCs,
310  * the USB dedicated RAM size is not more than 512 Bytes.
311  */
312 #define USB_STACK_DEDICATED_RAM_TYPE_BDT_GLOBAL 1
313 #define USB_STACK_DEDICATED_RAM_TYPE_BDT        2
314 
315 #if defined(__ICCARM__)
316 
317 #define USB_WEAK_VAR __attribute__((weak))
318 #define USB_WEAK_FUN __attribute__((weak))
319 /* disable misra 19.13 */
320 _Pragma("diag_suppress=Pm120")
321 #define USB_ALIGN_PRAGMA(x) _Pragma(#x)
322     _Pragma("diag_default=Pm120")
323 
324 #define USB_RAM_ADDRESS_ALIGNMENT(n) USB_ALIGN_PRAGMA(data_alignment = n)
325         _Pragma("diag_suppress=Pm120")
326 #define USB_LINK_SECTION_PART(str)  _Pragma(#str)
327 #define USB_LINK_DMA_INIT_DATA(sec) USB_LINK_SECTION_PART(location = #sec)
328 #define USB_LINK_USB_GLOBAL         _Pragma("location = \"m_usb_global\"")
329 #define USB_LINK_USB_BDT            _Pragma("location = \"m_usb_bdt\"")
330 #define USB_LINK_USB_GLOBAL_BSS
331 #define USB_LINK_USB_BDT_BSS
332             _Pragma("diag_default=Pm120")
333 #define USB_LINK_DMA_NONINIT_DATA      _Pragma("location = \"m_usb_dma_noninit_data\"")
334 #define USB_LINK_NONCACHE_NONINIT_DATA _Pragma("location = \"NonCacheable\"")
335 #elif defined(__CC_ARM) || (defined(__ARMCC_VERSION))
336 
337 #define USB_WEAK_VAR                 __attribute__((weak))
338 #define USB_WEAK_FUN                 __attribute__((weak))
339 #define USB_RAM_ADDRESS_ALIGNMENT(n) __attribute__((aligned(n)))
340 #define USB_LINK_DMA_INIT_DATA(sec)  __attribute__((section(#sec)))
341 #if defined(__CC_ARM)
342 #define USB_LINK_USB_GLOBAL __attribute__((section("m_usb_global"))) __attribute__((zero_init))
343 #else
344 #define USB_LINK_USB_GLOBAL __attribute__((section(".bss.m_usb_global")))
345 #endif
346 #if defined(__CC_ARM)
347 #define USB_LINK_USB_BDT __attribute__((section("m_usb_bdt"))) __attribute__((zero_init))
348 #else
349 #define USB_LINK_USB_BDT __attribute__((section(".bss.m_usb_bdt")))
350 #endif
351 #define USB_LINK_USB_GLOBAL_BSS
352 #define USB_LINK_USB_BDT_BSS
353 #if defined(__CC_ARM)
354 #define USB_LINK_DMA_NONINIT_DATA __attribute__((section("m_usb_dma_noninit_data"))) __attribute__((zero_init))
355 #else
356 #define USB_LINK_DMA_NONINIT_DATA __attribute__((section(".bss.m_usb_dma_noninit_data")))
357 #endif
358 #if defined(__CC_ARM)
359 #define USB_LINK_NONCACHE_NONINIT_DATA __attribute__((section("NonCacheable"))) __attribute__((zero_init))
360 #else
361 #define USB_LINK_NONCACHE_NONINIT_DATA __attribute__((section(".bss.NonCacheable")))
362 #endif
363 
364 #elif defined(__GNUC__)
365 
366 #define USB_WEAK_VAR                 __attribute__((weak))
367 #define USB_WEAK_FUN                 __attribute__((weak))
368 #define USB_RAM_ADDRESS_ALIGNMENT(n) __attribute__((aligned(n)))
369 #define USB_LINK_DMA_INIT_DATA(sec)  __attribute__((section(#sec)))
370 #define USB_LINK_USB_GLOBAL          __attribute__((section("m_usb_global, \"aw\", %nobits @")))
371 #define USB_LINK_USB_BDT             __attribute__((section("m_usb_bdt, \"aw\", %nobits @")))
372 #define USB_LINK_USB_GLOBAL_BSS
373 #define USB_LINK_USB_BDT_BSS
374 #define USB_LINK_DMA_NONINIT_DATA      __attribute__((section("m_usb_dma_noninit_data, \"aw\", %nobits @")))
375 #define USB_LINK_NONCACHE_NONINIT_DATA __attribute__((section("NonCacheable, \"aw\", %nobits @")))
376 
377 #elif (defined(__DSC__) && defined(__CW__))
378 #define MAX(a, b)                    (((a) > (b)) ? (a) : (b))
379 #define USB_WEAK_VAR                 __attribute__((weak))
380 #define USB_WEAK_FUN                 __attribute__((weak))
381 #define USB_RAM_ADDRESS_ALIGNMENT(n) __attribute__((aligned(n)))
382 #define USB_LINK_USB_BDT_BSS
383 #define USB_LINK_USB_GLOBAL_BSS
384 #else
385 #error The tool-chain is not supported.
386 #endif
387 
388 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
389     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
390 
391 #if ((defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)) && (defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)))
392 #define USB_CACHE_LINESIZE MAX(FSL_FEATURE_L2CACHE_LINESIZE_BYTE, FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
393 #elif (defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE))
394 #define USB_CACHE_LINESIZE MAX(FSL_FEATURE_L2CACHE_LINESIZE_BYTE, 0)
395 #elif (defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE))
396 #define USB_CACHE_LINESIZE MAX(0, FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
397 #else
398 #define USB_CACHE_LINESIZE 4U
399 #endif
400 
401 #else
402 #define USB_CACHE_LINESIZE 4U
403 #endif
404 
405 #if (USB_CACHE_LINESIZE > 4U)
406 #define USB_DATA_ALIGN_SIZE USB_CACHE_LINESIZE
407 #else
408 /* Change the USB_DATA_ALIGN_SIZE to 4, For the lpcip3511 driver, the lpcip3511 driver will do the memcpy for the
409    transfer buffer that is not in the USB dedicated RAM or not aligned to 64-byte boundaries. Hence the changes do not
410    bring the risk and improve the RAM usage rate but cause the lower perfromance. If requiring a higher performance on
411    the lpcip3511 platform, please change the macro to 64 and put the transfer buffer into the USB dedicated RAM. */
412 #define USB_DATA_ALIGN_SIZE 4U
413 #endif
414 
415 /* Due to the change of USB_DATA_ALIGN_SIZE from 64 to 4 on the lpcip3511 platform, the size of variables defined by
416    using this marco may be smaller on the lpcip3511 platform. If users don't want the lpcip3511 driver to do memcpy,
417    please use the macro to define the transfer buffer and change the USB_DATA_ALIGN_SIZE to 64 and put the transfer
418    buffer into the USB dedicated RAM. */
419 #define USB_DATA_ALIGN_SIZE_MULTIPLE(n) (((n) + USB_DATA_ALIGN_SIZE - 1U) & (~(USB_DATA_ALIGN_SIZE - 1U)))
420 
421 #if defined(USB_STACK_USE_DEDICATED_RAM) && (USB_STACK_USE_DEDICATED_RAM == USB_STACK_DEDICATED_RAM_TYPE_BDT_GLOBAL)
422 
423 #define USB_GLOBAL USB_LINK_USB_GLOBAL
424 #define USB_BDT    USB_LINK_USB_BDT
425 
426 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
427     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
428 #define USB_DMA_DATA_NONINIT_SUB USB_LINK_DMA_NONINIT_DATA
429 #define USB_DMA_DATA_INIT_SUB    USB_LINK_DMA_INIT_DATA(m_usb_dma_init_data)
430 #define USB_CONTROLLER_DATA      USB_LINK_NONCACHE_NONINIT_DATA
431 #else
432 #if (defined(DATA_SECTION_IS_CACHEABLE) && (DATA_SECTION_IS_CACHEABLE))
433 #define USB_DMA_DATA_NONINIT_SUB USB_LINK_NONCACHE_NONINIT_DATA
434 #define USB_DMA_DATA_INIT_SUB    USB_LINK_DMA_INIT_DATA(NonCacheable.init)
435 #define USB_CONTROLLER_DATA      USB_LINK_NONCACHE_NONINIT_DATA
436 #else
437 #define USB_DMA_DATA_NONINIT_SUB
438 #define USB_DMA_DATA_INIT_SUB
439 #define USB_CONTROLLER_DATA USB_LINK_USB_GLOBAL
440 #endif
441 #endif
442 
443 #elif defined(USB_STACK_USE_DEDICATED_RAM) && (USB_STACK_USE_DEDICATED_RAM == USB_STACK_DEDICATED_RAM_TYPE_BDT)
444 
445 #define USB_BDT USB_LINK_USB_BDT
446 
447 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
448     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
449 #define USB_GLOBAL               USB_LINK_DMA_NONINIT_DATA
450 #define USB_DMA_DATA_NONINIT_SUB USB_LINK_DMA_NONINIT_DATA
451 #define USB_DMA_DATA_INIT_SUB    USB_LINK_DMA_INIT_DATA(m_usb_dma_init_data)
452 #define USB_CONTROLLER_DATA      USB_LINK_NONCACHE_NONINIT_DATA
453 #else
454 #if (defined(DATA_SECTION_IS_CACHEABLE) && (DATA_SECTION_IS_CACHEABLE))
455 #define USB_GLOBAL               USB_LINK_NONCACHE_NONINIT_DATA
456 #define USB_DMA_DATA_NONINIT_SUB USB_LINK_NONCACHE_NONINIT_DATA
457 #define USB_DMA_DATA_INIT_SUB    USB_LINK_DMA_INIT_DATA(NonCacheable.init)
458 #define USB_CONTROLLER_DATA      USB_LINK_NONCACHE_NONINIT_DATA
459 #else
460 #define USB_GLOBAL USB_LINK_USB_GLOBAL_BSS
461 #define USB_DMA_DATA_NONINIT_SUB
462 #define USB_DMA_DATA_INIT_SUB
463 #define USB_CONTROLLER_DATA
464 #endif
465 #endif
466 
467 #else
468 
469 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
470     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
471 
472 #define USB_GLOBAL               USB_LINK_DMA_NONINIT_DATA
473 #define USB_BDT                  USB_LINK_NONCACHE_NONINIT_DATA
474 #define USB_DMA_DATA_NONINIT_SUB USB_LINK_DMA_NONINIT_DATA
475 #define USB_DMA_DATA_INIT_SUB    USB_LINK_DMA_INIT_DATA(m_usb_dma_init_data)
476 #define USB_CONTROLLER_DATA      USB_LINK_NONCACHE_NONINIT_DATA
477 
478 #else
479 
480 #if (defined(DATA_SECTION_IS_CACHEABLE) && (DATA_SECTION_IS_CACHEABLE))
481 #define USB_GLOBAL               USB_LINK_NONCACHE_NONINIT_DATA
482 #define USB_BDT                  USB_LINK_NONCACHE_NONINIT_DATA
483 #define USB_DMA_DATA_NONINIT_SUB USB_LINK_NONCACHE_NONINIT_DATA
484 #define USB_DMA_DATA_INIT_SUB    USB_LINK_DMA_INIT_DATA(NonCacheable.init)
485 #define USB_CONTROLLER_DATA      USB_LINK_NONCACHE_NONINIT_DATA
486 #else
487 #define USB_GLOBAL USB_LINK_USB_GLOBAL_BSS
488 #define USB_BDT    USB_LINK_USB_BDT_BSS
489 #define USB_DMA_DATA_NONINIT_SUB
490 #define USB_DMA_DATA_INIT_SUB
491 #define USB_CONTROLLER_DATA
492 #endif
493 
494 #endif
495 
496 #endif
497 
498 #define USB_DMA_NONINIT_DATA_ALIGN(n) USB_RAM_ADDRESS_ALIGNMENT(n) USB_DMA_DATA_NONINIT_SUB
499 #define USB_DMA_INIT_DATA_ALIGN(n)    USB_RAM_ADDRESS_ALIGNMENT(n) USB_DMA_DATA_INIT_SUB
500 
501 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
502     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
503 #define USB_DMA_DATA_NONCACHEABLE USB_LINK_NONCACHE_NONINIT_DATA
504 
505 #else
506 #define USB_DMA_DATA_NONCACHEABLE
507 #endif
508 
509 #define USB_GLOBAL_DEDICATED_RAM USB_LINK_USB_GLOBAL
510 
511 /* #define USB_RAM_ADDRESS_NONCACHEREG_ALIGNMENT(n, var) AT_NONCACHEABLE_SECTION_ALIGN(var, n) */
512 /* #define USB_RAM_ADDRESS_NONCACHEREG(var) AT_NONCACHEABLE_SECTION(var) */
513 
514 #endif /* __USB_MISC_H__ */
515