1 /*
2  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017, 2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #include "fsl_crc.h"
9 
10 /*******************************************************************************
11  * Definitions
12  ******************************************************************************/
13 
14 /* Component ID definition, used by tools. */
15 #ifndef FSL_COMPONENT_ID
16 #define FSL_COMPONENT_ID "platform.drivers.crc"
17 #endif
18 
19 /*! @internal @brief Has data register with name CRC. */
20 #if defined(FSL_FEATURE_CRC_HAS_CRC_REG) && FSL_FEATURE_CRC_HAS_CRC_REG
21 #define DATA   CRC
22 #define DATALL CRCLL
23 #endif
24 
25 #if defined(CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT) && CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT
26 /* @brief Default user configuration structure for CRC-16-CCITT */
27 #define CRC_DRIVER_DEFAULT_POLYNOMIAL 0x1021U
28 /*< CRC-16-CCIT polynomial x**16 + x**12 + x**5 + x**0 */
29 #define CRC_DRIVER_DEFAULT_SEED 0xFFFFU
30 /*< Default initial checksum */
31 #define CRC_DRIVER_DEFAULT_REFLECT_IN false
32 /*< Default is no transpose */
33 #define CRC_DRIVER_DEFAULT_REFLECT_OUT false
34 /*< Default is transpose bytes */
35 #define CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM false
36 /*< Default is without complement of CRC data register read data */
37 #define CRC_DRIVER_DEFAULT_CRC_BITS kCrcBits16
38 /*< Default is 16-bit CRC protocol */
39 #define CRC_DRIVER_DEFAULT_CRC_RESULT kCrcFinalChecksum
40 /*< Default is resutl type is final checksum */
41 #endif /* CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT */
42 
43 #if defined(CRC_RSTS)
44 #define CRC_RESETS_ARRAY CRC_RSTS
45 #endif
46 
47 /*! @brief CRC type of transpose of read write data */
48 typedef enum _crc_transpose_type
49 {
50     kCrcTransposeNone         = 0U, /*! No transpose  */
51     kCrcTransposeBits         = 1U, /*! Tranpose bits in bytes  */
52     kCrcTransposeBitsAndBytes = 2U, /*! Transpose bytes and bits in bytes */
53     kCrcTransposeBytes        = 3U, /*! Transpose bytes */
54 } crc_transpose_type_t;
55 
56 /*!
57  * @brief CRC module configuration.
58  *
59  * This structure holds the configuration for the CRC module.
60  */
61 typedef struct _crc_module_config
62 {
63     uint32_t polynomial;                 /*!< CRC Polynomial, MSBit first.@n
64                                               Example polynomial: 0x1021 = 1_0000_0010_0001 = x^12+x^5+1 */
65     uint32_t seed;                       /*!< Starting checksum value */
66     crc_transpose_type_t readTranspose;  /*!< Type of transpose when reading CRC result. */
67     crc_transpose_type_t writeTranspose; /*!< Type of transpose when writing CRC input data. */
68     bool complementChecksum;             /*!< True if the result shall be complement of the actual checksum. */
69     crc_bits_t crcBits;                  /*!< Selects 16- or 32- bit CRC protocol. */
70 } crc_module_config_t;
71 
72 /*******************************************************************************
73  * Prototypes
74  ******************************************************************************/
75 #if defined(CRC_RESETS_ARRAY)
76 /*!
77  * @brief Get instance number for CRC module.
78  *
79  * @param base CRC peripheral base address
80  */
81 static uint32_t CRC_GetInstance(CRC_Type *base);
82 #endif
83 /*******************************************************************************
84  * Variables
85  ******************************************************************************/
86 #if defined(CRC_RESETS_ARRAY)
87 static CRC_Type *const s_crcBases[] = CRC_BASE_PTRS;
88 
89 /* Reset array */
90 static const reset_ip_name_t s_crcResets[] = CRC_RESETS_ARRAY;
91 #endif
92 
93 /*******************************************************************************
94  * Code
95  ******************************************************************************/
96 #if defined(CRC_RESETS_ARRAY)
CRC_GetInstance(CRC_Type * base)97 static uint32_t CRC_GetInstance(CRC_Type *base)
98 {
99     uint32_t instance;
100 
101     /* Find the instance index from base address mappings. */
102     for (instance = 0; instance < ARRAY_SIZE(s_crcBases); instance++)
103     {
104         if (MSDK_REG_SECURE_ADDR(s_crcBases[instance]) == MSDK_REG_SECURE_ADDR(base))
105         {
106             break;
107         }
108     }
109 
110     assert(instance < ARRAY_SIZE(s_crcBases));
111 
112     return instance;
113 }
114 #endif
115 
116 /*!
117  * @brief Returns transpose type for CRC protocol reflect in parameter.
118  *
119  * This functions helps to set writeTranspose member of crc_config_t structure. Reflect in is CRC protocol parameter.
120  *
121  * @param enable True or false for the selected CRC protocol Reflect In (refin) parameter.
122  */
CRC_GetTransposeTypeFromReflectIn(bool enable)123 static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectIn(bool enable)
124 {
125     return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeBytes);
126 }
127 
128 /*!
129  * @brief Returns transpose type for CRC protocol reflect out parameter.
130  *
131  * This functions helps to set readTranspose member of crc_config_t structure. Reflect out is CRC protocol parameter.
132  *
133  * @param enable True or false for the selected CRC protocol Reflect Out (refout) parameter.
134  */
CRC_GetTransposeTypeFromReflectOut(bool enable)135 static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectOut(bool enable)
136 {
137     return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeNone);
138 }
139 
140 /*!
141  * @brief Starts checksum computation.
142  *
143  * Configures the CRC module for the specified CRC protocol. @n
144  * Starts the checksum computation by writing the seed value
145  *
146  * @param base CRC peripheral address.
147  * @param config Pointer to protocol configuration structure.
148  */
CRC_ConfigureAndStart(CRC_Type * base,const crc_module_config_t * config)149 static void CRC_ConfigureAndStart(CRC_Type *base, const crc_module_config_t *config)
150 {
151     uint32_t crcControl;
152 
153     /* pre-compute value for CRC control registger based on user configuraton without WAS field */
154     crcControl = 0U | CRC_CTRL_TOT(config->writeTranspose) | CRC_CTRL_TOTR(config->readTranspose) |
155                  CRC_CTRL_FXOR(config->complementChecksum) | CRC_CTRL_TCRC(config->crcBits);
156 
157     /* make sure the control register is clear - WAS is deasserted, and protocol is set */
158     base->CTRL = crcControl;
159 
160     /* write polynomial register */
161     base->GPOLY = config->polynomial;
162 
163     /* write pre-computed control register value along with WAS to start checksum computation */
164     base->CTRL = crcControl | CRC_CTRL_WAS(true);
165 
166     /* write seed (initial checksum) */
167     base->DATA = config->seed;
168 
169     /* deassert WAS by writing pre-computed CRC control register value */
170     base->CTRL = crcControl;
171 }
172 
173 /*!
174  * @brief Starts final checksum computation.
175  *
176  * Configures the CRC module for the specified CRC protocol. @n
177  * Starts final checksum computation by writing the seed value.
178  * @note CRC_Get16bitResult() or CRC_Get32bitResult() return final checksum
179  *       (output reflection and xor functions are applied).
180  *
181  * @param base CRC peripheral address.
182  * @param protocolConfig Pointer to protocol configuration structure.
183  */
CRC_SetProtocolConfig(CRC_Type * base,const crc_config_t * protocolConfig)184 static void CRC_SetProtocolConfig(CRC_Type *base, const crc_config_t *protocolConfig)
185 {
186     crc_module_config_t moduleConfig;
187     /* convert protocol to CRC peripheral module configuration, prepare for final checksum */
188     moduleConfig.polynomial         = protocolConfig->polynomial;
189     moduleConfig.seed               = protocolConfig->seed;
190     moduleConfig.readTranspose      = CRC_GetTransposeTypeFromReflectOut(protocolConfig->reflectOut);
191     moduleConfig.writeTranspose     = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn);
192     moduleConfig.complementChecksum = protocolConfig->complementChecksum;
193     moduleConfig.crcBits            = protocolConfig->crcBits;
194 
195     CRC_ConfigureAndStart(base, &moduleConfig);
196 }
197 
198 /*!
199  * @brief Starts intermediate checksum computation.
200  *
201  * Configures the CRC module for the specified CRC protocol. @n
202  * Starts intermediate checksum computation by writing the seed value.
203  * @note CRC_Get16bitResult() or CRC_Get32bitResult() return intermediate checksum (raw data register value).
204  *
205  * @param base CRC peripheral address.
206  * @param protocolConfig Pointer to protocol configuration structure.
207  */
CRC_SetRawProtocolConfig(CRC_Type * base,const crc_config_t * protocolConfig)208 static void CRC_SetRawProtocolConfig(CRC_Type *base, const crc_config_t *protocolConfig)
209 {
210     crc_module_config_t moduleConfig;
211     /* convert protocol to CRC peripheral module configuration, prepare for intermediate checksum */
212     moduleConfig.polynomial = protocolConfig->polynomial;
213     moduleConfig.seed       = protocolConfig->seed;
214     moduleConfig.readTranspose =
215         kCrcTransposeNone; /* intermediate checksum does no transpose of data register read value */
216     moduleConfig.writeTranspose     = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn);
217     moduleConfig.complementChecksum = false; /* intermediate checksum does no xor of data register read value */
218     moduleConfig.crcBits            = protocolConfig->crcBits;
219 
220     CRC_ConfigureAndStart(base, &moduleConfig);
221 }
222 
223 /*!
224  * brief Enables and configures the CRC peripheral module.
225  *
226  * This function enables the clock gate in the SIM module for the CRC peripheral.
227  * It also configures the CRC module and starts a checksum computation by writing the seed.
228  *
229  * param base CRC peripheral address.
230  * param config CRC module configuration structure.
231  */
CRC_Init(CRC_Type * base,const crc_config_t * config)232 void CRC_Init(CRC_Type *base, const crc_config_t *config)
233 {
234 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
235     /* ungate clock */
236     CLOCK_EnableClock(kCLOCK_Crc0);
237 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
238 
239 #if defined(CRC_RESETS_ARRAY)
240     RESET_ReleasePeripheralReset(s_crcResets[CRC_GetInstance(base)]);
241 #endif
242 
243     /* configure CRC module and write the seed */
244     if (config->crcResult == kCrcFinalChecksum)
245     {
246         CRC_SetProtocolConfig(base, config);
247     }
248     else
249     {
250         CRC_SetRawProtocolConfig(base, config);
251     }
252 }
253 
254 /*!
255  * brief Loads default values to the CRC protocol configuration structure.
256  *
257  * Loads default values to the CRC protocol configuration structure. The default values are as follows.
258  * code
259  *   config->polynomial = 0x1021;
260  *   config->seed = 0xFFFF;
261  *   config->reflectIn = false;
262  *   config->reflectOut = false;
263  *   config->complementChecksum = false;
264  *   config->crcBits = kCrcBits16;
265  *   config->crcResult = kCrcFinalChecksum;
266  * endcode
267  *
268  * param config CRC protocol configuration structure.
269  */
CRC_GetDefaultConfig(crc_config_t * config)270 void CRC_GetDefaultConfig(crc_config_t *config)
271 {
272     /* Initializes the configure structure to zero. */
273     (void)memset(config, 0, sizeof(*config));
274 
275     static const crc_config_t crc16ccit = {
276         CRC_DRIVER_DEFAULT_POLYNOMIAL,          CRC_DRIVER_DEFAULT_SEED,
277         CRC_DRIVER_DEFAULT_REFLECT_IN,          CRC_DRIVER_DEFAULT_REFLECT_OUT,
278         CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM, CRC_DRIVER_DEFAULT_CRC_BITS,
279         CRC_DRIVER_DEFAULT_CRC_RESULT,
280     };
281 
282     *config = crc16ccit;
283 }
284 
285 /*!
286  * brief Writes data to the CRC module.
287  *
288  * Writes input data buffer bytes to the CRC data register.
289  * The configured type of transpose is applied.
290  *
291  * param base CRC peripheral address.
292  * param data Input data stream, MSByte in data[0].
293  * param dataSize Size in bytes of the input data buffer.
294  */
CRC_WriteData(CRC_Type * base,const uint8_t * data,size_t dataSize)295 void CRC_WriteData(CRC_Type *base, const uint8_t *data, size_t dataSize)
296 {
297     const uint32_t *data32;
298 
299     /* 8-bit reads and writes till source address is aligned 4 bytes */
300     while ((0U != dataSize) && (0U != ((uint32_t)data & 3U)))
301     {
302         base->ACCESS8BIT.DATALL = *data;
303         data++;
304         dataSize--;
305     }
306 
307     /* use 32-bit reads and writes as long as possible */
308     data32 = (const uint32_t *)(uint32_t)data;
309     while (dataSize >= sizeof(uint32_t))
310     {
311         base->DATA = *data32;
312         data32++;
313         dataSize -= sizeof(uint32_t);
314     }
315 
316     data = (const uint8_t *)data32;
317 
318     /* 8-bit reads and writes till end of data buffer */
319     while (dataSize != 0U)
320     {
321         base->ACCESS8BIT.DATALL = *data;
322         data++;
323         dataSize--;
324     }
325 }
326 
327 /*!
328  * brief Reads the 32-bit checksum from the CRC module.
329  *
330  * Reads the CRC data register (either an intermediate or the final checksum).
331  * The configured type of transpose and complement is applied.
332  *
333  * param base CRC peripheral address.
334  * return An intermediate or the final 32-bit checksum, after configured transpose and complement operations.
335  */
CRC_Get32bitResult(CRC_Type * base)336 uint32_t CRC_Get32bitResult(CRC_Type *base)
337 {
338     return base->DATA;
339 }
340 
341 /*!
342  * brief Reads a 16-bit checksum from the CRC module.
343  *
344  * Reads the CRC data register (either an intermediate or the final checksum).
345  * The configured type of transpose and complement is applied.
346  *
347  * param base CRC peripheral address.
348  * return An intermediate or the final 16-bit checksum, after configured transpose and complement operations.
349  */
CRC_Get16bitResult(CRC_Type * base)350 uint16_t CRC_Get16bitResult(CRC_Type *base)
351 {
352     uint32_t retval;
353     uint32_t totr; /* type of transpose read bitfield */
354 
355     retval = base->DATA;
356     totr   = (base->CTRL & CRC_CTRL_TOTR_MASK) >> CRC_CTRL_TOTR_SHIFT;
357 
358     /* check transpose type to get 16-bit out of 32-bit register */
359     if (totr >= 2U)
360     {
361         /* transpose of bytes for read is set, the result CRC is in CRC_DATA[HU:HL] */
362         retval &= 0xFFFF0000U;
363         retval = retval >> 16U;
364     }
365     else
366     {
367         /* no transpose of bytes for read, the result CRC is in CRC_DATA[LU:LL] */
368         retval &= 0x0000FFFFU;
369     }
370     return (uint16_t)retval;
371 }
372