xref: /netbsd-src/sys/arch/prep/pnpbus/nvram_pnpbus.c (revision 7c3f385475147b6e1c4753f2bee961630e2dfc40)
1 /* $NetBSD: nvram_pnpbus.c,v 1.13 2008/03/29 17:55:35 matt 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.13 2008/03/29 17:55:35 matt 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/simplelock.h>
51 #include <sys/bus.h>
52 #include <sys/intr.h>
53 
54 #include <machine/isa_machdep.h>
55 /* clock stuff for motorolla machines */
56 #include <dev/clock_subr.h>
57 #include <dev/ic/mk48txxreg.h>
58 
59 #include <uvm/uvm_extern.h>
60 
61 #include <machine/residual.h>
62 #include <machine/nvram.h>
63 
64 #include <prep/pnpbus/pnpbusvar.h>
65 
66 #include "opt_nvram.h"
67 
68 static char *nvramData;
69 static NVRAM_MAP *nvram;
70 static char *nvramGEAp;		/* pointer to the GE area */
71 static char *nvramCAp;		/* pointer to the Config area */
72 static char *nvramOSAp;		/* pointer to the OSArea */
73 struct simplelock nvram_slock;	/* lock */
74 
75 int prep_clock_mk48txx;
76 
77 extern char bootpath[256];
78 extern RESIDUAL resdata;
79 
80 #define NVRAM_STD_DEV 0
81 
82 static int	nvram_pnpbus_probe(struct device *, struct cfdata *, void *);
83 static void	nvram_pnpbus_attach(struct device *, struct device *, void *);
84 uint8_t		prep_nvram_read_val(int);
85 char		*prep_nvram_next_var(char *);
86 char		*prep_nvram_find_var(const char *);
87 char		*prep_nvram_get_var(const char *);
88 int		prep_nvram_get_var_len(const char *);
89 int		prep_nvram_count_vars(void);
90 void		prep_nvram_write_val(int, uint8_t);
91 uint8_t		mkclock_pnpbus_nvrd(struct mk48txx_softc *, int);
92 void		mkclock_pnpbus_nvwr(struct mk48txx_softc *, int, uint8_t);
93 
94 CFATTACH_DECL(nvram_pnpbus, sizeof(struct nvram_pnpbus_softc),
95     nvram_pnpbus_probe, nvram_pnpbus_attach, NULL, NULL);
96 
97 dev_type_open(prep_nvramopen);
98 dev_type_ioctl(prep_nvramioctl);
99 dev_type_close(prep_nvramclose);
100 dev_type_read(prep_nvramread);
101 
102 const struct cdevsw nvram_cdevsw = {
103 	prep_nvramopen, prep_nvramclose, prep_nvramread, nowrite,
104 	prep_nvramioctl, nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
105 };
106 
107 extern struct cfdriver nvram_cd;
108 
109 static int
110 nvram_pnpbus_probe(struct device *parent, struct cfdata *match, void *aux)
111 {
112 	struct pnpbus_dev_attach_args *pna = aux;
113 	int ret = 0;
114 
115 	if (strcmp(pna->pna_devid, "IBM0008") == 0)
116 		ret = 1;
117 
118 	if (ret)
119 		pnpbus_scan(pna, pna->pna_ppc_dev);
120 
121 	return ret;
122 }
123 
124 static void
125 nvram_pnpbus_attach(struct device *parent, struct device *self, void *aux)
126 {
127 	struct nvram_pnpbus_softc *sc = (void *)self;
128 	struct pnpbus_dev_attach_args *pna = aux;
129 	int as_iobase, as_len, data_iobase, data_len, i, nvlen, cur;
130 	uint8_t *p;
131 	HEADER prep_nvram_header;
132 
133 	sc->sc_iot = pna->pna_iot;
134 
135 	pnpbus_getioport(&pna->pna_res, 0, &as_iobase, &as_len);
136 	pnpbus_getioport(&pna->pna_res, 1, &data_iobase, &data_len);
137 
138 	if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_as, &sc->sc_ash) ||
139 	    pnpbus_io_map(&pna->pna_res, 1, &sc->sc_data, &sc->sc_datah)) {
140 		aprint_error("nvram: couldn't map registers\n");
141 		return;
142 	}
143 
144 	simple_lock_init(&nvram_slock);
145 
146 	/* Initialize the nvram header */
147 	p = (uint8_t *) &prep_nvram_header;
148 	for (i = 0; i < sizeof(HEADER); i++)
149 		*p++ = prep_nvram_read_val(i);
150 
151 	/*
152 	 * now that we have the header, we know how big the NVRAM part on
153 	 * this machine really is.  Malloc space to save a copy.
154 	 */
155 
156 	nvlen = 1024 * prep_nvram_header.Size;
157 	nvramData = malloc(nvlen, M_DEVBUF, M_NOWAIT);
158 	p = (uint8_t *) nvramData;
159 
160 	/*
161 	 * now read the whole nvram in, one chunk at a time, marking down
162 	 * the main start points as we go.
163 	 */
164 	for (i = 0; i < sizeof(HEADER) && i < nvlen; i++)
165 		*p++ = prep_nvram_read_val(i);
166 	nvramGEAp = p;
167 	cur = i;
168 	for (; i < cur + prep_nvram_header.GELength && i < nvlen; i++)
169 		*p++ = prep_nvram_read_val(i);
170 	nvramOSAp = p;
171 	cur = i;
172 	for (; i < cur + prep_nvram_header.OSAreaLength && i < nvlen; i++)
173 		*p++ = prep_nvram_read_val(i);
174 	nvramCAp = p;
175 	cur = i;
176 	for (; i < cur + prep_nvram_header.ConfigLength && i < nvlen; i++)
177 		*p++ = prep_nvram_read_val(i);
178 
179 	/* we should be done here.  umm.. yay? */
180 	nvram = (NVRAM_MAP *)&nvramData[0];
181 	aprint_normal("\n");
182 	aprint_verbose("%s: Read %d bytes from nvram of size %d\n",
183 	    device_xname(self), i, nvlen);
184 
185 #if defined(NVRAM_DUMP)
186 	printf("Boot device: %s\n", prep_nvram_get_var("fw-boot-device"));
187 	printf("Dumping nvram\n");
188 	for (cur=0; cur < i; cur++) {
189 		printf("%c", nvramData[cur]);
190 		if (cur % 70 == 0)
191 			printf("\n");
192 	}
193 #endif
194 	strncpy(bootpath, prep_nvram_get_var("fw-boot-device"), 256);
195 
196 
197 	if (prep_clock_mk48txx == 0)
198 		return;
199 	/* otherwise, we have a motorolla clock chip.  Set it up. */
200 	sc->sc_mksc.sc_model = "mk48t18";
201 	sc->sc_mksc.sc_year0 = 1900;
202 	sc->sc_mksc.sc_nvrd = mkclock_pnpbus_nvrd;
203 	sc->sc_mksc.sc_nvwr = mkclock_pnpbus_nvwr;
204 	/* copy down the bus space tags */
205 	sc->sc_mksc.sc_bst = sc->sc_as;
206 	sc->sc_mksc.sc_bsh = sc->sc_ash;
207 	sc->sc_mksc.sc_data = sc->sc_data;
208 	sc->sc_mksc.sc_datah = sc->sc_datah;
209 
210 	aprint_normal("%s: attaching clock", device_xname(self));
211 	mk48txx_attach((struct mk48txx_softc *)&sc->sc_mksc);
212 	aprint_normal("\n");
213 }
214 
215 /*
216  * This function should be called at a high spl only, as it interfaces with
217  * real hardware.
218  */
219 
220 uint8_t
221 prep_nvram_read_val(int addr)
222 {
223 	struct nvram_pnpbus_softc *sc;
224 
225         sc = device_lookup_private(&nvram_cd, NVRAM_STD_DEV);
226 	if (sc == NULL)
227 		return 0;
228 
229 	/* tell the NVRAM what we want */
230 	bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
231 	bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
232 
233 	return bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
234 }
235 
236 /*
237  * This function should be called at a high spl only, as it interfaces with
238  * real hardware.
239  */
240 
241 void
242 prep_nvram_write_val(int addr, uint8_t val)
243 {
244 	struct nvram_pnpbus_softc *sc;
245 
246         sc = device_lookup_private(&nvram_cd, NVRAM_STD_DEV);
247 	if (sc == NULL)
248 		return;
249 
250 	/* tell the NVRAM what we want */
251 	bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
252 	bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
253 
254 	bus_space_write_1(sc->sc_data, sc->sc_datah, 0, val);
255 }
256 
257 /* the rest of these should all be called with the lock held */
258 
259 char *
260 prep_nvram_next_var(char *name)
261 {
262 	char *cp;
263 
264 	if (name == NULL)
265 		return NULL;
266 
267 	cp = name;
268 	/* skip forward to the first null char */
269 	while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp != '\0'))
270 		cp++;
271 	/* skip nulls */
272 	while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp == '\0'))
273 		cp++;
274 	if ((cp - nvramGEAp) < nvram->Header.GELength)
275 		return cp;
276 	else
277 		return NULL;
278 }
279 
280 char *
281 prep_nvram_find_var(const char *name)
282 {
283 	char *cp = nvramGEAp;
284 	size_t len;
285 
286 	len = strlen(name);
287 	while (cp != NULL) {
288 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
289 			return cp;
290 		cp = prep_nvram_next_var(cp);
291 	}
292 	return NULL;
293 }
294 
295 char *
296 prep_nvram_get_var(const char *name)
297 {
298 	char *cp = nvramGEAp;
299 	size_t len;
300 
301 	if (name == NULL)
302 		return NULL;
303 	len = strlen(name);
304 	while (cp != NULL) {
305 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
306 			return cp+len+1;
307 		cp = prep_nvram_next_var(cp);
308 	}
309 	return NULL;
310 }
311 
312 int
313 prep_nvram_get_var_len(const char *name)
314 {
315 	char *cp = nvramGEAp;
316 	char *ep;
317 	size_t len;
318 
319 	if (name == NULL)
320 		return -1;
321 
322 	len = strlen(name);
323 	while (cp != NULL) {
324 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
325 			goto out;
326 		cp = prep_nvram_next_var(cp);
327 	}
328 	return -1;
329 
330 out:
331 	ep = cp;
332 	while (ep != NULL && *ep != '\0')
333 		ep++;
334 	return ep-cp;
335 }
336 
337 int
338 prep_nvram_count_vars(void)
339 {
340 	char *cp = nvramGEAp;
341 	int i=0;
342 
343 	while (cp != NULL) {
344 		i++;
345 		cp = prep_nvram_next_var(cp);
346 	}
347 	return i;
348 }
349 
350 static int
351 nvramgetstr(int len, char *user, char **cpp)
352 {
353 	int error;
354 	char *cp;
355 
356 	/* Reject obvious bogus requests */
357 	if ((u_int)len > (8 * 1024) - 1)
358 		return ENAMETOOLONG;
359 
360 	*cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
361 	error = copyin(user, cp, len);
362 	cp[len] = '\0';
363 	return error;
364 }
365 
366 int
367 prep_nvramioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
368 {
369 	int len, error;
370 	struct pnviocdesc *pnv;
371 	char *np, *cp, *name;
372 
373 	pnv = (struct pnviocdesc *)data;
374 	error = 0;
375 	cp = name = NULL;
376 
377 	switch (cmd) {
378 	case PNVIOCGET:
379 		if (pnv->pnv_name == NULL)
380 			return EINVAL;
381 
382 		error = nvramgetstr(pnv->pnv_namelen, pnv->pnv_name, &name);
383 		simple_lock(&nvram_slock);
384 		np = prep_nvram_get_var(name);
385 		simple_unlock(&nvram_slock);
386 		if (np == NULL)
387 			return EINVAL;
388 		simple_lock(&nvram_slock);
389 		len = prep_nvram_get_var_len(name);
390 		simple_unlock(&nvram_slock);
391 
392 		if (len > pnv->pnv_buflen) {
393 			error = ENOMEM;
394 			break;
395 		}
396 		if (len <= 0)
397 			break;
398 		error = copyout(np, pnv->pnv_buf, len);
399 		pnv->pnv_buflen = len;
400 		break;
401 
402 	case PNVIOCGETNEXTNAME:
403 		/* if the first one is null, we give them the first name */
404 		simple_lock(&nvram_slock);
405 		if (pnv->pnv_name == NULL) {
406 			cp = nvramGEAp;
407 		} else {
408 			error = nvramgetstr(pnv->pnv_namelen, pnv->pnv_name,
409 			    &name);
410 			if (!error) {
411 				np = prep_nvram_find_var(name);
412 				cp = prep_nvram_next_var(np);
413 			}
414 		}
415 		simple_unlock(&nvram_slock);
416 		if (cp == NULL)
417 			error = EINVAL;
418 		if (error)
419 			break;
420 
421 		np = cp;
422 		while (*np != '=')
423 			np++;
424 		len = np-cp;
425 		if (len > pnv->pnv_buflen) {
426 			error = ENOMEM;
427 			break;
428 		}
429 		error = copyout(cp, pnv->pnv_buf, len);
430 		if (error)
431 			break;
432 		pnv->pnv_buflen = len;
433 		break;
434 
435 	case PNVIOCGETNUMGE:
436 		/* count the GE variables */
437 		simple_lock(&nvram_slock);
438 		pnv->pnv_num = prep_nvram_count_vars();
439 		simple_unlock(&nvram_slock);
440 		break;
441 	case PNVIOCSET:
442 		/* this will require some real work.  Not ready yet */
443 		return ENOTSUP;
444 
445 	default:
446 		return ENOTTY;
447 	}
448 	if (name)
449 		free(name, M_TEMP);
450 	return error;
451 }
452 
453 int
454 prep_nvramread(dev_t dev, struct uio *uio, int flags)
455 {
456 	int size, resid, error;
457 	u_int c;
458 	char *rdata;
459 
460 	error = 0;
461 	rdata = (char *)&resdata;
462 
463 	if (uio->uio_rw == UIO_WRITE) {
464 		uio->uio_resid = 0;
465 		return 0;
466 	}
467 
468 	switch (minor(dev)) {
469 	case DEV_NVRAM:
470 		size = nvram->Header.Size * 1024;
471 		break;
472 	case DEV_RESIDUAL:
473 		size = res->ResidualLength;
474 		break;
475 	default:
476 		return ENXIO;
477 	}
478 	resid = size;
479 	if (uio->uio_resid < resid)
480 		resid = uio->uio_resid;
481 	while (resid > 0 && error == 0 && uio->uio_offset < size) {
482 		switch (minor(dev)) {
483 		case DEV_NVRAM:
484 			c = min(resid, PAGE_SIZE);
485 			error = uiomove(&nvramData[uio->uio_offset], c, uio);
486 			break;
487 		case DEV_RESIDUAL:
488 			c = min(resid, PAGE_SIZE);
489 			error = uiomove(&rdata[uio->uio_offset], c, uio);
490 			break;
491 		default:
492 			return ENXIO;
493 		}
494 	}
495 	return error;
496 }
497 
498 int
499 prep_nvramopen(dev_t dev, int flags, int mode, struct lwp *l)
500 {
501 	struct nvram_pnpbus_softc *sc;
502 
503 	sc = device_lookup_private(&nvram_cd, NVRAM_STD_DEV);
504 	if (sc == NULL)
505 		return ENODEV;
506 
507 	if (sc->sc_open)
508 		return EBUSY;
509 
510 	sc->sc_open = 1;
511 
512 	return 0;
513 }
514 
515 int
516 prep_nvramclose(dev_t dev, int flags, int mode, struct lwp *l)
517 {
518 	struct nvram_pnpbus_softc *sc;
519 
520 	sc = device_lookup_private(&nvram_cd, NVRAM_STD_DEV);
521 	if (sc == NULL)
522 		return ENODEV;
523 	sc->sc_open = 0;
524 	return 0;
525 }
526 
527 /* Motorola mk48txx clock routines */
528 uint8_t
529 mkclock_pnpbus_nvrd(struct mk48txx_softc *osc, int off)
530 {
531 	struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
532 	uint8_t datum;
533 	int s;
534 
535 #ifdef DEBUG
536 	aprint_debug("mkclock_pnpbus_nvrd(%d)", off);
537 #endif
538 	s = splclock();
539 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
540 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
541 	datum = bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
542 	splx(s);
543 #ifdef DEBUG
544 	aprint_debug(" -> %02x\n", datum);
545 #endif
546 	return datum;
547 }
548 
549 void
550 mkclock_pnpbus_nvwr(struct mk48txx_softc *osc, int off, uint8_t datum)
551 {
552 	struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
553 	int s;
554 
555 #ifdef DEBUG
556 	aprint_debug("mkclock_isa_nvwr(%d, %02x)\n", off, datum);
557 #endif
558 	s = splclock();
559 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
560 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
561 	bus_space_write_1(sc->sc_data, sc->sc_datah, 0, datum);
562 	splx(s);
563 }
564