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