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