xref: /freebsd/sys/contrib/ncsw/inc/ncsw_ext.h (revision c2c014f24c10f90d85126ac5fbd4d8524de32b1c)
1 /* Copyright (c) 2008-2012 Freescale Semiconductor, Inc
2  * All rights reserved.
3  *
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.
14  *
15  *
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
19  * later version.
20  *
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.
31  */
32 
33 
34 /**************************************************************************//**
35  @File          ncsw_ext.h
36 
37  @Description   General NetCommSw Standard Definitions
38 *//***************************************************************************/
39 
40 #ifndef __NCSW_EXT_H
41 #define __NCSW_EXT_H
42 
43 
44 #include "memcpy_ext.h"
45 
46 #define WRITE_BLOCK                 IOMemSet32   /* include memcpy_ext.h */
47 #define COPY_BLOCK                  Mem2IOCpy32  /* include memcpy_ext.h */
48 
49 #define PTR_TO_UINT(_ptr)           ((uintptr_t)(_ptr))
50 #define UINT_TO_PTR(_val)           ((void*)(uintptr_t)(_val))
51 
52 #define PTR_MOVE(_ptr, _offset)     (void*)((uint8_t*)(_ptr) + (_offset))
53 
54 
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))
59 
60 /* Little-Endian access macros */
61 
62 #define WRITE_UINT16_LE(arg, data) \
63         WRITE_UINT16((arg), SwapUint16(data))
64 
65 #define WRITE_UINT32_LE(arg, data) \
66         WRITE_UINT32((arg), SwapUint32(data))
67 
68 #define WRITE_UINT64_LE(arg, data) \
69         WRITE_UINT64((arg), SwapUint64(data))
70 
71 #define GET_UINT16_LE(arg) \
72         SwapUint16(GET_UINT16(arg))
73 
74 #define GET_UINT32_LE(arg) \
75         SwapUint32(GET_UINT32(arg))
76 
77 #define GET_UINT64_LE(arg) \
78         SwapUint64(GET_UINT64(arg))
79 
80 /* Write and Read again macros */
81 #define WRITE_UINT_SYNC(size, arg, data)    \
82     do {                                    \
83         WRITE_UINT##size((arg), (data));    \
84         CORE_MemoryBarrier();               \
85     } while (0)
86 
87 #define WRITE_UINT8_SYNC(arg, data)     WRITE_UINT_SYNC(8, (arg), (data))
88 
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))
91 
92 #define MAKE_UINT64(high32, low32)      (((uint64_t)high32 << 32) | (low32))
93 
94 
95 /*----------------------*/
96 /* Miscellaneous macros */
97 /*----------------------*/
98 
99 #define UNUSED(_x)		((void)(_x))
100 
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 */
105 
106 #ifndef NO_IRQ
107 #define NO_IRQ		(0)
108 #endif
109 #define NCSW_MASTER_ID      (0)
110 
111 /* Macro for checking if a number is a power of 2 */
112 #define POWER_OF_2(n)   (!((n) & ((n)-1)))
113 
114 /* Macro for calculating log of base 2 */
115 #define LOG2(num, log2Num)      \
116     do                          \
117     {                           \
118         uint64_t tmp = (num);   \
119         log2Num = 0;            \
120         while (tmp > 1)         \
121         {                       \
122             log2Num++;          \
123             tmp >>= 1;          \
124         }                       \
125     } while (0)
126 
127 #define NEXT_POWER_OF_2(_num, _nextPow) \
128 do                                      \
129 {                                       \
130     if (POWER_OF_2(_num))               \
131         _nextPow = (_num);              \
132     else                                \
133     {                                   \
134         uint64_t tmp = (_num);          \
135         _nextPow = 1;                   \
136         while (tmp)                     \
137         {                               \
138             _nextPow <<= 1;             \
139             tmp >>= 1;                  \
140         }                               \
141     }                                   \
142 } while (0)
143 
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))
146 
147 /* Round up a number to be a multiple of a second number */
148 #define ROUND_UP(x,y)   ((((x) + (y) - 1) / (y)) * (y))
149 
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))
155 
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))
159 
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))
163 
164 /* Min, Max macros */
165 #define IN_RANGE(min,val,max) ((min)<=(val) && (val)<=(max))
166 
167 #define ABS(a)  ((a<0)?(a*-1):a)
168 
169 #if !(defined(ARRAY_SIZE))
170 #define ARRAY_SIZE(arr)   (sizeof(arr) / sizeof((arr)[0]))
171 #endif /* !defined(ARRAY_SIZE) */
172 
173 
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
179 
180 #define HALF_WORD_ALIGNED       0x00000001
181 #define WORD_ALIGNED            0x00000003
182 #define DOUBLE_WORD_ALIGNED     0x00000007
183 #define BURST_ALIGNED           0x0000001f
184 #ifndef IS_ALIGNED
185 #define IS_ALIGNED(n,align)     (!((uint32_t)(n) & (align - 1)))
186 #endif /* IS_ALIGNED */
187 
188 
189 #define LAST_BUF        1
190 #define FIRST_BUF       2
191 #define SINGLE_BUF      (LAST_BUF | FIRST_BUF)
192 #define MIDDLE_BUF      4
193 
194 #define ARRAY_END       -1
195 
196 #define ILLEGAL_BASE    (~0)
197 
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 };
200 
201 
202 /**************************************************************************//**
203  @Description   Timers operation mode
204 *//***************************************************************************/
205 typedef enum e_TimerMode
206 {
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. */
214 } e_TimerMode;
215 
216 
217 /**************************************************************************//**
218  @Description   Enumeration (bit flags) of communication modes (Transmit,
219                 receive or both).
220 *//***************************************************************************/
221 typedef enum e_CommMode
222 {
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 */
227 } e_CommMode;
228 
229 /**************************************************************************//**
230  @Description   General Diagnostic Mode
231 *//***************************************************************************/
232 typedef enum e_DiagMode
233 {
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 */
242 } e_DiagMode;
243 
244 /**************************************************************************//**
245  @Description   Possible RxStore callback responses.
246 *//***************************************************************************/
247 typedef enum e_RxStoreResponse
248 {
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. */
259 } e_RxStoreResponse;
260 
261 
262 /**************************************************************************//**
263  @Description   General Handle
264 *//***************************************************************************/
265 typedef void *      t_Handle;   /**< handle, used as object's descriptor */
266 
267 /**************************************************************************//**
268  @Description   MUTEX type
269 *//***************************************************************************/
270 typedef uint32_t    t_Mutex;
271 
272 /**************************************************************************//**
273  @Description   Error Code.
274 
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;
281 
282 /**************************************************************************//**
283  @Description   General prototype of interrupt service routine (ISR).
284 
285  @Param[in]     handle - Optional handle of the module handling the interrupt.
286 
287  @Return        None
288  *//***************************************************************************/
289 typedef void (t_Isr)(t_Handle handle);
290 
291 /**************************************************************************//**
292  @Anchor        mem_attr
293 
294  @Collection    Memory Attributes
295 
296                 Various attributes of memory partitions. These values may be
297                 or'ed together to create a mask of all memory attributes.
298  @{
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 */
305 
306 /* @} */
307 
308 
309 /**************************************************************************//**
310  @Function      t_GetBufFunction
311 
312  @Description   User callback function called by driver to get data buffer.
313 
314                 User provides this function. Driver invokes it.
315 
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
319 
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);
324 
325 /**************************************************************************//**
326  @Function      t_PutBufFunction
327 
328  @Description   User callback function called by driver to return data buffer.
329 
330                 User provides this function. Driver invokes it.
331 
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
335                                   the returned buffer
336 
337  @Return        E_OK on success; Error code otherwise
338  *//***************************************************************************/
339 typedef t_Error (t_PutBufFunction)(t_Handle h_BufferPool,
340                                    uint8_t  *p_Buffer,
341                                    t_Handle h_BufContext);
342 
343 /**************************************************************************//**
344  @Function      t_PhysToVirt
345 
346  @Description   Translates a physical address to the matching virtual address.
347 
348  @Param[in]     addr - The physical address to translate.
349 
350  @Return        Virtual address.
351 *//***************************************************************************/
352 typedef void * t_PhysToVirt(physAddress_t addr);
353 
354 /**************************************************************************//**
355  @Function      t_VirtToPhys
356 
357  @Description   Translates a virtual address to the matching physical address.
358 
359  @Param[in]     addr - The virtual address to translate.
360 
361  @Return        Physical address.
362 *//***************************************************************************/
363 typedef physAddress_t t_VirtToPhys(void *addr);
364 
365 /**************************************************************************//**
366  @Description   Buffer Pool Information Structure.
367 *//***************************************************************************/
368 typedef struct t_BufferPoolInfo
369 {
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) */
374 
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 */
379 } t_BufferPoolInfo;
380 
381 
382 /**************************************************************************//**
383  @Description   User callback function called by driver when transmit completed.
384 
385                 User provides this function. Driver invokes it.
386 
387  @Param[in]     h_App           - Application's handle, as was provided to the
388                                   driver by the user
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,
397                                 uint32_t    queueId,
398                                 uint8_t     *p_Data,
399                                 t_Handle    h_BufContext,
400                                 uint16_t    status,
401                                 uint32_t    flags);
402 
403 /**************************************************************************//**
404  @Description   User callback function called by driver with receive data.
405 
406                 User provides this function. Driver invokes it.
407 
408  @Param[in]     h_App           - Application's handle, as was provided to the
409                                   driver by the user
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
418 
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,
424                                               uint32_t  queueId,
425                                               uint8_t   *p_Data,
426                                               t_Handle  h_BufContext,
427                                               uint32_t  length,
428                                               uint16_t  status,
429                                               uint8_t   position,
430                                               uint32_t  flags);
431 
432 
433 #endif /* __NCSW_EXT_H */
434