blob: 55f7c07285cd3baf32612eaaf44fbed167a1ffcf [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
6#include "linux/crc8.h"
7
Stefan Roesea0d9b992016-04-08 15:56:29 +02008#define POLY (0x1070U << 3)
9
10static unsigned char _crc8(unsigned short data)
Simon Glass46e0bc92013-11-10 10:26:47 -070011{
Stefan Roesea0d9b992016-04-08 15:56:29 +020012 int i;
Simon Glass46e0bc92013-11-10 10:26:47 -070013
Stefan Roesea0d9b992016-04-08 15:56:29 +020014 for (i = 0; i < 8; i++) {
15 if (data & 0x8000)
16 data = data ^ POLY;
17 data = data << 1;
Simon Glass46e0bc92013-11-10 10:26:47 -070018 }
19
Stefan Roesea0d9b992016-04-08 15:56:29 +020020 return (unsigned char)(data >> 8);
21}
22
23unsigned int crc8(unsigned int crc, const unsigned char *vptr, int len)
24{
25 int i;
26
27 for (i = 0; i < len; i++)
28 crc = _crc8((crc ^ vptr[i]) << 8);
29
30 return crc;
Simon Glass46e0bc92013-11-10 10:26:47 -070031}