Home | History | Annotate | Line # | Download | only in flash
flash.h revision 1.6
      1 /*	$NetBSD: flash.h,v 1.6 2011/07/15 19:19:57 cliff Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 Department of Software Engineering,
      5  *		      University of Szeged, Hungary
      6  * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
      7  * Copyright (c) 2010 David Tengeri <dtengeri (at) inf.u-szeged.hu>
      8  * All rights reserved.
      9  *
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by the Department of Software Engineering, University of Szeged, Hungary
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #ifndef _FLASH_H_
     36 #define _FLASH_H_
     37 
     38 #include <sys/param.h>
     39 #include <sys/device.h>
     40 #include <sys/module.h>
     41 #include <sys/buf.h>
     42 #include <sys/flashio.h>
     43 
     44 #ifdef FLASH_DEBUG
     45 #define FLDPRINTF(x)	if (flashdebug) printf x
     46 #define FLDPRINTFN(n,x)	if (flashdebug>(n)) printf x
     47 #else
     48 #define FLDPRINTF(x)
     49 #define FLDPRINTFN(n,x)
     50 #endif
     51 
     52 struct flash_partition {
     53 	flash_off_t part_offset;
     54 	flash_size_t part_size;
     55 	int part_flags;
     56 };
     57 
     58 /**
     59  *  flash_softc - private structure for flash layer driver
     60  */
     61 
     62 struct flash_softc {
     63 	device_t sc_dev;
     64 	device_t sc_parent_dev;		/* Hardware (parent) device */
     65 	void *hw_softc;			/* Hardware device private softc */
     66 	struct flash_interface *flash_if;	/* Hardware interface */
     67 	struct flash_partition sc_partinfo;	/* partition information */
     68 
     69 	bool sc_readonly;		/* read only flash device */
     70 };
     71 
     72 struct flash_attach_args {
     73 	struct flash_interface *flash_if;	/* Hardware interface */
     74 	struct flash_partition partinfo;
     75 };
     76 
     77 /**
     78  * struct erase_instruction - instructions to erase a flash eraseblock
     79  */
     80 struct flash_erase_instruction {
     81 	flash_off_t ei_addr;
     82 	flash_off_t ei_len;
     83 	void (*ei_callback)(struct flash_erase_instruction *);
     84 	u_long ei_priv;
     85 	u_char ei_state;
     86 };
     87 
     88 enum {
     89 	FLASH_PART_READONLY	= (1<<0),
     90 	FLASH_PART_FILESYSTEM	= (1<<2)
     91 };
     92 
     93 /**
     94  * struct flash_interface - interface for flash operations
     95  */
     96 struct flash_interface {
     97 	int (*erase)(device_t, struct flash_erase_instruction *);
     98 	int (*read)(device_t, flash_off_t, size_t, size_t *, uint8_t *);
     99 	int (*write)(device_t, flash_off_t, size_t, size_t *, const uint8_t *);
    100 	int (*block_markbad)(device_t, flash_off_t);
    101 	int (*block_isbad)(device_t, flash_off_t, bool *);
    102 	int (*sync)(device_t);
    103 
    104 	int (*submit)(device_t, struct buf *);
    105 
    106 	uint32_t page_size;
    107 	uint32_t erasesize;
    108 	uint32_t writesize;
    109 	uint32_t minor;
    110 	uint8_t	type;
    111 };
    112 
    113 /**
    114  * struct cache - for caching writes on block device
    115  */
    116 struct flash_cache {
    117 	size_t fc_len;
    118 	flash_off_t fc_block;
    119 	uint8_t *fc_data;
    120 };
    121 
    122 device_t flash_attach_mi(struct flash_interface *, device_t);
    123 const struct flash_interface *flash_get_interface(dev_t);
    124 const struct flash_softc *flash_get_softc(dev_t);
    125 device_t flash_get_device(dev_t);
    126 
    127 /* flash operations should be used through these */
    128 int flash_erase(device_t, struct flash_erase_instruction *);
    129 int flash_read(device_t, flash_off_t, size_t, size_t *, uint8_t *);
    130 int flash_write(device_t, flash_off_t, size_t, size_t *, const uint8_t *);
    131 int flash_block_markbad(device_t, flash_off_t);
    132 int flash_block_isbad(device_t, flash_off_t, bool *);
    133 int flash_sync(device_t);
    134 
    135 /*
    136  * check_pattern - checks the buffer only contains the byte pattern
    137  *
    138  * This functions checks if the buffer only contains a specified byte pattern.
    139  * Returns %0 if found something else, %1 otherwise.
    140  */
    141 static inline int
    142 check_pattern(const void *buf, uint8_t patt, size_t offset, size_t size)
    143 {
    144 	size_t i;
    145 	for (i = offset; i < size; i++) {
    146 		if (((const uint8_t *)buf)[i] != patt)
    147 			return 0;
    148 	}
    149 	return 1;
    150 }
    151 
    152 #endif /* _FLASH_H_ */
    153