Home | History | Annotate | Line # | Download | only in dev
nvram.c revision 1.7
      1 /*	$NetBSD: nvram.c,v 1.7 2002/10/02 05:30:42 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1998	Internet Research Institute, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by
     18  *	Internet Research Institute, Inc.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/types.h>
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/conf.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/conf.h>
     42 
     43 #include <machine/autoconf.h>
     44 #include <machine/pio.h>
     45 
     46 #define NVRAM_NONE  0
     47 #define NVRAM_IOMEM 1
     48 #define NVRAM_PORT  2
     49 
     50 #define NVRAM_SIZE 0x2000
     51 
     52 static void nvram_attach __P((struct device *, struct device *, void *));
     53 static int nvram_match __P((struct device *, struct cfdata *, void *));
     54 
     55 struct nvram_softc {
     56 	struct device sc_dev;
     57 	int nv_type;
     58 	char *nv_port;
     59 	char *nv_data;
     60 };
     61 
     62 CFATTACH_DECL(nvram, sizeof(struct nvram_softc),
     63     nvram_match, nvram_attach, NULL, NULL);
     64 
     65 extern struct cfdriver nvram_cd;
     66 
     67 dev_type_read(nvramread);
     68 dev_type_write(nvramwrite);
     69 dev_type_mmap(nvrammmap);
     70 
     71 const struct cdevsw nvram_cdevsw = {
     72 	nullopen, nullclose, nvramread, nvramwrite, noioctl,
     73 	nostop, notty, nopoll, nvrammmap,
     74 };
     75 
     76 int
     77 nvram_match(parent, cf, aux)
     78 	struct device *parent;
     79 	struct cfdata *cf;
     80 	void *aux;
     81 {
     82 	struct confargs *ca = aux;
     83 
     84 	if (strcmp(ca->ca_name, "nvram") != 0)
     85 		return 0;
     86 
     87 	if (ca->ca_nreg == 0)
     88 		return 0;
     89 
     90 	return 1;
     91 }
     92 
     93 void
     94 nvram_attach(parent, self, aux)
     95 	struct device *parent, *self;
     96 	void *aux;
     97 {
     98 	struct nvram_softc *sc = (struct nvram_softc *)self;
     99 	struct confargs *ca = aux;
    100 	int *reg = ca->ca_reg;
    101 
    102 	printf("\n");
    103 
    104 	switch (ca->ca_nreg) {
    105 
    106 	case 8:						/* untested */
    107 		sc->nv_type = NVRAM_IOMEM;
    108 		sc->nv_data = mapiodev(ca->ca_baseaddr + reg[0], reg[1]);
    109 		break;
    110 
    111 	case 16:
    112 		sc->nv_type = NVRAM_PORT;
    113 		sc->nv_port = mapiodev(ca->ca_baseaddr + reg[0], reg[1]);
    114 		sc->nv_data = mapiodev(ca->ca_baseaddr + reg[2], reg[3]);
    115 		break;
    116 
    117 	case 0:
    118 	default:
    119 		sc->nv_type = NVRAM_NONE;
    120 		return;
    121 	}
    122 }
    123 
    124 int
    125 nvramread(dev, uio, flag)
    126 	dev_t dev;
    127 	struct uio *uio;
    128 	int flag;
    129 {
    130 	struct nvram_softc *sc;
    131 	u_int off, cnt;
    132 	int i;
    133 	int error = 0;
    134 	char *buf;
    135 
    136 	sc = nvram_cd.cd_devs[0];
    137 
    138 	off = uio->uio_offset;
    139 	cnt = uio->uio_resid;
    140 
    141 	if (off > NVRAM_SIZE || cnt > NVRAM_SIZE)
    142 		return EFAULT;
    143 
    144 	if (off + cnt > NVRAM_SIZE)
    145 		cnt = NVRAM_SIZE - off;
    146 
    147 	buf = malloc(NVRAM_SIZE, M_DEVBUF, M_WAITOK);
    148 	if (buf == NULL) {
    149 		error = EAGAIN;
    150 		goto out;
    151 	}
    152 
    153 	switch (sc->nv_type) {
    154 
    155 	case NVRAM_IOMEM:
    156 		for (i = 0; i < NVRAM_SIZE; i++)
    157 			buf[i] = sc->nv_data[i * 16];
    158 
    159 		break;
    160 
    161 	case NVRAM_PORT:
    162 		for (i = 0; i < NVRAM_SIZE; i += 32) {
    163 			int j;
    164 
    165 			out8(sc->nv_port, i / 32);
    166 			for (j = 0; j < 32; j++) {
    167 				buf[i + j] = sc->nv_data[j * 16];
    168 			}
    169 		}
    170 		break;
    171 
    172 	default:
    173 		goto out;
    174 	}
    175 
    176 	error = uiomove(buf + off, cnt, uio);
    177 
    178 out:
    179 	if (buf)
    180 		free(buf, M_DEVBUF);
    181 
    182 	return error;
    183 }
    184 
    185 int
    186 nvramwrite(dev, uio, flag)
    187 	dev_t dev;
    188 	struct uio *uio;
    189 	int flag;
    190 {
    191 	return ENXIO;
    192 }
    193 
    194 paddr_t
    195 nvrammmap(dev, off, prot)
    196         dev_t dev;
    197         off_t off;
    198 	int prot;
    199 {
    200 	return -1;
    201 }
    202