Home | History | Annotate | Line # | Download | only in dev
nvram.c revision 1.18
      1 /*	$NetBSD: nvram.c,v 1.18 2012/10/27 17:18:00 chs 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/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: nvram.c,v 1.18 2012/10/27 17:18:00 chs Exp $");
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/conf.h>
     41 #include <sys/kernel.h>
     42 #include <sys/device.h>
     43 #include <sys/malloc.h>
     44 #include <sys/event.h>
     45 
     46 #include <machine/autoconf.h>
     47 #include <machine/pio.h>
     48 
     49 #define NVRAM_NONE  0
     50 #define NVRAM_IOMEM 1
     51 #define NVRAM_PORT  2
     52 
     53 #define NVRAM_SIZE 0x2000
     54 
     55 static void nvram_attach(device_t, device_t, void *);
     56 static int nvram_match(device_t, cfdata_t, void *);
     57 
     58 struct nvram_softc {
     59 	int nv_type;
     60 	char *nv_port;
     61 	char *nv_data;
     62 };
     63 
     64 CFATTACH_DECL_NEW(nvram, sizeof(struct nvram_softc),
     65     nvram_match, nvram_attach, NULL, NULL);
     66 
     67 extern struct cfdriver nvram_cd;
     68 
     69 dev_type_read(nvramread);
     70 dev_type_write(nvramwrite);
     71 dev_type_mmap(nvrammmap);
     72 
     73 const struct cdevsw nvram_cdevsw = {
     74 	nullopen, nullclose, nvramread, nvramwrite, noioctl,
     75 	nostop, notty, nopoll, nvrammmap, nokqfilter,
     76 };
     77 
     78 int
     79 nvram_match(device_t parent, cfdata_t cf, void *aux)
     80 {
     81 	struct confargs *ca = aux;
     82 
     83 	if (strcmp(ca->ca_name, "nvram") != 0)
     84 		return 0;
     85 
     86 	if (ca->ca_nreg == 0)
     87 		return 0;
     88 
     89 	return 1;
     90 }
     91 
     92 void
     93 nvram_attach(device_t parent, device_t self, void *aux)
     94 {
     95 	struct nvram_softc *sc = device_private(self);
     96 	struct confargs *ca = aux;
     97 	int *reg = ca->ca_reg;
     98 
     99 	printf("\n");
    100 
    101 	switch (ca->ca_nreg) {
    102 
    103 	case 8:						/* untested */
    104 		sc->nv_type = NVRAM_IOMEM;
    105 		sc->nv_data = mapiodev(ca->ca_baseaddr + reg[0], reg[1], false);
    106 		break;
    107 
    108 	case 16:
    109 		sc->nv_type = NVRAM_PORT;
    110 		sc->nv_port = mapiodev(ca->ca_baseaddr + reg[0], reg[1], false);
    111 		sc->nv_data = mapiodev(ca->ca_baseaddr + reg[2], reg[3], false);
    112 		break;
    113 
    114 	case 0:
    115 	default:
    116 		sc->nv_type = NVRAM_NONE;
    117 		return;
    118 	}
    119 }
    120 
    121 int
    122 nvramread(dev_t dev, struct uio *uio, int flag)
    123 {
    124 	struct nvram_softc *sc;
    125 	u_int off, cnt;
    126 	int i;
    127 	int error = 0;
    128 	char *buf;
    129 
    130 	sc = device_lookup_private(&nvram_cd, 0);
    131 
    132 	off = uio->uio_offset;
    133 	cnt = uio->uio_resid;
    134 
    135 	if (off > NVRAM_SIZE || cnt > NVRAM_SIZE)
    136 		return EFAULT;
    137 
    138 	if (off + cnt > NVRAM_SIZE)
    139 		cnt = NVRAM_SIZE - off;
    140 
    141 	buf = malloc(NVRAM_SIZE, M_DEVBUF, M_WAITOK);
    142 	if (buf == NULL) {
    143 		error = EAGAIN;
    144 		goto out;
    145 	}
    146 
    147 	switch (sc->nv_type) {
    148 
    149 	case NVRAM_IOMEM:
    150 		for (i = 0; i < NVRAM_SIZE; i++)
    151 			buf[i] = sc->nv_data[i * 16];
    152 
    153 		break;
    154 
    155 	case NVRAM_PORT:
    156 		for (i = 0; i < NVRAM_SIZE; i += 32) {
    157 			int j;
    158 
    159 			out8(sc->nv_port, i / 32);
    160 			for (j = 0; j < 32; j++) {
    161 				buf[i + j] = sc->nv_data[j * 16];
    162 			}
    163 		}
    164 		break;
    165 
    166 	default:
    167 		goto out;
    168 	}
    169 
    170 	error = uiomove(buf + off, cnt, uio);
    171 
    172 out:
    173 	if (buf)
    174 		free(buf, M_DEVBUF);
    175 
    176 	return error;
    177 }
    178 
    179 int
    180 nvramwrite(dev_t dev, struct uio *uio, int flag)
    181 {
    182 	return ENXIO;
    183 }
    184 
    185 paddr_t
    186 nvrammmap(dev_t dev, off_t off, int prot)
    187 {
    188 	return -1;
    189 }
    190