xref: /netbsd-src/sys/dev/flash/flash.h (revision 87fd18f8e547eaf74cc8b13c2004dc30f5ff1662)
1*87fd18f8Schristos /*	$NetBSD: flash.h,v 1.9 2018/04/19 21:50:08 christos Exp $	*/
22b6ee221Sahoka 
32b6ee221Sahoka /*-
42b6ee221Sahoka  * Copyright (c) 2011 Department of Software Engineering,
52b6ee221Sahoka  *		      University of Szeged, Hungary
62b6ee221Sahoka  * Copyright (c) 2011 Adam Hoka <ahoka@NetBSD.org>
72b6ee221Sahoka  * Copyright (c) 2010 David Tengeri <dtengeri@inf.u-szeged.hu>
82b6ee221Sahoka  * All rights reserved.
92b6ee221Sahoka  *
102b6ee221Sahoka  * This code is derived from software contributed to The NetBSD Foundation
112b6ee221Sahoka  * by the Department of Software Engineering, University of Szeged, Hungary
122b6ee221Sahoka  *
132b6ee221Sahoka  * Redistribution and use in source and binary forms, with or without
142b6ee221Sahoka  * modification, are permitted provided that the following conditions
152b6ee221Sahoka  * are met:
162b6ee221Sahoka  * 1. Redistributions of source code must retain the above copyright
172b6ee221Sahoka  *    notice, this list of conditions and the following disclaimer.
182b6ee221Sahoka  * 2. Redistributions in binary form must reproduce the above copyright
192b6ee221Sahoka  *    notice, this list of conditions and the following disclaimer in the
202b6ee221Sahoka  *    documentation and/or other materials provided with the distribution.
212b6ee221Sahoka  *
222b6ee221Sahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
232b6ee221Sahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
242b6ee221Sahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
252b6ee221Sahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
262b6ee221Sahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
272b6ee221Sahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
282b6ee221Sahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
292b6ee221Sahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
302b6ee221Sahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312b6ee221Sahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322b6ee221Sahoka  * SUCH DAMAGE.
332b6ee221Sahoka  */
342b6ee221Sahoka 
352b6ee221Sahoka #ifndef _FLASH_H_
362b6ee221Sahoka #define _FLASH_H_
372b6ee221Sahoka 
382b6ee221Sahoka #include <sys/param.h>
392b6ee221Sahoka #include <sys/device.h>
402b6ee221Sahoka #include <sys/module.h>
412b6ee221Sahoka #include <sys/buf.h>
422b6ee221Sahoka #include <sys/flashio.h>
432b6ee221Sahoka 
440e8f635bSahoka #ifdef FLASH_DEBUG
450e8f635bSahoka #define FLDPRINTF(x)	if (flashdebug) printf x
460e8f635bSahoka #define FLDPRINTFN(n,x)	if (flashdebug>(n)) printf x
470e8f635bSahoka #else
480e8f635bSahoka #define FLDPRINTF(x)
490e8f635bSahoka #define FLDPRINTFN(n,x)
500e8f635bSahoka #endif
510e8f635bSahoka 
520e8f635bSahoka struct flash_partition {
530e8f635bSahoka 	flash_off_t part_offset;
540e8f635bSahoka 	flash_size_t part_size;
550e8f635bSahoka 	int part_flags;
5640b9d3eeSjmcneill 	const char *part_name;
570e8f635bSahoka };
580e8f635bSahoka 
592b6ee221Sahoka /**
602b6ee221Sahoka  *  flash_softc - private structure for flash layer driver
612b6ee221Sahoka  */
622b6ee221Sahoka 
632b6ee221Sahoka struct flash_softc {
642b6ee221Sahoka 	device_t sc_dev;
652b6ee221Sahoka 	device_t sc_parent_dev;		/* Hardware (parent) device */
662b6ee221Sahoka 	void *hw_softc;			/* Hardware device private softc */
672b6ee221Sahoka 	struct flash_interface *flash_if;	/* Hardware interface */
680e8f635bSahoka 	struct flash_partition sc_partinfo;	/* partition information */
692b6ee221Sahoka 
702b6ee221Sahoka 	bool sc_readonly;		/* read only flash device */
712b6ee221Sahoka };
722b6ee221Sahoka 
732b6ee221Sahoka struct flash_attach_args {
742b6ee221Sahoka 	struct flash_interface *flash_if;	/* Hardware interface */
750e8f635bSahoka 	struct flash_partition partinfo;
762b6ee221Sahoka };
772b6ee221Sahoka 
782b6ee221Sahoka /**
792b6ee221Sahoka  * struct erase_instruction - instructions to erase a flash eraseblock
802b6ee221Sahoka  */
812b6ee221Sahoka struct flash_erase_instruction {
826adb739dSahoka 	flash_off_t ei_addr;
836adb739dSahoka 	flash_off_t ei_len;
842b6ee221Sahoka 	void (*ei_callback)(struct flash_erase_instruction *);
852b6ee221Sahoka 	u_long ei_priv;
862b6ee221Sahoka 	u_char ei_state;
872b6ee221Sahoka };
882b6ee221Sahoka 
892b6ee221Sahoka enum {
902b6ee221Sahoka 	FLASH_PART_READONLY	= (1<<0),
912b6ee221Sahoka 	FLASH_PART_FILESYSTEM	= (1<<2)
922b6ee221Sahoka };
932b6ee221Sahoka 
942b6ee221Sahoka /**
952b6ee221Sahoka  * struct flash_interface - interface for flash operations
962b6ee221Sahoka  */
972b6ee221Sahoka struct flash_interface {
982b6ee221Sahoka 	int (*erase)(device_t, struct flash_erase_instruction *);
996adb739dSahoka 	int (*read)(device_t, flash_off_t, size_t, size_t *, uint8_t *);
1006adb739dSahoka 	int (*write)(device_t, flash_off_t, size_t, size_t *, const uint8_t *);
1016adb739dSahoka 	int (*block_markbad)(device_t, flash_off_t);
1026adb739dSahoka 	int (*block_isbad)(device_t, flash_off_t, bool *);
1032b6ee221Sahoka 	int (*sync)(device_t);
1042b6ee221Sahoka 
1052b6ee221Sahoka 	int (*submit)(device_t, struct buf *);
1062b6ee221Sahoka 
1072b6ee221Sahoka 	uint32_t page_size;
1082b6ee221Sahoka 	uint32_t erasesize;
1092b6ee221Sahoka 	uint32_t writesize;
1102b6ee221Sahoka 	uint32_t minor;
1112b6ee221Sahoka 	uint8_t	type;
1122b6ee221Sahoka };
1132b6ee221Sahoka 
1142b6ee221Sahoka /**
1152b6ee221Sahoka  * struct cache - for caching writes on block device
1162b6ee221Sahoka  */
1172b6ee221Sahoka struct flash_cache {
1182b6ee221Sahoka 	size_t fc_len;
1196adb739dSahoka 	flash_off_t fc_block;
1202b6ee221Sahoka 	uint8_t *fc_data;
1212b6ee221Sahoka };
1222b6ee221Sahoka 
123fb19d2b7Scliff device_t flash_attach_mi(struct flash_interface *, device_t);
12440b9d3eeSjmcneill void flash_attach_mtdparts(struct flash_interface *, device_t, flash_size_t, const char *, const char *);
125fb19d2b7Scliff const struct flash_interface *flash_get_interface(dev_t);
126fb19d2b7Scliff const struct flash_softc *flash_get_softc(dev_t);
127fb19d2b7Scliff device_t flash_get_device(dev_t);
128ed5192ddSahoka flash_size_t flash_get_size(dev_t);
129fb19d2b7Scliff 
1302b6ee221Sahoka /* flash operations should be used through these */
1312b6ee221Sahoka int flash_erase(device_t, struct flash_erase_instruction *);
1326adb739dSahoka int flash_read(device_t, flash_off_t, size_t, size_t *, uint8_t *);
1336adb739dSahoka int flash_write(device_t, flash_off_t, size_t, size_t *, const uint8_t *);
1346adb739dSahoka int flash_block_markbad(device_t, flash_off_t);
1356adb739dSahoka int flash_block_isbad(device_t, flash_off_t, bool *);
1362b6ee221Sahoka int flash_sync(device_t);
1372b6ee221Sahoka 
1382b6ee221Sahoka /*
1392b6ee221Sahoka  * check_pattern - checks the buffer only contains the byte pattern
1402b6ee221Sahoka  *
1412b6ee221Sahoka  * This functions checks if the buffer only contains a specified byte pattern.
1422b6ee221Sahoka  * Returns %0 if found something else, %1 otherwise.
1432b6ee221Sahoka  */
144*87fd18f8Schristos static __inline int
check_pattern(const void * buf,uint8_t patt,size_t offset,size_t size)1452b6ee221Sahoka check_pattern(const void *buf, uint8_t patt, size_t offset, size_t size)
1462b6ee221Sahoka {
1472b6ee221Sahoka 	size_t i;
1482b6ee221Sahoka 	for (i = offset; i < size; i++) {
1492b6ee221Sahoka 		if (((const uint8_t *)buf)[i] != patt)
1502b6ee221Sahoka 			return 0;
1512b6ee221Sahoka 	}
1522b6ee221Sahoka 	return 1;
1532b6ee221Sahoka }
1542b6ee221Sahoka 
1552b6ee221Sahoka #endif /* _FLASH_H_ */
156