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