blob: 20d46d161472da092582c01c065e99c3cfae188f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass46e0bc92013-11-10 10:26:47 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass46e0bc92013-11-10 10:26:47 -07004 */
5
Simon Glassca9b0af2019-11-14 12:57:14 -07006#ifdef USE_HOSTCC
7#include <arpa/inet.h>
Simon Glassca9b0af2019-11-14 12:57:14 -07008#endif
9#include <u-boot/crc.h>
Simon Glass46e0bc92013-11-10 10:26:47 -070010
Stefan Roesea0d9b992016-04-08 15:56:29 +020011#define POLY (0x1070U << 3)
12
13static unsigned char _crc8(unsigned short data)
Simon Glass46e0bc92013-11-10 10:26:47 -070014{
Stefan Roesea0d9b992016-04-08 15:56:29 +020015 int i;
Simon Glass46e0bc92013-11-10 10:26:47 -070016
Stefan Roesea0d9b992016-04-08 15:56:29 +020017 for (i = 0; i < 8; i++) {
18 if (data & 0x8000)
19 data = data ^ POLY;
20 data = data << 1;
Simon Glass46e0bc92013-11-10 10:26:47 -070021 }
22
Stefan Roesea0d9b992016-04-08 15:56:29 +020023 return (unsigned char)(data >> 8);
24}
25
26unsigned int crc8(unsigned int crc, const unsigned char *vptr, int len)
27{
28 int i;
29
30 for (i = 0; i < len; i++)
31 crc = _crc8((crc ^ vptr[i]) << 8);
32
33 return crc;
Simon Glass46e0bc92013-11-10 10:26:47 -070034}