Home | History | Annotate | Line # | Download | only in pnpbus
nvram_pnpbus.c revision 1.1.2.1
      1      1.1  garbled /* $NetBSD: nvram_pnpbus.c,v 1.1.2.1 2006/05/24 15:48:20 tron Exp $ */
      2      1.1  garbled 
      3      1.1  garbled /*-
      4      1.1  garbled  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5      1.1  garbled  * All rights reserved.
      6      1.1  garbled  *
      7      1.1  garbled  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  garbled  * by Tim Rightnour
      9      1.1  garbled  *
     10      1.1  garbled  * Redistribution and use in source and binary forms, with or without
     11      1.1  garbled  * modification, are permitted provided that the following conditions
     12      1.1  garbled  * are met:
     13      1.1  garbled  * 1. Redistributions of source code must retain the above copyright
     14      1.1  garbled  *    notice, this list of conditions and the following disclaimer.
     15      1.1  garbled  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  garbled  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  garbled  *    documentation and/or other materials provided with the distribution.
     18      1.1  garbled  * 3. All advertising materials mentioning features or use of this software
     19      1.1  garbled  *    must display the following acknowledgement:
     20      1.1  garbled  *        This product includes software developed by the NetBSD
     21      1.1  garbled  *        Foundation, Inc. and its contributors.
     22      1.1  garbled  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1  garbled  *    contributors may be used to endorse or promote products derived
     24      1.1  garbled  *    from this software without specific prior written permission.
     25      1.1  garbled  *
     26      1.1  garbled  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1  garbled  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1  garbled  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1  garbled  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1  garbled  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1  garbled  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1  garbled  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1  garbled  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1  garbled  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1  garbled  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1  garbled  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1  garbled  */
     38      1.1  garbled 
     39      1.1  garbled #include <sys/cdefs.h>
     40  1.1.2.1     tron __KERNEL_RCSID(0, "$NetBSD: nvram_pnpbus.c,v 1.1.2.1 2006/05/24 15:48:20 tron Exp $");
     41      1.1  garbled 
     42      1.1  garbled #include <sys/types.h>
     43      1.1  garbled #include <sys/param.h>
     44      1.1  garbled #include <sys/systm.h>
     45      1.1  garbled #include <sys/ioctl.h>
     46      1.1  garbled #include <sys/conf.h>
     47      1.1  garbled #include <sys/kthread.h>
     48      1.1  garbled #include <sys/device.h>
     49      1.1  garbled #include <sys/malloc.h>
     50      1.1  garbled #include <sys/lock.h>
     51      1.1  garbled 
     52      1.1  garbled #include <machine/bus.h>
     53      1.1  garbled #include <machine/intr.h>
     54      1.1  garbled #include <machine/isa_machdep.h>
     55      1.1  garbled #include <machine/nvram.h>
     56      1.1  garbled 
     57      1.1  garbled #include <prep/pnpbus/pnpbusvar.h>
     58      1.1  garbled 
     59      1.1  garbled #include "opt_nvram.h"
     60      1.1  garbled 
     61      1.1  garbled static char *nvramData;
     62      1.1  garbled static NVRAM_MAP *nvram;
     63      1.1  garbled static char *nvramGEAp;		/* pointer to the GE area */
     64      1.1  garbled static char *nvramCAp;		/* pointer to the Config area */
     65      1.1  garbled static char *nvramOSAp;		/* pointer to the OSArea */
     66      1.1  garbled struct simplelock nvram_slock;	/* lock */
     67      1.1  garbled 
     68      1.1  garbled extern char bootpath[256];
     69      1.1  garbled 
     70      1.1  garbled #define NVRAM_STD_DEV 0
     71      1.1  garbled 
     72      1.1  garbled static int	nvram_pnpbus_probe(struct device *, struct cfdata *, void *);
     73      1.1  garbled static void	nvram_pnpbus_attach(struct device *, struct device *, void *);
     74      1.1  garbled uint8_t		prep_nvram_read_val(int);
     75      1.1  garbled char		*prep_nvram_next_var(char *);
     76      1.1  garbled char		*prep_nvram_find_var(const char *);
     77      1.1  garbled char		*prep_nvram_get_var(const char *);
     78      1.1  garbled int		prep_nvram_get_var_len(const char *);
     79      1.1  garbled int		prep_nvram_count_vars(void);
     80      1.1  garbled void		prep_nvram_write_val(int, uint8_t);
     81      1.1  garbled 
     82      1.1  garbled CFATTACH_DECL(nvram_pnpbus, sizeof(struct nvram_pnpbus_softc),
     83      1.1  garbled     nvram_pnpbus_probe, nvram_pnpbus_attach, NULL, NULL);
     84      1.1  garbled 
     85      1.1  garbled dev_type_open(prep_nvramopen);
     86      1.1  garbled dev_type_ioctl(prep_nvramioctl);
     87      1.1  garbled dev_type_close(prep_nvramclose);
     88      1.1  garbled 
     89      1.1  garbled const struct cdevsw prep_nvram_cdevsw = {
     90      1.1  garbled 	prep_nvramopen, prep_nvramclose, noread, nowrite, prep_nvramioctl,
     91      1.1  garbled 	nostop, notty, nopoll, nommap, nokqfilter,
     92      1.1  garbled };
     93      1.1  garbled 
     94      1.1  garbled extern struct cfdriver nvram_cd;
     95      1.1  garbled 
     96      1.1  garbled static int
     97      1.1  garbled nvram_pnpbus_probe(struct device *parent, struct cfdata *match, void *aux)
     98      1.1  garbled {
     99      1.1  garbled 	struct pnpbus_dev_attach_args *pna = aux;
    100      1.1  garbled 	int ret = 0;
    101      1.1  garbled 
    102      1.1  garbled 	if (strcmp(pna->pna_devid, "IBM0008") == 0)
    103      1.1  garbled 		ret = 1;
    104      1.1  garbled 
    105      1.1  garbled 	if (ret)
    106      1.1  garbled 		pnpbus_scan(pna, pna->pna_ppc_dev);
    107      1.1  garbled 
    108      1.1  garbled 	return ret;
    109      1.1  garbled }
    110      1.1  garbled 
    111      1.1  garbled static void
    112      1.1  garbled nvram_pnpbus_attach(struct device *parent, struct device *self, void *aux)
    113      1.1  garbled {
    114      1.1  garbled 	struct nvram_pnpbus_softc *sc = (void *)self;
    115      1.1  garbled 	struct pnpbus_dev_attach_args *pna = aux;
    116      1.1  garbled 	int as_iobase, as_len, data_iobase, data_len, i, nvlen, cur;
    117      1.1  garbled 	uint8_t *p;
    118      1.1  garbled 	HEADER prep_nvram_header;
    119      1.1  garbled 
    120      1.1  garbled 	sc->sc_iot = pna->pna_iot;
    121      1.1  garbled 
    122      1.1  garbled 	pnpbus_getioport(&pna->pna_res, 0, &as_iobase, &as_len);
    123      1.1  garbled 	pnpbus_getioport(&pna->pna_res, 1, &data_iobase, &data_len);
    124      1.1  garbled 
    125      1.1  garbled 	if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_as, &sc->sc_ash) ||
    126      1.1  garbled 	    pnpbus_io_map(&pna->pna_res, 1, &sc->sc_data, &sc->sc_datah)) {
    127      1.1  garbled 		printf("nvram: couldn't map registers\n");
    128  1.1.2.1     tron 		return;
    129      1.1  garbled 	}
    130      1.1  garbled 
    131      1.1  garbled 	simple_lock_init(&nvram_slock);
    132      1.1  garbled 
    133      1.1  garbled 	/* Initialize the nvram header */
    134      1.1  garbled 	p = (uint8_t *) &prep_nvram_header;
    135      1.1  garbled 	for (i = 0; i < sizeof(HEADER); i++)
    136      1.1  garbled 		*p++ = prep_nvram_read_val(i);
    137      1.1  garbled 
    138      1.1  garbled 	/*
    139      1.1  garbled 	 * now that we have the header, we know how big the NVRAM part on
    140      1.1  garbled 	 * this machine really is.  Malloc space to save a copy.
    141      1.1  garbled 	 */
    142      1.1  garbled 
    143      1.1  garbled 	nvlen = 1024 * prep_nvram_header.Size;
    144      1.1  garbled 	nvramData = malloc(nvlen, M_DEVBUF, M_NOWAIT);
    145      1.1  garbled 	p = (uint8_t *) nvramData;
    146      1.1  garbled 
    147      1.1  garbled 	/*
    148      1.1  garbled 	 * now read the whole nvram in, one chunk at a time, marking down
    149      1.1  garbled 	 * the main start points as we go.
    150      1.1  garbled 	 */
    151      1.1  garbled 	for (i = 0; i < sizeof(HEADER) && i < nvlen; i++)
    152      1.1  garbled 		*p++ = prep_nvram_read_val(i);
    153      1.1  garbled 	nvramGEAp = p;
    154      1.1  garbled 	cur = i;
    155      1.1  garbled 	for (; i < cur + prep_nvram_header.GELength && i < nvlen; i++)
    156      1.1  garbled 		*p++ = prep_nvram_read_val(i);
    157      1.1  garbled 	nvramOSAp = p;
    158      1.1  garbled 	cur = i;
    159      1.1  garbled 	for (; i < cur + prep_nvram_header.OSAreaLength && i < nvlen; i++)
    160      1.1  garbled 		*p++ = prep_nvram_read_val(i);
    161      1.1  garbled 	nvramCAp = p;
    162      1.1  garbled 	cur = i;
    163      1.1  garbled 	for (; i < cur + prep_nvram_header.ConfigLength && i < nvlen; i++)
    164      1.1  garbled 		*p++ = prep_nvram_read_val(i);
    165      1.1  garbled 
    166      1.1  garbled 	/* we should be done here.  umm.. yay? */
    167      1.1  garbled 	nvram = (NVRAM_MAP *)&nvramData[0];
    168      1.1  garbled 	printf("%s: Read %d bytes from nvram of size %d\n", device_xname(self),
    169      1.1  garbled 	    i, nvlen);
    170      1.1  garbled 
    171      1.1  garbled #if defined(NVRAM_DUMP)
    172      1.1  garbled 	printf("Boot device: %s\n", prep_nvram_get_var("fw-boot-device"));
    173      1.1  garbled 	printf("Dumping nvram\n");
    174      1.1  garbled 	for (cur=0; cur < i; cur++) {
    175      1.1  garbled 		printf("%c", nvramData[cur]);
    176      1.1  garbled 		if (cur % 70 == 0)
    177      1.1  garbled 			printf("\n");
    178      1.1  garbled 	}
    179      1.1  garbled #endif
    180      1.1  garbled 	strncpy(bootpath, prep_nvram_get_var("fw-boot-device"), 256);
    181      1.1  garbled }
    182      1.1  garbled 
    183      1.1  garbled /*
    184      1.1  garbled  * This function should be called at a high spl only, as it interfaces with
    185      1.1  garbled  * real hardware.
    186      1.1  garbled  */
    187      1.1  garbled 
    188      1.1  garbled uint8_t
    189      1.1  garbled prep_nvram_read_val(int addr)
    190      1.1  garbled {
    191      1.1  garbled 	struct nvram_pnpbus_softc *sc;
    192      1.1  garbled 
    193      1.1  garbled 	if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
    194      1.1  garbled             || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
    195      1.1  garbled                 return 0;
    196      1.1  garbled         }
    197      1.1  garbled 
    198      1.1  garbled         sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
    199      1.1  garbled 
    200      1.1  garbled 	/* tell the NVRAM what we want */
    201      1.1  garbled 	bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
    202      1.1  garbled 	bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
    203      1.1  garbled 
    204      1.1  garbled 	return bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
    205      1.1  garbled }
    206      1.1  garbled 
    207      1.1  garbled /*
    208      1.1  garbled  * This function should be called at a high spl only, as it interfaces with
    209      1.1  garbled  * real hardware.
    210      1.1  garbled  */
    211      1.1  garbled 
    212      1.1  garbled void
    213      1.1  garbled prep_nvram_write_val(int addr, uint8_t val)
    214      1.1  garbled {
    215      1.1  garbled 	struct nvram_pnpbus_softc *sc;
    216      1.1  garbled 
    217      1.1  garbled 	if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
    218      1.1  garbled             || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
    219      1.1  garbled                 return;
    220      1.1  garbled         }
    221      1.1  garbled 
    222      1.1  garbled         sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
    223      1.1  garbled 
    224      1.1  garbled 	/* tell the NVRAM what we want */
    225      1.1  garbled 	bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
    226      1.1  garbled 	bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
    227      1.1  garbled 
    228      1.1  garbled 	bus_space_write_1(sc->sc_data, sc->sc_datah, 0, val);
    229      1.1  garbled }
    230      1.1  garbled 
    231      1.1  garbled /* the rest of these should all be called with the lock held */
    232      1.1  garbled 
    233      1.1  garbled char *
    234      1.1  garbled prep_nvram_next_var(char *name)
    235      1.1  garbled {
    236      1.1  garbled 	char *cp;
    237      1.1  garbled 
    238      1.1  garbled 	cp = name;
    239      1.1  garbled 	/* skip forward to the first null char */
    240      1.1  garbled 	while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp != '\0'))
    241      1.1  garbled 		cp++;
    242      1.1  garbled 	/* skip nulls */
    243      1.1  garbled 	while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp == '\0'))
    244      1.1  garbled 		cp++;
    245      1.1  garbled 	if ((cp - nvramGEAp) < nvram->Header.GELength)
    246      1.1  garbled 		return cp;
    247      1.1  garbled 	else
    248      1.1  garbled 		return NULL;
    249      1.1  garbled }
    250      1.1  garbled 
    251      1.1  garbled char *
    252      1.1  garbled prep_nvram_find_var(const char *name)
    253      1.1  garbled {
    254      1.1  garbled 	char *cp = nvramGEAp;
    255      1.1  garbled 	size_t len;
    256      1.1  garbled 
    257      1.1  garbled 	len = strlen(name);
    258      1.1  garbled 	while (cp != NULL) {
    259      1.1  garbled 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
    260      1.1  garbled 			return cp;
    261      1.1  garbled 		cp = prep_nvram_next_var(cp);
    262      1.1  garbled 	}
    263      1.1  garbled 	return NULL;
    264      1.1  garbled }
    265      1.1  garbled 
    266      1.1  garbled char *
    267      1.1  garbled prep_nvram_get_var(const char *name)
    268      1.1  garbled {
    269      1.1  garbled 	char *cp = nvramGEAp;
    270      1.1  garbled 	size_t len;
    271      1.1  garbled 
    272      1.1  garbled 	len = strlen(name);
    273      1.1  garbled 	while (cp != NULL) {
    274      1.1  garbled 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
    275      1.1  garbled 			return cp+len+1;
    276      1.1  garbled 		cp = prep_nvram_next_var(cp);
    277      1.1  garbled 	}
    278      1.1  garbled 	return NULL;
    279      1.1  garbled }
    280      1.1  garbled 
    281      1.1  garbled int
    282      1.1  garbled prep_nvram_get_var_len(const char *name)
    283      1.1  garbled {
    284      1.1  garbled 	char *cp = nvramGEAp;
    285      1.1  garbled 	char *ep;
    286      1.1  garbled 	size_t len;
    287      1.1  garbled 
    288      1.1  garbled 	len = strlen(name);
    289      1.1  garbled 	while (cp != NULL) {
    290      1.1  garbled 		if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
    291      1.1  garbled 			goto out;
    292      1.1  garbled 		cp = prep_nvram_next_var(cp);
    293      1.1  garbled 	}
    294      1.1  garbled 	return -1;
    295      1.1  garbled 
    296      1.1  garbled out:
    297      1.1  garbled 	ep = cp;
    298      1.1  garbled 	while (ep != NULL && *ep != '\0')
    299      1.1  garbled 		ep++;
    300      1.1  garbled 	return ep-cp;
    301      1.1  garbled }
    302      1.1  garbled 
    303      1.1  garbled int
    304      1.1  garbled prep_nvram_count_vars(void)
    305      1.1  garbled {
    306      1.1  garbled 	char *cp = nvramGEAp;
    307      1.1  garbled 	int i=0;
    308      1.1  garbled 
    309      1.1  garbled 	while (cp != NULL) {
    310      1.1  garbled 		i++;
    311      1.1  garbled 		cp = prep_nvram_next_var(cp);
    312      1.1  garbled 	}
    313      1.1  garbled 	return i;
    314      1.1  garbled }
    315      1.1  garbled 
    316      1.1  garbled int
    317      1.1  garbled prep_nvramioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
    318      1.1  garbled {
    319      1.1  garbled 	int len, error;
    320      1.1  garbled 	struct pnviocdesc *pnv;
    321      1.1  garbled 	char *np, *cp;
    322      1.1  garbled 
    323      1.1  garbled 	pnv = (struct pnviocdesc *)data;
    324      1.1  garbled 	error = 0;
    325      1.1  garbled 
    326      1.1  garbled 	switch (cmd) {
    327      1.1  garbled 	case PNVIOCGET:
    328      1.1  garbled 		if (pnv->pnv_name == NULL)
    329      1.1  garbled 			return EINVAL;
    330      1.1  garbled 
    331      1.1  garbled 		simple_lock(&nvram_slock);
    332      1.1  garbled 		np = prep_nvram_get_var(pnv->pnv_name);
    333      1.1  garbled 		simple_unlock(&nvram_slock);
    334      1.1  garbled 		if (np == NULL)
    335      1.1  garbled 			return EINVAL;
    336      1.1  garbled 		simple_lock(&nvram_slock);
    337      1.1  garbled 		len = prep_nvram_get_var_len(pnv->pnv_name);
    338      1.1  garbled 		simple_unlock(&nvram_slock);
    339      1.1  garbled 
    340      1.1  garbled 		if (len > pnv->pnv_buflen) {
    341      1.1  garbled 			error = ENOMEM;
    342      1.1  garbled 			break;
    343      1.1  garbled 		}
    344      1.1  garbled 		if (len <= 0)
    345      1.1  garbled 			break;
    346      1.1  garbled 		error = copyout(np, pnv->pnv_buf, len);
    347      1.1  garbled 		pnv->pnv_buflen = len;
    348      1.1  garbled 		break;
    349      1.1  garbled 
    350      1.1  garbled 	case PNVIOCGETNEXTNAME:
    351      1.1  garbled 		/* if the first one is null, we give them the first name */
    352      1.1  garbled 		simple_lock(&nvram_slock);
    353      1.1  garbled 		if (pnv->pnv_name == NULL) {
    354      1.1  garbled 			np = nvramGEAp;
    355      1.1  garbled 			cp = prep_nvram_next_var(np);
    356      1.1  garbled 		} else {
    357      1.1  garbled 			np = prep_nvram_find_var(pnv->pnv_name);
    358      1.1  garbled 			cp = prep_nvram_next_var(np);
    359      1.1  garbled 		}
    360      1.1  garbled 		simple_unlock(&nvram_slock);
    361      1.1  garbled 		np = cp;
    362      1.1  garbled 		while (*np != '=')
    363      1.1  garbled 			np++;
    364      1.1  garbled 		len = np-cp;
    365      1.1  garbled 		if (len > pnv->pnv_buflen) {
    366      1.1  garbled 			error = ENOMEM;
    367      1.1  garbled 			break;
    368      1.1  garbled 		}
    369      1.1  garbled 		error = copyout(cp, pnv->pnv_buf, len);
    370      1.1  garbled 		pnv->pnv_buflen = len;
    371      1.1  garbled 		break;
    372      1.1  garbled 	default:
    373      1.1  garbled 		return (ENOTTY);
    374      1.1  garbled 	}
    375      1.1  garbled 	return (error);
    376      1.1  garbled }
    377      1.1  garbled 
    378      1.1  garbled int
    379      1.1  garbled prep_nvramopen(dev_t dev, int flags, int mode, struct lwp *l)
    380      1.1  garbled {
    381      1.1  garbled 	struct nvram_pnpbus_softc *sc;
    382      1.1  garbled 
    383      1.1  garbled 	sc = device_lookup(&nvram_cd, dev);
    384      1.1  garbled 	if (sc == NULL)
    385      1.1  garbled 		return ENODEV;
    386      1.1  garbled 
    387      1.1  garbled 	if (sc->sc_open)
    388      1.1  garbled 		return EBUSY;
    389      1.1  garbled 
    390      1.1  garbled 	sc->sc_open = 1;
    391      1.1  garbled 
    392      1.1  garbled 	return 0;
    393      1.1  garbled }
    394      1.1  garbled 
    395      1.1  garbled int
    396      1.1  garbled prep_nvramclose(dev_t dev, int flags, int mode, struct lwp *l)
    397      1.1  garbled {
    398      1.1  garbled 	struct nvram_pnpbus_softc *sc;
    399      1.1  garbled 
    400      1.1  garbled 	sc = device_lookup(&nvram_cd, dev);
    401      1.1  garbled 	if (sc == NULL)
    402      1.1  garbled 		return ENODEV;
    403      1.1  garbled 	sc->sc_open = 0;
    404      1.1  garbled 	return 0;
    405      1.1  garbled }
    406