crc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <kernel.h>
  7. #include <init.h>
  8. #include <device.h>
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <drivers/se/se.h>
  15. #include <soc.h>
  16. #include <logging/log.h>
  17. LOG_MODULE_REGISTER(crc, CONFIG_LOG_DEFAULT_LEVEL);
  18. #define CRC_FIFO_DEPTH 16
  19. #define CRC_TYPE_16 0
  20. #define CRC_TYPE_32 1
  21. #define POLY_04C11DB7 0
  22. #define POLY_1021 1
  23. #define POLY_8005 2
  24. /* CRC mode and paramter */
  25. struct crc_param_data {
  26. uint8_t crc_type;
  27. uint8_t poly;
  28. bool in_invert;
  29. bool out_invert;
  30. uint32_t out_xor;
  31. uint32_t inital_val;
  32. };
  33. /****************************************************************************
  34. * Name: crc_hw_calc
  35. *
  36. * Description:
  37. * hardware calculates crc result.
  38. *
  39. ****************************************************************************/
  40. static uint32_t crc_hw_calc(const void *in, size_t size)
  41. {
  42. size_t size_remain;
  43. size_t crc_len;
  44. uint32_t crc_ctrl_val;
  45. uint32_t crc_infifoctl_val;
  46. uint8_t *crc_in = (uint8_t *)in;
  47. uint32_t crc_out_val;
  48. /* set SE FIFO to CRC */
  49. sys_write32(SE_FIFOCTRL_CRC, SE_FIFOCTRL);
  50. /* disable DRQ */
  51. crc_infifoctl_val = sys_read32(CRC_INFIFOCTL);
  52. crc_infifoctl_val &= ~CRC_INFIFOCTL_DE_MASK;
  53. sys_write32(crc_infifoctl_val, CRC_INFIFOCTL);
  54. size_remain = size;
  55. while (size_remain > 0) {
  56. if (size_remain >= CRC_FIFO_DEPTH) {
  57. crc_len = CRC_FIFO_DEPTH;
  58. size_remain -= CRC_FIFO_DEPTH;
  59. } else {
  60. crc_len = size_remain;
  61. size_remain = 0;
  62. }
  63. sys_write32(crc_len, CRC_LEN);
  64. se_memcpy((void *)CRC_INFIFO,
  65. crc_in, crc_len, CPY_MEMU8_TO_FIFO);
  66. /* enable crc */
  67. crc_ctrl_val = sys_read32(CRC_CTRL);
  68. sys_write32(crc_ctrl_val | CRC_CTRL_EN, CRC_CTRL);
  69. /* wait crc calculation completed */
  70. while ((sys_read32(CRC_CTRL) & CRC_CTRL_END) == 0);
  71. /* Clear completed flag for next CRC */
  72. crc_ctrl_val = sys_read32(CRC_CTRL);
  73. crc_ctrl_val |= AES_CTRL_END;
  74. sys_write32(crc_ctrl_val, AES_CTRL);
  75. crc_in += crc_len;
  76. }
  77. crc_out_val = sys_read32(CRC_DATAOUT);
  78. /* enable CRC reset */
  79. crc_ctrl_val = sys_read32(CRC_CTRL);
  80. crc_ctrl_val |= CRC_CTRL_RESET;
  81. sys_write32(crc_ctrl_val, CRC_CTRL);
  82. /* Wait finished */
  83. while (sys_read32(CRC_CTRL) & CRC_CTRL_RESET);
  84. return crc_out_val;
  85. }
  86. /****************************************************************************
  87. * Name: crc_setup_mr
  88. *
  89. * Description:
  90. * set crc mode and parameter.
  91. *
  92. ****************************************************************************/
  93. static int crc_setup_mr(struct crc_param_data *param)
  94. {
  95. uint32_t regval = 0;
  96. sys_write32(regval, CRC_MODE);
  97. if (param->crc_type == CRC_TYPE_32)
  98. regval |= CRC_MODE_TYPE_CRC32;
  99. if (param->poly == POLY_8005)
  100. regval |= CRC_MODE_CRC16_POLY_8005;
  101. if (param->in_invert)
  102. regval |= CRC_MODE_IN_INV_TRUE;
  103. if (param->out_invert)
  104. regval |= CRC_MODE_OUT_INV_TRUE;
  105. sys_write32(regval | CRC_MODE_INITDATA_UPD, CRC_MODE);
  106. sys_write32(param->out_xor, CRC_DATAOUTXOR);
  107. sys_write32(param->inital_val, CRC_DATAINIT);
  108. return 0;
  109. }
  110. /****************************************************************************
  111. * Name: crc_init
  112. *
  113. * Description:
  114. * before using crc, we should init crc clock and related things.
  115. *
  116. ****************************************************************************/
  117. static int crc_init(void)
  118. {
  119. uint32_t clken0_val;
  120. clken0_val = sys_read32(CMU_DEVCLKEN0);
  121. /* check whether the CLOCK_ID_SE has been enabled or not */
  122. if (!(clken0_val & (0x1 << CLOCK_ID_SE))) {
  123. /* if not initialized, we should init it */
  124. /* SE CLK = HOSC/1 */
  125. sys_write32((0 << 8) | (0 << 0), CMU_SECCLK);
  126. /* enable SE controller clock */
  127. acts_clock_peripheral_enable(CLOCK_ID_SE);
  128. /* reset SE controller */
  129. acts_reset_peripheral(RESET_ID_SE);
  130. }
  131. /* enable CRC controller clock */
  132. sys_write32(CRC_CTRL_CLK_EN, CRC_CTRL);
  133. return 0;
  134. }
  135. /****************************************************************************
  136. * Name: crc_deinit
  137. *
  138. * Description:
  139. * Disable CRC for energy saving purpose.
  140. *
  141. ****************************************************************************/
  142. static void crc_deinit(void)
  143. {
  144. /* disable crc */
  145. sys_write32(0, CRC_CTRL);
  146. }
  147. /****************************************************************************
  148. * Name: crc
  149. *
  150. * Description:
  151. * get the CRC result.
  152. *
  153. ****************************************************************************/
  154. static uint32_t crc(struct crc_param_data *param, const unsigned char *ptr, unsigned int len)
  155. {
  156. uint32_t crc_val;
  157. k_mutex_lock(&se_lock, K_FOREVER);
  158. crc_init();
  159. crc_setup_mr(param);
  160. crc_val = crc_hw_calc(ptr, len);
  161. crc_deinit();
  162. k_mutex_unlock(&se_lock);
  163. return crc_val;
  164. }
  165. /****************************************************************************
  166. * Name: crc16_ibm
  167. *
  168. * Description:
  169. * get the CRC16-IBM result.
  170. *
  171. ****************************************************************************/
  172. uint16_t crc16_ibm(uint16_t inital_val, const unsigned char* ptr, unsigned int len)
  173. {
  174. struct crc_param_data param;
  175. param.crc_type = CRC_TYPE_16;
  176. param.poly = POLY_8005;
  177. param.in_invert = true;
  178. param.out_invert = true;
  179. param.out_xor = 0x0000;
  180. param.inital_val = inital_val;
  181. return crc(&param, ptr, len);
  182. }
  183. /****************************************************************************
  184. * Name: crc16_maxim
  185. *
  186. * Description:
  187. * get the CRC16-MAXIM result.
  188. *
  189. ****************************************************************************/
  190. uint16_t crc16_maxim(uint16_t inital_val, const unsigned char* ptr, unsigned int len)
  191. {
  192. struct crc_param_data param;
  193. param.crc_type = CRC_TYPE_16;
  194. param.poly = POLY_8005;
  195. param.in_invert = true;
  196. param.out_invert = true;
  197. param.out_xor = 0xFFFF;
  198. param.inital_val = inital_val;
  199. return crc(&param, ptr, len);
  200. }
  201. /****************************************************************************
  202. * Name: crc16_usb
  203. *
  204. * Description:
  205. * get the CRC16-USB result.
  206. *
  207. ****************************************************************************/
  208. uint16_t crc16_usb(uint16_t inital_val, const unsigned char* ptr, unsigned int len)
  209. {
  210. struct crc_param_data param;
  211. param.crc_type = CRC_TYPE_16;
  212. param.poly = POLY_8005;
  213. param.in_invert = true;
  214. param.out_invert = true;
  215. param.out_xor = 0xFFFF;
  216. param.inital_val = inital_val;
  217. return crc(&param, ptr, len);
  218. }
  219. /****************************************************************************
  220. * Name: crc16_modbus
  221. *
  222. * Description:
  223. * get the CRC16-MODBUS result.
  224. *
  225. ****************************************************************************/
  226. uint16_t crc16_modbus(uint16_t inital_val, const unsigned char* ptr, unsigned int len)
  227. {
  228. struct crc_param_data param;
  229. param.crc_type = CRC_TYPE_16;
  230. param.poly = POLY_8005;
  231. param.in_invert = true;
  232. param.out_invert = true;
  233. param.out_xor = 0x0000;
  234. param.inital_val = inital_val;
  235. return crc(&param, ptr, len);
  236. }
  237. /****************************************************************************
  238. * Name: crc16_ccitt
  239. *
  240. * Description:
  241. * get the CRC16-CCITT result.
  242. *
  243. ****************************************************************************/
  244. uint16_t crc16_ccitt(uint16_t inital_val, const unsigned char* ptr, unsigned int len)
  245. {
  246. struct crc_param_data param;
  247. param.crc_type = CRC_TYPE_16;
  248. param.poly = POLY_1021;
  249. param.in_invert = false;
  250. param.out_invert = false;
  251. param.out_xor = 0x0000;
  252. param.inital_val = inital_val;
  253. return crc(&param, ptr, len);
  254. }
  255. /****************************************************************************
  256. * Name: crc16_ccitt_false
  257. *
  258. * Description:
  259. * get the CRC16-CCITT-FALSE result.
  260. *
  261. ****************************************************************************/
  262. uint16_t crc16_ccitt_false(uint16_t inital_val, const unsigned char* ptr, unsigned int len)
  263. {
  264. struct crc_param_data param;
  265. param.crc_type = CRC_TYPE_16;
  266. param.poly = POLY_1021;
  267. param.in_invert = false;
  268. param.out_invert = false;
  269. param.out_xor = 0x0000;
  270. param.inital_val = inital_val;
  271. return crc(&param, ptr, len);
  272. }
  273. /****************************************************************************
  274. * Name: crc16_x5
  275. *
  276. * Description:
  277. * get the CRC16-X5 result.
  278. *
  279. ****************************************************************************/
  280. uint16_t crc16_x5(uint16_t inital_val, const unsigned char* ptr, unsigned int len)
  281. {
  282. struct crc_param_data param;
  283. param.crc_type = CRC_TYPE_16;
  284. param.poly = POLY_1021;
  285. param.in_invert = true;
  286. param.out_invert = true;
  287. param.out_xor = 0xFFFF;
  288. param.inital_val = inital_val;
  289. return crc(&param, ptr, len);
  290. }
  291. /****************************************************************************
  292. * Name: crc16_xmodem
  293. *
  294. * Description:
  295. * get the CRC16-XMODEM result.
  296. *
  297. ****************************************************************************/
  298. uint16_t crc16_xmodem(uint16_t inital_val, const unsigned char* ptr, unsigned int len)
  299. {
  300. struct crc_param_data param;
  301. param.crc_type = CRC_TYPE_16;
  302. param.poly = POLY_1021;
  303. param.in_invert = false;
  304. param.out_invert = false;
  305. param.out_xor = 0x0000;
  306. param.inital_val = inital_val;
  307. return crc(&param, ptr, len);
  308. }
  309. /****************************************************************************
  310. * Name: crc32
  311. *
  312. * Description:
  313. * get the CRC32 result.
  314. *
  315. ****************************************************************************/
  316. uint32_t crc32(uint32_t inital_val, const unsigned char* ptr, unsigned int len)
  317. {
  318. struct crc_param_data param;
  319. param.crc_type = CRC_TYPE_32;
  320. param.poly = POLY_04C11DB7;
  321. param.in_invert = true;
  322. param.out_invert = true;
  323. param.out_xor = 0xFFFFFFFF;
  324. param.inital_val = inital_val;
  325. return crc(&param, ptr, len);
  326. }
  327. /****************************************************************************
  328. * Name: crc32_mpeg2
  329. *
  330. * Description:
  331. * get the CRC32-MPEG2 result.
  332. *
  333. ****************************************************************************/
  334. uint32_t crc32_mpeg2(uint32_t inital_val, const unsigned char* ptr, unsigned int len)
  335. {
  336. struct crc_param_data param;
  337. param.crc_type = CRC_TYPE_32;
  338. param.poly = POLY_04C11DB7;
  339. param.in_invert = false;
  340. param.out_invert = false;
  341. param.out_xor = 0x00000000;
  342. param.inital_val = inital_val;
  343. return crc(&param, ptr, len);
  344. }
  345. /****************************************************************************
  346. * Name: utils_crc32
  347. *
  348. * Description:
  349. * get the utils_CRC32 result.
  350. *
  351. ****************************************************************************/
  352. uint32_t utils_crc32(uint32_t crc, const uint8_t *ptr, int buf_len)
  353. {
  354. return crc32(~crc, ptr, buf_len);
  355. }