1 /* $NetBSD: flash.h,v 1.3 2011/04/04 14:25:09 ahoka Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 Department of Software Engineering, 5 * University of Szeged, Hungary 6 * Copyright (c) 2011 Adam Hoka <ahoka@NetBSD.org> 7 * Copyright (c) 2010 David Tengeri <dtengeri@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 /** 45 * flash_softc - private structure for flash layer driver 46 */ 47 48 struct flash_softc { 49 device_t sc_dev; 50 device_t sc_parent_dev; /* Hardware (parent) device */ 51 void *hw_softc; /* Hardware device private softc */ 52 struct flash_interface *flash_if; /* Hardware interface */ 53 54 bool sc_readonly; /* read only flash device */ 55 }; 56 57 struct flash_attach_args { 58 struct flash_interface *flash_if; /* Hardware interface */ 59 }; 60 61 device_t flash_attach_mi(struct flash_interface *, device_t); 62 const struct flash_interface *flash_get_interface(dev_t); 63 const struct flash_softc *flash_get_softc(dev_t); 64 device_t flash_get_device(dev_t); 65 66 /** 67 * struct erase_instruction - instructions to erase a flash eraseblock 68 * @fd: flash descriptor 69 * @addr: start address of the erase operation 70 * @len: the erase length 71 * @callback: callback operation, called when erase finished 72 * @priv: private data 73 * @state: the erase operation's result 74 */ 75 struct flash_erase_instruction { 76 flash_off_t ei_addr; 77 flash_off_t ei_len; 78 void (*ei_callback)(struct flash_erase_instruction *); 79 u_long ei_priv; 80 u_char ei_state; 81 }; 82 83 enum { 84 FLASH_PART_READONLY = (1<<0), 85 FLASH_PART_FILESYSTEM = (1<<2) 86 }; 87 88 struct flash_partition { 89 flash_off_t part_offset; 90 flash_off_t part_size; 91 int part_flags; 92 }; 93 94 /** 95 * struct flash_interface - interface for flash operations 96 * @type: type of flash device 97 * @size: size of flash 98 * @page_size: page size of flash 99 * @erasesize: erase size of flash 100 * @writesize: minimum write size of flash 101 * @minor: minor number of the character device attached to this driver 102 * @erase: erase operation of the flash 103 * @read: read operation of the flash 104 * @write: write operation of the flash 105 * @block_markbad: marks a block as bad on the flash 106 * @block_isbad: checks if a block is bad on flash 107 */ 108 struct flash_interface { 109 int (*erase)(device_t, struct flash_erase_instruction *); 110 int (*read)(device_t, flash_off_t, size_t, size_t *, uint8_t *); 111 int (*write)(device_t, flash_off_t, size_t, size_t *, const uint8_t *); 112 int (*block_markbad)(device_t, flash_off_t); 113 int (*block_isbad)(device_t, flash_off_t, bool *); 114 int (*sync)(device_t); 115 116 int (*submit)(device_t, struct buf *); 117 118 /* storage for partition info */ 119 struct flash_partition partition; 120 121 /* total size of mtd */ 122 flash_size_t size; 123 uint32_t page_size; 124 uint32_t erasesize; 125 uint32_t writesize; 126 uint32_t minor; 127 uint8_t type; 128 }; 129 130 /** 131 * struct cache - for caching writes on block device 132 */ 133 struct flash_cache { 134 size_t fc_len; 135 flash_off_t fc_block; 136 uint8_t *fc_data; 137 }; 138 139 /* flash operations should be used through these */ 140 int flash_erase(device_t, struct flash_erase_instruction *); 141 int flash_read(device_t, flash_off_t, size_t, size_t *, uint8_t *); 142 int flash_write(device_t, flash_off_t, size_t, size_t *, const uint8_t *); 143 int flash_block_markbad(device_t, flash_off_t); 144 int flash_block_isbad(device_t, flash_off_t, bool *); 145 int flash_sync(device_t); 146 147 /* 148 * check_pattern - checks the buffer only contains the byte pattern 149 * @buf: the buffer to check 150 * @patt: the pattern to match 151 * @offset: the starting byte number, the matching starts from here 152 * @size: the buffer size 153 * 154 * This functions checks if the buffer only contains a specified byte pattern. 155 * Returns %0 if found something else, %1 otherwise. 156 */ 157 static inline int 158 check_pattern(const void *buf, uint8_t patt, size_t offset, size_t size) 159 { 160 size_t i; 161 for (i = offset; i < size; i++) { 162 if (((const uint8_t *)buf)[i] != patt) 163 return 0; 164 } 165 return 1; 166 } 167 168 #endif /* _FLASH_H_ */ 169