Home | History | Annotate | Line # | Download | only in pnpbus
nvram_pnpbus.c revision 1.3
      1 /* $NetBSD: nvram_pnpbus.c,v 1.3 2006/06/15 18:15:32 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.3 2006/06/15 18:15:32 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 prep_nvram_cdevsw = {
     98 	prep_nvramopen, prep_nvramclose, noread, nowrite, prep_nvramioctl,
     99 	nostop, notty, nopoll, nommap, nokqfilter,
    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 	todr_attach(&sc->sc_mksc.sc_handle);
    208 }
    209 
    210 /*
    211  * This function should be called at a high spl only, as it interfaces with
    212  * real hardware.
    213  */
    214 
    215 uint8_t
    216 prep_nvram_read_val(int addr)
    217 {
    218 	struct nvram_pnpbus_softc *sc;
    219 
    220 	if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
    221             || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
    222                 return 0;
    223         }
    224 
    225         sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
    226 
    227 	/* tell the NVRAM what we want */
    228 	bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
    229 	bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
    230 
    231 	return bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
    232 }
    233 
    234 /*
    235  * This function should be called at a high spl only, as it interfaces with
    236  * real hardware.
    237  */
    238 
    239 void
    240 prep_nvram_write_val(int addr, uint8_t val)
    241 {
    242 	struct nvram_pnpbus_softc *sc;
    243 
    244 	if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
    245             || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
    246                 return;
    247         }
    248 
    249         sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
    250 
    251 	/* tell the NVRAM what we want */
    252 	bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
    253 	bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
    254 
    255 	bus_space_write_1(sc->sc_data, sc->sc_datah, 0, val);
    256 }
    257 
    258 /* the rest of these should all be called with the lock held */
    259 
    260 char *
    261 prep_nvram_next_var(char *name)
    262 {
    263 	char *cp;
    264 
    265 	cp = name;
    266 	/* skip forward to the first null char */
    267 	while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp != '\0'))
    268 		cp++;
    269 	/* skip nulls */
    270 	while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp == '\0'))
    271 		cp++;
    272 	if ((cp - nvramGEAp) < nvram->Header.GELength)
    273 		return cp;
    274 	else
    275 		return NULL;
    276 }
    277 
    278 char *
    279 prep_nvram_find_var(const char *name)
    280 {
    281 	char *cp = nvramGEAp;
    282 	size_t len;
    283 
    284 	len = strlen(name);
    285 	while (cp != NULL) {
    286 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
    287 			return cp;
    288 		cp = prep_nvram_next_var(cp);
    289 	}
    290 	return NULL;
    291 }
    292 
    293 char *
    294 prep_nvram_get_var(const char *name)
    295 {
    296 	char *cp = nvramGEAp;
    297 	size_t len;
    298 
    299 	len = strlen(name);
    300 	while (cp != NULL) {
    301 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
    302 			return cp+len+1;
    303 		cp = prep_nvram_next_var(cp);
    304 	}
    305 	return NULL;
    306 }
    307 
    308 int
    309 prep_nvram_get_var_len(const char *name)
    310 {
    311 	char *cp = nvramGEAp;
    312 	char *ep;
    313 	size_t len;
    314 
    315 	len = strlen(name);
    316 	while (cp != NULL) {
    317 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
    318 			goto out;
    319 		cp = prep_nvram_next_var(cp);
    320 	}
    321 	return -1;
    322 
    323 out:
    324 	ep = cp;
    325 	while (ep != NULL && *ep != '\0')
    326 		ep++;
    327 	return ep-cp;
    328 }
    329 
    330 int
    331 prep_nvram_count_vars(void)
    332 {
    333 	char *cp = nvramGEAp;
    334 	int i=0;
    335 
    336 	while (cp != NULL) {
    337 		i++;
    338 		cp = prep_nvram_next_var(cp);
    339 	}
    340 	return i;
    341 }
    342 
    343 int
    344 prep_nvramioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
    345 {
    346 	int len, error;
    347 	struct pnviocdesc *pnv;
    348 	char *np, *cp;
    349 
    350 	pnv = (struct pnviocdesc *)data;
    351 	error = 0;
    352 
    353 	switch (cmd) {
    354 	case PNVIOCGET:
    355 		if (pnv->pnv_name == NULL)
    356 			return EINVAL;
    357 
    358 		simple_lock(&nvram_slock);
    359 		np = prep_nvram_get_var(pnv->pnv_name);
    360 		simple_unlock(&nvram_slock);
    361 		if (np == NULL)
    362 			return EINVAL;
    363 		simple_lock(&nvram_slock);
    364 		len = prep_nvram_get_var_len(pnv->pnv_name);
    365 		simple_unlock(&nvram_slock);
    366 
    367 		if (len > pnv->pnv_buflen) {
    368 			error = ENOMEM;
    369 			break;
    370 		}
    371 		if (len <= 0)
    372 			break;
    373 		error = copyout(np, pnv->pnv_buf, len);
    374 		pnv->pnv_buflen = len;
    375 		break;
    376 
    377 	case PNVIOCGETNEXTNAME:
    378 		/* if the first one is null, we give them the first name */
    379 		simple_lock(&nvram_slock);
    380 		if (pnv->pnv_name == NULL) {
    381 			np = nvramGEAp;
    382 			cp = prep_nvram_next_var(np);
    383 		} else {
    384 			np = prep_nvram_find_var(pnv->pnv_name);
    385 			cp = prep_nvram_next_var(np);
    386 		}
    387 		simple_unlock(&nvram_slock);
    388 		np = cp;
    389 		while (*np != '=')
    390 			np++;
    391 		len = np-cp;
    392 		if (len > pnv->pnv_buflen) {
    393 			error = ENOMEM;
    394 			break;
    395 		}
    396 		error = copyout(cp, pnv->pnv_buf, len);
    397 		pnv->pnv_buflen = len;
    398 		break;
    399 	default:
    400 		return (ENOTTY);
    401 	}
    402 	return (error);
    403 }
    404 
    405 int
    406 prep_nvramopen(dev_t dev, int flags, int mode, struct lwp *l)
    407 {
    408 	struct nvram_pnpbus_softc *sc;
    409 
    410 	sc = device_lookup(&nvram_cd, dev);
    411 	if (sc == NULL)
    412 		return ENODEV;
    413 
    414 	if (sc->sc_open)
    415 		return EBUSY;
    416 
    417 	sc->sc_open = 1;
    418 
    419 	return 0;
    420 }
    421 
    422 int
    423 prep_nvramclose(dev_t dev, int flags, int mode, struct lwp *l)
    424 {
    425 	struct nvram_pnpbus_softc *sc;
    426 
    427 	sc = device_lookup(&nvram_cd, dev);
    428 	if (sc == NULL)
    429 		return ENODEV;
    430 	sc->sc_open = 0;
    431 	return 0;
    432 }
    433 
    434 /* Motorola mk48txx clock routines */
    435 uint8_t
    436 mkclock_pnpbus_nvrd(struct mk48txx_softc *osc, int off)
    437 {
    438 	struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
    439 	uint8_t datum;
    440 	int s;
    441 
    442 #ifdef DEBUG
    443 	printf("mkclock_pnpbus_nvrd(%d)", off);
    444 #endif
    445 	s = splclock();
    446 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
    447 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
    448 	datum = bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
    449 	splx(s);
    450 #ifdef DEBUG
    451 	printf(" -> %02x\n", datum);
    452 #endif
    453 	return datum;
    454 }
    455 
    456 void
    457 mkclock_pnpbus_nvwr(struct mk48txx_softc *osc, int off, uint8_t datum)
    458 {
    459 	struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
    460 	int s;
    461 
    462 #ifdef DEBUG
    463 	printf("mkclock_isa_nvwr(%d, %02x)\n", off, datum);
    464 #endif
    465 	s = splclock();
    466 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
    467 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
    468 	bus_space_write_1(sc->sc_data, sc->sc_datah, 0, datum);
    469 	splx(s);
    470 }
    471