2 * Copyright (c) 2019 Justin Hibbits
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/param.h>
27 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/kthread.h>
33 #include <sys/module.h>
34 #include <sys/mutex.h>
36 #include <geom/geom_disk.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/ofw/openfirm.h>
47 * OPAL System flash driver, using OPAL firmware calls to access the device.
49 * This just presents the base block interface. The fdt_slicer can be used on
50 * top to present the partitions listed in the fdt.
52 * There are three OPAL methods used: OPAL_FLASH_READ, OPAL_FLASH_WRITE, and
53 * OPAL_FLASH_ERASE. At the firmware layer, READ and WRITE can be on arbitrary
54 * boundaries, but ERASE is only at flash-block-size block alignments and sizes.
55 * To account for this, the following restrictions are in place:
57 * - Reads are on a 512-byte block boundary and size
58 * - Writes and Erases are aligned and sized on flash-block-size bytes.
60 * In order to support the fdt_slicer we present a type attribute of
63 struct opalflash_softc {
68 struct bio_queue_head sc_bio_queue;
70 bool sc_erase; /* Erase is needed before write. */
73 #define OPALFLASH_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
74 #define OPALFLASH_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
75 #define OPALFLASH_LOCK_INIT(sc) \
76 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->sc_dev), \
79 #define FLASH_BLOCKSIZE 512
81 static int opalflash_probe(device_t);
82 static int opalflash_attach(device_t);
84 static device_method_t opalflash_methods[] = {
85 /* Device interface */
86 DEVMETHOD(device_probe, opalflash_probe),
87 DEVMETHOD(device_attach, opalflash_attach),
92 static driver_t opalflash_driver = {
95 sizeof(struct opalflash_softc)
98 DRIVER_MODULE(opalflash, opal, opalflash_driver, 0, 0);
100 /* GEOM Disk interfaces. */
102 opalflash_open(struct disk *dp)
109 opalflash_close(struct disk *dp)
116 opalflash_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
123 /* Handle the one attribute we need to play nice with geom_flashmap. */
125 opalflash_getattr(struct bio *bp)
127 struct opalflash_softc *sc;
130 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
133 sc = bp->bio_disk->d_drv1;
136 if (strcmp(bp->bio_attribute, "NAND::device") == 0) {
137 if (bp->bio_length != sizeof(dev))
139 bcopy(&dev, bp->bio_data, sizeof(dev));
146 opalflash_strategy(struct bio *bp)
148 struct opalflash_softc *sc;
150 sc = (struct opalflash_softc *)bp->bio_disk->d_drv1;
152 bioq_disksort(&sc->sc_bio_queue, bp);
154 OPALFLASH_UNLOCK(sc);
158 opalflash_read(struct opalflash_softc *sc, off_t off,
159 caddr_t data, off_t count)
164 /* Ensure we write aligned to a full block size. */
165 if (off % sc->sc_disk->d_sectorsize != 0 ||
166 count % sc->sc_disk->d_sectorsize != 0)
169 token = opal_alloc_async_token();
172 * Read one page at a time. It's not guaranteed that the buffer is
173 * physically contiguous.
177 size = MIN(count, PAGE_SIZE);
178 size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK));
179 rv = opal_call(OPAL_FLASH_READ, sc->sc_opal_id, off,
180 vtophys(data), size, token);
181 if (rv == OPAL_ASYNC_COMPLETION) {
182 rv = opal_wait_completion(&msg, sizeof(msg), token);
183 if (rv == OPAL_SUCCESS)
186 if (rv != OPAL_SUCCESS)
192 opal_free_async_token(token);
193 if (rv == OPAL_SUCCESS)
202 opalflash_erase(struct opalflash_softc *sc, off_t off, off_t count)
207 /* Ensure we write aligned to a full block size. */
208 if (off % sc->sc_disk->d_stripesize != 0 ||
209 count % sc->sc_disk->d_stripesize != 0)
212 token = opal_alloc_async_token();
214 rv = opal_call(OPAL_FLASH_ERASE, sc->sc_opal_id, off, count, token);
215 if (rv == OPAL_ASYNC_COMPLETION) {
216 rv = opal_wait_completion(&msg, sizeof(msg), token);
217 if (rv == OPAL_SUCCESS)
220 opal_free_async_token(token);
222 if (rv == OPAL_SUCCESS)
231 opalflash_write(struct opalflash_softc *sc, off_t off,
232 caddr_t data, off_t count)
237 /* Ensure we write aligned to a full block size. */
238 if (off % sc->sc_disk->d_sectorsize != 0 ||
239 count % sc->sc_disk->d_sectorsize != 0)
243 /* Erase the full block first, then write in page chunks. */
244 rv = opalflash_erase(sc, off, count);
249 token = opal_alloc_async_token();
252 * Write one page at a time. It's not guaranteed that the buffer is
253 * physically contiguous.
256 size = MIN(count, PAGE_SIZE);
257 size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK));
258 rv = opal_call(OPAL_FLASH_WRITE, sc->sc_opal_id, off,
259 vtophys(data), size, token);
260 if (rv == OPAL_ASYNC_COMPLETION) {
261 rv = opal_wait_completion(&msg, sizeof(msg), token);
262 if (rv == OPAL_SUCCESS)
265 if (rv != OPAL_SUCCESS)
271 opal_free_async_token(token);
273 if (rv == OPAL_SUCCESS)
281 /* Main flash handling task. */
283 opalflash_task(void *arg)
285 struct opalflash_softc *sc;
293 bp = bioq_first(&sc->sc_bio_queue);
295 msleep(sc, &sc->sc_mtx, PRIBIO, "opalflash", 0);
296 } while (bp == NULL);
297 bioq_remove(&sc->sc_bio_queue, bp);
298 OPALFLASH_UNLOCK(sc);
300 switch (bp->bio_cmd) {
302 bp->bio_error = opalflash_erase(sc, bp->bio_offset,
306 bp->bio_error = opalflash_read(sc, bp->bio_offset,
307 bp->bio_data, bp->bio_bcount);
310 bp->bio_error = opalflash_write(sc, bp->bio_offset,
311 bp->bio_data, bp->bio_bcount);
314 bp->bio_error = EINVAL;
320 /* Device driver interfaces. */
323 opalflash_probe(device_t dev)
325 if (!ofw_bus_is_compatible(dev, "ibm,opal-flash"))
328 device_set_desc(dev, "OPAL System Flash");
330 return (BUS_PROBE_GENERIC);
334 opalflash_attach(device_t dev)
336 struct opalflash_softc *sc;
338 cell_t flash_blocksize, opal_id;
341 sc = device_get_softc(dev);
344 node = ofw_bus_get_node(dev);
345 OF_getencprop(node, "ibm,opal-id", &opal_id, sizeof(opal_id));
346 sc->sc_opal_id = opal_id;
348 if (OF_getencprop(node, "ibm,flash-block-size",
349 &flash_blocksize, sizeof(flash_blocksize)) < 0) {
350 device_printf(dev, "Cannot determine flash block size.\n");
354 if (!OF_hasprop(node, "no-erase"))
357 OPALFLASH_LOCK_INIT(sc);
359 if (OF_getencprop(node, "reg", regs, sizeof(regs)) < 0) {
360 device_printf(dev, "Unable to get flash size.\n");
364 sc->sc_disk = disk_alloc();
365 sc->sc_disk->d_name = "opalflash";
366 sc->sc_disk->d_open = opalflash_open;
367 sc->sc_disk->d_close = opalflash_close;
368 sc->sc_disk->d_strategy = opalflash_strategy;
369 sc->sc_disk->d_ioctl = opalflash_ioctl;
370 sc->sc_disk->d_getattr = opalflash_getattr;
371 sc->sc_disk->d_drv1 = sc;
372 sc->sc_disk->d_maxsize = DFLTPHYS;
373 sc->sc_disk->d_mediasize = regs[1];
374 sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
375 sc->sc_disk->d_sectorsize = FLASH_BLOCKSIZE;
376 sc->sc_disk->d_stripesize = flash_blocksize;
377 sc->sc_disk->d_dump = NULL;
379 disk_create(sc->sc_disk, DISK_VERSION);
380 bioq_init(&sc->sc_bio_queue);
382 kproc_create(&opalflash_task, sc, &sc->sc_p, 0, 0, "task: OPAL Flash");