Home | History | Annotate | Line # | Download | only in pnpbus
nvram_pnpbus.c revision 1.9
      1 /* $NetBSD: nvram_pnpbus.c,v 1.9 2007/10/17 19:56:52 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.9 2007/10/17 19:56:52 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 /* 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 	todr_attach(&sc->sc_mksc.sc_handle);
    214 }
    215 
    216 /*
    217  * This function should be called at a high spl only, as it interfaces with
    218  * real hardware.
    219  */
    220 
    221 uint8_t
    222 prep_nvram_read_val(int addr)
    223 {
    224 	struct nvram_pnpbus_softc *sc;
    225 
    226 	if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
    227             || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
    228                 return 0;
    229         }
    230 
    231         sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
    232 
    233 	/* tell the NVRAM what we want */
    234 	bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
    235 	bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
    236 
    237 	return bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
    238 }
    239 
    240 /*
    241  * This function should be called at a high spl only, as it interfaces with
    242  * real hardware.
    243  */
    244 
    245 void
    246 prep_nvram_write_val(int addr, uint8_t val)
    247 {
    248 	struct nvram_pnpbus_softc *sc;
    249 
    250 	if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
    251             || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
    252                 return;
    253         }
    254 
    255         sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
    256 
    257 	/* tell the NVRAM what we want */
    258 	bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
    259 	bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
    260 
    261 	bus_space_write_1(sc->sc_data, sc->sc_datah, 0, val);
    262 }
    263 
    264 /* the rest of these should all be called with the lock held */
    265 
    266 char *
    267 prep_nvram_next_var(char *name)
    268 {
    269 	char *cp;
    270 
    271 	if (name == NULL)
    272 		return NULL;
    273 
    274 	cp = name;
    275 	/* skip forward to the first null char */
    276 	while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp != '\0'))
    277 		cp++;
    278 	/* skip nulls */
    279 	while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp == '\0'))
    280 		cp++;
    281 	if ((cp - nvramGEAp) < nvram->Header.GELength)
    282 		return cp;
    283 	else
    284 		return NULL;
    285 }
    286 
    287 char *
    288 prep_nvram_find_var(const char *name)
    289 {
    290 	char *cp = nvramGEAp;
    291 	size_t len;
    292 
    293 	len = strlen(name);
    294 	while (cp != NULL) {
    295 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
    296 			return cp;
    297 		cp = prep_nvram_next_var(cp);
    298 	}
    299 	return NULL;
    300 }
    301 
    302 char *
    303 prep_nvram_get_var(const char *name)
    304 {
    305 	char *cp = nvramGEAp;
    306 	size_t len;
    307 
    308 	if (name == NULL)
    309 		return NULL;
    310 	len = strlen(name);
    311 	while (cp != NULL) {
    312 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
    313 			return cp+len+1;
    314 		cp = prep_nvram_next_var(cp);
    315 	}
    316 	return NULL;
    317 }
    318 
    319 int
    320 prep_nvram_get_var_len(const char *name)
    321 {
    322 	char *cp = nvramGEAp;
    323 	char *ep;
    324 	size_t len;
    325 
    326 	if (name == NULL)
    327 		return -1;
    328 
    329 	len = strlen(name);
    330 	while (cp != NULL) {
    331 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
    332 			goto out;
    333 		cp = prep_nvram_next_var(cp);
    334 	}
    335 	return -1;
    336 
    337 out:
    338 	ep = cp;
    339 	while (ep != NULL && *ep != '\0')
    340 		ep++;
    341 	return ep-cp;
    342 }
    343 
    344 int
    345 prep_nvram_count_vars(void)
    346 {
    347 	char *cp = nvramGEAp;
    348 	int i=0;
    349 
    350 	while (cp != NULL) {
    351 		i++;
    352 		cp = prep_nvram_next_var(cp);
    353 	}
    354 	return i;
    355 }
    356 
    357 static int
    358 nvramgetstr(int len, char *user, char **cpp)
    359 {
    360 	int error;
    361 	char *cp;
    362 
    363 	/* Reject obvious bogus requests */
    364 	if ((u_int)len > (8 * 1024) - 1)
    365 		return ENAMETOOLONG;
    366 
    367 	*cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
    368 	error = copyin(user, cp, len);
    369 	cp[len] = '\0';
    370 	return error;
    371 }
    372 
    373 int
    374 prep_nvramioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    375 {
    376 	int len, error;
    377 	struct pnviocdesc *pnv;
    378 	char *np, *cp, *name;
    379 
    380 	pnv = (struct pnviocdesc *)data;
    381 	error = 0;
    382 	cp = name = NULL;
    383 
    384 	switch (cmd) {
    385 	case PNVIOCGET:
    386 		if (pnv->pnv_name == NULL)
    387 			return EINVAL;
    388 
    389 		error = nvramgetstr(pnv->pnv_namelen, pnv->pnv_name, &name);
    390 		simple_lock(&nvram_slock);
    391 		np = prep_nvram_get_var(name);
    392 		simple_unlock(&nvram_slock);
    393 		if (np == NULL)
    394 			return EINVAL;
    395 		simple_lock(&nvram_slock);
    396 		len = prep_nvram_get_var_len(name);
    397 		simple_unlock(&nvram_slock);
    398 
    399 		if (len > pnv->pnv_buflen) {
    400 			error = ENOMEM;
    401 			break;
    402 		}
    403 		if (len <= 0)
    404 			break;
    405 		error = copyout(np, pnv->pnv_buf, len);
    406 		pnv->pnv_buflen = len;
    407 		break;
    408 
    409 	case PNVIOCGETNEXTNAME:
    410 		/* if the first one is null, we give them the first name */
    411 		simple_lock(&nvram_slock);
    412 		if (pnv->pnv_name == NULL) {
    413 			cp = nvramGEAp;
    414 		} else {
    415 			error = nvramgetstr(pnv->pnv_namelen, pnv->pnv_name,
    416 			    &name);
    417 			if (!error) {
    418 				np = prep_nvram_find_var(name);
    419 				cp = prep_nvram_next_var(np);
    420 			}
    421 		}
    422 		simple_unlock(&nvram_slock);
    423 		if (cp == NULL)
    424 			error = EINVAL;
    425 		if (error)
    426 			break;
    427 
    428 		np = cp;
    429 		while (*np != '=')
    430 			np++;
    431 		len = np-cp;
    432 		if (len > pnv->pnv_buflen) {
    433 			error = ENOMEM;
    434 			break;
    435 		}
    436 		error = copyout(cp, pnv->pnv_buf, len);
    437 		if (error)
    438 			break;
    439 		pnv->pnv_buflen = len;
    440 		break;
    441 
    442 	case PNVIOCGETNUMGE:
    443 		/* count the GE variables */
    444 		simple_lock(&nvram_slock);
    445 		pnv->pnv_num = prep_nvram_count_vars();
    446 		simple_unlock(&nvram_slock);
    447 		break;
    448 	case PNVIOCSET:
    449 		/* this will require some real work.  Not ready yet */
    450 		return ENOTSUP;
    451 
    452 	default:
    453 		return ENOTTY;
    454 	}
    455 	if (name)
    456 		free(name, M_TEMP);
    457 	return error;
    458 }
    459 
    460 int
    461 prep_nvramread(dev_t dev, struct uio *uio, int flags)
    462 {
    463 	int size, resid, error;
    464 	u_int c;
    465 	char *rdata;
    466 
    467 	error = 0;
    468 	rdata = (char *)&resdata;
    469 
    470 	if (uio->uio_rw == UIO_WRITE) {
    471 		uio->uio_resid = 0;
    472 		return 0;
    473 	}
    474 
    475 	switch (minor(dev)) {
    476 	case DEV_NVRAM:
    477 		size = nvram->Header.Size * 1024;
    478 		break;
    479 	case DEV_RESIDUAL:
    480 		size = res->ResidualLength;
    481 		break;
    482 	default:
    483 		return ENXIO;
    484 	}
    485 	resid = size;
    486 	if (uio->uio_resid < resid)
    487 		resid = uio->uio_resid;
    488 	while (resid > 0 && error == 0 && uio->uio_offset < size) {
    489 		switch (minor(dev)) {
    490 		case DEV_NVRAM:
    491 			c = min(resid, PAGE_SIZE);
    492 			error = uiomove(&nvramData[uio->uio_offset], c, uio);
    493 			break;
    494 		case DEV_RESIDUAL:
    495 			c = min(resid, PAGE_SIZE);
    496 			error = uiomove(&rdata[uio->uio_offset], c, uio);
    497 			break;
    498 		default:
    499 			return ENXIO;
    500 		}
    501 	}
    502 	return error;
    503 }
    504 
    505 int
    506 prep_nvramopen(dev_t dev, int flags, int mode, struct lwp *l)
    507 {
    508 	struct nvram_pnpbus_softc *sc;
    509 
    510 	sc = device_lookup(&nvram_cd, NVRAM_STD_DEV);
    511 	if (sc == NULL)
    512 		return ENODEV;
    513 
    514 	if (sc->sc_open)
    515 		return EBUSY;
    516 
    517 	sc->sc_open = 1;
    518 
    519 	return 0;
    520 }
    521 
    522 int
    523 prep_nvramclose(dev_t dev, int flags, int mode, struct lwp *l)
    524 {
    525 	struct nvram_pnpbus_softc *sc;
    526 
    527 	sc = device_lookup(&nvram_cd, NVRAM_STD_DEV);
    528 	if (sc == NULL)
    529 		return ENODEV;
    530 	sc->sc_open = 0;
    531 	return 0;
    532 }
    533 
    534 /* Motorola mk48txx clock routines */
    535 uint8_t
    536 mkclock_pnpbus_nvrd(struct mk48txx_softc *osc, int off)
    537 {
    538 	struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
    539 	uint8_t datum;
    540 	int s;
    541 
    542 #ifdef DEBUG
    543 	aprint_debug("mkclock_pnpbus_nvrd(%d)", off);
    544 #endif
    545 	s = splclock();
    546 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
    547 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
    548 	datum = bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
    549 	splx(s);
    550 #ifdef DEBUG
    551 	aprint_debug(" -> %02x\n", datum);
    552 #endif
    553 	return datum;
    554 }
    555 
    556 void
    557 mkclock_pnpbus_nvwr(struct mk48txx_softc *osc, int off, uint8_t datum)
    558 {
    559 	struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
    560 	int s;
    561 
    562 #ifdef DEBUG
    563 	aprint_debug("mkclock_isa_nvwr(%d, %02x)\n", off, datum);
    564 #endif
    565 	s = splclock();
    566 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
    567 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
    568 	bus_space_write_1(sc->sc_data, sc->sc_datah, 0, datum);
    569 	splx(s);
    570 }
    571