xref: /freebsd/sys/powerpc/powernv/opal_flash.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
16775dfdfSJustin Hibbits /*-
26775dfdfSJustin Hibbits  * Copyright (c) 2019 Justin Hibbits
36775dfdfSJustin Hibbits  *
46775dfdfSJustin Hibbits  * Redistribution and use in source and binary forms, with or without
56775dfdfSJustin Hibbits  * modification, are permitted provided that the following conditions
66775dfdfSJustin Hibbits  * are met:
76775dfdfSJustin Hibbits  * 1. Redistributions of source code must retain the above copyright
86775dfdfSJustin Hibbits  *    notice, this list of conditions and the following disclaimer.
96775dfdfSJustin Hibbits  * 2. Redistributions in binary form must reproduce the above copyright
106775dfdfSJustin Hibbits  *    notice, this list of conditions and the following disclaimer in the
116775dfdfSJustin Hibbits  *    documentation and/or other materials provided with the distribution.
126775dfdfSJustin Hibbits  *
136775dfdfSJustin Hibbits  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
146775dfdfSJustin Hibbits  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
156775dfdfSJustin Hibbits  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
166775dfdfSJustin Hibbits  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
176775dfdfSJustin Hibbits  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
186775dfdfSJustin Hibbits  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
196775dfdfSJustin Hibbits  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
206775dfdfSJustin Hibbits  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
216775dfdfSJustin Hibbits  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
226775dfdfSJustin Hibbits  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
236775dfdfSJustin Hibbits  * SUCH DAMAGE.
246775dfdfSJustin Hibbits  */
256775dfdfSJustin Hibbits 
266775dfdfSJustin Hibbits #include <sys/param.h>
276775dfdfSJustin Hibbits #include <sys/systm.h>
286775dfdfSJustin Hibbits #include <sys/bio.h>
296775dfdfSJustin Hibbits #include <sys/bus.h>
306775dfdfSJustin Hibbits #include <sys/kernel.h>
316775dfdfSJustin Hibbits #include <sys/kthread.h>
32e2e050c8SConrad Meyer #include <sys/lock.h>
336775dfdfSJustin Hibbits #include <sys/module.h>
346775dfdfSJustin Hibbits #include <sys/mutex.h>
356775dfdfSJustin Hibbits #include <sys/proc.h>
366775dfdfSJustin Hibbits #include <geom/geom_disk.h>
376775dfdfSJustin Hibbits 
386775dfdfSJustin Hibbits #include <vm/vm.h>
396775dfdfSJustin Hibbits #include <vm/pmap.h>
406775dfdfSJustin Hibbits 
416775dfdfSJustin Hibbits #include <dev/ofw/ofw_bus.h>
426775dfdfSJustin Hibbits #include <dev/ofw/ofw_bus_subr.h>
436775dfdfSJustin Hibbits #include <dev/ofw/openfirm.h>
446775dfdfSJustin Hibbits #include "opal.h"
456775dfdfSJustin Hibbits 
466775dfdfSJustin Hibbits /*
476775dfdfSJustin Hibbits  * OPAL System flash driver, using OPAL firmware calls to access the device.
486775dfdfSJustin Hibbits  *
496775dfdfSJustin Hibbits  * This just presents the base block interface.  The fdt_slicer can be used on
506775dfdfSJustin Hibbits  * top to present the partitions listed in the fdt.
516775dfdfSJustin Hibbits  *
526775dfdfSJustin Hibbits  * There are three OPAL methods used: OPAL_FLASH_READ, OPAL_FLASH_WRITE, and
536775dfdfSJustin Hibbits  * OPAL_FLASH_ERASE.  At the firmware layer, READ and WRITE can be on arbitrary
546775dfdfSJustin Hibbits  * boundaries, but ERASE is only at flash-block-size block alignments and sizes.
556775dfdfSJustin Hibbits  * To account for this, the following restrictions are in place:
566775dfdfSJustin Hibbits  *
576775dfdfSJustin Hibbits  * - Reads are on a 512-byte block boundary and size
586775dfdfSJustin Hibbits  * - Writes and Erases are aligned and sized on flash-block-size bytes.
596775dfdfSJustin Hibbits  *
606775dfdfSJustin Hibbits  * In order to support the fdt_slicer we present a type attribute of
616775dfdfSJustin Hibbits  * NAND::device.
626775dfdfSJustin Hibbits  */
636775dfdfSJustin Hibbits struct opalflash_softc {
646775dfdfSJustin Hibbits 	device_t		 sc_dev;
656775dfdfSJustin Hibbits 	struct mtx		 sc_mtx;
666775dfdfSJustin Hibbits 	struct disk		*sc_disk;
676775dfdfSJustin Hibbits 	struct proc		*sc_p;
686775dfdfSJustin Hibbits 	struct bio_queue_head	 sc_bio_queue;
696775dfdfSJustin Hibbits 	int		 	 sc_opal_id;
70bc60451aSJustin Hibbits 	bool			 sc_erase; /* Erase is needed before write. */
716775dfdfSJustin Hibbits };
726775dfdfSJustin Hibbits 
736775dfdfSJustin Hibbits #define	OPALFLASH_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
746775dfdfSJustin Hibbits #define	OPALFLASH_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
756775dfdfSJustin Hibbits #define	OPALFLASH_LOCK_INIT(sc) \
766775dfdfSJustin Hibbits 	mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->sc_dev), \
776775dfdfSJustin Hibbits 	    "opalflash", MTX_DEF)
786775dfdfSJustin Hibbits 
796775dfdfSJustin Hibbits #define	FLASH_BLOCKSIZE			512
806775dfdfSJustin Hibbits 
816775dfdfSJustin Hibbits static int	opalflash_probe(device_t);
826775dfdfSJustin Hibbits static int	opalflash_attach(device_t);
836775dfdfSJustin Hibbits 
846775dfdfSJustin Hibbits static device_method_t  opalflash_methods[] = {
856775dfdfSJustin Hibbits 	/* Device interface */
866775dfdfSJustin Hibbits 	DEVMETHOD(device_probe,		opalflash_probe),
876775dfdfSJustin Hibbits 	DEVMETHOD(device_attach,	opalflash_attach),
886775dfdfSJustin Hibbits 
896775dfdfSJustin Hibbits 	DEVMETHOD_END
906775dfdfSJustin Hibbits };
916775dfdfSJustin Hibbits 
926775dfdfSJustin Hibbits static driver_t opalflash_driver = {
936775dfdfSJustin Hibbits 	"opalflash",
946775dfdfSJustin Hibbits 	opalflash_methods,
956775dfdfSJustin Hibbits 	sizeof(struct opalflash_softc)
966775dfdfSJustin Hibbits };
976775dfdfSJustin Hibbits 
98*5edf159fSJohn Baldwin DRIVER_MODULE(opalflash, opal, opalflash_driver, 0, 0);
996775dfdfSJustin Hibbits 
1006775dfdfSJustin Hibbits /* GEOM Disk interfaces. */
1016775dfdfSJustin Hibbits static int
opalflash_open(struct disk * dp)1026775dfdfSJustin Hibbits opalflash_open(struct disk *dp)
1036775dfdfSJustin Hibbits {
1046775dfdfSJustin Hibbits 
1056775dfdfSJustin Hibbits 	return (0);
1066775dfdfSJustin Hibbits }
1076775dfdfSJustin Hibbits 
1086775dfdfSJustin Hibbits static int
opalflash_close(struct disk * dp)1096775dfdfSJustin Hibbits opalflash_close(struct disk *dp)
1106775dfdfSJustin Hibbits {
1116775dfdfSJustin Hibbits 
1126775dfdfSJustin Hibbits 	return (0);
1136775dfdfSJustin Hibbits }
1146775dfdfSJustin Hibbits 
1156775dfdfSJustin Hibbits static int
opalflash_ioctl(struct disk * dp,u_long cmd,void * data,int fflag,struct thread * td)1166775dfdfSJustin Hibbits opalflash_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
1176775dfdfSJustin Hibbits 	struct thread *td)
1186775dfdfSJustin Hibbits {
1196775dfdfSJustin Hibbits 
1206775dfdfSJustin Hibbits 	return (EINVAL);
1216775dfdfSJustin Hibbits }
1226775dfdfSJustin Hibbits 
1236775dfdfSJustin Hibbits /* Handle the one attribute we need to play nice with geom_flashmap. */
1246775dfdfSJustin Hibbits static int
opalflash_getattr(struct bio * bp)1256775dfdfSJustin Hibbits opalflash_getattr(struct bio *bp)
1266775dfdfSJustin Hibbits {
1276775dfdfSJustin Hibbits 	struct opalflash_softc *sc;
1286775dfdfSJustin Hibbits 	device_t dev;
1296775dfdfSJustin Hibbits 
1306775dfdfSJustin Hibbits 	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
1316775dfdfSJustin Hibbits 		return (ENXIO);
1326775dfdfSJustin Hibbits 
1336775dfdfSJustin Hibbits 	sc = bp->bio_disk->d_drv1;
1346775dfdfSJustin Hibbits 	dev = sc->sc_dev;
1356775dfdfSJustin Hibbits 
1366775dfdfSJustin Hibbits 	if (strcmp(bp->bio_attribute, "NAND::device") == 0) {
1376775dfdfSJustin Hibbits 		if (bp->bio_length != sizeof(dev))
1386775dfdfSJustin Hibbits 			return (EFAULT);
1396775dfdfSJustin Hibbits 		bcopy(&dev, bp->bio_data, sizeof(dev));
1406775dfdfSJustin Hibbits 	} else
1416775dfdfSJustin Hibbits 		return (-1);
1426775dfdfSJustin Hibbits 	return (0);
1436775dfdfSJustin Hibbits }
1446775dfdfSJustin Hibbits 
1456775dfdfSJustin Hibbits static void
opalflash_strategy(struct bio * bp)1466775dfdfSJustin Hibbits opalflash_strategy(struct bio *bp)
1476775dfdfSJustin Hibbits {
1486775dfdfSJustin Hibbits 	struct opalflash_softc *sc;
1496775dfdfSJustin Hibbits 
1506775dfdfSJustin Hibbits 	sc = (struct opalflash_softc *)bp->bio_disk->d_drv1;
1516775dfdfSJustin Hibbits 	OPALFLASH_LOCK(sc);
1526775dfdfSJustin Hibbits 	bioq_disksort(&sc->sc_bio_queue, bp);
1536775dfdfSJustin Hibbits 	wakeup(sc);
1546775dfdfSJustin Hibbits 	OPALFLASH_UNLOCK(sc);
1556775dfdfSJustin Hibbits }
1566775dfdfSJustin Hibbits 
1576775dfdfSJustin Hibbits static int
opalflash_read(struct opalflash_softc * sc,off_t off,caddr_t data,off_t count)1586775dfdfSJustin Hibbits opalflash_read(struct opalflash_softc *sc, off_t off,
1596775dfdfSJustin Hibbits     caddr_t data, off_t count)
1606775dfdfSJustin Hibbits {
1616775dfdfSJustin Hibbits 	struct opal_msg msg;
1626775dfdfSJustin Hibbits 	int rv, size, token;
1636775dfdfSJustin Hibbits 
1646775dfdfSJustin Hibbits 	/* Ensure we write aligned to a full block size. */
1656775dfdfSJustin Hibbits 	if (off % sc->sc_disk->d_sectorsize != 0 ||
1666775dfdfSJustin Hibbits 	    count % sc->sc_disk->d_sectorsize != 0)
1676775dfdfSJustin Hibbits 		return (EIO);
1686775dfdfSJustin Hibbits 
1696775dfdfSJustin Hibbits 	token = opal_alloc_async_token();
1706775dfdfSJustin Hibbits 
1716775dfdfSJustin Hibbits 	/*
1726775dfdfSJustin Hibbits 	 * Read one page at a time.  It's not guaranteed that the buffer is
1736775dfdfSJustin Hibbits 	 * physically contiguous.
1746775dfdfSJustin Hibbits 	 */
1753c8c50f9SJustin Hibbits 	rv = 0;
1766775dfdfSJustin Hibbits 	while (count > 0) {
1776775dfdfSJustin Hibbits 		size = MIN(count, PAGE_SIZE);
1783c8c50f9SJustin Hibbits 		size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK));
1796775dfdfSJustin Hibbits 		rv = opal_call(OPAL_FLASH_READ, sc->sc_opal_id, off,
1806775dfdfSJustin Hibbits 		    vtophys(data), size, token);
1813c8c50f9SJustin Hibbits 		if (rv == OPAL_ASYNC_COMPLETION) {
1826775dfdfSJustin Hibbits 			rv = opal_wait_completion(&msg, sizeof(msg), token);
1833c8c50f9SJustin Hibbits 			if (rv == OPAL_SUCCESS)
1843c8c50f9SJustin Hibbits 				rv = msg.params[1];
1853c8c50f9SJustin Hibbits 		}
1866775dfdfSJustin Hibbits 		if (rv != OPAL_SUCCESS)
1876775dfdfSJustin Hibbits 			break;
1886775dfdfSJustin Hibbits 		count -= size;
1896775dfdfSJustin Hibbits 		off += size;
1903c8c50f9SJustin Hibbits 		data += size;
1916775dfdfSJustin Hibbits 	}
1926775dfdfSJustin Hibbits 	opal_free_async_token(token);
1936775dfdfSJustin Hibbits 	if (rv == OPAL_SUCCESS)
1946775dfdfSJustin Hibbits 		rv = 0;
1956775dfdfSJustin Hibbits 	else
1966775dfdfSJustin Hibbits 		rv = EIO;
1976775dfdfSJustin Hibbits 
1986775dfdfSJustin Hibbits 	return (rv);
1996775dfdfSJustin Hibbits }
2006775dfdfSJustin Hibbits 
2016775dfdfSJustin Hibbits static int
opalflash_erase(struct opalflash_softc * sc,off_t off,off_t count)2026775dfdfSJustin Hibbits opalflash_erase(struct opalflash_softc *sc, off_t off, off_t count)
2036775dfdfSJustin Hibbits {
2046775dfdfSJustin Hibbits 	struct opal_msg msg;
2056775dfdfSJustin Hibbits 	int rv, token;
2066775dfdfSJustin Hibbits 
2076775dfdfSJustin Hibbits 	/* Ensure we write aligned to a full block size. */
2086775dfdfSJustin Hibbits 	if (off % sc->sc_disk->d_stripesize != 0 ||
2096775dfdfSJustin Hibbits 	    count % sc->sc_disk->d_stripesize != 0)
2106775dfdfSJustin Hibbits 		return (EIO);
2116775dfdfSJustin Hibbits 
2126775dfdfSJustin Hibbits 	token = opal_alloc_async_token();
2136775dfdfSJustin Hibbits 
2146775dfdfSJustin Hibbits 	rv = opal_call(OPAL_FLASH_ERASE, sc->sc_opal_id, off, count, token);
2153c8c50f9SJustin Hibbits 	if (rv == OPAL_ASYNC_COMPLETION) {
2166775dfdfSJustin Hibbits 		rv = opal_wait_completion(&msg, sizeof(msg), token);
2173c8c50f9SJustin Hibbits 		if (rv == OPAL_SUCCESS)
2183c8c50f9SJustin Hibbits 			rv = msg.params[1];
2193c8c50f9SJustin Hibbits 	}
2206775dfdfSJustin Hibbits 	opal_free_async_token(token);
2216775dfdfSJustin Hibbits 
2226775dfdfSJustin Hibbits 	if (rv == OPAL_SUCCESS)
2236775dfdfSJustin Hibbits 		rv = 0;
2246775dfdfSJustin Hibbits 	else
2256775dfdfSJustin Hibbits 		rv = EIO;
2266775dfdfSJustin Hibbits 
2276775dfdfSJustin Hibbits 	return (rv);
2286775dfdfSJustin Hibbits }
2296775dfdfSJustin Hibbits 
2306775dfdfSJustin Hibbits static int
opalflash_write(struct opalflash_softc * sc,off_t off,caddr_t data,off_t count)2316775dfdfSJustin Hibbits opalflash_write(struct opalflash_softc *sc, off_t off,
2326775dfdfSJustin Hibbits     caddr_t data, off_t count)
2336775dfdfSJustin Hibbits {
2346775dfdfSJustin Hibbits 	struct opal_msg msg;
2356775dfdfSJustin Hibbits 	int rv, size, token;
2366775dfdfSJustin Hibbits 
2376775dfdfSJustin Hibbits 	/* Ensure we write aligned to a full block size. */
23893096fecSJustin Hibbits 	if (off % sc->sc_disk->d_sectorsize != 0 ||
23993096fecSJustin Hibbits 	    count % sc->sc_disk->d_sectorsize != 0)
2406775dfdfSJustin Hibbits 		return (EIO);
2416775dfdfSJustin Hibbits 
242bc60451aSJustin Hibbits 	if (sc->sc_erase) {
2436775dfdfSJustin Hibbits 	    /* Erase the full block first, then write in page chunks. */
2446775dfdfSJustin Hibbits 	    rv = opalflash_erase(sc, off, count);
2456775dfdfSJustin Hibbits 	    if (rv != 0)
2466775dfdfSJustin Hibbits 		    return (rv);
247bc60451aSJustin Hibbits 	}
2486775dfdfSJustin Hibbits 
2496775dfdfSJustin Hibbits 	token = opal_alloc_async_token();
2506775dfdfSJustin Hibbits 
2516775dfdfSJustin Hibbits 	/*
2526775dfdfSJustin Hibbits 	 * Write one page at a time.  It's not guaranteed that the buffer is
2536775dfdfSJustin Hibbits 	 * physically contiguous.
2546775dfdfSJustin Hibbits 	 */
2556775dfdfSJustin Hibbits 	while (count > 0) {
2566775dfdfSJustin Hibbits 		size = MIN(count, PAGE_SIZE);
2573c8c50f9SJustin Hibbits 		size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK));
2586775dfdfSJustin Hibbits 		rv = opal_call(OPAL_FLASH_WRITE, sc->sc_opal_id, off,
2596775dfdfSJustin Hibbits 		    vtophys(data), size, token);
2603c8c50f9SJustin Hibbits 		if (rv == OPAL_ASYNC_COMPLETION) {
2616775dfdfSJustin Hibbits 			rv = opal_wait_completion(&msg, sizeof(msg), token);
2623c8c50f9SJustin Hibbits 			if (rv == OPAL_SUCCESS)
2633c8c50f9SJustin Hibbits 				rv = msg.params[1];
2643c8c50f9SJustin Hibbits 		}
2656775dfdfSJustin Hibbits 		if (rv != OPAL_SUCCESS)
2666775dfdfSJustin Hibbits 			break;
2676775dfdfSJustin Hibbits 		count -= size;
2686775dfdfSJustin Hibbits 		off += size;
2693c8c50f9SJustin Hibbits 		data += size;
2706775dfdfSJustin Hibbits 	}
2716775dfdfSJustin Hibbits 	opal_free_async_token(token);
2726775dfdfSJustin Hibbits 
2736775dfdfSJustin Hibbits 	if (rv == OPAL_SUCCESS)
2746775dfdfSJustin Hibbits 		rv = 0;
2756775dfdfSJustin Hibbits 	else
2766775dfdfSJustin Hibbits 		rv = EIO;
2776775dfdfSJustin Hibbits 
2786775dfdfSJustin Hibbits 	return (rv);
2796775dfdfSJustin Hibbits }
2806775dfdfSJustin Hibbits 
2816775dfdfSJustin Hibbits /* Main flash handling task. */
2826775dfdfSJustin Hibbits static void
opalflash_task(void * arg)2836775dfdfSJustin Hibbits opalflash_task(void *arg)
2846775dfdfSJustin Hibbits {
2856775dfdfSJustin Hibbits 	struct opalflash_softc *sc;
2866775dfdfSJustin Hibbits 	struct bio *bp;
2876775dfdfSJustin Hibbits 
2886775dfdfSJustin Hibbits 	sc = arg;
2896775dfdfSJustin Hibbits 
2906775dfdfSJustin Hibbits 	for (;;) {
2916775dfdfSJustin Hibbits 		OPALFLASH_LOCK(sc);
2926775dfdfSJustin Hibbits 		do {
2936775dfdfSJustin Hibbits 			bp = bioq_first(&sc->sc_bio_queue);
2946775dfdfSJustin Hibbits 			if (bp == NULL)
2956775dfdfSJustin Hibbits 				msleep(sc, &sc->sc_mtx, PRIBIO, "opalflash", 0);
2966775dfdfSJustin Hibbits 		} while (bp == NULL);
2976775dfdfSJustin Hibbits 		bioq_remove(&sc->sc_bio_queue, bp);
2986775dfdfSJustin Hibbits 		OPALFLASH_UNLOCK(sc);
2996775dfdfSJustin Hibbits 
3006775dfdfSJustin Hibbits 		switch (bp->bio_cmd) {
3016775dfdfSJustin Hibbits 		case BIO_DELETE:
3026775dfdfSJustin Hibbits 			bp->bio_error = opalflash_erase(sc, bp->bio_offset,
3036775dfdfSJustin Hibbits 			    bp->bio_bcount);
3046775dfdfSJustin Hibbits 			break;
3056775dfdfSJustin Hibbits 		case BIO_READ:
3066775dfdfSJustin Hibbits 			bp->bio_error = opalflash_read(sc, bp->bio_offset,
3076775dfdfSJustin Hibbits 			    bp->bio_data, bp->bio_bcount);
3086775dfdfSJustin Hibbits 			break;
3096775dfdfSJustin Hibbits 		case BIO_WRITE:
3106775dfdfSJustin Hibbits 			bp->bio_error = opalflash_write(sc, bp->bio_offset,
3116775dfdfSJustin Hibbits 			    bp->bio_data, bp->bio_bcount);
3126775dfdfSJustin Hibbits 			break;
3136775dfdfSJustin Hibbits 		default:
3146775dfdfSJustin Hibbits 			bp->bio_error = EINVAL;
3156775dfdfSJustin Hibbits 		}
3166775dfdfSJustin Hibbits 		biodone(bp);
3176775dfdfSJustin Hibbits 	}
3186775dfdfSJustin Hibbits }
3196775dfdfSJustin Hibbits 
3206775dfdfSJustin Hibbits /* Device driver interfaces. */
3216775dfdfSJustin Hibbits 
3226775dfdfSJustin Hibbits static int
opalflash_probe(device_t dev)3236775dfdfSJustin Hibbits opalflash_probe(device_t dev)
3246775dfdfSJustin Hibbits {
3256775dfdfSJustin Hibbits 	if (!ofw_bus_is_compatible(dev, "ibm,opal-flash"))
3266775dfdfSJustin Hibbits 		return (ENXIO);
3276775dfdfSJustin Hibbits 
3286775dfdfSJustin Hibbits 	device_set_desc(dev, "OPAL System Flash");
3296775dfdfSJustin Hibbits 
3306775dfdfSJustin Hibbits 	return (BUS_PROBE_GENERIC);
3316775dfdfSJustin Hibbits }
3326775dfdfSJustin Hibbits 
3336775dfdfSJustin Hibbits static int
opalflash_attach(device_t dev)3346775dfdfSJustin Hibbits opalflash_attach(device_t dev)
3356775dfdfSJustin Hibbits {
3366775dfdfSJustin Hibbits 	struct opalflash_softc *sc;
3376775dfdfSJustin Hibbits 	phandle_t node;
3386775dfdfSJustin Hibbits 	cell_t flash_blocksize, opal_id;
3396775dfdfSJustin Hibbits 	uint32_t regs[2];
3406775dfdfSJustin Hibbits 
3416775dfdfSJustin Hibbits 	sc = device_get_softc(dev);
3426775dfdfSJustin Hibbits 	sc->sc_dev = dev;
3436775dfdfSJustin Hibbits 
3446775dfdfSJustin Hibbits 	node = ofw_bus_get_node(dev);
3456775dfdfSJustin Hibbits 	OF_getencprop(node, "ibm,opal-id", &opal_id, sizeof(opal_id));
3466775dfdfSJustin Hibbits 	sc->sc_opal_id = opal_id;
3476775dfdfSJustin Hibbits 
3486775dfdfSJustin Hibbits 	if (OF_getencprop(node, "ibm,flash-block-size",
3496775dfdfSJustin Hibbits 	    &flash_blocksize, sizeof(flash_blocksize)) < 0) {
3506775dfdfSJustin Hibbits 		device_printf(dev, "Cannot determine flash block size.\n");
3516775dfdfSJustin Hibbits 		return (ENXIO);
3526775dfdfSJustin Hibbits 	}
3536775dfdfSJustin Hibbits 
354bc60451aSJustin Hibbits 	if (!OF_hasprop(node, "no-erase"))
355bc60451aSJustin Hibbits 		sc->sc_erase = true;
356bc60451aSJustin Hibbits 
3576775dfdfSJustin Hibbits 	OPALFLASH_LOCK_INIT(sc);
3586775dfdfSJustin Hibbits 
3596775dfdfSJustin Hibbits 	if (OF_getencprop(node, "reg", regs, sizeof(regs)) < 0) {
3606775dfdfSJustin Hibbits 		device_printf(dev, "Unable to get flash size.\n");
3616775dfdfSJustin Hibbits 		return (ENXIO);
3626775dfdfSJustin Hibbits 	}
3636775dfdfSJustin Hibbits 
3646775dfdfSJustin Hibbits 	sc->sc_disk = disk_alloc();
3656775dfdfSJustin Hibbits 	sc->sc_disk->d_name = "opalflash";
3666775dfdfSJustin Hibbits 	sc->sc_disk->d_open = opalflash_open;
3676775dfdfSJustin Hibbits 	sc->sc_disk->d_close = opalflash_close;
3686775dfdfSJustin Hibbits 	sc->sc_disk->d_strategy = opalflash_strategy;
3696775dfdfSJustin Hibbits 	sc->sc_disk->d_ioctl = opalflash_ioctl;
3706775dfdfSJustin Hibbits 	sc->sc_disk->d_getattr = opalflash_getattr;
3716775dfdfSJustin Hibbits 	sc->sc_disk->d_drv1 = sc;
3726775dfdfSJustin Hibbits 	sc->sc_disk->d_maxsize = DFLTPHYS;
3736775dfdfSJustin Hibbits 	sc->sc_disk->d_mediasize = regs[1];
3746775dfdfSJustin Hibbits 	sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
3756775dfdfSJustin Hibbits 	sc->sc_disk->d_sectorsize = FLASH_BLOCKSIZE;
3766775dfdfSJustin Hibbits 	    sc->sc_disk->d_stripesize = flash_blocksize;
3776775dfdfSJustin Hibbits 	sc->sc_disk->d_dump = NULL;
3786775dfdfSJustin Hibbits 
3796775dfdfSJustin Hibbits 	disk_create(sc->sc_disk, DISK_VERSION);
3806775dfdfSJustin Hibbits 	bioq_init(&sc->sc_bio_queue);
3816775dfdfSJustin Hibbits 
3826775dfdfSJustin Hibbits 	kproc_create(&opalflash_task, sc, &sc->sc_p, 0, 0, "task: OPAL Flash");
3836775dfdfSJustin Hibbits 
3846775dfdfSJustin Hibbits 	return (0);
3856775dfdfSJustin Hibbits }
386