]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/bhnd/nvram/bhnd_sprom.c
Pull down pjdfstest 0.1
[FreeBSD/FreeBSD.git] / sys / dev / bhnd / nvram / bhnd_sprom.c
1 /*-
2  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34  * BHND SPROM driver.
35  * 
36  * Abstract driver for memory-mapped SPROM devices.
37  */
38
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/limits.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/systm.h>
46
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <machine/resource.h>
50
51 #include <dev/bhnd/bhnd.h>
52
53 #include "bhnd_nvram_if.h"
54
55 #include "bhnd_nvram_io.h"
56
57 #include "bhnd_spromvar.h"
58
59 /**
60  * Default bhnd sprom driver implementation of DEVICE_PROBE().
61  */
62 int
63 bhnd_sprom_probe(device_t dev)
64 {
65         device_set_desc(dev, "SPROM/OTP");
66
67         /* Refuse wildcard attachments */
68         return (BUS_PROBE_NOWILDCARD);
69 }
70
71 /* Default DEVICE_ATTACH() implementation; assumes a zero offset to the
72  * SPROM data */
73 static int
74 bhnd_sprom_attach_meth(device_t dev)
75 {
76         return (bhnd_sprom_attach(dev, 0));
77 }
78
79 /**
80  * BHND SPROM device attach.
81  * 
82  * This should be called from DEVICE_ATTACH() with the @p offset to the
83  * SPROM data.
84  * 
85  * Assumes SPROM is mapped via SYS_RES_MEMORY resource with RID 0.
86  * 
87  * @param dev BHND SPROM device.
88  * @param offset Offset to the SPROM data.
89  */
90 int
91 bhnd_sprom_attach(device_t dev, bus_size_t offset)
92 {
93         struct bhnd_sprom_softc *sc;
94         struct bhnd_nvram_io    *io;
95         struct bhnd_resource    *r;
96         bus_size_t               r_size, sprom_size;
97         int                      rid;
98         int                      error;
99
100         sc = device_get_softc(dev);
101         sc->dev = dev;
102
103         io = NULL;
104
105         /* Allocate SPROM resource */
106         rid = 0;
107         r = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
108         if (r == NULL) {
109                 device_printf(dev, "failed to allocate resources\n");
110                 return (ENXIO);
111         }
112
113         /* Determine SPROM size */
114         r_size = rman_get_size(r->res);
115         if (r_size <= offset || (r_size - offset) > BUS_SPACE_MAXSIZE) {
116                 device_printf(dev, "invalid sprom offset\n");
117                 error = ENXIO;
118                 goto failed;
119         }
120
121         sprom_size = r_size - offset;
122
123         /* Allocate an I/O context for the SPROM parser. All SPROM reads
124          * must be 16-bit aligned */
125         io = bhnd_nvram_iores_new(r, offset, sprom_size, sizeof(uint16_t));
126         if (io == NULL) {
127                 error = ENXIO;
128                 goto failed;
129         }
130
131         /* Initialize NVRAM data store */
132         error = bhnd_nvram_store_parse_new(&sc->store, io,
133             &bhnd_nvram_sprom_class);
134         if (error)
135                 goto failed;
136
137         /* Clean up our temporary I/O context and its backing resource */
138         bhnd_nvram_io_free(io);
139         bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
140
141         return (0);
142
143 failed:
144         /* Clean up I/O context before releasing its backing resource */
145         if (io != NULL)
146                 bhnd_nvram_io_free(io);
147
148         bhnd_release_resource(dev, SYS_RES_MEMORY, rid, r);
149
150         return (error);
151 }
152
153 /**
154  * Default bhnd_sprom implementation of DEVICE_RESUME().
155  */
156 int
157 bhnd_sprom_resume(device_t dev)
158 {
159         return (0);
160 }
161
162 /**
163  * Default bhnd sprom driver implementation of DEVICE_SUSPEND().
164  */
165 int
166 bhnd_sprom_suspend(device_t dev)
167 {
168         return (0);
169 }
170
171 /**
172  * Default bhnd sprom driver implementation of DEVICE_DETACH().
173  */
174 int
175 bhnd_sprom_detach(device_t dev)
176 {
177         struct bhnd_sprom_softc *sc;
178         
179         sc = device_get_softc(dev);
180
181         bhnd_nvram_store_free(sc->store);
182
183         return (0);
184 }
185
186 /**
187  * Default bhnd sprom driver implementation of BHND_NVRAM_GETVAR().
188  */
189 static int
190 bhnd_sprom_getvar_method(device_t dev, const char *name, void *buf, size_t *len,
191     bhnd_nvram_type type)
192 {
193         struct bhnd_sprom_softc *sc = device_get_softc(dev);
194
195         return (bhnd_nvram_store_getvar(sc->store, name, buf, len, type));
196 }
197
198 /**
199  * Default bhnd sprom driver implementation of BHND_NVRAM_SETVAR().
200  */
201 static int
202 bhnd_sprom_setvar_method(device_t dev, const char *name, const void *buf,
203     size_t len, bhnd_nvram_type type)
204 {
205         struct bhnd_sprom_softc *sc = device_get_softc(dev);
206
207         return (bhnd_nvram_store_setvar(sc->store, name, buf, len, type));
208 }
209
210 static device_method_t bhnd_sprom_methods[] = {
211         /* Device interface */
212         DEVMETHOD(device_probe,                 bhnd_sprom_probe),
213         DEVMETHOD(device_attach,                bhnd_sprom_attach_meth),
214         DEVMETHOD(device_resume,                bhnd_sprom_resume),
215         DEVMETHOD(device_suspend,               bhnd_sprom_suspend),
216         DEVMETHOD(device_detach,                bhnd_sprom_detach),
217
218         /* NVRAM interface */
219         DEVMETHOD(bhnd_nvram_getvar,            bhnd_sprom_getvar_method),
220         DEVMETHOD(bhnd_nvram_setvar,            bhnd_sprom_setvar_method),
221
222         DEVMETHOD_END
223 };
224
225 DEFINE_CLASS_0(bhnd_nvram_store, bhnd_sprom_driver, bhnd_sprom_methods, sizeof(struct bhnd_sprom_softc));
226 MODULE_VERSION(bhnd_sprom, 1);