flash.c revision 1.4.4.2 1 1.4.4.2 jruoho /* $NetBSD: flash.c,v 1.4.4.2 2011/06/06 09:07:48 jruoho Exp $ */
2 1.4.4.2 jruoho
3 1.4.4.2 jruoho /*-
4 1.4.4.2 jruoho * Copyright (c) 2011 Department of Software Engineering,
5 1.4.4.2 jruoho * University of Szeged, Hungary
6 1.4.4.2 jruoho * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
7 1.4.4.2 jruoho * Copyright (c) 2010 David Tengeri <dtengeri (at) inf.u-szeged.hu>
8 1.4.4.2 jruoho * All rights reserved.
9 1.4.4.2 jruoho *
10 1.4.4.2 jruoho * This code is derived from software contributed to The NetBSD Foundation
11 1.4.4.2 jruoho * by the Department of Software Engineering, University of Szeged, Hungary
12 1.4.4.2 jruoho *
13 1.4.4.2 jruoho * Redistribution and use in source and binary forms, with or without
14 1.4.4.2 jruoho * modification, are permitted provided that the following conditions
15 1.4.4.2 jruoho * are met:
16 1.4.4.2 jruoho * 1. Redistributions of source code must retain the above copyright
17 1.4.4.2 jruoho * notice, this list of conditions and the following disclaimer.
18 1.4.4.2 jruoho * 2. Redistributions in binary form must reproduce the above copyright
19 1.4.4.2 jruoho * notice, this list of conditions and the following disclaimer in the
20 1.4.4.2 jruoho * documentation and/or other materials provided with the distribution.
21 1.4.4.2 jruoho *
22 1.4.4.2 jruoho * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.4.4.2 jruoho * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.4.4.2 jruoho * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.4.4.2 jruoho * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.4.4.2 jruoho * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 1.4.4.2 jruoho * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 1.4.4.2 jruoho * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 1.4.4.2 jruoho * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.4.4.2 jruoho * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.4.4.2 jruoho * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.4.4.2 jruoho * SUCH DAMAGE.
33 1.4.4.2 jruoho */
34 1.4.4.2 jruoho
35 1.4.4.2 jruoho /*-
36 1.4.4.2 jruoho * Framework for storage devices based on Flash technology
37 1.4.4.2 jruoho */
38 1.4.4.2 jruoho
39 1.4.4.2 jruoho #include <sys/cdefs.h>
40 1.4.4.2 jruoho __KERNEL_RCSID(0, "$NetBSD: flash.c,v 1.4.4.2 2011/06/06 09:07:48 jruoho Exp $");
41 1.4.4.2 jruoho
42 1.4.4.2 jruoho #include <sys/param.h>
43 1.4.4.2 jruoho #include <sys/types.h>
44 1.4.4.2 jruoho #include <sys/proc.h>
45 1.4.4.2 jruoho #include <sys/errno.h>
46 1.4.4.2 jruoho #include <sys/ioctl.h>
47 1.4.4.2 jruoho #include <sys/device.h>
48 1.4.4.2 jruoho #include <sys/conf.h>
49 1.4.4.2 jruoho #include <sys/kmem.h>
50 1.4.4.2 jruoho #include <sys/uio.h>
51 1.4.4.2 jruoho #include <sys/kernel.h>
52 1.4.4.2 jruoho
53 1.4.4.2 jruoho #include <sys/atomic.h>
54 1.4.4.2 jruoho #include <sys/buf.h>
55 1.4.4.2 jruoho #include <sys/bufq.h>
56 1.4.4.2 jruoho #include <sys/disk.h>
57 1.4.4.2 jruoho #include <sys/disklabel.h>
58 1.4.4.2 jruoho #include <sys/malloc.h>
59 1.4.4.2 jruoho #include <sys/reboot.h>
60 1.4.4.2 jruoho
61 1.4.4.2 jruoho #include <sys/flashio.h>
62 1.4.4.2 jruoho #include "flash.h"
63 1.4.4.2 jruoho
64 1.4.4.2 jruoho #define FLASH_DEBUG 1
65 1.4.4.2 jruoho #ifdef FLASH_DEBUG
66 1.4.4.2 jruoho #define DPRINTF(x) if (flashdebug) printf x
67 1.4.4.2 jruoho #define DPRINTFN(n,x) if (flashdebug>(n)) printf x
68 1.4.4.2 jruoho int flashdebug = FLASH_DEBUG;
69 1.4.4.2 jruoho #else
70 1.4.4.2 jruoho #define DPRINTF(x)
71 1.4.4.2 jruoho #define DPRINTFN(n,x)
72 1.4.4.2 jruoho #endif
73 1.4.4.2 jruoho
74 1.4.4.2 jruoho extern struct cfdriver flash_cd;
75 1.4.4.2 jruoho
76 1.4.4.2 jruoho dev_type_open(flashopen);
77 1.4.4.2 jruoho dev_type_close(flashclose);
78 1.4.4.2 jruoho dev_type_read(flashread);
79 1.4.4.2 jruoho dev_type_write(flashwrite);
80 1.4.4.2 jruoho dev_type_ioctl(flashioctl);
81 1.4.4.2 jruoho dev_type_strategy(flashstrategy);
82 1.4.4.2 jruoho dev_type_dump(flashdump);
83 1.4.4.2 jruoho dev_type_size(flashsize);
84 1.4.4.2 jruoho
85 1.4.4.2 jruoho int flash_print(void *aux, const char *pnp);
86 1.4.4.2 jruoho
87 1.4.4.2 jruoho bool flash_shutdown(device_t dev, int how);
88 1.4.4.2 jruoho int flash_nsectors(struct buf *bp);
89 1.4.4.2 jruoho int flash_sector(struct buf *bp);
90 1.4.4.2 jruoho
91 1.4.4.2 jruoho static inline flash_off_t flash_get_part_offset(struct flash_softc *fl,
92 1.4.4.2 jruoho size_t poffset);
93 1.4.4.2 jruoho
94 1.4.4.2 jruoho int flash_match(device_t parent, cfdata_t match, void *aux);
95 1.4.4.2 jruoho void flash_attach(device_t parent, device_t self, void *aux);
96 1.4.4.2 jruoho int flash_detach(device_t device, int flags);
97 1.4.4.2 jruoho
98 1.4.4.2 jruoho CFATTACH_DECL_NEW(flash, sizeof(struct flash_softc),
99 1.4.4.2 jruoho flash_match, flash_attach, flash_detach, NULL);
100 1.4.4.2 jruoho
101 1.4.4.2 jruoho /**
102 1.4.4.2 jruoho * Block device's operation
103 1.4.4.2 jruoho */
104 1.4.4.2 jruoho const struct bdevsw flash_bdevsw = {
105 1.4.4.2 jruoho .d_open = flashopen,
106 1.4.4.2 jruoho .d_close = flashclose,
107 1.4.4.2 jruoho .d_strategy = flashstrategy,
108 1.4.4.2 jruoho .d_ioctl = flashioctl,
109 1.4.4.2 jruoho .d_dump = flashdump,
110 1.4.4.2 jruoho .d_psize = flashsize,
111 1.4.4.2 jruoho .d_flag = D_DISK | D_MPSAFE
112 1.4.4.2 jruoho };
113 1.4.4.2 jruoho
114 1.4.4.2 jruoho /**
115 1.4.4.2 jruoho * Character device's operations
116 1.4.4.2 jruoho */
117 1.4.4.2 jruoho const struct cdevsw flash_cdevsw = {
118 1.4.4.2 jruoho .d_open = flashopen,
119 1.4.4.2 jruoho .d_close = flashclose,
120 1.4.4.2 jruoho .d_read = flashread,
121 1.4.4.2 jruoho .d_write = flashwrite,
122 1.4.4.2 jruoho .d_ioctl = flashioctl,
123 1.4.4.2 jruoho .d_stop = nostop,
124 1.4.4.2 jruoho .d_tty = notty,
125 1.4.4.2 jruoho .d_poll = nopoll,
126 1.4.4.2 jruoho .d_mmap = nommap,
127 1.4.4.2 jruoho .d_kqfilter = nokqfilter,
128 1.4.4.2 jruoho .d_flag = D_DISK | D_MPSAFE
129 1.4.4.2 jruoho };
130 1.4.4.2 jruoho
131 1.4.4.2 jruoho /* ARGSUSED */
132 1.4.4.2 jruoho int
133 1.4.4.2 jruoho flash_match(device_t parent, cfdata_t match, void *aux)
134 1.4.4.2 jruoho {
135 1.4.4.2 jruoho /* pseudo device, always attaches */
136 1.4.4.2 jruoho return 1;
137 1.4.4.2 jruoho }
138 1.4.4.2 jruoho
139 1.4.4.2 jruoho /* ARGSUSED */
140 1.4.4.2 jruoho void
141 1.4.4.2 jruoho flash_attach(device_t parent, device_t self, void *aux)
142 1.4.4.2 jruoho {
143 1.4.4.2 jruoho struct flash_softc *sc = device_private(self);
144 1.4.4.2 jruoho struct flash_attach_args *faa = aux;
145 1.4.4.2 jruoho char pbuf[2][sizeof("9999 KB")];
146 1.4.4.2 jruoho
147 1.4.4.2 jruoho sc->sc_dev = self;
148 1.4.4.2 jruoho sc->sc_parent_dev = parent;
149 1.4.4.2 jruoho sc->flash_if = faa->flash_if;
150 1.4.4.2 jruoho sc->hw_softc = device_private(parent);
151 1.4.4.2 jruoho
152 1.4.4.2 jruoho format_bytes(pbuf[0], sizeof(pbuf[0]), sc->flash_if->size);
153 1.4.4.2 jruoho format_bytes(pbuf[1], sizeof(pbuf[1]), sc->flash_if->erasesize);
154 1.4.4.2 jruoho
155 1.4.4.2 jruoho aprint_naive("\n");
156 1.4.4.2 jruoho
157 1.4.4.2 jruoho switch (sc->flash_if->type) {
158 1.4.4.2 jruoho case FLASH_TYPE_NOR:
159 1.4.4.2 jruoho aprint_normal(": %s NOR flash\n", pbuf[0]);
160 1.4.4.2 jruoho break;
161 1.4.4.2 jruoho
162 1.4.4.2 jruoho case FLASH_TYPE_NAND:
163 1.4.4.2 jruoho aprint_normal(": %s NAND flash\n", pbuf[0]);
164 1.4.4.2 jruoho break;
165 1.4.4.2 jruoho
166 1.4.4.2 jruoho default:
167 1.4.4.2 jruoho aprint_normal(": %s unknown flash\n", pbuf[0]);
168 1.4.4.2 jruoho }
169 1.4.4.2 jruoho
170 1.4.4.2 jruoho aprint_normal_dev(sc->sc_dev,
171 1.4.4.2 jruoho "size: 0x%jx, offset: 0x%jx",
172 1.4.4.2 jruoho (uintmax_t )sc->flash_if->partition.part_size,
173 1.4.4.2 jruoho (uintmax_t )sc->flash_if->partition.part_offset);
174 1.4.4.2 jruoho
175 1.4.4.2 jruoho if (sc->flash_if->partition.part_flags & FLASH_PART_READONLY) {
176 1.4.4.2 jruoho sc->sc_readonly = true;
177 1.4.4.2 jruoho aprint_normal(", read only");
178 1.4.4.2 jruoho } else {
179 1.4.4.2 jruoho sc->sc_readonly = false;
180 1.4.4.2 jruoho }
181 1.4.4.2 jruoho
182 1.4.4.2 jruoho aprint_normal("\n");
183 1.4.4.2 jruoho
184 1.4.4.2 jruoho if (sc->flash_if->partition.part_size == 0) {
185 1.4.4.2 jruoho aprint_error_dev(self,
186 1.4.4.2 jruoho "partition size must be larger than 0\n");
187 1.4.4.2 jruoho return;
188 1.4.4.2 jruoho }
189 1.4.4.2 jruoho
190 1.4.4.2 jruoho switch (sc->flash_if->type) {
191 1.4.4.2 jruoho case FLASH_TYPE_NOR:
192 1.4.4.2 jruoho aprint_normal_dev(sc->sc_dev,
193 1.4.4.2 jruoho "erase size %s bytes, write size %d bytes\n",
194 1.4.4.2 jruoho pbuf[1], sc->flash_if->writesize);
195 1.4.4.2 jruoho break;
196 1.4.4.2 jruoho
197 1.4.4.2 jruoho case FLASH_TYPE_NAND:
198 1.4.4.2 jruoho default:
199 1.4.4.2 jruoho aprint_normal_dev(sc->sc_dev,
200 1.4.4.2 jruoho "erase size %s, page size %d bytes, write size %d bytes\n",
201 1.4.4.2 jruoho pbuf[1], sc->flash_if->page_size,
202 1.4.4.2 jruoho sc->flash_if->writesize);
203 1.4.4.2 jruoho break;
204 1.4.4.2 jruoho }
205 1.4.4.2 jruoho
206 1.4.4.2 jruoho if (!pmf_device_register1(sc->sc_dev, NULL, NULL, flash_shutdown))
207 1.4.4.2 jruoho aprint_error_dev(sc->sc_dev,
208 1.4.4.2 jruoho "couldn't establish power handler\n");
209 1.4.4.2 jruoho }
210 1.4.4.2 jruoho
211 1.4.4.2 jruoho int
212 1.4.4.2 jruoho flash_detach(device_t device, int flags)
213 1.4.4.2 jruoho {
214 1.4.4.2 jruoho struct flash_softc *sc = device_private(device);
215 1.4.4.2 jruoho
216 1.4.4.2 jruoho pmf_device_deregister(sc->sc_dev);
217 1.4.4.2 jruoho
218 1.4.4.2 jruoho /* freeing flash_if is our responsibility */
219 1.4.4.2 jruoho kmem_free(sc->flash_if, sizeof(*sc->flash_if));
220 1.4.4.2 jruoho
221 1.4.4.2 jruoho return 0;
222 1.4.4.2 jruoho }
223 1.4.4.2 jruoho
224 1.4.4.2 jruoho int
225 1.4.4.2 jruoho flash_print(void *aux, const char *pnp)
226 1.4.4.2 jruoho {
227 1.4.4.2 jruoho struct flash_attach_args *arg;
228 1.4.4.2 jruoho const char *type;
229 1.4.4.2 jruoho
230 1.4.4.2 jruoho if (pnp != NULL) {
231 1.4.4.2 jruoho arg = aux;
232 1.4.4.2 jruoho switch (arg->flash_if->type) {
233 1.4.4.2 jruoho case FLASH_TYPE_NOR:
234 1.4.4.2 jruoho type = "NOR";
235 1.4.4.2 jruoho break;
236 1.4.4.2 jruoho case FLASH_TYPE_NAND:
237 1.4.4.2 jruoho type = "NAND";
238 1.4.4.2 jruoho break;
239 1.4.4.2 jruoho default:
240 1.4.4.2 jruoho panic("flash_print: unknown type %d",
241 1.4.4.2 jruoho arg->flash_if->type);
242 1.4.4.2 jruoho }
243 1.4.4.2 jruoho aprint_normal("%s flash at %s", type, pnp);
244 1.4.4.2 jruoho }
245 1.4.4.2 jruoho return UNCONF;
246 1.4.4.2 jruoho }
247 1.4.4.2 jruoho
248 1.4.4.2 jruoho device_t
249 1.4.4.2 jruoho flash_attach_mi(struct flash_interface *flash_if, device_t device)
250 1.4.4.2 jruoho {
251 1.4.4.2 jruoho struct flash_attach_args arg;
252 1.4.4.2 jruoho
253 1.4.4.2 jruoho #ifdef DIAGNOSTIC
254 1.4.4.2 jruoho if (flash_if == NULL) {
255 1.4.4.2 jruoho aprint_error("flash_attach_mi: NULL\n");
256 1.4.4.2 jruoho return 0;
257 1.4.4.2 jruoho }
258 1.4.4.2 jruoho #endif
259 1.4.4.2 jruoho arg.flash_if = flash_if;
260 1.4.4.2 jruoho
261 1.4.4.2 jruoho return config_found_ia(device, "flashbus", &arg, flash_print);
262 1.4.4.2 jruoho }
263 1.4.4.2 jruoho
264 1.4.4.2 jruoho /**
265 1.4.4.2 jruoho * flash_open - open the character device
266 1.4.4.2 jruoho * Checks if there is a driver registered to the minor number of the open
267 1.4.4.2 jruoho * request.
268 1.4.4.2 jruoho */
269 1.4.4.2 jruoho int
270 1.4.4.2 jruoho flashopen(dev_t dev, int flags, int fmt, struct lwp *l)
271 1.4.4.2 jruoho {
272 1.4.4.2 jruoho int unit = minor(dev);
273 1.4.4.2 jruoho struct flash_softc *sc;
274 1.4.4.2 jruoho
275 1.4.4.2 jruoho DPRINTFN(1, ("flash: opening device unit %d\n", unit));
276 1.4.4.2 jruoho
277 1.4.4.2 jruoho if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
278 1.4.4.2 jruoho return ENXIO;
279 1.4.4.2 jruoho
280 1.4.4.2 jruoho /* TODO return eperm if want to open for writing a read only dev */
281 1.4.4.2 jruoho
282 1.4.4.2 jruoho /* reset buffer length */
283 1.4.4.2 jruoho // sc->sc_cache->fc_len = 0;
284 1.4.4.2 jruoho
285 1.4.4.2 jruoho return 0;
286 1.4.4.2 jruoho }
287 1.4.4.2 jruoho
288 1.4.4.2 jruoho /**
289 1.4.4.2 jruoho * flash_close - close device
290 1.4.4.2 jruoho * We don't have to release any resources, so just return 0.
291 1.4.4.2 jruoho */
292 1.4.4.2 jruoho int
293 1.4.4.2 jruoho flashclose(dev_t dev, int flags, int fmt, struct lwp *l)
294 1.4.4.2 jruoho {
295 1.4.4.2 jruoho int unit = minor(dev);
296 1.4.4.2 jruoho struct flash_softc *sc;
297 1.4.4.2 jruoho int err;
298 1.4.4.2 jruoho
299 1.4.4.2 jruoho DPRINTFN(1, ("flash: closing flash device unit %d\n", unit));
300 1.4.4.2 jruoho
301 1.4.4.2 jruoho if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
302 1.4.4.2 jruoho return ENXIO;
303 1.4.4.2 jruoho
304 1.4.4.2 jruoho if (!sc->sc_readonly) {
305 1.4.4.2 jruoho err = flash_sync(sc->sc_dev);
306 1.4.4.2 jruoho if (err)
307 1.4.4.2 jruoho return err;
308 1.4.4.2 jruoho }
309 1.4.4.2 jruoho
310 1.4.4.2 jruoho return 0;
311 1.4.4.2 jruoho }
312 1.4.4.2 jruoho
313 1.4.4.2 jruoho /**
314 1.4.4.2 jruoho * flash_read - read from character device
315 1.4.4.2 jruoho * This function uses the registered driver's read function to read the requested length to
316 1.4.4.2 jruoho * a buffer and then moves this buffer to userspace.
317 1.4.4.2 jruoho */
318 1.4.4.2 jruoho int
319 1.4.4.2 jruoho flashread(dev_t dev, struct uio *uio, int flag)
320 1.4.4.2 jruoho {
321 1.4.4.2 jruoho return physio(flashstrategy, NULL, dev, B_READ, minphys, uio);
322 1.4.4.2 jruoho }
323 1.4.4.2 jruoho
324 1.4.4.2 jruoho /**
325 1.4.4.2 jruoho * flash_write - write to character device
326 1.4.4.2 jruoho * This function moves the data into a buffer from userspace to kernel space,
327 1.4.4.2 jruoho * then uses the registered driver's write function to write out the data to
328 1.4.4.2 jruoho * the media.
329 1.4.4.2 jruoho */
330 1.4.4.2 jruoho int
331 1.4.4.2 jruoho flashwrite(dev_t dev, struct uio *uio, int flag)
332 1.4.4.2 jruoho {
333 1.4.4.2 jruoho return physio(flashstrategy, NULL, dev, B_WRITE, minphys, uio);
334 1.4.4.2 jruoho }
335 1.4.4.2 jruoho
336 1.4.4.2 jruoho void
337 1.4.4.2 jruoho flashstrategy(struct buf *bp)
338 1.4.4.2 jruoho {
339 1.4.4.2 jruoho struct flash_softc *sc;
340 1.4.4.2 jruoho const struct flash_interface *flash_if;
341 1.4.4.2 jruoho const struct flash_partition *part;
342 1.4.4.2 jruoho int unit, device_blks;
343 1.4.4.2 jruoho
344 1.4.4.2 jruoho unit = minor(bp->b_dev);
345 1.4.4.2 jruoho sc = device_lookup_private(&flash_cd, unit);
346 1.4.4.2 jruoho if (sc == NULL) {
347 1.4.4.2 jruoho bp->b_error = ENXIO;
348 1.4.4.2 jruoho goto done;
349 1.4.4.2 jruoho }
350 1.4.4.2 jruoho
351 1.4.4.2 jruoho flash_if = sc->flash_if;
352 1.4.4.2 jruoho part = &flash_if->partition;
353 1.4.4.2 jruoho
354 1.4.4.2 jruoho /* divider */
355 1.4.4.2 jruoho KASSERT(flash_if->writesize != 0);
356 1.4.4.2 jruoho
357 1.4.4.2 jruoho aprint_debug_dev(sc->sc_dev, "flash_strategy()\n");
358 1.4.4.2 jruoho
359 1.4.4.2 jruoho if (!(bp->b_flags & B_READ) && sc->sc_readonly) {
360 1.4.4.2 jruoho bp->b_error = EACCES;
361 1.4.4.2 jruoho goto done;
362 1.4.4.2 jruoho }
363 1.4.4.2 jruoho
364 1.4.4.2 jruoho /* check if length is not negative */
365 1.4.4.2 jruoho if (bp->b_blkno < 0) {
366 1.4.4.2 jruoho bp->b_error = EINVAL;
367 1.4.4.2 jruoho goto done;
368 1.4.4.2 jruoho }
369 1.4.4.2 jruoho
370 1.4.4.2 jruoho /* zero lenght i/o */
371 1.4.4.2 jruoho if (bp->b_bcount == 0) {
372 1.4.4.2 jruoho goto done;
373 1.4.4.2 jruoho }
374 1.4.4.2 jruoho
375 1.4.4.2 jruoho device_blks = sc->flash_if->size / DEV_BSIZE;
376 1.4.4.2 jruoho KASSERT(part->part_offset % DEV_BSIZE == 0);
377 1.4.4.2 jruoho bp->b_rawblkno = bp->b_blkno + (part->part_offset / DEV_BSIZE);
378 1.4.4.2 jruoho
379 1.4.4.2 jruoho if (bounds_check_with_mediasize(bp, DEV_BSIZE, device_blks) <= 0) {
380 1.4.4.2 jruoho goto done;
381 1.4.4.2 jruoho }
382 1.4.4.2 jruoho
383 1.4.4.2 jruoho bp->b_resid = bp->b_bcount;
384 1.4.4.2 jruoho flash_if->submit(sc->sc_parent_dev, bp);
385 1.4.4.2 jruoho
386 1.4.4.2 jruoho return;
387 1.4.4.2 jruoho done:
388 1.4.4.2 jruoho bp->b_resid = bp->b_bcount;
389 1.4.4.2 jruoho biodone(bp);
390 1.4.4.2 jruoho }
391 1.4.4.2 jruoho
392 1.4.4.2 jruoho /*
393 1.4.4.2 jruoho * Handle the ioctl for the device
394 1.4.4.2 jruoho */
395 1.4.4.2 jruoho int
396 1.4.4.2 jruoho flashioctl(dev_t dev, u_long command, void *data, int flags, struct lwp *l)
397 1.4.4.2 jruoho {
398 1.4.4.2 jruoho struct flash_erase_params *ep;
399 1.4.4.2 jruoho struct flash_info_params *ip;
400 1.4.4.2 jruoho struct flash_dump_params *dp;
401 1.4.4.2 jruoho struct flash_badblock_params *bbp;
402 1.4.4.2 jruoho struct flash_erase_instruction ei;
403 1.4.4.2 jruoho struct flash_softc *sc;
404 1.4.4.2 jruoho int unit, err;
405 1.4.4.2 jruoho size_t retlen;
406 1.4.4.2 jruoho flash_off_t offset;
407 1.4.4.2 jruoho bool bad;
408 1.4.4.2 jruoho
409 1.4.4.2 jruoho unit = minor(dev);
410 1.4.4.2 jruoho if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
411 1.4.4.2 jruoho return ENXIO;
412 1.4.4.2 jruoho
413 1.4.4.2 jruoho err = 0;
414 1.4.4.2 jruoho switch (command) {
415 1.4.4.2 jruoho case FLASH_ERASE_BLOCK:
416 1.4.4.2 jruoho /**
417 1.4.4.2 jruoho * Set up an erase instruction then call the registered
418 1.4.4.2 jruoho * driver's erase operation.
419 1.4.4.2 jruoho */
420 1.4.4.2 jruoho ep = data;
421 1.4.4.2 jruoho
422 1.4.4.2 jruoho if (sc->sc_readonly) {
423 1.4.4.2 jruoho return EACCES;
424 1.4.4.2 jruoho }
425 1.4.4.2 jruoho
426 1.4.4.2 jruoho ei.ei_addr = ep->ep_addr;
427 1.4.4.2 jruoho ei.ei_len = ep->ep_len;
428 1.4.4.2 jruoho ei.ei_callback = NULL;
429 1.4.4.2 jruoho
430 1.4.4.2 jruoho err = flash_erase(sc->sc_dev, &ei);
431 1.4.4.2 jruoho if (err) {
432 1.4.4.2 jruoho return err;
433 1.4.4.2 jruoho }
434 1.4.4.2 jruoho
435 1.4.4.2 jruoho break;
436 1.4.4.2 jruoho case FLASH_BLOCK_ISBAD:
437 1.4.4.2 jruoho /**
438 1.4.4.2 jruoho * Set up an erase instruction then call the registered
439 1.4.4.2 jruoho * driver's erase operation.
440 1.4.4.2 jruoho */
441 1.4.4.2 jruoho bbp = data;
442 1.4.4.2 jruoho
443 1.4.4.2 jruoho err = flash_block_isbad(sc->sc_dev, bbp->bbp_addr, &bad);
444 1.4.4.2 jruoho if (err) {
445 1.4.4.2 jruoho return err;
446 1.4.4.2 jruoho }
447 1.4.4.2 jruoho bbp->bbp_isbad = bad;
448 1.4.4.2 jruoho
449 1.4.4.2 jruoho break;
450 1.4.4.2 jruoho case FLASH_BLOCK_MARKBAD:
451 1.4.4.2 jruoho bbp = data;
452 1.4.4.2 jruoho
453 1.4.4.2 jruoho err = flash_block_markbad(sc->sc_dev, bbp->bbp_addr);
454 1.4.4.2 jruoho
455 1.4.4.2 jruoho break;
456 1.4.4.2 jruoho case FLASH_DUMP:
457 1.4.4.2 jruoho dp = data;
458 1.4.4.2 jruoho offset = dp->dp_block * sc->flash_if->erasesize;
459 1.4.4.2 jruoho DPRINTF(("Reading from block: %jd len: %jd\n",
460 1.4.4.2 jruoho (intmax_t )dp->dp_block, (intmax_t )dp->dp_len));
461 1.4.4.2 jruoho err = flash_read(sc->sc_parent_dev, offset, dp->dp_len,
462 1.4.4.2 jruoho &retlen, dp->dp_buf);
463 1.4.4.2 jruoho if (err)
464 1.4.4.2 jruoho return err;
465 1.4.4.2 jruoho if (retlen != dp->dp_len) {
466 1.4.4.2 jruoho dp->dp_len = -1;
467 1.4.4.2 jruoho dp->dp_buf = NULL;
468 1.4.4.2 jruoho }
469 1.4.4.2 jruoho
470 1.4.4.2 jruoho break;
471 1.4.4.2 jruoho case FLASH_GET_INFO:
472 1.4.4.2 jruoho ip = data;
473 1.4.4.2 jruoho
474 1.4.4.2 jruoho ip->ip_page_size = sc->flash_if->page_size;
475 1.4.4.2 jruoho ip->ip_erase_size = sc->flash_if->erasesize;
476 1.4.4.2 jruoho ip->ip_flash_type = sc->flash_if->type;
477 1.4.4.2 jruoho ip->ip_flash_size = sc->flash_if->size;
478 1.4.4.2 jruoho break;
479 1.4.4.2 jruoho default:
480 1.4.4.2 jruoho err = ENODEV;
481 1.4.4.2 jruoho }
482 1.4.4.2 jruoho
483 1.4.4.2 jruoho return err;
484 1.4.4.2 jruoho }
485 1.4.4.2 jruoho
486 1.4.4.2 jruoho int
487 1.4.4.2 jruoho flashsize(dev_t dev)
488 1.4.4.2 jruoho {
489 1.4.4.2 jruoho return -1;
490 1.4.4.2 jruoho }
491 1.4.4.2 jruoho
492 1.4.4.2 jruoho int
493 1.4.4.2 jruoho flashdump(dev_t dev, daddr_t blkno, void *va, size_t size)
494 1.4.4.2 jruoho {
495 1.4.4.2 jruoho return EACCES;
496 1.4.4.2 jruoho }
497 1.4.4.2 jruoho
498 1.4.4.2 jruoho bool
499 1.4.4.2 jruoho flash_shutdown(device_t self, int how)
500 1.4.4.2 jruoho {
501 1.4.4.2 jruoho struct flash_softc *sc = device_private(self);
502 1.4.4.2 jruoho
503 1.4.4.2 jruoho if ((how & RB_NOSYNC) == 0 && !sc->sc_readonly)
504 1.4.4.2 jruoho flash_sync(self);
505 1.4.4.2 jruoho
506 1.4.4.2 jruoho return true;
507 1.4.4.2 jruoho }
508 1.4.4.2 jruoho
509 1.4.4.2 jruoho const struct flash_interface *
510 1.4.4.2 jruoho flash_get_interface(dev_t dev)
511 1.4.4.2 jruoho {
512 1.4.4.2 jruoho struct flash_softc *sc;
513 1.4.4.2 jruoho int unit;
514 1.4.4.2 jruoho
515 1.4.4.2 jruoho unit = minor(dev);
516 1.4.4.2 jruoho if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
517 1.4.4.2 jruoho return NULL;
518 1.4.4.2 jruoho
519 1.4.4.2 jruoho return sc->flash_if;
520 1.4.4.2 jruoho }
521 1.4.4.2 jruoho
522 1.4.4.2 jruoho const struct flash_softc *
523 1.4.4.2 jruoho flash_get_softc(dev_t dev)
524 1.4.4.2 jruoho {
525 1.4.4.2 jruoho struct flash_softc *sc;
526 1.4.4.2 jruoho int unit;
527 1.4.4.2 jruoho
528 1.4.4.2 jruoho unit = minor(dev);
529 1.4.4.2 jruoho sc = device_lookup_private(&flash_cd, unit);
530 1.4.4.2 jruoho
531 1.4.4.2 jruoho return sc;
532 1.4.4.2 jruoho }
533 1.4.4.2 jruoho
534 1.4.4.2 jruoho device_t
535 1.4.4.2 jruoho flash_get_device(dev_t dev)
536 1.4.4.2 jruoho {
537 1.4.4.2 jruoho struct flash_softc *sc;
538 1.4.4.2 jruoho int unit;
539 1.4.4.2 jruoho
540 1.4.4.2 jruoho unit = minor(dev);
541 1.4.4.2 jruoho sc = device_lookup_private(&flash_cd, unit);
542 1.4.4.2 jruoho
543 1.4.4.2 jruoho return sc->sc_dev;
544 1.4.4.2 jruoho }
545 1.4.4.2 jruoho
546 1.4.4.2 jruoho static inline flash_off_t
547 1.4.4.2 jruoho flash_get_part_offset(struct flash_softc *fl, size_t poffset)
548 1.4.4.2 jruoho {
549 1.4.4.2 jruoho return fl->flash_if->partition.part_offset + poffset;
550 1.4.4.2 jruoho }
551 1.4.4.2 jruoho
552 1.4.4.2 jruoho int
553 1.4.4.2 jruoho flash_erase(device_t self, struct flash_erase_instruction *ei)
554 1.4.4.2 jruoho {
555 1.4.4.2 jruoho struct flash_softc *sc = device_private(self);
556 1.4.4.2 jruoho KASSERT(ei != NULL);
557 1.4.4.2 jruoho struct flash_erase_instruction e = *ei;
558 1.4.4.2 jruoho
559 1.4.4.2 jruoho if (sc->sc_readonly)
560 1.4.4.2 jruoho return EACCES;
561 1.4.4.2 jruoho
562 1.4.4.2 jruoho /* adjust for flash partition */
563 1.4.4.2 jruoho e.ei_addr += sc->flash_if->partition.part_offset;
564 1.4.4.2 jruoho
565 1.4.4.2 jruoho /* bounds check for flash partition */
566 1.4.4.2 jruoho if (e.ei_addr + e.ei_len > sc->flash_if->partition.part_size +
567 1.4.4.2 jruoho sc->flash_if->partition.part_offset)
568 1.4.4.2 jruoho return EINVAL;
569 1.4.4.2 jruoho
570 1.4.4.2 jruoho return sc->flash_if->erase(device_parent(self), &e);
571 1.4.4.2 jruoho }
572 1.4.4.2 jruoho
573 1.4.4.2 jruoho int
574 1.4.4.2 jruoho flash_read(device_t self,
575 1.4.4.2 jruoho flash_off_t offset, size_t len, size_t *retlen, uint8_t *buf)
576 1.4.4.2 jruoho {
577 1.4.4.2 jruoho struct flash_softc *sc = device_private(self);
578 1.4.4.2 jruoho
579 1.4.4.2 jruoho offset += sc->flash_if->partition.part_offset;
580 1.4.4.2 jruoho
581 1.4.4.2 jruoho if (offset + len > sc->flash_if->partition.part_size +
582 1.4.4.2 jruoho sc->flash_if->partition.part_offset)
583 1.4.4.2 jruoho return EINVAL;
584 1.4.4.2 jruoho
585 1.4.4.2 jruoho return sc->flash_if->read(device_parent(self),
586 1.4.4.2 jruoho offset, len, retlen, buf);
587 1.4.4.2 jruoho }
588 1.4.4.2 jruoho
589 1.4.4.2 jruoho int
590 1.4.4.2 jruoho flash_write(device_t self,
591 1.4.4.2 jruoho flash_off_t offset, size_t len, size_t *retlen, const uint8_t *buf)
592 1.4.4.2 jruoho {
593 1.4.4.2 jruoho struct flash_softc *sc = device_private(self);
594 1.4.4.2 jruoho
595 1.4.4.2 jruoho if (sc->sc_readonly)
596 1.4.4.2 jruoho return EACCES;
597 1.4.4.2 jruoho
598 1.4.4.2 jruoho offset += sc->flash_if->partition.part_offset;
599 1.4.4.2 jruoho
600 1.4.4.2 jruoho if (offset + len > sc->flash_if->partition.part_size +
601 1.4.4.2 jruoho sc->flash_if->partition.part_offset)
602 1.4.4.2 jruoho return EINVAL;
603 1.4.4.2 jruoho
604 1.4.4.2 jruoho return sc->flash_if->write(device_parent(self),
605 1.4.4.2 jruoho offset, len, retlen, buf);
606 1.4.4.2 jruoho }
607 1.4.4.2 jruoho
608 1.4.4.2 jruoho int
609 1.4.4.2 jruoho flash_block_markbad(device_t self, flash_off_t offset)
610 1.4.4.2 jruoho {
611 1.4.4.2 jruoho struct flash_softc *sc = device_private(self);
612 1.4.4.2 jruoho
613 1.4.4.2 jruoho if (sc->sc_readonly)
614 1.4.4.2 jruoho return EACCES;
615 1.4.4.2 jruoho
616 1.4.4.2 jruoho offset += sc->flash_if->partition.part_offset;
617 1.4.4.2 jruoho
618 1.4.4.2 jruoho if (offset + sc->flash_if->erasesize >=
619 1.4.4.2 jruoho sc->flash_if->partition.part_size +
620 1.4.4.2 jruoho sc->flash_if->partition.part_offset)
621 1.4.4.2 jruoho return EINVAL;
622 1.4.4.2 jruoho
623 1.4.4.2 jruoho return sc->flash_if->block_markbad(device_parent(self), offset);
624 1.4.4.2 jruoho }
625 1.4.4.2 jruoho
626 1.4.4.2 jruoho int
627 1.4.4.2 jruoho flash_block_isbad(device_t self, flash_off_t offset, bool *bad)
628 1.4.4.2 jruoho {
629 1.4.4.2 jruoho struct flash_softc *sc = device_private(self);
630 1.4.4.2 jruoho
631 1.4.4.2 jruoho offset += sc->flash_if->partition.part_offset;
632 1.4.4.2 jruoho
633 1.4.4.2 jruoho if (offset + sc->flash_if->erasesize >
634 1.4.4.2 jruoho sc->flash_if->partition.part_size +
635 1.4.4.2 jruoho sc->flash_if->partition.part_offset)
636 1.4.4.2 jruoho return EINVAL;
637 1.4.4.2 jruoho
638 1.4.4.2 jruoho return sc->flash_if->block_isbad(device_parent(self), offset, bad);
639 1.4.4.2 jruoho }
640 1.4.4.2 jruoho
641 1.4.4.2 jruoho int
642 1.4.4.2 jruoho flash_sync(device_t self)
643 1.4.4.2 jruoho {
644 1.4.4.2 jruoho struct flash_softc *sc = device_private(self);
645 1.4.4.2 jruoho
646 1.4.4.2 jruoho if (sc->sc_readonly)
647 1.4.4.2 jruoho return EACCES;
648 1.4.4.2 jruoho
649 1.4.4.2 jruoho /* noop now TODO: implement */
650 1.4.4.2 jruoho return 0;
651 1.4.4.2 jruoho }
652 1.4.4.2 jruoho
653 1.4.4.2 jruoho MODULE(MODULE_CLASS_DRIVER, flash, NULL);
654 1.4.4.2 jruoho
655 1.4.4.2 jruoho #ifdef _MODULE
656 1.4.4.2 jruoho #include "ioconf.c"
657 1.4.4.2 jruoho #endif
658 1.4.4.2 jruoho
659 1.4.4.2 jruoho static int
660 1.4.4.2 jruoho flash_modcmd(modcmd_t cmd, void *opaque)
661 1.4.4.2 jruoho {
662 1.4.4.2 jruoho int error = 0;
663 1.4.4.2 jruoho #ifdef _MODULE
664 1.4.4.2 jruoho int bmaj = -1, cmaj = -1;
665 1.4.4.2 jruoho #endif
666 1.4.4.2 jruoho
667 1.4.4.2 jruoho switch (cmd) {
668 1.4.4.2 jruoho case MODULE_CMD_INIT:
669 1.4.4.2 jruoho #ifdef _MODULE
670 1.4.4.2 jruoho error = config_init_component(cfdriver_ioconf_flash,
671 1.4.4.2 jruoho cfattach_ioconf_flash, cfdata_ioconf_flash);
672 1.4.4.2 jruoho if (error)
673 1.4.4.2 jruoho return error;
674 1.4.4.2 jruoho error = devsw_attach("flash", &flash_bdevsw, &bmaj,
675 1.4.4.2 jruoho &flash_cdevsw, &cmaj);
676 1.4.4.2 jruoho if (error)
677 1.4.4.2 jruoho config_fini_component(cfdriver_ioconf_flash,
678 1.4.4.2 jruoho cfattach_ioconf_flash, cfdata_ioconf_flash);
679 1.4.4.2 jruoho #endif
680 1.4.4.2 jruoho return error;
681 1.4.4.2 jruoho case MODULE_CMD_FINI:
682 1.4.4.2 jruoho #ifdef _MODULE
683 1.4.4.2 jruoho devsw_detach(&flash_bdevsw, &flash_cdevsw);
684 1.4.4.2 jruoho error = config_fini_component(cfdriver_ioconf_flash,
685 1.4.4.2 jruoho cfattach_ioconf_flash, cfdata_ioconf_flash);
686 1.4.4.2 jruoho #endif
687 1.4.4.2 jruoho return error;
688 1.4.4.2 jruoho default:
689 1.4.4.2 jruoho return ENOTTY;
690 1.4.4.2 jruoho }
691 1.4.4.2 jruoho }
692