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