xref: /freebsd/sys/dev/bhnd/nvram/bhnd_sprom.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1e83ce340SAdrian Chadd /*-
2f4a3eb02SAdrian Chadd  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3*8e35bf83SLandon J. Fuller  * Copyright (c) 2017 The FreeBSD Foundation
4e83ce340SAdrian Chadd  * All rights reserved.
5e83ce340SAdrian Chadd  *
6*8e35bf83SLandon J. Fuller  * Portions of this software were developed by Landon Fuller
7*8e35bf83SLandon J. Fuller  * under sponsorship from the FreeBSD Foundation.
8*8e35bf83SLandon J. Fuller  *
9e83ce340SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
10e83ce340SAdrian Chadd  * modification, are permitted provided that the following conditions
11e83ce340SAdrian Chadd  * are met:
12e83ce340SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
13e83ce340SAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
14e83ce340SAdrian Chadd  *    without modification.
15e83ce340SAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16e83ce340SAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17e83ce340SAdrian Chadd  *    redistribution must be conditioned upon including a substantially
18e83ce340SAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
19e83ce340SAdrian Chadd  *
20e83ce340SAdrian Chadd  * NO WARRANTY
21e83ce340SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22e83ce340SAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23e83ce340SAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24e83ce340SAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25e83ce340SAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26e83ce340SAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27e83ce340SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28e83ce340SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29e83ce340SAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30e83ce340SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31e83ce340SAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
32e83ce340SAdrian Chadd  */
33e83ce340SAdrian Chadd 
34e83ce340SAdrian Chadd #include <sys/cdefs.h>
35f4a3eb02SAdrian Chadd /*
36f4a3eb02SAdrian Chadd  * BHND SPROM driver.
37f4a3eb02SAdrian Chadd  *
38f4a3eb02SAdrian Chadd  * Abstract driver for memory-mapped SPROM devices.
39f4a3eb02SAdrian Chadd  */
40f4a3eb02SAdrian Chadd 
41e83ce340SAdrian Chadd #include <sys/param.h>
42f4a3eb02SAdrian Chadd #include <sys/kernel.h>
43e83ce340SAdrian Chadd #include <sys/bus.h>
44f4a3eb02SAdrian Chadd #include <sys/limits.h>
45f4a3eb02SAdrian Chadd #include <sys/malloc.h>
46f4a3eb02SAdrian Chadd #include <sys/module.h>
47e83ce340SAdrian Chadd #include <sys/systm.h>
48e83ce340SAdrian Chadd 
49e83ce340SAdrian Chadd #include <machine/bus.h>
50f4a3eb02SAdrian Chadd #include <sys/rman.h>
51e83ce340SAdrian Chadd #include <machine/resource.h>
52e83ce340SAdrian Chadd 
53f4a3eb02SAdrian Chadd #include <dev/bhnd/bhnd.h>
54e83ce340SAdrian Chadd 
55f4a3eb02SAdrian Chadd #include "bhnd_nvram_if.h"
56e83ce340SAdrian Chadd 
5777cb4d3eSLandon J. Fuller #include "bhnd_nvram_io.h"
58e83ce340SAdrian Chadd 
5977cb4d3eSLandon J. Fuller #include "bhnd_spromvar.h"
60e83ce340SAdrian Chadd 
61e83ce340SAdrian Chadd /**
62f4a3eb02SAdrian Chadd  * Default bhnd sprom driver implementation of DEVICE_PROBE().
63e83ce340SAdrian Chadd  */
64e83ce340SAdrian Chadd int
bhnd_sprom_probe(device_t dev)65f4a3eb02SAdrian Chadd bhnd_sprom_probe(device_t dev)
66e83ce340SAdrian Chadd {
6756a4cdd1SLandon J. Fuller 	device_set_desc(dev, "SPROM/OTP");
68f4a3eb02SAdrian Chadd 
69f4a3eb02SAdrian Chadd 	/* Refuse wildcard attachments */
70f4a3eb02SAdrian Chadd 	return (BUS_PROBE_NOWILDCARD);
71f4a3eb02SAdrian Chadd }
72f4a3eb02SAdrian Chadd 
7356a4cdd1SLandon J. Fuller /* Default DEVICE_ATTACH() implementation; assumes a zero offset to the
7456a4cdd1SLandon J. Fuller  * SPROM data */
7556a4cdd1SLandon J. Fuller static int
bhnd_sprom_attach_meth(device_t dev)7656a4cdd1SLandon J. Fuller bhnd_sprom_attach_meth(device_t dev)
7756a4cdd1SLandon J. Fuller {
7856a4cdd1SLandon J. Fuller 	return (bhnd_sprom_attach(dev, 0));
7956a4cdd1SLandon J. Fuller }
8056a4cdd1SLandon J. Fuller 
81f4a3eb02SAdrian Chadd /**
8256a4cdd1SLandon J. Fuller  * BHND SPROM device attach.
83f4a3eb02SAdrian Chadd  *
8456a4cdd1SLandon J. Fuller  * This should be called from DEVICE_ATTACH() with the @p offset to the
8556a4cdd1SLandon J. Fuller  * SPROM data.
8656a4cdd1SLandon J. Fuller  *
8756a4cdd1SLandon J. Fuller  * Assumes SPROM is mapped via SYS_RES_MEMORY resource with RID 0.
8856a4cdd1SLandon J. Fuller  *
8956a4cdd1SLandon J. Fuller  * @param dev BHND SPROM device.
9056a4cdd1SLandon J. Fuller  * @param offset Offset to the SPROM data.
91f4a3eb02SAdrian Chadd  */
92f4a3eb02SAdrian Chadd int
bhnd_sprom_attach(device_t dev,bus_size_t offset)9356a4cdd1SLandon J. Fuller bhnd_sprom_attach(device_t dev, bus_size_t offset)
94f4a3eb02SAdrian Chadd {
95f4a3eb02SAdrian Chadd 	struct bhnd_sprom_softc	*sc;
9677cb4d3eSLandon J. Fuller 	struct bhnd_nvram_io	*io;
9777cb4d3eSLandon J. Fuller 	struct bhnd_resource	*r;
9877cb4d3eSLandon J. Fuller 	bus_size_t		 r_size, sprom_size;
9977cb4d3eSLandon J. Fuller 	int			 rid;
100e83ce340SAdrian Chadd 	int			 error;
101e83ce340SAdrian Chadd 
102f4a3eb02SAdrian Chadd 	sc = device_get_softc(dev);
103f4a3eb02SAdrian Chadd 	sc->dev = dev;
104*8e35bf83SLandon J. Fuller 	sc->store = NULL;
105e83ce340SAdrian Chadd 
10677cb4d3eSLandon J. Fuller 	io = NULL;
107*8e35bf83SLandon J. Fuller 	r = NULL;
10877cb4d3eSLandon J. Fuller 
109f4a3eb02SAdrian Chadd 	/* Allocate SPROM resource */
11077cb4d3eSLandon J. Fuller 	rid = 0;
11177cb4d3eSLandon J. Fuller 	r = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
11277cb4d3eSLandon J. Fuller 	if (r == NULL) {
113f4a3eb02SAdrian Chadd 		device_printf(dev, "failed to allocate resources\n");
114f4a3eb02SAdrian Chadd 		return (ENXIO);
115f4a3eb02SAdrian Chadd 	}
116e83ce340SAdrian Chadd 
11777cb4d3eSLandon J. Fuller 	/* Determine SPROM size */
11877cb4d3eSLandon J. Fuller 	r_size = rman_get_size(r->res);
11977cb4d3eSLandon J. Fuller 	if (r_size <= offset || (r_size - offset) > BUS_SPACE_MAXSIZE) {
12077cb4d3eSLandon J. Fuller 		device_printf(dev, "invalid sprom offset\n");
12177cb4d3eSLandon J. Fuller 		error = ENXIO;
12277cb4d3eSLandon J. Fuller 		goto failed;
12377cb4d3eSLandon J. Fuller 	}
12477cb4d3eSLandon J. Fuller 
12577cb4d3eSLandon J. Fuller 	sprom_size = r_size - offset;
12677cb4d3eSLandon J. Fuller 
12726e4f220SLandon J. Fuller 	/* Allocate an I/O context for the SPROM parser. All SPROM reads
12826e4f220SLandon J. Fuller 	 * must be 16-bit aligned */
12926e4f220SLandon J. Fuller 	io = bhnd_nvram_iores_new(r, offset, sprom_size, sizeof(uint16_t));
13077cb4d3eSLandon J. Fuller 	if (io == NULL) {
13177cb4d3eSLandon J. Fuller 		error = ENXIO;
13277cb4d3eSLandon J. Fuller 		goto failed;
13377cb4d3eSLandon J. Fuller 	}
13477cb4d3eSLandon J. Fuller 
13577cb4d3eSLandon J. Fuller 	/* Initialize NVRAM data store */
13677cb4d3eSLandon J. Fuller 	error = bhnd_nvram_store_parse_new(&sc->store, io,
13777cb4d3eSLandon J. Fuller 	    &bhnd_nvram_sprom_class);
13877cb4d3eSLandon J. Fuller 	if (error)
139f4a3eb02SAdrian Chadd 		goto failed;
140e83ce340SAdrian Chadd 
14177cb4d3eSLandon J. Fuller 	/* Clean up our temporary I/O context and its backing resource */
14277cb4d3eSLandon J. Fuller 	bhnd_nvram_io_free(io);
14377cb4d3eSLandon J. Fuller 	bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
144e83ce340SAdrian Chadd 
145*8e35bf83SLandon J. Fuller 	io = NULL;
146*8e35bf83SLandon J. Fuller 	r = NULL;
147*8e35bf83SLandon J. Fuller 
148*8e35bf83SLandon J. Fuller 	/* Register ourselves with the bus */
149*8e35bf83SLandon J. Fuller 	if ((error = bhnd_register_provider(dev, BHND_SERVICE_NVRAM))) {
150*8e35bf83SLandon J. Fuller 		device_printf(dev, "failed to register NVRAM provider: %d\n",
151*8e35bf83SLandon J. Fuller 		    error);
152*8e35bf83SLandon J. Fuller 		goto failed;
153*8e35bf83SLandon J. Fuller 	}
154*8e35bf83SLandon J. Fuller 
155f4a3eb02SAdrian Chadd 	return (0);
156f4a3eb02SAdrian Chadd 
157f4a3eb02SAdrian Chadd failed:
15877cb4d3eSLandon J. Fuller 	/* Clean up I/O context before releasing its backing resource */
15977cb4d3eSLandon J. Fuller 	if (io != NULL)
16077cb4d3eSLandon J. Fuller 		bhnd_nvram_io_free(io);
16177cb4d3eSLandon J. Fuller 
162*8e35bf83SLandon J. Fuller 	if (r != NULL)
16377cb4d3eSLandon J. Fuller 		bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
16477cb4d3eSLandon J. Fuller 
165*8e35bf83SLandon J. Fuller 	if (sc->store != NULL)
166*8e35bf83SLandon J. Fuller 		bhnd_nvram_store_free(sc->store);
167*8e35bf83SLandon J. Fuller 
168e83ce340SAdrian Chadd 	return (error);
169f4a3eb02SAdrian Chadd }
170f4a3eb02SAdrian Chadd 
171f4a3eb02SAdrian Chadd /**
17256a4cdd1SLandon J. Fuller  * Default bhnd_sprom implementation of DEVICE_RESUME().
173f4a3eb02SAdrian Chadd  */
174f4a3eb02SAdrian Chadd int
bhnd_sprom_resume(device_t dev)175f4a3eb02SAdrian Chadd bhnd_sprom_resume(device_t dev)
176f4a3eb02SAdrian Chadd {
177f4a3eb02SAdrian Chadd 	return (0);
178f4a3eb02SAdrian Chadd }
179f4a3eb02SAdrian Chadd 
180f4a3eb02SAdrian Chadd /**
18156a4cdd1SLandon J. Fuller  * Default bhnd sprom driver implementation of DEVICE_SUSPEND().
182f4a3eb02SAdrian Chadd  */
183f4a3eb02SAdrian Chadd int
bhnd_sprom_suspend(device_t dev)184f4a3eb02SAdrian Chadd bhnd_sprom_suspend(device_t dev)
185f4a3eb02SAdrian Chadd {
186f4a3eb02SAdrian Chadd 	return (0);
187f4a3eb02SAdrian Chadd }
188f4a3eb02SAdrian Chadd 
189f4a3eb02SAdrian Chadd /**
190f4a3eb02SAdrian Chadd  * Default bhnd sprom driver implementation of DEVICE_DETACH().
191f4a3eb02SAdrian Chadd  */
192f4a3eb02SAdrian Chadd int
bhnd_sprom_detach(device_t dev)193f4a3eb02SAdrian Chadd bhnd_sprom_detach(device_t dev)
194f4a3eb02SAdrian Chadd {
195f4a3eb02SAdrian Chadd 	struct bhnd_sprom_softc	*sc;
196*8e35bf83SLandon J. Fuller 	int			 error;
197f4a3eb02SAdrian Chadd 
198f4a3eb02SAdrian Chadd 	sc = device_get_softc(dev);
199f4a3eb02SAdrian Chadd 
200*8e35bf83SLandon J. Fuller 	if ((error = bhnd_deregister_provider(dev, BHND_SERVICE_ANY)))
201*8e35bf83SLandon J. Fuller 		return (error);
202*8e35bf83SLandon J. Fuller 
20377cb4d3eSLandon J. Fuller 	bhnd_nvram_store_free(sc->store);
204e83ce340SAdrian Chadd 
205e83ce340SAdrian Chadd 	return (0);
206e83ce340SAdrian Chadd }
207e83ce340SAdrian Chadd 
208e83ce340SAdrian Chadd /**
209f4a3eb02SAdrian Chadd  * Default bhnd sprom driver implementation of BHND_NVRAM_GETVAR().
210e83ce340SAdrian Chadd  */
211f4a3eb02SAdrian Chadd static int
bhnd_sprom_getvar_method(device_t dev,const char * name,void * buf,size_t * len,bhnd_nvram_type type)2121728aef2SLandon J. Fuller bhnd_sprom_getvar_method(device_t dev, const char *name, void *buf, size_t *len,
2131728aef2SLandon J. Fuller     bhnd_nvram_type type)
214e83ce340SAdrian Chadd {
21577cb4d3eSLandon J. Fuller 	struct bhnd_sprom_softc	*sc = device_get_softc(dev);
216e83ce340SAdrian Chadd 
21777cb4d3eSLandon J. Fuller 	return (bhnd_nvram_store_getvar(sc->store, name, buf, len, type));
218e83ce340SAdrian Chadd }
219e83ce340SAdrian Chadd 
220e83ce340SAdrian Chadd /**
221f4a3eb02SAdrian Chadd  * Default bhnd sprom driver implementation of BHND_NVRAM_SETVAR().
222e83ce340SAdrian Chadd  */
223f4a3eb02SAdrian Chadd static int
bhnd_sprom_setvar_method(device_t dev,const char * name,const void * buf,size_t len,bhnd_nvram_type type)2241728aef2SLandon J. Fuller bhnd_sprom_setvar_method(device_t dev, const char *name, const void *buf,
2251728aef2SLandon J. Fuller     size_t len, bhnd_nvram_type type)
226e83ce340SAdrian Chadd {
22777cb4d3eSLandon J. Fuller 	struct bhnd_sprom_softc	*sc = device_get_softc(dev);
228e83ce340SAdrian Chadd 
22977cb4d3eSLandon J. Fuller 	return (bhnd_nvram_store_setvar(sc->store, name, buf, len, type));
230e83ce340SAdrian Chadd }
231e83ce340SAdrian Chadd 
232f4a3eb02SAdrian Chadd static device_method_t bhnd_sprom_methods[] = {
233f4a3eb02SAdrian Chadd 	/* Device interface */
234f4a3eb02SAdrian Chadd 	DEVMETHOD(device_probe,			bhnd_sprom_probe),
23556a4cdd1SLandon J. Fuller 	DEVMETHOD(device_attach,		bhnd_sprom_attach_meth),
236f4a3eb02SAdrian Chadd 	DEVMETHOD(device_resume,		bhnd_sprom_resume),
237f4a3eb02SAdrian Chadd 	DEVMETHOD(device_suspend,		bhnd_sprom_suspend),
238f4a3eb02SAdrian Chadd 	DEVMETHOD(device_detach,		bhnd_sprom_detach),
239e83ce340SAdrian Chadd 
240f4a3eb02SAdrian Chadd 	/* NVRAM interface */
2411728aef2SLandon J. Fuller 	DEVMETHOD(bhnd_nvram_getvar,		bhnd_sprom_getvar_method),
2421728aef2SLandon J. Fuller 	DEVMETHOD(bhnd_nvram_setvar,		bhnd_sprom_setvar_method),
243e83ce340SAdrian Chadd 
244f4a3eb02SAdrian Chadd 	DEVMETHOD_END
245f4a3eb02SAdrian Chadd };
246e83ce340SAdrian Chadd 
24777cb4d3eSLandon J. Fuller DEFINE_CLASS_0(bhnd_nvram_store, bhnd_sprom_driver, bhnd_sprom_methods, sizeof(struct bhnd_sprom_softc));
248f4a3eb02SAdrian Chadd MODULE_VERSION(bhnd_sprom, 1);
249