cmos.c revision 1.1 1 /* $Id: cmos.c,v 1.1 2007/02/06 07:29:00 dyoung Exp $ */
2
3 /*
4 * Copyright (C) 2003 JONE System Co., Inc.
5 * All right reserved.
6 *
7 * Copyright (C) 1999, 2000, 2001, 2002 JEPRO Co., Ltd.
8 * All right reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of JEPRO Co., Ltd. nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36 /*
37 * Copyright (c) 2007 David Young. All right reserved.
38 *
39 * Redistribution and use in source and binary forms, with or
40 * without modification, are permitted provided that the following
41 * conditions are met:
42 *
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above
46 * copyright notice, this list of conditions and the following
47 * disclaimer in the documentation and/or other materials provided
48 * with the distribution.
49 * 3. The name of David Young may not be used to endorse or promote
50 * products derived from this software without specific prior
51 * written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
54 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
55 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
59 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
60 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
63 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 */
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/ioctl.h>
71 #include <sys/proc.h>
72 #include <sys/device.h>
73 #include <sys/conf.h>
74 #include <sys/kauth.h>
75
76 #include <machine/bus.h>
77 #include <machine/intr.h>
78
79 #include <dev/isa/isareg.h>
80 #include <dev/isa/isavar.h>
81 #include <dev/ic/mc146818reg.h>
82 #include <i386/isa/nvram.h>
83
84 #define CMOS_SUM 32
85 #define CMOS_BIOSSPEC 34 /* start of BIOS-specific configuration data */
86
87 #define NVRAM_SUM (MC_NVRAM_START + CMOS_SUM)
88 #define NVRAM_BIOSSPEC (MC_NVRAM_START + CMOS_BIOSSPEC)
89
90 #define CMOS_SIZE NVRAM_BIOSSPEC
91
92 struct cmos_softc { /* driver status information */
93 int sc_open;
94 uint8_t sc_buf[CMOS_SIZE];
95 } cmos_softc;
96
97 #ifdef CMOS_DEBUG
98 int cmos_debug = 0;
99 #endif
100
101 void cmosattach(int);
102 dev_type_open(cmos_open);
103 dev_type_close(cmos_close);
104 dev_type_read(cmos_read);
105 dev_type_write(cmos_write);
106
107 static void cmos_sum(uint8_t *, int, int, int);
108 #ifdef CMOS_DEBUG
109 static void cmos_dump(uint8_t *);
110 #endif
111
112 const struct cdevsw cmos_cdevsw = {
113 .d_open = cmos_open, .d_close = cmos_close, .d_read = cmos_read,
114 .d_write = cmos_write, .d_ioctl = noioctl,
115 .d_stop = nostop, .d_tty = notty, .d_poll = nopoll, .d_mmap = nommap,
116 .d_kqfilter = nokqfilter,
117 };
118
119 void
120 cmosattach(int n)
121 {
122 printf("cmos: attached.\n");
123 }
124
125 int
126 cmos_open(dev_t dev, int flags, int ifmt, struct lwp *l)
127 {
128 struct cmos_softc *sc = &cmos_softc;
129 struct proc *p = l->l_proc;
130 int error;
131
132 error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
133 &p->p_acflag);
134
135 if (error != 0)
136 return error;
137
138 sc->sc_open = 1;
139 #ifdef CMOS_DEBUG
140 printf("cmos: opened\n");
141 #endif
142 return 0;
143 }
144
145 int
146 cmos_close(dev_t dev, int flags, int ifmt, struct lwp *l)
147 {
148 struct cmos_softc *sc = &cmos_softc;
149
150 sc->sc_open = 0;
151 #ifdef CMOS_DEBUG
152 printf("cmos: closed\n");
153 #endif
154 return 0;
155 }
156
157 static void
158 cmos_fetch(struct cmos_softc *sc)
159 {
160 int i, s;
161 uint8_t *p;
162
163 p = sc->sc_buf;
164
165 s = splclock();
166 for (i = 0; i < CMOS_SIZE; i++)
167 *p++ = mc146818_read(sc, i);
168 splx(s);
169 }
170
171 int
172 cmos_read(dev_t dev, struct uio *uio, int ioflag)
173 {
174 struct cmos_softc *sc = &cmos_softc;
175
176 if (uio->uio_resid > CMOS_SIZE)
177 return EINVAL;
178
179 #ifdef CMOS_DEBUG
180 printf("cmos: try to read to %d\n", CMOS_SIZE);
181 #endif
182 cmos_fetch(sc);
183
184 return uiomove(sc->sc_buf, CMOS_SIZE, uio);
185 }
186
187 int
188 cmos_write(dev_t dev, struct uio *uio, int ioflag)
189 {
190 struct cmos_softc *sc = &cmos_softc;
191 int error = 0, i, s;
192
193 cmos_fetch(sc);
194
195 error = uiomove(sc->sc_buf, CMOS_SIZE, uio);
196 #ifdef CMOS_DEBUG
197 printf("write %d\n", l);
198 if (cmos_debug)
199 cmos_dump(sc->sc_buf);
200 #endif
201 if (error)
202 return error;
203
204 cmos_sum(sc->sc_buf, NVRAM_DISKETTE, NVRAM_SUM, NVRAM_SUM);
205
206 #ifdef CMOS_DEBUG
207 if (cmos_debug)
208 cmos_dump(sc->sc_buf);
209 #endif
210 s = splclock();
211 for (i = NVRAM_DISKETTE; i < CMOS_SIZE; i++)
212 mc146818_write(sc, i, sc->sc_buf[i]);
213 splx(s);
214
215 return error;
216 }
217
218 static void
219 cmos_sum(uint8_t *p, int from, int to, int offset)
220 {
221 int i;
222 uint16_t sum;
223
224 #ifdef CMOS_DEBUG
225 printf("%s: from %d to %d and store %d\n", __func__, from, to, offset);
226 #endif
227
228 sum = 0;
229 for (i = from; i < to; i++)
230 sum += p[i];
231 p[offset] = sum >> 8;
232 p[offset + 1] = sum & 0xff;
233 }
234
235 #ifdef CMOS_DEBUG
236 static void
237 cmos_dump(uint8_t *p)
238 {
239 int i;
240
241 for (i = 0; i < CMOS_SIZE; i++) {
242 if (i % 16 == 0)
243 printf("%02x:", i);
244 printf(" %02x", p[i]);
245 if (i % 16 == 15)
246 printf("\n", buf);
247 }
248 printf("\n");
249 }
250 #endif
251