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