/*-1* Copyright (c) 2015-2016 Landon Fuller <[email protected]>2* Copyright (c) 2017 The FreeBSD Foundation3* All rights reserved.4*5* Portions of this software were developed by Landon Fuller6* under sponsorship from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer,13* without modification.14* 2. Redistributions in binary form must reproduce at minimum a disclaimer15* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any16* redistribution must be conditioned upon including a substantially17* similar Disclaimer requirement for further binary redistribution.18*19* NO WARRANTY20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY23* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL24* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,25* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER28* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF30* THE POSSIBILITY OF SUCH DAMAGES.31*/3233#include <sys/cdefs.h>34/*35* BHND SPROM driver.36*37* Abstract driver for memory-mapped SPROM devices.38*/3940#include <sys/param.h>41#include <sys/kernel.h>42#include <sys/bus.h>43#include <sys/limits.h>44#include <sys/malloc.h>45#include <sys/module.h>46#include <sys/systm.h>4748#include <machine/bus.h>49#include <sys/rman.h>50#include <machine/resource.h>5152#include <dev/bhnd/bhnd.h>5354#include "bhnd_nvram_if.h"5556#include "bhnd_nvram_io.h"5758#include "bhnd_spromvar.h"5960/**61* Default bhnd sprom driver implementation of DEVICE_PROBE().62*/63int64bhnd_sprom_probe(device_t dev)65{66device_set_desc(dev, "SPROM/OTP");6768/* Refuse wildcard attachments */69return (BUS_PROBE_NOWILDCARD);70}7172/* Default DEVICE_ATTACH() implementation; assumes a zero offset to the73* SPROM data */74static int75bhnd_sprom_attach_meth(device_t dev)76{77return (bhnd_sprom_attach(dev, 0));78}7980/**81* BHND SPROM device attach.82*83* This should be called from DEVICE_ATTACH() with the @p offset to the84* SPROM data.85*86* Assumes SPROM is mapped via SYS_RES_MEMORY resource with RID 0.87*88* @param dev BHND SPROM device.89* @param offset Offset to the SPROM data.90*/91int92bhnd_sprom_attach(device_t dev, bus_size_t offset)93{94struct bhnd_sprom_softc *sc;95struct bhnd_nvram_io *io;96struct bhnd_resource *r;97bus_size_t r_size, sprom_size;98int error;99100sc = device_get_softc(dev);101sc->dev = dev;102sc->store = NULL;103104io = NULL;105r = NULL;106107/* Allocate SPROM resource */108r = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, 0, RF_ACTIVE);109if (r == NULL) {110device_printf(dev, "failed to allocate resources\n");111return (ENXIO);112}113114/* Determine SPROM size */115r_size = rman_get_size(r->res);116if (r_size <= offset || (r_size - offset) > BUS_SPACE_MAXSIZE) {117device_printf(dev, "invalid sprom offset\n");118error = ENXIO;119goto failed;120}121122sprom_size = r_size - offset;123124/* Allocate an I/O context for the SPROM parser. All SPROM reads125* must be 16-bit aligned */126io = bhnd_nvram_iores_new(r, offset, sprom_size, sizeof(uint16_t));127if (io == NULL) {128error = ENXIO;129goto failed;130}131132/* Initialize NVRAM data store */133error = bhnd_nvram_store_parse_new(&sc->store, io,134&bhnd_nvram_sprom_class);135if (error)136goto failed;137138/* Clean up our temporary I/O context and its backing resource */139bhnd_nvram_io_free(io);140bhnd_release_resource(dev, r);141142io = NULL;143r = NULL;144145/* Register ourselves with the bus */146if ((error = bhnd_register_provider(dev, BHND_SERVICE_NVRAM))) {147device_printf(dev, "failed to register NVRAM provider: %d\n",148error);149goto failed;150}151152return (0);153154failed:155/* Clean up I/O context before releasing its backing resource */156if (io != NULL)157bhnd_nvram_io_free(io);158159if (r != NULL)160bhnd_release_resource(dev, r);161162if (sc->store != NULL)163bhnd_nvram_store_free(sc->store);164165return (error);166}167168/**169* Default bhnd_sprom implementation of DEVICE_RESUME().170*/171int172bhnd_sprom_resume(device_t dev)173{174return (0);175}176177/**178* Default bhnd sprom driver implementation of DEVICE_SUSPEND().179*/180int181bhnd_sprom_suspend(device_t dev)182{183return (0);184}185186/**187* Default bhnd sprom driver implementation of DEVICE_DETACH().188*/189int190bhnd_sprom_detach(device_t dev)191{192struct bhnd_sprom_softc *sc;193int error;194195sc = device_get_softc(dev);196197if ((error = bhnd_deregister_provider(dev, BHND_SERVICE_ANY)))198return (error);199200bhnd_nvram_store_free(sc->store);201202return (0);203}204205/**206* Default bhnd sprom driver implementation of BHND_NVRAM_GETVAR().207*/208static int209bhnd_sprom_getvar_method(device_t dev, const char *name, void *buf, size_t *len,210bhnd_nvram_type type)211{212struct bhnd_sprom_softc *sc = device_get_softc(dev);213214return (bhnd_nvram_store_getvar(sc->store, name, buf, len, type));215}216217/**218* Default bhnd sprom driver implementation of BHND_NVRAM_SETVAR().219*/220static int221bhnd_sprom_setvar_method(device_t dev, const char *name, const void *buf,222size_t len, bhnd_nvram_type type)223{224struct bhnd_sprom_softc *sc = device_get_softc(dev);225226return (bhnd_nvram_store_setvar(sc->store, name, buf, len, type));227}228229static device_method_t bhnd_sprom_methods[] = {230/* Device interface */231DEVMETHOD(device_probe, bhnd_sprom_probe),232DEVMETHOD(device_attach, bhnd_sprom_attach_meth),233DEVMETHOD(device_resume, bhnd_sprom_resume),234DEVMETHOD(device_suspend, bhnd_sprom_suspend),235DEVMETHOD(device_detach, bhnd_sprom_detach),236237/* NVRAM interface */238DEVMETHOD(bhnd_nvram_getvar, bhnd_sprom_getvar_method),239DEVMETHOD(bhnd_nvram_setvar, bhnd_sprom_setvar_method),240241DEVMETHOD_END242};243244DEFINE_CLASS_0(bhnd_nvram_store, bhnd_sprom_driver, bhnd_sprom_methods, sizeof(struct bhnd_sprom_softc));245MODULE_VERSION(bhnd_sprom, 1);246247248