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