1/* Copyright (c) 2008-2011 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: crc_mac_addr_ext.h                             */
36/*                                                      */
37/* Description:                                         */
38/*    Define a macro that calculate the crc value of    */
39/*    an Ethernet MAC address (48 bitd address          */
40/*------------------------------------------------------*/
41
42#ifndef __crc_mac_addr_ext_h
43#define __crc_mac_addr_ext_h
44
45#include "std_ext.h"
46
47
48static uint32_t crc_table[256] =
49{
50    0x00000000,
51    0x77073096,
52    0xee0e612c,
53    0x990951ba,
54    0x076dc419,
55    0x706af48f,
56    0xe963a535,
57    0x9e6495a3,
58    0x0edb8832,
59    0x79dcb8a4,
60    0xe0d5e91e,
61    0x97d2d988,
62    0x09b64c2b,
63    0x7eb17cbd,
64    0xe7b82d07,
65    0x90bf1d91,
66    0x1db71064,
67    0x6ab020f2,
68    0xf3b97148,
69    0x84be41de,
70    0x1adad47d,
71    0x6ddde4eb,
72    0xf4d4b551,
73    0x83d385c7,
74    0x136c9856,
75    0x646ba8c0,
76    0xfd62f97a,
77    0x8a65c9ec,
78    0x14015c4f,
79    0x63066cd9,
80    0xfa0f3d63,
81    0x8d080df5,
82    0x3b6e20c8,
83    0x4c69105e,
84    0xd56041e4,
85    0xa2677172,
86    0x3c03e4d1,
87    0x4b04d447,
88    0xd20d85fd,
89    0xa50ab56b,
90    0x35b5a8fa,
91    0x42b2986c,
92    0xdbbbc9d6,
93    0xacbcf940,
94    0x32d86ce3,
95    0x45df5c75,
96    0xdcd60dcf,
97    0xabd13d59,
98    0x26d930ac,
99    0x51de003a,
100    0xc8d75180,
101    0xbfd06116,
102    0x21b4f4b5,
103    0x56b3c423,
104    0xcfba9599,
105    0xb8bda50f,
106    0x2802b89e,
107    0x5f058808,
108    0xc60cd9b2,
109    0xb10be924,
110    0x2f6f7c87,
111    0x58684c11,
112    0xc1611dab,
113    0xb6662d3d,
114    0x76dc4190,
115    0x01db7106,
116    0x98d220bc,
117    0xefd5102a,
118    0x71b18589,
119    0x06b6b51f,
120    0x9fbfe4a5,
121    0xe8b8d433,
122    0x7807c9a2,
123    0x0f00f934,
124    0x9609a88e,
125    0xe10e9818,
126    0x7f6a0dbb,
127    0x086d3d2d,
128    0x91646c97,
129    0xe6635c01,
130    0x6b6b51f4,
131    0x1c6c6162,
132    0x856530d8,
133    0xf262004e,
134    0x6c0695ed,
135    0x1b01a57b,
136    0x8208f4c1,
137    0xf50fc457,
138    0x65b0d9c6,
139    0x12b7e950,
140    0x8bbeb8ea,
141    0xfcb9887c,
142    0x62dd1ddf,
143    0x15da2d49,
144    0x8cd37cf3,
145    0xfbd44c65,
146    0x4db26158,
147    0x3ab551ce,
148    0xa3bc0074,
149    0xd4bb30e2,
150    0x4adfa541,
151    0x3dd895d7,
152    0xa4d1c46d,
153    0xd3d6f4fb,
154    0x4369e96a,
155    0x346ed9fc,
156    0xad678846,
157    0xda60b8d0,
158    0x44042d73,
159    0x33031de5,
160    0xaa0a4c5f,
161    0xdd0d7cc9,
162    0x5005713c,
163    0x270241aa,
164    0xbe0b1010,
165    0xc90c2086,
166    0x5768b525,
167    0x206f85b3,
168    0xb966d409,
169    0xce61e49f,
170    0x5edef90e,
171    0x29d9c998,
172    0xb0d09822,
173    0xc7d7a8b4,
174    0x59b33d17,
175    0x2eb40d81,
176    0xb7bd5c3b,
177    0xc0ba6cad,
178    0xedb88320,
179    0x9abfb3b6,
180    0x03b6e20c,
181    0x74b1d29a,
182    0xead54739,
183    0x9dd277af,
184    0x04db2615,
185    0x73dc1683,
186    0xe3630b12,
187    0x94643b84,
188    0x0d6d6a3e,
189    0x7a6a5aa8,
190    0xe40ecf0b,
191    0x9309ff9d,
192    0x0a00ae27,
193    0x7d079eb1,
194    0xf00f9344,
195    0x8708a3d2,
196    0x1e01f268,
197    0x6906c2fe,
198    0xf762575d,
199    0x806567cb,
200    0x196c3671,
201    0x6e6b06e7,
202    0xfed41b76,
203    0x89d32be0,
204    0x10da7a5a,
205    0x67dd4acc,
206    0xf9b9df6f,
207    0x8ebeeff9,
208    0x17b7be43,
209    0x60b08ed5,
210    0xd6d6a3e8,
211    0xa1d1937e,
212    0x38d8c2c4,
213    0x4fdff252,
214    0xd1bb67f1,
215    0xa6bc5767,
216    0x3fb506dd,
217    0x48b2364b,
218    0xd80d2bda,
219    0xaf0a1b4c,
220    0x36034af6,
221    0x41047a60,
222    0xdf60efc3,
223    0xa867df55,
224    0x316e8eef,
225    0x4669be79,
226    0xcb61b38c,
227    0xbc66831a,
228    0x256fd2a0,
229    0x5268e236,
230    0xcc0c7795,
231    0xbb0b4703,
232    0x220216b9,
233    0x5505262f,
234    0xc5ba3bbe,
235    0xb2bd0b28,
236    0x2bb45a92,
237    0x5cb36a04,
238    0xc2d7ffa7,
239    0xb5d0cf31,
240    0x2cd99e8b,
241    0x5bdeae1d,
242    0x9b64c2b0,
243    0xec63f226,
244    0x756aa39c,
245    0x026d930a,
246    0x9c0906a9,
247    0xeb0e363f,
248    0x72076785,
249    0x05005713,
250    0x95bf4a82,
251    0xe2b87a14,
252    0x7bb12bae,
253    0x0cb61b38,
254    0x92d28e9b,
255    0xe5d5be0d,
256    0x7cdcefb7,
257    0x0bdbdf21,
258    0x86d3d2d4,
259    0xf1d4e242,
260    0x68ddb3f8,
261    0x1fda836e,
262    0x81be16cd,
263    0xf6b9265b,
264    0x6fb077e1,
265    0x18b74777,
266    0x88085ae6,
267    0xff0f6a70,
268    0x66063bca,
269    0x11010b5c,
270    0x8f659eff,
271    0xf862ae69,
272    0x616bffd3,
273    0x166ccf45,
274    0xa00ae278,
275    0xd70dd2ee,
276    0x4e048354,
277    0x3903b3c2,
278    0xa7672661,
279    0xd06016f7,
280    0x4969474d,
281    0x3e6e77db,
282    0xaed16a4a,
283    0xd9d65adc,
284    0x40df0b66,
285    0x37d83bf0,
286    0xa9bcae53,
287    0xdebb9ec5,
288    0x47b2cf7f,
289    0x30b5ffe9,
290    0xbdbdf21c,
291    0xcabac28a,
292    0x53b39330,
293    0x24b4a3a6,
294    0xbad03605,
295    0xcdd70693,
296    0x54de5729,
297    0x23d967bf,
298    0xb3667a2e,
299    0xc4614ab8,
300    0x5d681b02,
301    0x2a6f2b94,
302    0xb40bbe37,
303    0xc30c8ea1,
304    0x5a05df1b,
305    0x2d02ef8d
306};
307
308
309#define GET_MAC_ADDR_CRC(addr, crc)             \
310{                                               \
311    uint32_t    i;                              \
312    uint8_t     data;                           \
313                                                \
314    /* CRC calculation */                       \
315    crc = 0xffffffff;                           \
316    for (i=0; i < 6; i++)                       \
317    {                                           \
318        data = (uint8_t)(addr >> ((5-i)*8));    \
319        crc = crc^data;                         \
320        crc = crc_table[crc&0xff] ^ (crc>>8);   \
321    }                                           \
322}                                               \
323
324/*    Define a macro for getting the mirrored value of      */
325/*    a byte size number. (0x11010011 --> 0x11001011)       */
326/*    Sometimes the mirrored value of the CRC is required   */
327static __inline__ uint8_t GetMirror(uint8_t n)
328{
329    uint8_t mirror[16] =
330        {
331            0x00,
332            0x08,
333            0x04,
334            0x0c,
335            0x02,
336            0x0a,
337            0x06,
338            0x0e,
339            0x01,
340            0x09,
341            0x05,
342            0x0d,
343            0x03,
344            0x0b,
345            0x07,
346            0x0f
347        };
348    return ((uint8_t)(((mirror[n & 0x0f] << 4) | (mirror[n >> 4]))));
349}
350
351static __inline__ uint32_t GetMirror32(uint32_t n)
352{
353    return (((uint32_t)GetMirror((uint8_t)(n))<<24) |
354            ((uint32_t)GetMirror((uint8_t)(n>>8))<<16) |
355            ((uint32_t)GetMirror((uint8_t)(n>>16))<<8) |
356            ((uint32_t)GetMirror((uint8_t)(n>>24))));
357}
358
359#define MIRROR      GetMirror
360#define MIRROR_32   GetMirror32
361
362
363#endif /* __crc_mac_addr_ext_h */
364