xref: /freebsd/sys/dev/bhnd/nvram/bhnd_sprom.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 /*
36  * BHND SPROM driver.
37  *
38  * Abstract driver for memory-mapped SPROM devices.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/bus.h>
44 #include <sys/limits.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/systm.h>
48 
49 #include <machine/bus.h>
50 #include <sys/rman.h>
51 #include <machine/resource.h>
52 
53 #include <dev/bhnd/bhnd.h>
54 
55 #include "bhnd_nvram_if.h"
56 
57 #include "bhnd_nvram_io.h"
58 
59 #include "bhnd_spromvar.h"
60 
61 /**
62  * Default bhnd sprom driver implementation of DEVICE_PROBE().
63  */
64 int
65 bhnd_sprom_probe(device_t dev)
66 {
67 	device_set_desc(dev, "SPROM/OTP");
68 
69 	/* Refuse wildcard attachments */
70 	return (BUS_PROBE_NOWILDCARD);
71 }
72 
73 /* Default DEVICE_ATTACH() implementation; assumes a zero offset to the
74  * SPROM data */
75 static int
76 bhnd_sprom_attach_meth(device_t dev)
77 {
78 	return (bhnd_sprom_attach(dev, 0));
79 }
80 
81 /**
82  * BHND SPROM device attach.
83  *
84  * This should be called from DEVICE_ATTACH() with the @p offset to the
85  * SPROM data.
86  *
87  * Assumes SPROM is mapped via SYS_RES_MEMORY resource with RID 0.
88  *
89  * @param dev BHND SPROM device.
90  * @param offset Offset to the SPROM data.
91  */
92 int
93 bhnd_sprom_attach(device_t dev, bus_size_t offset)
94 {
95 	struct bhnd_sprom_softc	*sc;
96 	struct bhnd_nvram_io	*io;
97 	struct bhnd_resource	*r;
98 	bus_size_t		 r_size, sprom_size;
99 	int			 rid;
100 	int			 error;
101 
102 	sc = device_get_softc(dev);
103 	sc->dev = dev;
104 	sc->store = NULL;
105 
106 	io = NULL;
107 	r = NULL;
108 
109 	/* Allocate SPROM resource */
110 	rid = 0;
111 	r = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
112 	if (r == NULL) {
113 		device_printf(dev, "failed to allocate resources\n");
114 		return (ENXIO);
115 	}
116 
117 	/* Determine SPROM size */
118 	r_size = rman_get_size(r->res);
119 	if (r_size <= offset || (r_size - offset) > BUS_SPACE_MAXSIZE) {
120 		device_printf(dev, "invalid sprom offset\n");
121 		error = ENXIO;
122 		goto failed;
123 	}
124 
125 	sprom_size = r_size - offset;
126 
127 	/* Allocate an I/O context for the SPROM parser. All SPROM reads
128 	 * must be 16-bit aligned */
129 	io = bhnd_nvram_iores_new(r, offset, sprom_size, sizeof(uint16_t));
130 	if (io == NULL) {
131 		error = ENXIO;
132 		goto failed;
133 	}
134 
135 	/* Initialize NVRAM data store */
136 	error = bhnd_nvram_store_parse_new(&sc->store, io,
137 	    &bhnd_nvram_sprom_class);
138 	if (error)
139 		goto failed;
140 
141 	/* Clean up our temporary I/O context and its backing resource */
142 	bhnd_nvram_io_free(io);
143 	bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
144 
145 	io = NULL;
146 	r = NULL;
147 
148 	/* Register ourselves with the bus */
149 	if ((error = bhnd_register_provider(dev, BHND_SERVICE_NVRAM))) {
150 		device_printf(dev, "failed to register NVRAM provider: %d\n",
151 		    error);
152 		goto failed;
153 	}
154 
155 	return (0);
156 
157 failed:
158 	/* Clean up I/O context before releasing its backing resource */
159 	if (io != NULL)
160 		bhnd_nvram_io_free(io);
161 
162 	if (r != NULL)
163 		bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
164 
165 	if (sc->store != NULL)
166 		bhnd_nvram_store_free(sc->store);
167 
168 	return (error);
169 }
170 
171 /**
172  * Default bhnd_sprom implementation of DEVICE_RESUME().
173  */
174 int
175 bhnd_sprom_resume(device_t dev)
176 {
177 	return (0);
178 }
179 
180 /**
181  * Default bhnd sprom driver implementation of DEVICE_SUSPEND().
182  */
183 int
184 bhnd_sprom_suspend(device_t dev)
185 {
186 	return (0);
187 }
188 
189 /**
190  * Default bhnd sprom driver implementation of DEVICE_DETACH().
191  */
192 int
193 bhnd_sprom_detach(device_t dev)
194 {
195 	struct bhnd_sprom_softc	*sc;
196 	int			 error;
197 
198 	sc = device_get_softc(dev);
199 
200 	if ((error = bhnd_deregister_provider(dev, BHND_SERVICE_ANY)))
201 		return (error);
202 
203 	bhnd_nvram_store_free(sc->store);
204 
205 	return (0);
206 }
207 
208 /**
209  * Default bhnd sprom driver implementation of BHND_NVRAM_GETVAR().
210  */
211 static int
212 bhnd_sprom_getvar_method(device_t dev, const char *name, void *buf, size_t *len,
213     bhnd_nvram_type type)
214 {
215 	struct bhnd_sprom_softc	*sc = device_get_softc(dev);
216 
217 	return (bhnd_nvram_store_getvar(sc->store, name, buf, len, type));
218 }
219 
220 /**
221  * Default bhnd sprom driver implementation of BHND_NVRAM_SETVAR().
222  */
223 static int
224 bhnd_sprom_setvar_method(device_t dev, const char *name, const void *buf,
225     size_t len, bhnd_nvram_type type)
226 {
227 	struct bhnd_sprom_softc	*sc = device_get_softc(dev);
228 
229 	return (bhnd_nvram_store_setvar(sc->store, name, buf, len, type));
230 }
231 
232 static device_method_t bhnd_sprom_methods[] = {
233 	/* Device interface */
234 	DEVMETHOD(device_probe,			bhnd_sprom_probe),
235 	DEVMETHOD(device_attach,		bhnd_sprom_attach_meth),
236 	DEVMETHOD(device_resume,		bhnd_sprom_resume),
237 	DEVMETHOD(device_suspend,		bhnd_sprom_suspend),
238 	DEVMETHOD(device_detach,		bhnd_sprom_detach),
239 
240 	/* NVRAM interface */
241 	DEVMETHOD(bhnd_nvram_getvar,		bhnd_sprom_getvar_method),
242 	DEVMETHOD(bhnd_nvram_setvar,		bhnd_sprom_setvar_method),
243 
244 	DEVMETHOD_END
245 };
246 
247 DEFINE_CLASS_0(bhnd_nvram_store, bhnd_sprom_driver, bhnd_sprom_methods, sizeof(struct bhnd_sprom_softc));
248 MODULE_VERSION(bhnd_sprom, 1);
249