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