2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
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 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
36 * Abstract driver for memory-mapped SPROM devices.
39 #include <sys/param.h>
40 #include <sys/kernel.h>
42 #include <sys/limits.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/systm.h>
47 #include <machine/bus.h>
49 #include <machine/resource.h>
51 #include <dev/bhnd/bhnd.h>
53 #include "bhnd_nvram_if.h"
55 #include "bhnd_nvram_io.h"
57 #include "bhnd_spromvar.h"
60 * Default bhnd sprom driver implementation of DEVICE_PROBE().
63 bhnd_sprom_probe(device_t dev)
65 device_set_desc(dev, "SPROM/OTP");
67 /* Refuse wildcard attachments */
68 return (BUS_PROBE_NOWILDCARD);
71 /* Default DEVICE_ATTACH() implementation; assumes a zero offset to the
74 bhnd_sprom_attach_meth(device_t dev)
76 return (bhnd_sprom_attach(dev, 0));
80 * BHND SPROM device attach.
82 * This should be called from DEVICE_ATTACH() with the @p offset to the
85 * Assumes SPROM is mapped via SYS_RES_MEMORY resource with RID 0.
87 * @param dev BHND SPROM device.
88 * @param offset Offset to the SPROM data.
91 bhnd_sprom_attach(device_t dev, bus_size_t offset)
93 struct bhnd_sprom_softc *sc;
94 struct bhnd_nvram_io *io;
95 struct bhnd_resource *r;
96 bus_size_t r_size, sprom_size;
100 sc = device_get_softc(dev);
105 /* Allocate SPROM resource */
107 r = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
109 device_printf(dev, "failed to allocate resources\n");
113 /* Determine SPROM size */
114 r_size = rman_get_size(r->res);
115 if (r_size <= offset || (r_size - offset) > BUS_SPACE_MAXSIZE) {
116 device_printf(dev, "invalid sprom offset\n");
121 sprom_size = r_size - offset;
123 /* Allocate an I/O context for the SPROM parser. All SPROM reads
124 * must be 16-bit aligned */
125 io = bhnd_nvram_iores_new(r, offset, sprom_size, sizeof(uint16_t));
131 /* Initialize NVRAM data store */
132 error = bhnd_nvram_store_parse_new(&sc->store, io,
133 &bhnd_nvram_sprom_class);
137 /* Clean up our temporary I/O context and its backing resource */
138 bhnd_nvram_io_free(io);
139 bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
144 /* Clean up I/O context before releasing its backing resource */
146 bhnd_nvram_io_free(io);
148 bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
154 * Default bhnd_sprom implementation of DEVICE_RESUME().
157 bhnd_sprom_resume(device_t dev)
163 * Default bhnd sprom driver implementation of DEVICE_SUSPEND().
166 bhnd_sprom_suspend(device_t dev)
172 * Default bhnd sprom driver implementation of DEVICE_DETACH().
175 bhnd_sprom_detach(device_t dev)
177 struct bhnd_sprom_softc *sc;
179 sc = device_get_softc(dev);
181 bhnd_nvram_store_free(sc->store);
187 * Default bhnd sprom driver implementation of BHND_NVRAM_GETVAR().
190 bhnd_sprom_getvar_method(device_t dev, const char *name, void *buf, size_t *len,
191 bhnd_nvram_type type)
193 struct bhnd_sprom_softc *sc = device_get_softc(dev);
195 return (bhnd_nvram_store_getvar(sc->store, name, buf, len, type));
199 * Default bhnd sprom driver implementation of BHND_NVRAM_SETVAR().
202 bhnd_sprom_setvar_method(device_t dev, const char *name, const void *buf,
203 size_t len, bhnd_nvram_type type)
205 struct bhnd_sprom_softc *sc = device_get_softc(dev);
207 return (bhnd_nvram_store_setvar(sc->store, name, buf, len, type));
210 static device_method_t bhnd_sprom_methods[] = {
211 /* Device interface */
212 DEVMETHOD(device_probe, bhnd_sprom_probe),
213 DEVMETHOD(device_attach, bhnd_sprom_attach_meth),
214 DEVMETHOD(device_resume, bhnd_sprom_resume),
215 DEVMETHOD(device_suspend, bhnd_sprom_suspend),
216 DEVMETHOD(device_detach, bhnd_sprom_detach),
218 /* NVRAM interface */
219 DEVMETHOD(bhnd_nvram_getvar, bhnd_sprom_getvar_method),
220 DEVMETHOD(bhnd_nvram_setvar, bhnd_sprom_setvar_method),
225 DEFINE_CLASS_0(bhnd_nvram_store, bhnd_sprom_driver, bhnd_sprom_methods, sizeof(struct bhnd_sprom_softc));
226 MODULE_VERSION(bhnd_sprom, 1);