1 /* Copyright (c) 2008-2012 Freescale Semiconductor, Inc
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of Freescale Semiconductor nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
16 * ALTERNATIVELY, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") as published by the Free Software
18 * Foundation, either version 2 of that License or (at your option) any
21 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 /**************************************************************************//**
37 @Description General NetCommSw Standard Definitions
38 *//***************************************************************************/
44 #include "memcpy_ext.h"
46 #define WRITE_BLOCK IOMemSet32 /* include memcpy_ext.h */
47 #define COPY_BLOCK Mem2IOCpy32 /* include memcpy_ext.h */
49 #define PTR_TO_UINT(_ptr) ((uintptr_t)(_ptr))
50 #define UINT_TO_PTR(_val) ((void*)(uintptr_t)(_val))
52 #define PTR_MOVE(_ptr, _offset) (void*)((uint8_t*)(_ptr) + (_offset))
55 #define WRITE_UINT8_UINT24(arg, data08, data24) \
56 WRITE_UINT32(arg,((uint32_t)(data08)<<24)|((uint32_t)(data24)&0x00FFFFFF))
57 #define WRITE_UINT24_UINT8(arg, data24, data08) \
58 WRITE_UINT32(arg,((uint32_t)(data24)<< 8)|((uint32_t)(data08)&0x000000FF))
60 /* Little-Endian access macros */
62 #define WRITE_UINT16_LE(arg, data) \
63 WRITE_UINT16((arg), SwapUint16(data))
65 #define WRITE_UINT32_LE(arg, data) \
66 WRITE_UINT32((arg), SwapUint32(data))
68 #define WRITE_UINT64_LE(arg, data) \
69 WRITE_UINT64((arg), SwapUint64(data))
71 #define GET_UINT16_LE(arg) \
72 SwapUint16(GET_UINT16(arg))
74 #define GET_UINT32_LE(arg) \
75 SwapUint32(GET_UINT32(arg))
77 #define GET_UINT64_LE(arg) \
78 SwapUint64(GET_UINT64(arg))
80 /* Write and Read again macros */
81 #define WRITE_UINT_SYNC(size, arg, data) \
83 WRITE_UINT##size((arg), (data)); \
84 CORE_MemoryBarrier(); \
87 #define WRITE_UINT8_SYNC(arg, data) WRITE_UINT_SYNC(8, (arg), (data))
89 #define WRITE_UINT16_SYNC(arg, data) WRITE_UINT_SYNC(16, (arg), (data))
90 #define WRITE_UINT32_SYNC(arg, data) WRITE_UINT_SYNC(32, (arg), (data))
92 #define MAKE_UINT64(high32, low32) (((uint64_t)high32 << 32) | (low32))
95 /*----------------------*/
96 /* Miscellaneous macros */
97 /*----------------------*/
99 #define UNUSED(_x) ((void)(_x))
101 #define KILOBYTE 0x400UL /* 1024 */
102 #define MEGABYTE (KILOBYTE * KILOBYTE) /* 1024*1024 */
103 #define GIGABYTE ((uint64_t)(KILOBYTE * MEGABYTE)) /* 1024*1024*1024 */
104 #define TERABYTE ((uint64_t)(KILOBYTE * GIGABYTE)) /* 1024*1024*1024*1024 */
109 #define NCSW_MASTER_ID (0)
111 /* Macro for checking if a number is a power of 2 */
112 #define POWER_OF_2(n) (!((n) & ((n)-1)))
114 /* Macro for calculating log of base 2 */
115 #define LOG2(num, log2Num) \
118 uint64_t tmp = (num); \
127 #define NEXT_POWER_OF_2(_num, _nextPow) \
130 if (POWER_OF_2(_num)) \
134 uint64_t tmp = (_num); \
144 /* Ceiling division - not the fastest way, but safer in terms of overflow */
145 #define DIV_CEIL(x,y) (((x)/(y)) + (((((x)/(y))*(y)) == (x)) ? 0 : 1))
147 /* Round up a number to be a multiple of a second number */
148 #define ROUND_UP(x,y) ((((x) + (y) - 1) / (y)) * (y))
150 /* Timing macro for converting usec units to number of ticks. */
151 /* (number of usec * clock_Hz) / 1,000,000) - since */
152 /* clk is in MHz units, no division needed. */
153 #define USEC_TO_CLK(usec,clk) ((usec) * (clk))
154 #define CYCLES_TO_USEC(cycles,clk) ((cycles) / (clk))
156 /* Timing macros for converting between nsec units and number of clocks. */
157 #define NSEC_TO_CLK(nsec,clk) DIV_CEIL(((nsec) * (clk)), 1000)
158 #define CYCLES_TO_NSEC(cycles,clk) (((cycles) * 1000) / (clk))
160 /* Timing macros for converting between psec units and number of clocks. */
161 #define PSEC_TO_CLK(psec,clk) DIV_CEIL(((psec) * (clk)), 1000000)
162 #define CYCLES_TO_PSEC(cycles,clk) (((cycles) * 1000000) / (clk))
164 /* Min, Max macros */
165 #define IN_RANGE(min,val,max) ((min)<=(val) && (val)<=(max))
167 #define ABS(a) ((a<0)?(a*-1):a)
169 #if !(defined(ARRAY_SIZE))
170 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
171 #endif /* !defined(ARRAY_SIZE) */
174 /* possible alignments */
175 #define HALF_WORD_ALIGNMENT 2
176 #define WORD_ALIGNMENT 4
177 #define DOUBLE_WORD_ALIGNMENT 8
178 #define BURST_ALIGNMENT 32
180 #define HALF_WORD_ALIGNED 0x00000001
181 #define WORD_ALIGNED 0x00000003
182 #define DOUBLE_WORD_ALIGNED 0x00000007
183 #define BURST_ALIGNED 0x0000001f
185 #define IS_ALIGNED(n,align) (!((uint32_t)(n) & (align - 1)))
186 #endif /* IS_ALIGNED */
191 #define SINGLE_BUF (LAST_BUF | FIRST_BUF)
196 #define ILLEGAL_BASE (~0)
198 #define BUF_POSITION(first, last) state[(!!(last))<<1 | !!(first)]
199 #define DECLARE_POSITION static uint8_t state[4] = { (uint8_t)MIDDLE_BUF, (uint8_t)FIRST_BUF, (uint8_t)LAST_BUF, (uint8_t)SINGLE_BUF };
202 /**************************************************************************//**
203 @Description Timers operation mode
204 *//***************************************************************************/
205 typedef enum e_TimerMode
207 e_TIMER_MODE_INVALID = 0,
208 e_TIMER_MODE_FREE_RUN, /**< Free run - counter continues to increase
209 after reaching the reference value. */
210 e_TIMER_MODE_PERIODIC, /**< Periodic - counter restarts counting from 0
211 after reaching the reference value. */
212 e_TIMER_MODE_SINGLE /**< Single (one-shot) - counter stops counting
213 after reaching the reference value. */
217 /**************************************************************************//**
218 @Description Enumeration (bit flags) of communication modes (Transmit,
220 *//***************************************************************************/
221 typedef enum e_CommMode
223 e_COMM_MODE_NONE = 0, /**< No transmit/receive communication */
224 e_COMM_MODE_RX = 1, /**< Only receive communication */
225 e_COMM_MODE_TX = 2, /**< Only transmit communication */
226 e_COMM_MODE_RX_AND_TX = 3 /**< Both transmit and receive communication */
229 /**************************************************************************//**
230 @Description General Diagnostic Mode
231 *//***************************************************************************/
232 typedef enum e_DiagMode
234 e_DIAG_MODE_NONE = 0, /**< Normal operation; no diagnostic mode */
235 e_DIAG_MODE_CTRL_LOOPBACK, /**< Loopback in the controller */
236 e_DIAG_MODE_CHIP_LOOPBACK, /**< Loopback in the chip but not in the
237 controller; e.g. IO-pins, SerDes, etc. */
238 e_DIAG_MODE_PHY_LOOPBACK, /**< Loopback in the external PHY */
239 e_DIAG_MODE_EXT_LOOPBACK, /**< Loopback in the external line (beyond the PHY) */
240 e_DIAG_MODE_CTRL_ECHO, /**< Echo incoming data by the controller */
241 e_DIAG_MODE_PHY_ECHO /**< Echo incoming data by the PHY */
244 /**************************************************************************//**
245 @Description Possible RxStore callback responses.
246 *//***************************************************************************/
247 typedef enum e_RxStoreResponse
249 e_RX_STORE_RESPONSE_PAUSE /**< Pause invoking callback with received data;
250 in polling mode, start again invoking callback
251 only next time user invokes the receive routine;
252 in interrupt mode, start again invoking callback
253 only next time a receive event triggers an interrupt;
254 in all cases, received data that are pending are not
255 lost, rather, their processing is temporarily deferred;
256 in all cases, received data are processed in the order
257 in which they were received. */
258 , e_RX_STORE_RESPONSE_CONTINUE /**< Continue invoking callback with received data. */
262 /**************************************************************************//**
263 @Description General Handle
264 *//***************************************************************************/
265 typedef void * t_Handle; /**< handle, used as object's descriptor */
267 /**************************************************************************//**
268 @Description MUTEX type
269 *//***************************************************************************/
270 typedef uint32_t t_Mutex;
272 /**************************************************************************//**
273 @Description Error Code.
275 The high word of the error code is the code of the software
276 module (driver). The low word is the error type (e_ErrorType).
277 To get the values from the error code, use GET_ERROR_TYPE()
278 and GET_ERROR_MODULE().
279 *//***************************************************************************/
280 typedef uint32_t t_Error;
282 /**************************************************************************//**
283 @Description General prototype of interrupt service routine (ISR).
285 @Param[in] handle - Optional handle of the module handling the interrupt.
288 *//***************************************************************************/
289 typedef void (t_Isr)(t_Handle handle);
291 /**************************************************************************//**
294 @Collection Memory Attributes
296 Various attributes of memory partitions. These values may be
297 or'ed together to create a mask of all memory attributes.
299 *//***************************************************************************/
300 #define MEMORY_ATTR_CACHEABLE 0x00000001
301 /**< Memory is cacheable */
302 #define MEMORY_ATTR_QE_2ND_BUS_ACCESS 0x00000002
303 /**< Memory can be accessed by QUICC Engine
304 through its secondary bus interface */
309 /**************************************************************************//**
310 @Function t_GetBufFunction
312 @Description User callback function called by driver to get data buffer.
314 User provides this function. Driver invokes it.
316 @Param[in] h_BufferPool - A handle to buffer pool manager
317 @Param[out] p_BufContextHandle - Returns the user's private context that
318 should be associated with the buffer
320 @Return Pointer to data buffer, NULL if error
321 *//***************************************************************************/
322 typedef uint8_t * (t_GetBufFunction)(t_Handle h_BufferPool,
323 t_Handle *p_BufContextHandle);
325 /**************************************************************************//**
326 @Function t_PutBufFunction
328 @Description User callback function called by driver to return data buffer.
330 User provides this function. Driver invokes it.
332 @Param[in] h_BufferPool - A handle to buffer pool manager
333 @Param[in] p_Buffer - A pointer to buffer to return
334 @Param[in] h_BufContext - The user's private context associated with
337 @Return E_OK on success; Error code otherwise
338 *//***************************************************************************/
339 typedef t_Error (t_PutBufFunction)(t_Handle h_BufferPool,
341 t_Handle h_BufContext);
343 /**************************************************************************//**
344 @Function t_PhysToVirt
346 @Description Translates a physical address to the matching virtual address.
348 @Param[in] addr - The physical address to translate.
350 @Return Virtual address.
351 *//***************************************************************************/
352 typedef void * t_PhysToVirt(physAddress_t addr);
354 /**************************************************************************//**
355 @Function t_VirtToPhys
357 @Description Translates a virtual address to the matching physical address.
359 @Param[in] addr - The virtual address to translate.
361 @Return Physical address.
362 *//***************************************************************************/
363 typedef physAddress_t t_VirtToPhys(void *addr);
365 /**************************************************************************//**
366 @Description Buffer Pool Information Structure.
367 *//***************************************************************************/
368 typedef struct t_BufferPoolInfo
370 t_Handle h_BufferPool; /**< A handle to the buffer pool manager */
371 t_GetBufFunction *f_GetBuf; /**< User callback to get a free buffer */
372 t_PutBufFunction *f_PutBuf; /**< User callback to return a buffer */
373 uint16_t bufferSize; /**< Buffer size (in bytes) */
375 t_PhysToVirt *f_PhysToVirt; /**< User callback to translate pool buffers
376 physical addresses to virtual addresses */
377 t_VirtToPhys *f_VirtToPhys; /**< User callback to translate pool buffers
378 virtual addresses to physical addresses */
382 /**************************************************************************//**
383 @Description User callback function called by driver when transmit completed.
385 User provides this function. Driver invokes it.
387 @Param[in] h_App - Application's handle, as was provided to the
389 @Param[in] queueId - Transmit queue ID
390 @Param[in] p_Data - Pointer to the data buffer
391 @Param[in] h_BufContext - The user's private context associated with
392 the given data buffer
393 @Param[in] status - Transmit status and errors
394 @Param[in] flags - Driver-dependent information
395 *//***************************************************************************/
396 typedef void (t_TxConfFunction)(t_Handle h_App,
399 t_Handle h_BufContext,
403 /**************************************************************************//**
404 @Description User callback function called by driver with receive data.
406 User provides this function. Driver invokes it.
408 @Param[in] h_App - Application's handle, as was provided to the
410 @Param[in] queueId - Receive queue ID
411 @Param[in] p_Data - Pointer to the buffer with received data
412 @Param[in] h_BufContext - The user's private context associated with
413 the given data buffer
414 @Param[in] length - Length of received data
415 @Param[in] status - Receive status and errors
416 @Param[in] position - Position of buffer in frame
417 @Param[in] flags - Driver-dependent information
419 @Retval e_RX_STORE_RESPONSE_CONTINUE - order the driver to continue Rx
420 operation for all ready data.
421 @Retval e_RX_STORE_RESPONSE_PAUSE - order the driver to stop Rx operation.
422 *//***************************************************************************/
423 typedef e_RxStoreResponse (t_RxStoreFunction)(t_Handle h_App,
426 t_Handle h_BufContext,
433 #endif /* __NCSW_EXT_H */