/*-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 rid;99int error;100101sc = device_get_softc(dev);102sc->dev = dev;103sc->store = NULL;104105io = NULL;106r = NULL;107108/* Allocate SPROM resource */109rid = 0;110r = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);111if (r == NULL) {112device_printf(dev, "failed to allocate resources\n");113return (ENXIO);114}115116/* Determine SPROM size */117r_size = rman_get_size(r->res);118if (r_size <= offset || (r_size - offset) > BUS_SPACE_MAXSIZE) {119device_printf(dev, "invalid sprom offset\n");120error = ENXIO;121goto failed;122}123124sprom_size = r_size - offset;125126/* Allocate an I/O context for the SPROM parser. All SPROM reads127* must be 16-bit aligned */128io = bhnd_nvram_iores_new(r, offset, sprom_size, sizeof(uint16_t));129if (io == NULL) {130error = ENXIO;131goto failed;132}133134/* Initialize NVRAM data store */135error = bhnd_nvram_store_parse_new(&sc->store, io,136&bhnd_nvram_sprom_class);137if (error)138goto failed;139140/* Clean up our temporary I/O context and its backing resource */141bhnd_nvram_io_free(io);142bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);143144io = NULL;145r = NULL;146147/* Register ourselves with the bus */148if ((error = bhnd_register_provider(dev, BHND_SERVICE_NVRAM))) {149device_printf(dev, "failed to register NVRAM provider: %d\n",150error);151goto failed;152}153154return (0);155156failed:157/* Clean up I/O context before releasing its backing resource */158if (io != NULL)159bhnd_nvram_io_free(io);160161if (r != NULL)162bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);163164if (sc->store != NULL)165bhnd_nvram_store_free(sc->store);166167return (error);168}169170/**171* Default bhnd_sprom implementation of DEVICE_RESUME().172*/173int174bhnd_sprom_resume(device_t dev)175{176return (0);177}178179/**180* Default bhnd sprom driver implementation of DEVICE_SUSPEND().181*/182int183bhnd_sprom_suspend(device_t dev)184{185return (0);186}187188/**189* Default bhnd sprom driver implementation of DEVICE_DETACH().190*/191int192bhnd_sprom_detach(device_t dev)193{194struct bhnd_sprom_softc *sc;195int error;196197sc = device_get_softc(dev);198199if ((error = bhnd_deregister_provider(dev, BHND_SERVICE_ANY)))200return (error);201202bhnd_nvram_store_free(sc->store);203204return (0);205}206207/**208* Default bhnd sprom driver implementation of BHND_NVRAM_GETVAR().209*/210static int211bhnd_sprom_getvar_method(device_t dev, const char *name, void *buf, size_t *len,212bhnd_nvram_type type)213{214struct bhnd_sprom_softc *sc = device_get_softc(dev);215216return (bhnd_nvram_store_getvar(sc->store, name, buf, len, type));217}218219/**220* Default bhnd sprom driver implementation of BHND_NVRAM_SETVAR().221*/222static int223bhnd_sprom_setvar_method(device_t dev, const char *name, const void *buf,224size_t len, bhnd_nvram_type type)225{226struct bhnd_sprom_softc *sc = device_get_softc(dev);227228return (bhnd_nvram_store_setvar(sc->store, name, buf, len, type));229}230231static device_method_t bhnd_sprom_methods[] = {232/* Device interface */233DEVMETHOD(device_probe, bhnd_sprom_probe),234DEVMETHOD(device_attach, bhnd_sprom_attach_meth),235DEVMETHOD(device_resume, bhnd_sprom_resume),236DEVMETHOD(device_suspend, bhnd_sprom_suspend),237DEVMETHOD(device_detach, bhnd_sprom_detach),238239/* NVRAM interface */240DEVMETHOD(bhnd_nvram_getvar, bhnd_sprom_getvar_method),241DEVMETHOD(bhnd_nvram_setvar, bhnd_sprom_setvar_method),242243DEVMETHOD_END244};245246DEFINE_CLASS_0(bhnd_nvram_store, bhnd_sprom_driver, bhnd_sprom_methods, sizeof(struct bhnd_sprom_softc));247MODULE_VERSION(bhnd_sprom, 1);248249250