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