nvram_pnpbus.c revision 1.2 1 /* $NetBSD: nvram_pnpbus.c,v 1.2 2006/04/26 19:48:01 garbled 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.2 2006/04/26 19:48:01 garbled 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 return;
129 }
130
131 simple_lock_init(&nvram_slock);
132
133 /* Initialize the nvram header */
134 p = (uint8_t *) &prep_nvram_header;
135 for (i = 0; i < sizeof(HEADER); i++)
136 *p++ = prep_nvram_read_val(i);
137
138 /*
139 * now that we have the header, we know how big the NVRAM part on
140 * this machine really is. Malloc space to save a copy.
141 */
142
143 nvlen = 1024 * prep_nvram_header.Size;
144 nvramData = malloc(nvlen, M_DEVBUF, M_NOWAIT);
145 p = (uint8_t *) nvramData;
146
147 /*
148 * now read the whole nvram in, one chunk at a time, marking down
149 * the main start points as we go.
150 */
151 for (i = 0; i < sizeof(HEADER) && i < nvlen; i++)
152 *p++ = prep_nvram_read_val(i);
153 nvramGEAp = p;
154 cur = i;
155 for (; i < cur + prep_nvram_header.GELength && i < nvlen; i++)
156 *p++ = prep_nvram_read_val(i);
157 nvramOSAp = p;
158 cur = i;
159 for (; i < cur + prep_nvram_header.OSAreaLength && i < nvlen; i++)
160 *p++ = prep_nvram_read_val(i);
161 nvramCAp = p;
162 cur = i;
163 for (; i < cur + prep_nvram_header.ConfigLength && i < nvlen; i++)
164 *p++ = prep_nvram_read_val(i);
165
166 /* we should be done here. umm.. yay? */
167 nvram = (NVRAM_MAP *)&nvramData[0];
168 printf("%s: Read %d bytes from nvram of size %d\n", device_xname(self),
169 i, nvlen);
170
171 #if defined(NVRAM_DUMP)
172 printf("Boot device: %s\n", prep_nvram_get_var("fw-boot-device"));
173 printf("Dumping nvram\n");
174 for (cur=0; cur < i; cur++) {
175 printf("%c", nvramData[cur]);
176 if (cur % 70 == 0)
177 printf("\n");
178 }
179 #endif
180 strncpy(bootpath, prep_nvram_get_var("fw-boot-device"), 256);
181 }
182
183 /*
184 * This function should be called at a high spl only, as it interfaces with
185 * real hardware.
186 */
187
188 uint8_t
189 prep_nvram_read_val(int addr)
190 {
191 struct nvram_pnpbus_softc *sc;
192
193 if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
194 || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
195 return 0;
196 }
197
198 sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
199
200 /* tell the NVRAM what we want */
201 bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
202 bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
203
204 return bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
205 }
206
207 /*
208 * This function should be called at a high spl only, as it interfaces with
209 * real hardware.
210 */
211
212 void
213 prep_nvram_write_val(int addr, uint8_t val)
214 {
215 struct nvram_pnpbus_softc *sc;
216
217 if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
218 || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
219 return;
220 }
221
222 sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
223
224 /* tell the NVRAM what we want */
225 bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
226 bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
227
228 bus_space_write_1(sc->sc_data, sc->sc_datah, 0, val);
229 }
230
231 /* the rest of these should all be called with the lock held */
232
233 char *
234 prep_nvram_next_var(char *name)
235 {
236 char *cp;
237
238 cp = name;
239 /* skip forward to the first null char */
240 while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp != '\0'))
241 cp++;
242 /* skip nulls */
243 while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp == '\0'))
244 cp++;
245 if ((cp - nvramGEAp) < nvram->Header.GELength)
246 return cp;
247 else
248 return NULL;
249 }
250
251 char *
252 prep_nvram_find_var(const char *name)
253 {
254 char *cp = nvramGEAp;
255 size_t len;
256
257 len = strlen(name);
258 while (cp != NULL) {
259 if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
260 return cp;
261 cp = prep_nvram_next_var(cp);
262 }
263 return NULL;
264 }
265
266 char *
267 prep_nvram_get_var(const char *name)
268 {
269 char *cp = nvramGEAp;
270 size_t len;
271
272 len = strlen(name);
273 while (cp != NULL) {
274 if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
275 return cp+len+1;
276 cp = prep_nvram_next_var(cp);
277 }
278 return NULL;
279 }
280
281 int
282 prep_nvram_get_var_len(const char *name)
283 {
284 char *cp = nvramGEAp;
285 char *ep;
286 size_t len;
287
288 len = strlen(name);
289 while (cp != NULL) {
290 if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
291 goto out;
292 cp = prep_nvram_next_var(cp);
293 }
294 return -1;
295
296 out:
297 ep = cp;
298 while (ep != NULL && *ep != '\0')
299 ep++;
300 return ep-cp;
301 }
302
303 int
304 prep_nvram_count_vars(void)
305 {
306 char *cp = nvramGEAp;
307 int i=0;
308
309 while (cp != NULL) {
310 i++;
311 cp = prep_nvram_next_var(cp);
312 }
313 return i;
314 }
315
316 int
317 prep_nvramioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
318 {
319 int len, error;
320 struct pnviocdesc *pnv;
321 char *np, *cp;
322
323 pnv = (struct pnviocdesc *)data;
324 error = 0;
325
326 switch (cmd) {
327 case PNVIOCGET:
328 if (pnv->pnv_name == NULL)
329 return EINVAL;
330
331 simple_lock(&nvram_slock);
332 np = prep_nvram_get_var(pnv->pnv_name);
333 simple_unlock(&nvram_slock);
334 if (np == NULL)
335 return EINVAL;
336 simple_lock(&nvram_slock);
337 len = prep_nvram_get_var_len(pnv->pnv_name);
338 simple_unlock(&nvram_slock);
339
340 if (len > pnv->pnv_buflen) {
341 error = ENOMEM;
342 break;
343 }
344 if (len <= 0)
345 break;
346 error = copyout(np, pnv->pnv_buf, len);
347 pnv->pnv_buflen = len;
348 break;
349
350 case PNVIOCGETNEXTNAME:
351 /* if the first one is null, we give them the first name */
352 simple_lock(&nvram_slock);
353 if (pnv->pnv_name == NULL) {
354 np = nvramGEAp;
355 cp = prep_nvram_next_var(np);
356 } else {
357 np = prep_nvram_find_var(pnv->pnv_name);
358 cp = prep_nvram_next_var(np);
359 }
360 simple_unlock(&nvram_slock);
361 np = cp;
362 while (*np != '=')
363 np++;
364 len = np-cp;
365 if (len > pnv->pnv_buflen) {
366 error = ENOMEM;
367 break;
368 }
369 error = copyout(cp, pnv->pnv_buf, len);
370 pnv->pnv_buflen = len;
371 break;
372 default:
373 return (ENOTTY);
374 }
375 return (error);
376 }
377
378 int
379 prep_nvramopen(dev_t dev, int flags, int mode, struct lwp *l)
380 {
381 struct nvram_pnpbus_softc *sc;
382
383 sc = device_lookup(&nvram_cd, dev);
384 if (sc == NULL)
385 return ENODEV;
386
387 if (sc->sc_open)
388 return EBUSY;
389
390 sc->sc_open = 1;
391
392 return 0;
393 }
394
395 int
396 prep_nvramclose(dev_t dev, int flags, int mode, struct lwp *l)
397 {
398 struct nvram_pnpbus_softc *sc;
399
400 sc = device_lookup(&nvram_cd, dev);
401 if (sc == NULL)
402 return ENODEV;
403 sc->sc_open = 0;
404 return 0;
405 }
406