2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3 * Copyright (c) 2017 The FreeBSD Foundation
6 * Portions of this software were developed by Landon Fuller
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17 * redistribution must be conditioned upon including a substantially
18 * similar Disclaimer requirement for further binary redistribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGES.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
40 * Abstract driver for memory-mapped SPROM devices.
43 #include <sys/param.h>
44 #include <sys/kernel.h>
46 #include <sys/limits.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/systm.h>
51 #include <machine/bus.h>
53 #include <machine/resource.h>
55 #include <dev/bhnd/bhnd.h>
57 #include "bhnd_nvram_if.h"
59 #include "bhnd_nvram_io.h"
61 #include "bhnd_spromvar.h"
64 * Default bhnd sprom driver implementation of DEVICE_PROBE().
67 bhnd_sprom_probe(device_t dev)
69 device_set_desc(dev, "SPROM/OTP");
71 /* Refuse wildcard attachments */
72 return (BUS_PROBE_NOWILDCARD);
75 /* Default DEVICE_ATTACH() implementation; assumes a zero offset to the
78 bhnd_sprom_attach_meth(device_t dev)
80 return (bhnd_sprom_attach(dev, 0));
84 * BHND SPROM device attach.
86 * This should be called from DEVICE_ATTACH() with the @p offset to the
89 * Assumes SPROM is mapped via SYS_RES_MEMORY resource with RID 0.
91 * @param dev BHND SPROM device.
92 * @param offset Offset to the SPROM data.
95 bhnd_sprom_attach(device_t dev, bus_size_t offset)
97 struct bhnd_sprom_softc *sc;
98 struct bhnd_nvram_io *io;
99 struct bhnd_resource *r;
100 bus_size_t r_size, sprom_size;
104 sc = device_get_softc(dev);
111 /* Allocate SPROM resource */
113 r = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
115 device_printf(dev, "failed to allocate resources\n");
119 /* Determine SPROM size */
120 r_size = rman_get_size(r->res);
121 if (r_size <= offset || (r_size - offset) > BUS_SPACE_MAXSIZE) {
122 device_printf(dev, "invalid sprom offset\n");
127 sprom_size = r_size - offset;
129 /* Allocate an I/O context for the SPROM parser. All SPROM reads
130 * must be 16-bit aligned */
131 io = bhnd_nvram_iores_new(r, offset, sprom_size, sizeof(uint16_t));
137 /* Initialize NVRAM data store */
138 error = bhnd_nvram_store_parse_new(&sc->store, io,
139 &bhnd_nvram_sprom_class);
143 /* Clean up our temporary I/O context and its backing resource */
144 bhnd_nvram_io_free(io);
145 bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
150 /* Register ourselves with the bus */
151 if ((error = bhnd_register_provider(dev, BHND_SERVICE_NVRAM))) {
152 device_printf(dev, "failed to register NVRAM provider: %d\n",
160 /* Clean up I/O context before releasing its backing resource */
162 bhnd_nvram_io_free(io);
165 bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
167 if (sc->store != NULL)
168 bhnd_nvram_store_free(sc->store);
174 * Default bhnd_sprom implementation of DEVICE_RESUME().
177 bhnd_sprom_resume(device_t dev)
183 * Default bhnd sprom driver implementation of DEVICE_SUSPEND().
186 bhnd_sprom_suspend(device_t dev)
192 * Default bhnd sprom driver implementation of DEVICE_DETACH().
195 bhnd_sprom_detach(device_t dev)
197 struct bhnd_sprom_softc *sc;
200 sc = device_get_softc(dev);
202 if ((error = bhnd_deregister_provider(dev, BHND_SERVICE_ANY)))
205 bhnd_nvram_store_free(sc->store);
211 * Default bhnd sprom driver implementation of BHND_NVRAM_GETVAR().
214 bhnd_sprom_getvar_method(device_t dev, const char *name, void *buf, size_t *len,
215 bhnd_nvram_type type)
217 struct bhnd_sprom_softc *sc = device_get_softc(dev);
219 return (bhnd_nvram_store_getvar(sc->store, name, buf, len, type));
223 * Default bhnd sprom driver implementation of BHND_NVRAM_SETVAR().
226 bhnd_sprom_setvar_method(device_t dev, const char *name, const void *buf,
227 size_t len, bhnd_nvram_type type)
229 struct bhnd_sprom_softc *sc = device_get_softc(dev);
231 return (bhnd_nvram_store_setvar(sc->store, name, buf, len, type));
234 static device_method_t bhnd_sprom_methods[] = {
235 /* Device interface */
236 DEVMETHOD(device_probe, bhnd_sprom_probe),
237 DEVMETHOD(device_attach, bhnd_sprom_attach_meth),
238 DEVMETHOD(device_resume, bhnd_sprom_resume),
239 DEVMETHOD(device_suspend, bhnd_sprom_suspend),
240 DEVMETHOD(device_detach, bhnd_sprom_detach),
242 /* NVRAM interface */
243 DEVMETHOD(bhnd_nvram_getvar, bhnd_sprom_getvar_method),
244 DEVMETHOD(bhnd_nvram_setvar, bhnd_sprom_setvar_method),
249 DEFINE_CLASS_0(bhnd_nvram_store, bhnd_sprom_driver, bhnd_sprom_methods, sizeof(struct bhnd_sprom_softc));
250 MODULE_VERSION(bhnd_sprom, 1);