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