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