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