Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 46e0bc9 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 2 | /* |
3 | * Copyright (c) 2013 Google, Inc | ||||
Simon Glass | 46e0bc9 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 4 | */ |
5 | |||||
6 | #include "linux/crc8.h" | ||||
7 | |||||
Stefan Roese | a0d9b99 | 2016-04-08 15:56:29 +0200 | [diff] [blame] | 8 | #define POLY (0x1070U << 3) |
9 | |||||
10 | static unsigned char _crc8(unsigned short data) | ||||
Simon Glass | 46e0bc9 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 11 | { |
Stefan Roese | a0d9b99 | 2016-04-08 15:56:29 +0200 | [diff] [blame] | 12 | int i; |
Simon Glass | 46e0bc9 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 13 | |
Stefan Roese | a0d9b99 | 2016-04-08 15:56:29 +0200 | [diff] [blame] | 14 | for (i = 0; i < 8; i++) { |
15 | if (data & 0x8000) | ||||
16 | data = data ^ POLY; | ||||
17 | data = data << 1; | ||||
Simon Glass | 46e0bc9 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 18 | } |
19 | |||||
Stefan Roese | a0d9b99 | 2016-04-08 15:56:29 +0200 | [diff] [blame] | 20 | return (unsigned char)(data >> 8); |
21 | } | ||||
22 | |||||
23 | unsigned 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 Glass | 46e0bc9 | 2013-11-10 10:26:47 -0700 | [diff] [blame] | 31 | } |