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