xref: /freebsd/sys/dev/bhnd/nvram/bhnd_sprom.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3  * Copyright (c) 2017 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Landon Fuller
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
19  *
20  * NO WARRANTY
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.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * BHND SPROM driver.
39  *
40  * Abstract driver for memory-mapped SPROM devices.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/bus.h>
46 #include <sys/limits.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/systm.h>
50 
51 #include <machine/bus.h>
52 #include <sys/rman.h>
53 #include <machine/resource.h>
54 
55 #include <dev/bhnd/bhnd.h>
56 
57 #include "bhnd_nvram_if.h"
58 
59 #include "bhnd_nvram_io.h"
60 
61 #include "bhnd_spromvar.h"
62 
63 /**
64  * Default bhnd sprom driver implementation of DEVICE_PROBE().
65  */
66 int
67 bhnd_sprom_probe(device_t dev)
68 {
69 	device_set_desc(dev, "SPROM/OTP");
70 
71 	/* Refuse wildcard attachments */
72 	return (BUS_PROBE_NOWILDCARD);
73 }
74 
75 /* Default DEVICE_ATTACH() implementation; assumes a zero offset to the
76  * SPROM data */
77 static int
78 bhnd_sprom_attach_meth(device_t dev)
79 {
80 	return (bhnd_sprom_attach(dev, 0));
81 }
82 
83 /**
84  * BHND SPROM device attach.
85  *
86  * This should be called from DEVICE_ATTACH() with the @p offset to the
87  * SPROM data.
88  *
89  * Assumes SPROM is mapped via SYS_RES_MEMORY resource with RID 0.
90  *
91  * @param dev BHND SPROM device.
92  * @param offset Offset to the SPROM data.
93  */
94 int
95 bhnd_sprom_attach(device_t dev, bus_size_t offset)
96 {
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;
101 	int			 rid;
102 	int			 error;
103 
104 	sc = device_get_softc(dev);
105 	sc->dev = dev;
106 	sc->store = NULL;
107 
108 	io = NULL;
109 	r = NULL;
110 
111 	/* Allocate SPROM resource */
112 	rid = 0;
113 	r = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
114 	if (r == NULL) {
115 		device_printf(dev, "failed to allocate resources\n");
116 		return (ENXIO);
117 	}
118 
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");
123 		error = ENXIO;
124 		goto failed;
125 	}
126 
127 	sprom_size = r_size - offset;
128 
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));
132 	if (io == NULL) {
133 		error = ENXIO;
134 		goto failed;
135 	}
136 
137 	/* Initialize NVRAM data store */
138 	error = bhnd_nvram_store_parse_new(&sc->store, io,
139 	    &bhnd_nvram_sprom_class);
140 	if (error)
141 		goto failed;
142 
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);
146 
147 	io = NULL;
148 	r = NULL;
149 
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",
153 		    error);
154 		goto failed;
155 	}
156 
157 	return (0);
158 
159 failed:
160 	/* Clean up I/O context before releasing its backing resource */
161 	if (io != NULL)
162 		bhnd_nvram_io_free(io);
163 
164 	if (r != NULL)
165 		bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
166 
167 	if (sc->store != NULL)
168 		bhnd_nvram_store_free(sc->store);
169 
170 	return (error);
171 }
172 
173 /**
174  * Default bhnd_sprom implementation of DEVICE_RESUME().
175  */
176 int
177 bhnd_sprom_resume(device_t dev)
178 {
179 	return (0);
180 }
181 
182 /**
183  * Default bhnd sprom driver implementation of DEVICE_SUSPEND().
184  */
185 int
186 bhnd_sprom_suspend(device_t dev)
187 {
188 	return (0);
189 }
190 
191 /**
192  * Default bhnd sprom driver implementation of DEVICE_DETACH().
193  */
194 int
195 bhnd_sprom_detach(device_t dev)
196 {
197 	struct bhnd_sprom_softc	*sc;
198 	int			 error;
199 
200 	sc = device_get_softc(dev);
201 
202 	if ((error = bhnd_deregister_provider(dev, BHND_SERVICE_ANY)))
203 		return (error);
204 
205 	bhnd_nvram_store_free(sc->store);
206 
207 	return (0);
208 }
209 
210 /**
211  * Default bhnd sprom driver implementation of BHND_NVRAM_GETVAR().
212  */
213 static int
214 bhnd_sprom_getvar_method(device_t dev, const char *name, void *buf, size_t *len,
215     bhnd_nvram_type type)
216 {
217 	struct bhnd_sprom_softc	*sc = device_get_softc(dev);
218 
219 	return (bhnd_nvram_store_getvar(sc->store, name, buf, len, type));
220 }
221 
222 /**
223  * Default bhnd sprom driver implementation of BHND_NVRAM_SETVAR().
224  */
225 static int
226 bhnd_sprom_setvar_method(device_t dev, const char *name, const void *buf,
227     size_t len, bhnd_nvram_type type)
228 {
229 	struct bhnd_sprom_softc	*sc = device_get_softc(dev);
230 
231 	return (bhnd_nvram_store_setvar(sc->store, name, buf, len, type));
232 }
233 
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),
241 
242 	/* NVRAM interface */
243 	DEVMETHOD(bhnd_nvram_getvar,		bhnd_sprom_getvar_method),
244 	DEVMETHOD(bhnd_nvram_setvar,		bhnd_sprom_setvar_method),
245 
246 	DEVMETHOD_END
247 };
248 
249 DEFINE_CLASS_0(bhnd_nvram_store, bhnd_sprom_driver, bhnd_sprom_methods, sizeof(struct bhnd_sprom_softc));
250 MODULE_VERSION(bhnd_sprom, 1);
251