2 * Copyright (c) 2007, Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/ioccom.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
43 #include <sys/sysctl.h>
44 #include <sys/types.h>
47 #include <sys/cfictl.h>
49 #include <machine/atomic.h>
50 #include <machine/bus.h>
52 #include <dev/cfi/cfi_var.h>
54 static d_open_t cfi_devopen;
55 static d_close_t cfi_devclose;
56 static d_read_t cfi_devread;
57 static d_write_t cfi_devwrite;
58 static d_ioctl_t cfi_devioctl;
60 struct cdevsw cfi_cdevsw = {
61 .d_version = D_VERSION,
63 .d_name = cfi_driver_name,
64 .d_open = cfi_devopen,
65 .d_close = cfi_devclose,
66 .d_read = cfi_devread,
67 .d_write = cfi_devwrite,
68 .d_ioctl = cfi_devioctl,
72 * Begin writing into a new block/sector. We read the sector into
73 * memory and keep updating that, until we move into another sector
74 * or the process stops writing. At that time we write the whole
75 * sector to flash (see cfi_block_finish).
78 cfi_block_start(struct cfi_softc *sc, u_int ofs)
90 for (r = 0; r < sc->sc_regions; r++) {
91 rsz = sc->sc_region[r].r_blocks * sc->sc_region[r].r_blksz;
96 if (r == sc->sc_regions)
99 sc->sc_wrbufsz = sc->sc_region[r].r_blksz;
100 sc->sc_wrbuf = malloc(sc->sc_wrbufsz, M_TEMP, M_WAITOK);
101 sc->sc_wrofs = ofs - (ofs - rofs) % sc->sc_wrbufsz;
103 /* Read the block from flash for byte-serving. */
104 ptr.x8 = sc->sc_wrbuf;
105 for (r = 0; r < sc->sc_wrbufsz; r += sc->sc_width) {
106 val = cfi_read(sc, sc->sc_wrofs + r);
107 switch (sc->sc_width) {
124 * Finish updating the current block/sector by writing the compound
125 * set of changes to the flash.
128 cfi_block_finish(struct cfi_softc *sc)
132 error = cfi_write_block(sc);
133 free(sc->sc_wrbuf, M_TEMP);
142 cfi_devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
144 struct cfi_softc *sc;
147 /* We allow only 1 open. */
148 if (!atomic_cmpset_acq_ptr((uintptr_t *)&sc->sc_opened,
149 (uintptr_t)NULL, (uintptr_t)td->td_proc))
155 cfi_devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
157 struct cfi_softc *sc;
161 /* Sanity. Not really necessary. */
162 if (sc->sc_opened != td->td_proc)
165 error = (sc->sc_writing) ? cfi_block_finish(sc) : 0;
166 sc->sc_opened = NULL;
171 cfi_devread(struct cdev *dev, struct uio *uio, int ioflag)
178 struct cfi_softc *sc;
185 error = (sc->sc_writing) ? cfi_block_finish(sc) : 0;
187 error = (uio->uio_offset > sc->sc_size) ? EIO : 0;
189 while (error == 0 && uio->uio_resid > 0 &&
190 uio->uio_offset < sc->sc_size) {
191 ofs = uio->uio_offset;
192 val = cfi_read(sc, ofs);
193 switch (sc->sc_width) {
204 ofs &= sc->sc_width - 1;
205 error = uiomove(buf.x8 + ofs,
206 MIN(uio->uio_resid, sc->sc_width - ofs), uio);
212 cfi_devwrite(struct cdev *dev, struct uio *uio, int ioflag)
214 struct cfi_softc *sc;
220 error = (uio->uio_offset > sc->sc_size) ? EIO : 0;
221 while (error == 0 && uio->uio_resid > 0 &&
222 uio->uio_offset < sc->sc_size) {
223 ofs = uio->uio_offset;
226 * Finish the current block if we're about to write
227 * to a different block.
229 if (sc->sc_writing) {
230 top = sc->sc_wrofs + sc->sc_wrbufsz;
231 if (ofs < sc->sc_wrofs || ofs >= top)
232 cfi_block_finish(sc);
235 /* Start writing to a (new) block if applicable. */
236 if (!sc->sc_writing) {
237 error = cfi_block_start(sc, uio->uio_offset);
242 top = sc->sc_wrofs + sc->sc_wrbufsz;
243 error = uiomove(sc->sc_wrbuf + ofs - sc->sc_wrofs,
244 MIN(top - ofs, uio->uio_resid), uio);
250 cfi_devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
253 struct cfi_softc *sc;
263 if (sc->sc_writing) {
264 error = cfi_block_finish(sc);
268 rq = (struct cfiocqry *)data;
269 if (rq->offset >= sc->sc_size / sc->sc_width)
271 if (rq->offset + rq->count > sc->sc_size / sc->sc_width)
274 while (!error && rq->count--) {
275 val = cfi_read_qry(sc, rq->offset++);
276 error = copyout(&val, rq->buffer++, 1);
279 #ifdef CFI_SUPPORT_STRATAFLASH
280 case CFIOCGFACTORYPR:
281 error = cfi_intel_get_factory_pr(sc, (uint64_t *)data);
284 error = cfi_intel_get_oem_pr(sc, (uint64_t *)data);
287 error = cfi_intel_set_oem_pr(sc, *(uint64_t *)data);
290 error = cfi_intel_get_plr(sc, (uint32_t *)data);
293 error = cfi_intel_set_plr(sc);
295 #endif /* CFI_SUPPORT_STRATAFLASH */