1*2b6ee221Sahoka /* $NetBSD: nand_crc.c,v 1.1 2011/02/26 18:07:31 ahoka Exp $ */
2*2b6ee221Sahoka
3*2b6ee221Sahoka /*-
4*2b6ee221Sahoka * Copyright (c) 2010 Department of Software Engineering,
5*2b6ee221Sahoka * University of Szeged, Hungary
6*2b6ee221Sahoka * Copyright (c) 2010 Adam Hoka <ahoka@NetBSD.org>
7*2b6ee221Sahoka * All rights reserved.
8*2b6ee221Sahoka *
9*2b6ee221Sahoka * This code is derived from software contributed to The NetBSD Foundation
10*2b6ee221Sahoka * by the Department of Software Engineering, University of Szeged, Hungary
11*2b6ee221Sahoka *
12*2b6ee221Sahoka * Redistribution and use in source and binary forms, with or without
13*2b6ee221Sahoka * modification, are permitted provided that the following conditions
14*2b6ee221Sahoka * are met:
15*2b6ee221Sahoka * 1. Redistributions of source code must retain the above copyright
16*2b6ee221Sahoka * notice, this list of conditions and the following disclaimer.
17*2b6ee221Sahoka * 2. Redistributions in binary form must reproduce the above copyright
18*2b6ee221Sahoka * notice, this list of conditions and the following disclaimer in the
19*2b6ee221Sahoka * documentation and/or other materials provided with the distribution.
20*2b6ee221Sahoka *
21*2b6ee221Sahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22*2b6ee221Sahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23*2b6ee221Sahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24*2b6ee221Sahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25*2b6ee221Sahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*2b6ee221Sahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27*2b6ee221Sahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*2b6ee221Sahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*2b6ee221Sahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*2b6ee221Sahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*2b6ee221Sahoka * SUCH DAMAGE.
32*2b6ee221Sahoka */
33*2b6ee221Sahoka
34*2b6ee221Sahoka /* Implements CRC-16 as required by the ONFI 2.3 specification */
35*2b6ee221Sahoka
36*2b6ee221Sahoka #include <sys/cdefs.h>
37*2b6ee221Sahoka __KERNEL_RCSID(0, "$NetBSD: nand_crc.c,v 1.1 2011/02/26 18:07:31 ahoka Exp $");
38*2b6ee221Sahoka
39*2b6ee221Sahoka #include "nand_crc.h"
40*2b6ee221Sahoka
41*2b6ee221Sahoka uint16_t
nand_crc16(uint8_t * data,size_t len)42*2b6ee221Sahoka nand_crc16(uint8_t *data, size_t len)
43*2b6ee221Sahoka {
44*2b6ee221Sahoka const uint16_t init = 0x4f4e;
45*2b6ee221Sahoka const uint16_t polynom = 0x8005;
46*2b6ee221Sahoka const uint16_t highbit = 0x0001 << 15;
47*2b6ee221Sahoka uint16_t crc = init;
48*2b6ee221Sahoka size_t i;
49*2b6ee221Sahoka int j;
50*2b6ee221Sahoka
51*2b6ee221Sahoka for (i = 0; i < len; i++) {
52*2b6ee221Sahoka crc ^= data[i] << 8;
53*2b6ee221Sahoka
54*2b6ee221Sahoka for (j = 0; j < 8; j++) {
55*2b6ee221Sahoka if ((crc & highbit) != 0x00)
56*2b6ee221Sahoka crc = (crc << 1) ^ polynom;
57*2b6ee221Sahoka else
58*2b6ee221Sahoka crc <<= 1;
59*2b6ee221Sahoka }
60*2b6ee221Sahoka }
61*2b6ee221Sahoka
62*2b6ee221Sahoka return crc;
63*2b6ee221Sahoka }
64