1 /*
2  *  Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *
8  *    Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  *    Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the
14  *    distribution.
15  *
16  *    Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 //*****************************************************************************
34 //
35 //  crc.c
36 //
37 //  Driver for the CRC module.
38 //
39 //*****************************************************************************
40 
41 //*****************************************************************************
42 //
43 //! \addtogroup CRC_Cyclic_Redundancy_Check_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #include <stdbool.h>
49 #include <stdint.h>
50 #include "inc/hw_dthe.h"
51 #include "inc/hw_memmap.h"
52 #include "inc/hw_types.h"
53 #include "driverlib/crc.h"
54 #include "driverlib/debug.h"
55 
56 //*****************************************************************************
57 //
58 //! Set the configuration of CRC functionality with the EC module.
59 //!
60 //! \param ui32Base is the base address of the EC module.
61 //! \param ui32CRCConfig is the configuration of the CRC engine.
62 //!
63 //! This function configures the operation of the CRC engine within the EC
64 //! module.  The configuration is specified with the \e ui32CRCConfig argument.
65 //! It is the logical OR of any of the following options:
66 //!
67 //! CRC Initialization Value
68 //! - \b EC_CRC_CFG_INIT_SEED - Initialize with seed value
69 //! - \b EC_CRC_CFG_INIT_0 - Initialize to all '0s'
70 //! - \b EC_CRC_CFG_INIT_1 - Initialize to all '1s'
71 //!
72 //! Input Data Size
73 //! - \b EC_CRC_CFG_SIZE_8BIT - Input data size of 8 bits
74 //! - \b EC_CRC_CFG_SIZE_32BIT - Input data size of 32 bits
75 //!
76 //! Post Process Reverse/Inverse
77 //! - \b EC_CRC_CFG_RESINV - Result inverse enable
78 //! - \b EC_CRC_CFG_OBR - Output reverse enable
79 //!
80 //! Input Bit Reverse
81 //! - \b EC_CRC_CFG_IBR - Bit reverse enable
82 //!
83 //! Endian Control
84 //! - \b EC_CRC_CFG_ENDIAN_SBHW - Swap byte in half-word
85 //! - \b EC_CRC_CFG_ENDIAN_SHW - Swap half-word
86 //!
87 //! Operation Type
88 //! - \b EC_CRC_CFG_TYPE_P8005 - Polynomial 0x8005
89 //! - \b EC_CRC_CFG_TYPE_P1021 - Polynomial 0x1021
90 //! - \b EC_CRC_CFG_TYPE_P4C11DB7 - Polynomial 0x4C11DB7
91 //! - \b EC_CRC_CFG_TYPE_P1EDC6F41 - Polynomial 0x1EDC6F41
92 //! - \b EC_CRC_CFG_TYPE_TCPCHKSUM - TCP checksum
93 //!
94 //! \return None.
95 //
96 //*****************************************************************************
97 void
CRCConfigSet(uint32_t ui32Base,uint32_t ui32CRCConfig)98 CRCConfigSet(uint32_t ui32Base, uint32_t ui32CRCConfig)
99 {
100     //
101     // Check the arguments.
102     //
103     ASSERT(ui32Base == DTHE_BASE);
104     ASSERT((ui32CRCConfig & CRC_CFG_INIT_SEED) ||
105            (ui32CRCConfig & CRC_CFG_INIT_0) ||
106            (ui32CRCConfig & CRC_CFG_INIT_1) ||
107            (ui32CRCConfig & CRC_CFG_SIZE_8BIT) ||
108            (ui32CRCConfig & CRC_CFG_SIZE_32BIT) ||
109            (ui32CRCConfig & CRC_CFG_RESINV) ||
110            (ui32CRCConfig & CRC_CFG_OBR) ||
111            (ui32CRCConfig & CRC_CFG_IBR) ||
112            (ui32CRCConfig & CRC_CFG_ENDIAN_SBHW) ||
113            (ui32CRCConfig & CRC_CFG_ENDIAN_SHW) ||
114            (ui32CRCConfig & CRC_CFG_TYPE_P8005) ||
115            (ui32CRCConfig & CRC_CFG_TYPE_P1021) ||
116            (ui32CRCConfig & CRC_CFG_TYPE_P4C11DB7) ||
117            (ui32CRCConfig & CRC_CFG_TYPE_P1EDC6F41) ||
118            (ui32CRCConfig & CRC_CFG_TYPE_TCPCHKSUM));
119 
120     //
121     // Write the control register with the configuration.
122     //
123     HWREG(ui32Base + DTHE_O_CRC_CTRL) = ui32CRCConfig;
124 }
125 
126 //*****************************************************************************
127 //
128 //! Write the seed value for CRC operations in the EC module.
129 //!
130 //! \param ui32Base is the base address of the EC module.
131 //! \param ui32Seed is the seed value.
132 //!
133 //! This function writes the seed value for use with CRC operations in the
134 //! EC module.  This value is the start value for CRC operations.  If this
135 //! value is not written, then the residual seed from the previous operation
136 //! is used as the starting value.
137 //!
138 //! \note The seed must be written only if \b EC_CRC_CFG_INIT_SEED is
139 //! set with the CRCConfigSet() function.
140 //
141 //*****************************************************************************
142 void
CRCSeedSet(uint32_t ui32Base,uint32_t ui32Seed)143 CRCSeedSet(uint32_t ui32Base, uint32_t ui32Seed)
144 {
145     //
146     // Check the arguments.
147     //
148     ASSERT(ui32Base == DTHE_BASE);
149 
150     //
151     // Write the seed value to the seed register.
152     //
153     HWREG(ui32Base + DTHE_O_CRC_SEED) = ui32Seed;
154 }
155 
156 //*****************************************************************************
157 //
158 //! Write data into the EC module for CRC operations.
159 //!
160 //! \param ui32Base is the base address of the EC module.
161 //! \param ui32Data is the data to be written.
162 //!
163 //! This function writes either 8 or 32 bits of data into the EC module for
164 //! CRC operations.  The distinction between 8 and 32 bits of data is made
165 //! when the \b EC_CRC_CFG_SIZE_8BIT or \b EC_CRC_CFG_SIZE_32BIT flag
166 //! is set using the CRCConfigSet() function.
167 //!
168 //! When writing 8 bits of data, ensure the data is in the least signficant
169 //! byte position.  The remaining bytes should be written with zero.  For
170 //! example, when writing 0xAB, \e ui32Data should be 0x000000AB.
171 //!
172 //! \return None
173 //
174 //*****************************************************************************
175 void
CRCDataWrite(uint32_t ui32Base,uint32_t ui32Data)176 CRCDataWrite(uint32_t ui32Base, uint32_t ui32Data)
177 {
178     //
179     // Check the arguments.
180     //
181     ASSERT(ui32Base == DTHE_BASE);
182 
183     //
184     // Write the data
185     //
186     HWREG(DTHE_BASE + DTHE_O_CRC_DIN) = ui32Data;
187 }
188 
189 //*****************************************************************************
190 //
191 //! Reads the result of a CRC operation in the EC module.
192 //!
193 //! \param ui32Base is the base address of the EC module.
194 //!
195 //! This function reads either the unmodified CRC result or the post
196 //! processed CRC result from the EC module.  The post-processing options
197 //! are selectable through \b EC_CRC_CFG_RESINV and \b EC_CRC_CFG_OBR
198 //! parameters in the CRCConfigSet() function.
199 //!
200 //! \return The CRC result.
201 //
202 //*****************************************************************************
203 uint32_t
CRCResultRead(uint32_t ui32Base)204 CRCResultRead(uint32_t ui32Base)
205 {
206     //
207     // Check the arguments.
208     //
209     ASSERT(ui32Base == DTHE_BASE);
210 
211     //
212     // return value.
213     //
214     return(HWREG(DTHE_BASE + DTHE_O_CRC_RSLT_PP));
215 
216 }
217 
218 //*****************************************************************************
219 //
220 //! Process data to generate a CRC with the EC module.
221 //!
222 //! \param ui32Base is the base address of the EC module.
223 //! \param puiDataIn is a pointer to an array of data that is processed.
224 //! \param ui32DataLength is the number of data items that are processed
225 //! to produce the CRC.
226 //! \param ui32Config the config parameter to determine the CRC mode
227 //!
228 //! This function processes an array of data to produce a CRC result.
229 //! This function takes the CRC mode as the parameter.
230 //!
231 //! The data in the array pointed to be \e pui32DataIn is either an array
232 //! of bytes or an array or words depending on the selection of the input
233 //! data size options \b EC_CRC_CFG_SIZE_8BIT and
234 //! \b EC_CRC_CFG_SIZE_32BIT.
235 //!
236 //! This function returns either the unmodified CRC result or the
237 //! post- processed CRC result from the EC module.  The post-processing
238 //! options are selectable through \b EC_CRC_CFG_RESINV and
239 //! \b EC_CRC_CFG_OBR parameters.
240 //!
241 //! \return The CRC result.
242 //
243 //*****************************************************************************
244 uint32_t
CRCDataProcess(uint32_t ui32Base,void * puiDataIn,uint32_t ui32DataLength,uint32_t ui32Config)245 CRCDataProcess(uint32_t ui32Base, void *puiDataIn,
246                uint32_t ui32DataLength, uint32_t ui32Config)
247 {
248     uint8_t *pui8DataIn;
249     uint32_t *pui32DataIn;
250 
251     //
252     // Check the arguments.
253     //
254     ASSERT(ui32Base == DTHE_BASE);
255 
256     //
257     // See if the CRC is operating in 8-bit or 32-bit mode.
258     //
259     if(ui32Config & DTHE_CRC_CTRL_SIZE)
260     {
261         //
262         // The CRC is operating in 8-bit mode, so create an 8-bit pointer to
263         // the data.
264         //
265         pui8DataIn = (uint8_t *)puiDataIn;
266 
267         //
268         // Loop through the input data.
269         //
270         while(ui32DataLength--)
271         {
272             //
273             // Write the next data byte.
274             //
275             HWREG(ui32Base + DTHE_O_CRC_DIN) = *pui8DataIn++;
276         }
277     }
278     else
279     {
280         //
281         // The CRC is operating in 32-bit mode, so loop through the input data.
282         //
283     	pui32DataIn = (uint32_t *)puiDataIn;
284         while(ui32DataLength--)
285         {
286             //
287             // Write the next data word.
288             //
289             HWREG(ui32Base + DTHE_O_CRC_DIN) = *pui32DataIn++;
290         }
291     }
292 
293     //
294     // Return the result.
295     //
296     return(CRCResultRead(ui32Base));
297 }
298 
299 
300 
301 //*****************************************************************************
302 //
303 // Close the Doxygen group.
304 //! @}
305 //
306 //*****************************************************************************
307