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