nvram_pnpbus.c revision 1.3.2.5 1 1.3.2.5 yamt /* $NetBSD: nvram_pnpbus.c,v 1.3.2.5 2007/10/27 11:28:09 yamt Exp $ */
2 1.3.2.2 yamt
3 1.3.2.2 yamt /*-
4 1.3.2.2 yamt * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.3.2.2 yamt * All rights reserved.
6 1.3.2.2 yamt *
7 1.3.2.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 yamt * by Tim Rightnour
9 1.3.2.2 yamt *
10 1.3.2.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 yamt * modification, are permitted provided that the following conditions
12 1.3.2.2 yamt * are met:
13 1.3.2.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 yamt * documentation and/or other materials provided with the distribution.
18 1.3.2.2 yamt * 3. All advertising materials mentioning features or use of this software
19 1.3.2.2 yamt * must display the following acknowledgement:
20 1.3.2.2 yamt * This product includes software developed by the NetBSD
21 1.3.2.2 yamt * Foundation, Inc. and its contributors.
22 1.3.2.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.2.2 yamt * contributors may be used to endorse or promote products derived
24 1.3.2.2 yamt * from this software without specific prior written permission.
25 1.3.2.2 yamt *
26 1.3.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.3.2.2 yamt */
38 1.3.2.2 yamt
39 1.3.2.2 yamt #include <sys/cdefs.h>
40 1.3.2.5 yamt __KERNEL_RCSID(0, "$NetBSD: nvram_pnpbus.c,v 1.3.2.5 2007/10/27 11:28:09 yamt Exp $");
41 1.3.2.2 yamt
42 1.3.2.2 yamt #include <sys/types.h>
43 1.3.2.2 yamt #include <sys/param.h>
44 1.3.2.2 yamt #include <sys/systm.h>
45 1.3.2.2 yamt #include <sys/ioctl.h>
46 1.3.2.2 yamt #include <sys/conf.h>
47 1.3.2.2 yamt #include <sys/kthread.h>
48 1.3.2.2 yamt #include <sys/device.h>
49 1.3.2.2 yamt #include <sys/malloc.h>
50 1.3.2.2 yamt #include <sys/lock.h>
51 1.3.2.2 yamt
52 1.3.2.2 yamt #include <machine/bus.h>
53 1.3.2.2 yamt #include <machine/intr.h>
54 1.3.2.2 yamt #include <machine/isa_machdep.h>
55 1.3.2.2 yamt /* clock stuff for motorolla machines */
56 1.3.2.2 yamt #include <dev/clock_subr.h>
57 1.3.2.2 yamt #include <dev/ic/mk48txxreg.h>
58 1.3.2.2 yamt
59 1.3.2.4 yamt #include <uvm/uvm_extern.h>
60 1.3.2.4 yamt
61 1.3.2.4 yamt #include <machine/residual.h>
62 1.3.2.2 yamt #include <machine/nvram.h>
63 1.3.2.2 yamt
64 1.3.2.2 yamt #include <prep/pnpbus/pnpbusvar.h>
65 1.3.2.2 yamt
66 1.3.2.2 yamt #include "opt_nvram.h"
67 1.3.2.2 yamt
68 1.3.2.2 yamt static char *nvramData;
69 1.3.2.2 yamt static NVRAM_MAP *nvram;
70 1.3.2.2 yamt static char *nvramGEAp; /* pointer to the GE area */
71 1.3.2.2 yamt static char *nvramCAp; /* pointer to the Config area */
72 1.3.2.2 yamt static char *nvramOSAp; /* pointer to the OSArea */
73 1.3.2.2 yamt struct simplelock nvram_slock; /* lock */
74 1.3.2.2 yamt
75 1.3.2.2 yamt int prep_clock_mk48txx;
76 1.3.2.2 yamt
77 1.3.2.2 yamt extern char bootpath[256];
78 1.3.2.4 yamt extern RESIDUAL resdata;
79 1.3.2.2 yamt
80 1.3.2.2 yamt #define NVRAM_STD_DEV 0
81 1.3.2.2 yamt
82 1.3.2.2 yamt static int nvram_pnpbus_probe(struct device *, struct cfdata *, void *);
83 1.3.2.2 yamt static void nvram_pnpbus_attach(struct device *, struct device *, void *);
84 1.3.2.2 yamt uint8_t prep_nvram_read_val(int);
85 1.3.2.2 yamt char *prep_nvram_next_var(char *);
86 1.3.2.2 yamt char *prep_nvram_find_var(const char *);
87 1.3.2.2 yamt char *prep_nvram_get_var(const char *);
88 1.3.2.2 yamt int prep_nvram_get_var_len(const char *);
89 1.3.2.2 yamt int prep_nvram_count_vars(void);
90 1.3.2.2 yamt void prep_nvram_write_val(int, uint8_t);
91 1.3.2.2 yamt uint8_t mkclock_pnpbus_nvrd(struct mk48txx_softc *, int);
92 1.3.2.2 yamt void mkclock_pnpbus_nvwr(struct mk48txx_softc *, int, uint8_t);
93 1.3.2.2 yamt
94 1.3.2.2 yamt CFATTACH_DECL(nvram_pnpbus, sizeof(struct nvram_pnpbus_softc),
95 1.3.2.2 yamt nvram_pnpbus_probe, nvram_pnpbus_attach, NULL, NULL);
96 1.3.2.2 yamt
97 1.3.2.2 yamt dev_type_open(prep_nvramopen);
98 1.3.2.2 yamt dev_type_ioctl(prep_nvramioctl);
99 1.3.2.2 yamt dev_type_close(prep_nvramclose);
100 1.3.2.4 yamt dev_type_read(prep_nvramread);
101 1.3.2.2 yamt
102 1.3.2.4 yamt const struct cdevsw nvram_cdevsw = {
103 1.3.2.4 yamt prep_nvramopen, prep_nvramclose, prep_nvramread, nowrite,
104 1.3.2.4 yamt prep_nvramioctl, nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
105 1.3.2.2 yamt };
106 1.3.2.2 yamt
107 1.3.2.2 yamt extern struct cfdriver nvram_cd;
108 1.3.2.2 yamt
109 1.3.2.2 yamt static int
110 1.3.2.2 yamt nvram_pnpbus_probe(struct device *parent, struct cfdata *match, void *aux)
111 1.3.2.2 yamt {
112 1.3.2.2 yamt struct pnpbus_dev_attach_args *pna = aux;
113 1.3.2.2 yamt int ret = 0;
114 1.3.2.2 yamt
115 1.3.2.2 yamt if (strcmp(pna->pna_devid, "IBM0008") == 0)
116 1.3.2.2 yamt ret = 1;
117 1.3.2.2 yamt
118 1.3.2.2 yamt if (ret)
119 1.3.2.2 yamt pnpbus_scan(pna, pna->pna_ppc_dev);
120 1.3.2.2 yamt
121 1.3.2.2 yamt return ret;
122 1.3.2.2 yamt }
123 1.3.2.2 yamt
124 1.3.2.2 yamt static void
125 1.3.2.2 yamt nvram_pnpbus_attach(struct device *parent, struct device *self, void *aux)
126 1.3.2.2 yamt {
127 1.3.2.2 yamt struct nvram_pnpbus_softc *sc = (void *)self;
128 1.3.2.2 yamt struct pnpbus_dev_attach_args *pna = aux;
129 1.3.2.2 yamt int as_iobase, as_len, data_iobase, data_len, i, nvlen, cur;
130 1.3.2.2 yamt uint8_t *p;
131 1.3.2.2 yamt HEADER prep_nvram_header;
132 1.3.2.2 yamt
133 1.3.2.2 yamt sc->sc_iot = pna->pna_iot;
134 1.3.2.2 yamt
135 1.3.2.2 yamt pnpbus_getioport(&pna->pna_res, 0, &as_iobase, &as_len);
136 1.3.2.2 yamt pnpbus_getioport(&pna->pna_res, 1, &data_iobase, &data_len);
137 1.3.2.2 yamt
138 1.3.2.2 yamt if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_as, &sc->sc_ash) ||
139 1.3.2.2 yamt pnpbus_io_map(&pna->pna_res, 1, &sc->sc_data, &sc->sc_datah)) {
140 1.3.2.2 yamt aprint_error("nvram: couldn't map registers\n");
141 1.3.2.2 yamt return;
142 1.3.2.2 yamt }
143 1.3.2.2 yamt
144 1.3.2.2 yamt simple_lock_init(&nvram_slock);
145 1.3.2.2 yamt
146 1.3.2.2 yamt /* Initialize the nvram header */
147 1.3.2.2 yamt p = (uint8_t *) &prep_nvram_header;
148 1.3.2.2 yamt for (i = 0; i < sizeof(HEADER); i++)
149 1.3.2.2 yamt *p++ = prep_nvram_read_val(i);
150 1.3.2.2 yamt
151 1.3.2.2 yamt /*
152 1.3.2.2 yamt * now that we have the header, we know how big the NVRAM part on
153 1.3.2.2 yamt * this machine really is. Malloc space to save a copy.
154 1.3.2.2 yamt */
155 1.3.2.2 yamt
156 1.3.2.2 yamt nvlen = 1024 * prep_nvram_header.Size;
157 1.3.2.2 yamt nvramData = malloc(nvlen, M_DEVBUF, M_NOWAIT);
158 1.3.2.2 yamt p = (uint8_t *) nvramData;
159 1.3.2.2 yamt
160 1.3.2.2 yamt /*
161 1.3.2.2 yamt * now read the whole nvram in, one chunk at a time, marking down
162 1.3.2.2 yamt * the main start points as we go.
163 1.3.2.2 yamt */
164 1.3.2.2 yamt for (i = 0; i < sizeof(HEADER) && i < nvlen; i++)
165 1.3.2.2 yamt *p++ = prep_nvram_read_val(i);
166 1.3.2.2 yamt nvramGEAp = p;
167 1.3.2.2 yamt cur = i;
168 1.3.2.2 yamt for (; i < cur + prep_nvram_header.GELength && i < nvlen; i++)
169 1.3.2.2 yamt *p++ = prep_nvram_read_val(i);
170 1.3.2.2 yamt nvramOSAp = p;
171 1.3.2.2 yamt cur = i;
172 1.3.2.2 yamt for (; i < cur + prep_nvram_header.OSAreaLength && i < nvlen; i++)
173 1.3.2.2 yamt *p++ = prep_nvram_read_val(i);
174 1.3.2.2 yamt nvramCAp = p;
175 1.3.2.2 yamt cur = i;
176 1.3.2.2 yamt for (; i < cur + prep_nvram_header.ConfigLength && i < nvlen; i++)
177 1.3.2.2 yamt *p++ = prep_nvram_read_val(i);
178 1.3.2.2 yamt
179 1.3.2.2 yamt /* we should be done here. umm.. yay? */
180 1.3.2.2 yamt nvram = (NVRAM_MAP *)&nvramData[0];
181 1.3.2.2 yamt aprint_normal("\n");
182 1.3.2.2 yamt aprint_verbose("%s: Read %d bytes from nvram of size %d\n",
183 1.3.2.2 yamt device_xname(self), i, nvlen);
184 1.3.2.2 yamt
185 1.3.2.2 yamt #if defined(NVRAM_DUMP)
186 1.3.2.2 yamt printf("Boot device: %s\n", prep_nvram_get_var("fw-boot-device"));
187 1.3.2.2 yamt printf("Dumping nvram\n");
188 1.3.2.2 yamt for (cur=0; cur < i; cur++) {
189 1.3.2.2 yamt printf("%c", nvramData[cur]);
190 1.3.2.2 yamt if (cur % 70 == 0)
191 1.3.2.2 yamt printf("\n");
192 1.3.2.2 yamt }
193 1.3.2.2 yamt #endif
194 1.3.2.2 yamt strncpy(bootpath, prep_nvram_get_var("fw-boot-device"), 256);
195 1.3.2.2 yamt
196 1.3.2.2 yamt
197 1.3.2.2 yamt if (prep_clock_mk48txx == 0)
198 1.3.2.2 yamt return;
199 1.3.2.2 yamt /* otherwise, we have a motorolla clock chip. Set it up. */
200 1.3.2.2 yamt sc->sc_mksc.sc_model = "mk48t18";
201 1.3.2.2 yamt sc->sc_mksc.sc_year0 = 1900;
202 1.3.2.2 yamt sc->sc_mksc.sc_nvrd = mkclock_pnpbus_nvrd;
203 1.3.2.2 yamt sc->sc_mksc.sc_nvwr = mkclock_pnpbus_nvwr;
204 1.3.2.2 yamt /* copy down the bus space tags */
205 1.3.2.2 yamt sc->sc_mksc.sc_bst = sc->sc_as;
206 1.3.2.2 yamt sc->sc_mksc.sc_bsh = sc->sc_ash;
207 1.3.2.2 yamt sc->sc_mksc.sc_data = sc->sc_data;
208 1.3.2.2 yamt sc->sc_mksc.sc_datah = sc->sc_datah;
209 1.3.2.2 yamt
210 1.3.2.2 yamt aprint_normal("%s: attaching clock", device_xname(self));
211 1.3.2.2 yamt mk48txx_attach((struct mk48txx_softc *)&sc->sc_mksc);
212 1.3.2.3 yamt aprint_normal("\n");
213 1.3.2.2 yamt todr_attach(&sc->sc_mksc.sc_handle);
214 1.3.2.2 yamt }
215 1.3.2.2 yamt
216 1.3.2.2 yamt /*
217 1.3.2.2 yamt * This function should be called at a high spl only, as it interfaces with
218 1.3.2.2 yamt * real hardware.
219 1.3.2.2 yamt */
220 1.3.2.2 yamt
221 1.3.2.2 yamt uint8_t
222 1.3.2.2 yamt prep_nvram_read_val(int addr)
223 1.3.2.2 yamt {
224 1.3.2.2 yamt struct nvram_pnpbus_softc *sc;
225 1.3.2.2 yamt
226 1.3.2.2 yamt if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
227 1.3.2.2 yamt || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
228 1.3.2.2 yamt return 0;
229 1.3.2.2 yamt }
230 1.3.2.2 yamt
231 1.3.2.2 yamt sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
232 1.3.2.2 yamt
233 1.3.2.2 yamt /* tell the NVRAM what we want */
234 1.3.2.2 yamt bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
235 1.3.2.2 yamt bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
236 1.3.2.2 yamt
237 1.3.2.2 yamt return bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
238 1.3.2.2 yamt }
239 1.3.2.2 yamt
240 1.3.2.2 yamt /*
241 1.3.2.2 yamt * This function should be called at a high spl only, as it interfaces with
242 1.3.2.2 yamt * real hardware.
243 1.3.2.2 yamt */
244 1.3.2.2 yamt
245 1.3.2.2 yamt void
246 1.3.2.2 yamt prep_nvram_write_val(int addr, uint8_t val)
247 1.3.2.2 yamt {
248 1.3.2.2 yamt struct nvram_pnpbus_softc *sc;
249 1.3.2.2 yamt
250 1.3.2.2 yamt if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
251 1.3.2.2 yamt || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
252 1.3.2.2 yamt return;
253 1.3.2.2 yamt }
254 1.3.2.2 yamt
255 1.3.2.2 yamt sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
256 1.3.2.2 yamt
257 1.3.2.2 yamt /* tell the NVRAM what we want */
258 1.3.2.2 yamt bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
259 1.3.2.2 yamt bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
260 1.3.2.2 yamt
261 1.3.2.2 yamt bus_space_write_1(sc->sc_data, sc->sc_datah, 0, val);
262 1.3.2.2 yamt }
263 1.3.2.2 yamt
264 1.3.2.2 yamt /* the rest of these should all be called with the lock held */
265 1.3.2.2 yamt
266 1.3.2.2 yamt char *
267 1.3.2.2 yamt prep_nvram_next_var(char *name)
268 1.3.2.2 yamt {
269 1.3.2.2 yamt char *cp;
270 1.3.2.2 yamt
271 1.3.2.4 yamt if (name == NULL)
272 1.3.2.4 yamt return NULL;
273 1.3.2.4 yamt
274 1.3.2.2 yamt cp = name;
275 1.3.2.2 yamt /* skip forward to the first null char */
276 1.3.2.2 yamt while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp != '\0'))
277 1.3.2.2 yamt cp++;
278 1.3.2.2 yamt /* skip nulls */
279 1.3.2.2 yamt while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp == '\0'))
280 1.3.2.2 yamt cp++;
281 1.3.2.2 yamt if ((cp - nvramGEAp) < nvram->Header.GELength)
282 1.3.2.2 yamt return cp;
283 1.3.2.2 yamt else
284 1.3.2.2 yamt return NULL;
285 1.3.2.2 yamt }
286 1.3.2.2 yamt
287 1.3.2.2 yamt char *
288 1.3.2.2 yamt prep_nvram_find_var(const char *name)
289 1.3.2.2 yamt {
290 1.3.2.2 yamt char *cp = nvramGEAp;
291 1.3.2.2 yamt size_t len;
292 1.3.2.2 yamt
293 1.3.2.2 yamt len = strlen(name);
294 1.3.2.2 yamt while (cp != NULL) {
295 1.3.2.2 yamt if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
296 1.3.2.2 yamt return cp;
297 1.3.2.2 yamt cp = prep_nvram_next_var(cp);
298 1.3.2.2 yamt }
299 1.3.2.2 yamt return NULL;
300 1.3.2.2 yamt }
301 1.3.2.2 yamt
302 1.3.2.2 yamt char *
303 1.3.2.2 yamt prep_nvram_get_var(const char *name)
304 1.3.2.2 yamt {
305 1.3.2.2 yamt char *cp = nvramGEAp;
306 1.3.2.2 yamt size_t len;
307 1.3.2.2 yamt
308 1.3.2.4 yamt if (name == NULL)
309 1.3.2.4 yamt return NULL;
310 1.3.2.2 yamt len = strlen(name);
311 1.3.2.2 yamt while (cp != NULL) {
312 1.3.2.2 yamt if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
313 1.3.2.2 yamt return cp+len+1;
314 1.3.2.2 yamt cp = prep_nvram_next_var(cp);
315 1.3.2.2 yamt }
316 1.3.2.2 yamt return NULL;
317 1.3.2.2 yamt }
318 1.3.2.2 yamt
319 1.3.2.2 yamt int
320 1.3.2.2 yamt prep_nvram_get_var_len(const char *name)
321 1.3.2.2 yamt {
322 1.3.2.2 yamt char *cp = nvramGEAp;
323 1.3.2.2 yamt char *ep;
324 1.3.2.2 yamt size_t len;
325 1.3.2.2 yamt
326 1.3.2.4 yamt if (name == NULL)
327 1.3.2.4 yamt return -1;
328 1.3.2.4 yamt
329 1.3.2.2 yamt len = strlen(name);
330 1.3.2.2 yamt while (cp != NULL) {
331 1.3.2.2 yamt if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
332 1.3.2.2 yamt goto out;
333 1.3.2.2 yamt cp = prep_nvram_next_var(cp);
334 1.3.2.2 yamt }
335 1.3.2.2 yamt return -1;
336 1.3.2.2 yamt
337 1.3.2.2 yamt out:
338 1.3.2.2 yamt ep = cp;
339 1.3.2.2 yamt while (ep != NULL && *ep != '\0')
340 1.3.2.2 yamt ep++;
341 1.3.2.2 yamt return ep-cp;
342 1.3.2.2 yamt }
343 1.3.2.2 yamt
344 1.3.2.2 yamt int
345 1.3.2.2 yamt prep_nvram_count_vars(void)
346 1.3.2.2 yamt {
347 1.3.2.2 yamt char *cp = nvramGEAp;
348 1.3.2.2 yamt int i=0;
349 1.3.2.2 yamt
350 1.3.2.2 yamt while (cp != NULL) {
351 1.3.2.2 yamt i++;
352 1.3.2.2 yamt cp = prep_nvram_next_var(cp);
353 1.3.2.2 yamt }
354 1.3.2.2 yamt return i;
355 1.3.2.2 yamt }
356 1.3.2.2 yamt
357 1.3.2.4 yamt static int
358 1.3.2.4 yamt nvramgetstr(int len, char *user, char **cpp)
359 1.3.2.4 yamt {
360 1.3.2.4 yamt int error;
361 1.3.2.4 yamt char *cp;
362 1.3.2.4 yamt
363 1.3.2.4 yamt /* Reject obvious bogus requests */
364 1.3.2.4 yamt if ((u_int)len > (8 * 1024) - 1)
365 1.3.2.4 yamt return ENAMETOOLONG;
366 1.3.2.4 yamt
367 1.3.2.4 yamt *cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
368 1.3.2.4 yamt error = copyin(user, cp, len);
369 1.3.2.4 yamt cp[len] = '\0';
370 1.3.2.4 yamt return error;
371 1.3.2.4 yamt }
372 1.3.2.4 yamt
373 1.3.2.2 yamt int
374 1.3.2.4 yamt prep_nvramioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
375 1.3.2.2 yamt {
376 1.3.2.2 yamt int len, error;
377 1.3.2.2 yamt struct pnviocdesc *pnv;
378 1.3.2.4 yamt char *np, *cp, *name;
379 1.3.2.2 yamt
380 1.3.2.2 yamt pnv = (struct pnviocdesc *)data;
381 1.3.2.2 yamt error = 0;
382 1.3.2.4 yamt cp = name = NULL;
383 1.3.2.2 yamt
384 1.3.2.2 yamt switch (cmd) {
385 1.3.2.2 yamt case PNVIOCGET:
386 1.3.2.2 yamt if (pnv->pnv_name == NULL)
387 1.3.2.2 yamt return EINVAL;
388 1.3.2.2 yamt
389 1.3.2.4 yamt error = nvramgetstr(pnv->pnv_namelen, pnv->pnv_name, &name);
390 1.3.2.2 yamt simple_lock(&nvram_slock);
391 1.3.2.4 yamt np = prep_nvram_get_var(name);
392 1.3.2.2 yamt simple_unlock(&nvram_slock);
393 1.3.2.2 yamt if (np == NULL)
394 1.3.2.2 yamt return EINVAL;
395 1.3.2.2 yamt simple_lock(&nvram_slock);
396 1.3.2.4 yamt len = prep_nvram_get_var_len(name);
397 1.3.2.2 yamt simple_unlock(&nvram_slock);
398 1.3.2.2 yamt
399 1.3.2.2 yamt if (len > pnv->pnv_buflen) {
400 1.3.2.2 yamt error = ENOMEM;
401 1.3.2.2 yamt break;
402 1.3.2.2 yamt }
403 1.3.2.2 yamt if (len <= 0)
404 1.3.2.2 yamt break;
405 1.3.2.2 yamt error = copyout(np, pnv->pnv_buf, len);
406 1.3.2.2 yamt pnv->pnv_buflen = len;
407 1.3.2.2 yamt break;
408 1.3.2.2 yamt
409 1.3.2.2 yamt case PNVIOCGETNEXTNAME:
410 1.3.2.2 yamt /* if the first one is null, we give them the first name */
411 1.3.2.2 yamt simple_lock(&nvram_slock);
412 1.3.2.2 yamt if (pnv->pnv_name == NULL) {
413 1.3.2.4 yamt cp = nvramGEAp;
414 1.3.2.2 yamt } else {
415 1.3.2.4 yamt error = nvramgetstr(pnv->pnv_namelen, pnv->pnv_name,
416 1.3.2.4 yamt &name);
417 1.3.2.4 yamt if (!error) {
418 1.3.2.4 yamt np = prep_nvram_find_var(name);
419 1.3.2.4 yamt cp = prep_nvram_next_var(np);
420 1.3.2.4 yamt }
421 1.3.2.2 yamt }
422 1.3.2.2 yamt simple_unlock(&nvram_slock);
423 1.3.2.4 yamt if (cp == NULL)
424 1.3.2.4 yamt error = EINVAL;
425 1.3.2.4 yamt if (error)
426 1.3.2.4 yamt break;
427 1.3.2.4 yamt
428 1.3.2.2 yamt np = cp;
429 1.3.2.2 yamt while (*np != '=')
430 1.3.2.2 yamt np++;
431 1.3.2.2 yamt len = np-cp;
432 1.3.2.2 yamt if (len > pnv->pnv_buflen) {
433 1.3.2.2 yamt error = ENOMEM;
434 1.3.2.2 yamt break;
435 1.3.2.2 yamt }
436 1.3.2.2 yamt error = copyout(cp, pnv->pnv_buf, len);
437 1.3.2.4 yamt if (error)
438 1.3.2.4 yamt break;
439 1.3.2.2 yamt pnv->pnv_buflen = len;
440 1.3.2.2 yamt break;
441 1.3.2.4 yamt
442 1.3.2.4 yamt case PNVIOCGETNUMGE:
443 1.3.2.4 yamt /* count the GE variables */
444 1.3.2.4 yamt simple_lock(&nvram_slock);
445 1.3.2.4 yamt pnv->pnv_num = prep_nvram_count_vars();
446 1.3.2.4 yamt simple_unlock(&nvram_slock);
447 1.3.2.4 yamt break;
448 1.3.2.4 yamt case PNVIOCSET:
449 1.3.2.4 yamt /* this will require some real work. Not ready yet */
450 1.3.2.4 yamt return ENOTSUP;
451 1.3.2.4 yamt
452 1.3.2.2 yamt default:
453 1.3.2.4 yamt return ENOTTY;
454 1.3.2.4 yamt }
455 1.3.2.4 yamt if (name)
456 1.3.2.4 yamt free(name, M_TEMP);
457 1.3.2.4 yamt return error;
458 1.3.2.4 yamt }
459 1.3.2.4 yamt
460 1.3.2.4 yamt int
461 1.3.2.4 yamt prep_nvramread(dev_t dev, struct uio *uio, int flags)
462 1.3.2.4 yamt {
463 1.3.2.4 yamt int size, resid, error;
464 1.3.2.4 yamt u_int c;
465 1.3.2.4 yamt char *rdata;
466 1.3.2.4 yamt
467 1.3.2.4 yamt error = 0;
468 1.3.2.4 yamt rdata = (char *)&resdata;
469 1.3.2.4 yamt
470 1.3.2.4 yamt if (uio->uio_rw == UIO_WRITE) {
471 1.3.2.4 yamt uio->uio_resid = 0;
472 1.3.2.4 yamt return 0;
473 1.3.2.4 yamt }
474 1.3.2.4 yamt
475 1.3.2.4 yamt switch (minor(dev)) {
476 1.3.2.4 yamt case DEV_NVRAM:
477 1.3.2.4 yamt size = nvram->Header.Size * 1024;
478 1.3.2.4 yamt break;
479 1.3.2.4 yamt case DEV_RESIDUAL:
480 1.3.2.4 yamt size = res->ResidualLength;
481 1.3.2.4 yamt break;
482 1.3.2.4 yamt default:
483 1.3.2.4 yamt return ENXIO;
484 1.3.2.4 yamt }
485 1.3.2.4 yamt resid = size;
486 1.3.2.4 yamt if (uio->uio_resid < resid)
487 1.3.2.4 yamt resid = uio->uio_resid;
488 1.3.2.4 yamt while (resid > 0 && error == 0 && uio->uio_offset < size) {
489 1.3.2.4 yamt switch (minor(dev)) {
490 1.3.2.4 yamt case DEV_NVRAM:
491 1.3.2.4 yamt c = min(resid, PAGE_SIZE);
492 1.3.2.4 yamt error = uiomove(&nvramData[uio->uio_offset], c, uio);
493 1.3.2.4 yamt break;
494 1.3.2.4 yamt case DEV_RESIDUAL:
495 1.3.2.4 yamt c = min(resid, PAGE_SIZE);
496 1.3.2.4 yamt error = uiomove(&rdata[uio->uio_offset], c, uio);
497 1.3.2.4 yamt break;
498 1.3.2.4 yamt default:
499 1.3.2.4 yamt return ENXIO;
500 1.3.2.4 yamt }
501 1.3.2.2 yamt }
502 1.3.2.4 yamt return error;
503 1.3.2.2 yamt }
504 1.3.2.2 yamt
505 1.3.2.2 yamt int
506 1.3.2.2 yamt prep_nvramopen(dev_t dev, int flags, int mode, struct lwp *l)
507 1.3.2.2 yamt {
508 1.3.2.2 yamt struct nvram_pnpbus_softc *sc;
509 1.3.2.2 yamt
510 1.3.2.4 yamt sc = device_lookup(&nvram_cd, NVRAM_STD_DEV);
511 1.3.2.2 yamt if (sc == NULL)
512 1.3.2.2 yamt return ENODEV;
513 1.3.2.2 yamt
514 1.3.2.2 yamt if (sc->sc_open)
515 1.3.2.2 yamt return EBUSY;
516 1.3.2.2 yamt
517 1.3.2.2 yamt sc->sc_open = 1;
518 1.3.2.2 yamt
519 1.3.2.2 yamt return 0;
520 1.3.2.2 yamt }
521 1.3.2.2 yamt
522 1.3.2.2 yamt int
523 1.3.2.2 yamt prep_nvramclose(dev_t dev, int flags, int mode, struct lwp *l)
524 1.3.2.2 yamt {
525 1.3.2.2 yamt struct nvram_pnpbus_softc *sc;
526 1.3.2.2 yamt
527 1.3.2.4 yamt sc = device_lookup(&nvram_cd, NVRAM_STD_DEV);
528 1.3.2.2 yamt if (sc == NULL)
529 1.3.2.2 yamt return ENODEV;
530 1.3.2.2 yamt sc->sc_open = 0;
531 1.3.2.2 yamt return 0;
532 1.3.2.2 yamt }
533 1.3.2.2 yamt
534 1.3.2.2 yamt /* Motorola mk48txx clock routines */
535 1.3.2.2 yamt uint8_t
536 1.3.2.2 yamt mkclock_pnpbus_nvrd(struct mk48txx_softc *osc, int off)
537 1.3.2.2 yamt {
538 1.3.2.2 yamt struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
539 1.3.2.2 yamt uint8_t datum;
540 1.3.2.2 yamt int s;
541 1.3.2.2 yamt
542 1.3.2.2 yamt #ifdef DEBUG
543 1.3.2.5 yamt aprint_debug("mkclock_pnpbus_nvrd(%d)", off);
544 1.3.2.2 yamt #endif
545 1.3.2.2 yamt s = splclock();
546 1.3.2.2 yamt bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
547 1.3.2.2 yamt bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
548 1.3.2.2 yamt datum = bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
549 1.3.2.2 yamt splx(s);
550 1.3.2.2 yamt #ifdef DEBUG
551 1.3.2.5 yamt aprint_debug(" -> %02x\n", datum);
552 1.3.2.2 yamt #endif
553 1.3.2.2 yamt return datum;
554 1.3.2.2 yamt }
555 1.3.2.2 yamt
556 1.3.2.2 yamt void
557 1.3.2.2 yamt mkclock_pnpbus_nvwr(struct mk48txx_softc *osc, int off, uint8_t datum)
558 1.3.2.2 yamt {
559 1.3.2.2 yamt struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
560 1.3.2.2 yamt int s;
561 1.3.2.2 yamt
562 1.3.2.2 yamt #ifdef DEBUG
563 1.3.2.5 yamt aprint_debug("mkclock_isa_nvwr(%d, %02x)\n", off, datum);
564 1.3.2.2 yamt #endif
565 1.3.2.2 yamt s = splclock();
566 1.3.2.2 yamt bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
567 1.3.2.2 yamt bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
568 1.3.2.2 yamt bus_space_write_1(sc->sc_data, sc->sc_datah, 0, datum);
569 1.3.2.2 yamt splx(s);
570 1.3.2.2 yamt }
571