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(&sc->sc_opened, NULL, td->td_proc))
154 cfi_devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
156 struct cfi_softc *sc;
160 /* Sanity. Not really necessary. */
161 if (sc->sc_opened != td->td_proc)
164 error = (sc->sc_writing) ? cfi_block_finish(sc) : 0;
165 sc->sc_opened = NULL;
170 cfi_devread(struct cdev *dev, struct uio *uio, int ioflag)
177 struct cfi_softc *sc;
184 error = (sc->sc_writing) ? cfi_block_finish(sc) : 0;
186 error = (uio->uio_offset > sc->sc_size) ? EIO : 0;
188 while (error == 0 && uio->uio_resid > 0 &&
189 uio->uio_offset < sc->sc_size) {
190 ofs = uio->uio_offset;
191 val = cfi_read(sc, ofs);
192 switch (sc->sc_width) {
203 ofs &= sc->sc_width - 1;
204 error = uiomove(buf.x8 + ofs,
205 MIN(uio->uio_resid, sc->sc_width - ofs), uio);
211 cfi_devwrite(struct cdev *dev, struct uio *uio, int ioflag)
213 struct cfi_softc *sc;
219 error = (uio->uio_offset > sc->sc_size) ? EIO : 0;
220 while (error == 0 && uio->uio_resid > 0 &&
221 uio->uio_offset < sc->sc_size) {
222 ofs = uio->uio_offset;
225 * Finish the current block if we're about to write
226 * to a different block.
228 if (sc->sc_writing) {
229 top = sc->sc_wrofs + sc->sc_wrbufsz;
230 if (ofs < sc->sc_wrofs || ofs >= top)
231 cfi_block_finish(sc);
234 /* Start writing to a (new) block if applicable. */
235 if (!sc->sc_writing) {
236 error = cfi_block_start(sc, uio->uio_offset);
241 top = sc->sc_wrofs + sc->sc_wrbufsz;
242 error = uiomove(sc->sc_wrbuf + ofs - sc->sc_wrofs,
243 MIN(top - ofs, uio->uio_resid), uio);
249 cfi_devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
252 struct cfi_softc *sc;
262 if (sc->sc_writing) {
263 error = cfi_block_finish(sc);
267 rq = (struct cfiocqry *)data;
268 if (rq->offset >= sc->sc_size / sc->sc_width)
270 if (rq->offset + rq->count > sc->sc_size / sc->sc_width)
273 while (!error && rq->count--) {
274 val = cfi_read_qry(sc, rq->offset++);
275 error = copyout(&val, rq->buffer++, 1);
278 #ifdef CFI_SUPPORT_STRATAFLASH
279 case CFIOCGFACTORYPR:
280 error = cfi_intel_get_factory_pr(sc, (uint64_t *)data);
283 error = cfi_intel_get_oem_pr(sc, (uint64_t *)data);
286 error = cfi_intel_set_oem_pr(sc, *(uint64_t *)data);
289 error = cfi_intel_get_plr(sc, (uint32_t *)data);
292 error = cfi_intel_set_plr(sc);
294 #endif /* CFI_SUPPORT_STRATAFLASH */