nvram_pnpbus.c revision 1.1.4.2 1 /* $NetBSD: nvram_pnpbus.c,v 1.1.4.2 2006/04/11 12:20:51 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tim Rightnour
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: nvram_pnpbus.c,v 1.1.4.2 2006/04/11 12:20:51 yamt Exp $");
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/ioctl.h>
46 #include <sys/conf.h>
47 #include <sys/kthread.h>
48 #include <sys/device.h>
49 #include <sys/malloc.h>
50 #include <sys/lock.h>
51
52 #include <machine/bus.h>
53 #include <machine/intr.h>
54 #include <machine/isa_machdep.h>
55 #include <machine/nvram.h>
56
57 #include <prep/pnpbus/pnpbusvar.h>
58
59 #include "opt_nvram.h"
60
61 static char *nvramData;
62 static NVRAM_MAP *nvram;
63 static char *nvramGEAp; /* pointer to the GE area */
64 static char *nvramCAp; /* pointer to the Config area */
65 static char *nvramOSAp; /* pointer to the OSArea */
66 struct simplelock nvram_slock; /* lock */
67
68 extern char bootpath[256];
69
70 #define NVRAM_STD_DEV 0
71
72 static int nvram_pnpbus_probe(struct device *, struct cfdata *, void *);
73 static void nvram_pnpbus_attach(struct device *, struct device *, void *);
74 uint8_t prep_nvram_read_val(int);
75 char *prep_nvram_next_var(char *);
76 char *prep_nvram_find_var(const char *);
77 char *prep_nvram_get_var(const char *);
78 int prep_nvram_get_var_len(const char *);
79 int prep_nvram_count_vars(void);
80 void prep_nvram_write_val(int, uint8_t);
81
82 CFATTACH_DECL(nvram_pnpbus, sizeof(struct nvram_pnpbus_softc),
83 nvram_pnpbus_probe, nvram_pnpbus_attach, NULL, NULL);
84
85 dev_type_open(prep_nvramopen);
86 dev_type_ioctl(prep_nvramioctl);
87 dev_type_close(prep_nvramclose);
88
89 const struct cdevsw prep_nvram_cdevsw = {
90 prep_nvramopen, prep_nvramclose, noread, nowrite, prep_nvramioctl,
91 nostop, notty, nopoll, nommap, nokqfilter,
92 };
93
94 extern struct cfdriver nvram_cd;
95
96 static int
97 nvram_pnpbus_probe(struct device *parent, struct cfdata *match, void *aux)
98 {
99 struct pnpbus_dev_attach_args *pna = aux;
100 int ret = 0;
101
102 if (strcmp(pna->pna_devid, "IBM0008") == 0)
103 ret = 1;
104
105 if (ret)
106 pnpbus_scan(pna, pna->pna_ppc_dev);
107
108 return ret;
109 }
110
111 static void
112 nvram_pnpbus_attach(struct device *parent, struct device *self, void *aux)
113 {
114 struct nvram_pnpbus_softc *sc = (void *)self;
115 struct pnpbus_dev_attach_args *pna = aux;
116 int as_iobase, as_len, data_iobase, data_len, i, nvlen, cur;
117 uint8_t *p;
118 HEADER prep_nvram_header;
119
120 sc->sc_iot = pna->pna_iot;
121
122 pnpbus_getioport(&pna->pna_res, 0, &as_iobase, &as_len);
123 pnpbus_getioport(&pna->pna_res, 1, &data_iobase, &data_len);
124
125 if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_as, &sc->sc_ash) ||
126 pnpbus_io_map(&pna->pna_res, 1, &sc->sc_data, &sc->sc_datah)) {
127 printf("nvram: couldn't map registers\n");
128 }
129
130 simple_lock_init(&nvram_slock);
131
132 /* Initialize the nvram header */
133 p = (uint8_t *) &prep_nvram_header;
134 for (i = 0; i < sizeof(HEADER); i++)
135 *p++ = prep_nvram_read_val(i);
136
137 /*
138 * now that we have the header, we know how big the NVRAM part on
139 * this machine really is. Malloc space to save a copy.
140 */
141
142 nvlen = 1024 * prep_nvram_header.Size;
143 nvramData = malloc(nvlen, M_DEVBUF, M_NOWAIT);
144 p = (uint8_t *) nvramData;
145
146 /*
147 * now read the whole nvram in, one chunk at a time, marking down
148 * the main start points as we go.
149 */
150 for (i = 0; i < sizeof(HEADER) && i < nvlen; i++)
151 *p++ = prep_nvram_read_val(i);
152 nvramGEAp = p;
153 cur = i;
154 for (; i < cur + prep_nvram_header.GELength && i < nvlen; i++)
155 *p++ = prep_nvram_read_val(i);
156 nvramOSAp = p;
157 cur = i;
158 for (; i < cur + prep_nvram_header.OSAreaLength && i < nvlen; i++)
159 *p++ = prep_nvram_read_val(i);
160 nvramCAp = p;
161 cur = i;
162 for (; i < cur + prep_nvram_header.ConfigLength && i < nvlen; i++)
163 *p++ = prep_nvram_read_val(i);
164
165 /* we should be done here. umm.. yay? */
166 nvram = (NVRAM_MAP *)&nvramData[0];
167 printf("%s: Read %d bytes from nvram of size %d\n", device_xname(self),
168 i, nvlen);
169
170 #if defined(NVRAM_DUMP)
171 printf("Boot device: %s\n", prep_nvram_get_var("fw-boot-device"));
172 printf("Dumping nvram\n");
173 for (cur=0; cur < i; cur++) {
174 printf("%c", nvramData[cur]);
175 if (cur % 70 == 0)
176 printf("\n");
177 }
178 #endif
179 strncpy(bootpath, prep_nvram_get_var("fw-boot-device"), 256);
180 }
181
182 /*
183 * This function should be called at a high spl only, as it interfaces with
184 * real hardware.
185 */
186
187 uint8_t
188 prep_nvram_read_val(int addr)
189 {
190 struct nvram_pnpbus_softc *sc;
191
192 if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
193 || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
194 return 0;
195 }
196
197 sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
198
199 /* tell the NVRAM what we want */
200 bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
201 bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
202
203 return bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
204 }
205
206 /*
207 * This function should be called at a high spl only, as it interfaces with
208 * real hardware.
209 */
210
211 void
212 prep_nvram_write_val(int addr, uint8_t val)
213 {
214 struct nvram_pnpbus_softc *sc;
215
216 if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
217 || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
218 return;
219 }
220
221 sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
222
223 /* tell the NVRAM what we want */
224 bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
225 bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
226
227 bus_space_write_1(sc->sc_data, sc->sc_datah, 0, val);
228 }
229
230 /* the rest of these should all be called with the lock held */
231
232 char *
233 prep_nvram_next_var(char *name)
234 {
235 char *cp;
236
237 cp = name;
238 /* skip forward to the first null char */
239 while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp != '\0'))
240 cp++;
241 /* skip nulls */
242 while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp == '\0'))
243 cp++;
244 if ((cp - nvramGEAp) < nvram->Header.GELength)
245 return cp;
246 else
247 return NULL;
248 }
249
250 char *
251 prep_nvram_find_var(const char *name)
252 {
253 char *cp = nvramGEAp;
254 size_t len;
255
256 len = strlen(name);
257 while (cp != NULL) {
258 if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
259 return cp;
260 cp = prep_nvram_next_var(cp);
261 }
262 return NULL;
263 }
264
265 char *
266 prep_nvram_get_var(const char *name)
267 {
268 char *cp = nvramGEAp;
269 size_t len;
270
271 len = strlen(name);
272 while (cp != NULL) {
273 if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
274 return cp+len+1;
275 cp = prep_nvram_next_var(cp);
276 }
277 return NULL;
278 }
279
280 int
281 prep_nvram_get_var_len(const char *name)
282 {
283 char *cp = nvramGEAp;
284 char *ep;
285 size_t len;
286
287 len = strlen(name);
288 while (cp != NULL) {
289 if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
290 goto out;
291 cp = prep_nvram_next_var(cp);
292 }
293 return -1;
294
295 out:
296 ep = cp;
297 while (ep != NULL && *ep != '\0')
298 ep++;
299 return ep-cp;
300 }
301
302 int
303 prep_nvram_count_vars(void)
304 {
305 char *cp = nvramGEAp;
306 int i=0;
307
308 while (cp != NULL) {
309 i++;
310 cp = prep_nvram_next_var(cp);
311 }
312 return i;
313 }
314
315 int
316 prep_nvramioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
317 {
318 int len, error;
319 struct pnviocdesc *pnv;
320 char *np, *cp;
321
322 pnv = (struct pnviocdesc *)data;
323 error = 0;
324
325 switch (cmd) {
326 case PNVIOCGET:
327 if (pnv->pnv_name == NULL)
328 return EINVAL;
329
330 simple_lock(&nvram_slock);
331 np = prep_nvram_get_var(pnv->pnv_name);
332 simple_unlock(&nvram_slock);
333 if (np == NULL)
334 return EINVAL;
335 simple_lock(&nvram_slock);
336 len = prep_nvram_get_var_len(pnv->pnv_name);
337 simple_unlock(&nvram_slock);
338
339 if (len > pnv->pnv_buflen) {
340 error = ENOMEM;
341 break;
342 }
343 if (len <= 0)
344 break;
345 error = copyout(np, pnv->pnv_buf, len);
346 pnv->pnv_buflen = len;
347 break;
348
349 case PNVIOCGETNEXTNAME:
350 /* if the first one is null, we give them the first name */
351 simple_lock(&nvram_slock);
352 if (pnv->pnv_name == NULL) {
353 np = nvramGEAp;
354 cp = prep_nvram_next_var(np);
355 } else {
356 np = prep_nvram_find_var(pnv->pnv_name);
357 cp = prep_nvram_next_var(np);
358 }
359 simple_unlock(&nvram_slock);
360 np = cp;
361 while (*np != '=')
362 np++;
363 len = np-cp;
364 if (len > pnv->pnv_buflen) {
365 error = ENOMEM;
366 break;
367 }
368 error = copyout(cp, pnv->pnv_buf, len);
369 pnv->pnv_buflen = len;
370 break;
371 default:
372 return (ENOTTY);
373 }
374 return (error);
375 }
376
377 int
378 prep_nvramopen(dev_t dev, int flags, int mode, struct lwp *l)
379 {
380 struct nvram_pnpbus_softc *sc;
381
382 sc = device_lookup(&nvram_cd, dev);
383 if (sc == NULL)
384 return ENODEV;
385
386 if (sc->sc_open)
387 return EBUSY;
388
389 sc->sc_open = 1;
390
391 return 0;
392 }
393
394 int
395 prep_nvramclose(dev_t dev, int flags, int mode, struct lwp *l)
396 {
397 struct nvram_pnpbus_softc *sc;
398
399 sc = device_lookup(&nvram_cd, dev);
400 if (sc == NULL)
401 return ENODEV;
402 sc->sc_open = 0;
403 return 0;
404 }
405