xref: /netbsd-src/sys/arch/i386/isa/cmos.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: cmos.c,v 1.2 2007/12/11 11:54:36 lukem 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 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: cmos.c,v 1.2 2007/12/11 11:54:36 lukem Exp $");
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/malloc.h>
74 #include <sys/ioctl.h>
75 #include <sys/proc.h>
76 #include <sys/device.h>
77 #include <sys/conf.h>
78 #include <sys/kauth.h>
79 
80 #include <machine/bus.h>
81 #include <machine/intr.h>
82 
83 #include <dev/isa/isareg.h>
84 #include <dev/isa/isavar.h>
85 #include <dev/ic/mc146818reg.h>
86 #include <i386/isa/nvram.h>
87 
88 #define	CMOS_SUM	32
89 #define	CMOS_BIOSSPEC	34	/* start of BIOS-specific configuration data */
90 
91 #define	NVRAM_SUM	(MC_NVRAM_START + CMOS_SUM)
92 #define	NVRAM_BIOSSPEC	(MC_NVRAM_START + CMOS_BIOSSPEC)
93 
94 #define	CMOS_SIZE	NVRAM_BIOSSPEC
95 
96 struct cmos_softc {			/* driver status information */
97 	int	sc_open;
98 	uint8_t	sc_buf[CMOS_SIZE];
99 } cmos_softc;
100 
101 #ifdef CMOS_DEBUG
102 int cmos_debug = 0;
103 #endif
104 
105 void cmosattach(int);
106 dev_type_open(cmos_open);
107 dev_type_close(cmos_close);
108 dev_type_read(cmos_read);
109 dev_type_write(cmos_write);
110 
111 static void cmos_sum(uint8_t *, int, int, int);
112 #ifdef CMOS_DEBUG
113 static void cmos_dump(uint8_t *);
114 #endif
115 
116 const struct cdevsw cmos_cdevsw = {
117 	.d_open = cmos_open, .d_close = cmos_close, .d_read = cmos_read,
118 	.d_write = cmos_write, .d_ioctl = noioctl,
119 	.d_stop = nostop, .d_tty = notty, .d_poll = nopoll, .d_mmap = nommap,
120 	.d_kqfilter = nokqfilter,
121 };
122 
123 void
124 cmosattach(int n)
125 {
126 	printf("cmos: attached.\n");
127 }
128 
129 int
130 cmos_open(dev_t dev, int flags, int ifmt, struct lwp *l)
131 {
132 	struct cmos_softc *sc = &cmos_softc;
133 	struct proc *p = l->l_proc;
134 	int error;
135 
136 	error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
137 	    &p->p_acflag);
138 
139 	if (error != 0)
140 		return error;
141 
142 	sc->sc_open = 1;
143 #ifdef CMOS_DEBUG
144 	printf("cmos: opened\n");
145 #endif
146 	return 0;
147 }
148 
149 int
150 cmos_close(dev_t dev, int flags, int ifmt, struct lwp *l)
151 {
152 	struct cmos_softc *sc = &cmos_softc;
153 
154 	sc->sc_open = 0;
155 #ifdef CMOS_DEBUG
156 	printf("cmos: closed\n");
157 #endif
158 	return 0;
159 }
160 
161 static void
162 cmos_fetch(struct cmos_softc *sc)
163 {
164 	int i, s;
165 	uint8_t *p;
166 
167 	p = sc->sc_buf;
168 
169 	s = splclock();
170 	for (i = 0; i < CMOS_SIZE; i++)
171 		*p++ = mc146818_read(sc, i);
172 	splx(s);
173 }
174 
175 int
176 cmos_read(dev_t dev, struct uio *uio, int ioflag)
177 {
178 	struct cmos_softc *sc = &cmos_softc;
179 
180 	if (uio->uio_resid > CMOS_SIZE)
181 		return EINVAL;
182 
183 #ifdef CMOS_DEBUG
184 	printf("cmos: try to read to %d\n", CMOS_SIZE);
185 #endif
186 	cmos_fetch(sc);
187 
188 	return uiomove(sc->sc_buf, CMOS_SIZE, uio);
189 }
190 
191 int
192 cmos_write(dev_t dev, struct uio *uio, int ioflag)
193 {
194 	struct cmos_softc *sc = &cmos_softc;
195 	int error = 0, i, s;
196 
197 	cmos_fetch(sc);
198 
199 	error = uiomove(sc->sc_buf, CMOS_SIZE, uio);
200 #ifdef CMOS_DEBUG
201 	printf("write %d\n", l);
202 	if (cmos_debug)
203 		cmos_dump(sc->sc_buf);
204 #endif
205 	if (error)
206 		return error;
207 
208 	cmos_sum(sc->sc_buf, NVRAM_DISKETTE, NVRAM_SUM, NVRAM_SUM);
209 
210 #ifdef CMOS_DEBUG
211 	if (cmos_debug)
212 		cmos_dump(sc->sc_buf);
213 #endif
214 	s = splclock();
215 	for (i = NVRAM_DISKETTE; i < CMOS_SIZE; i++)
216 		mc146818_write(sc, i, sc->sc_buf[i]);
217 	splx(s);
218 
219 	return error;
220 }
221 
222 static void
223 cmos_sum(uint8_t *p, int from, int to, int offset)
224 {
225 	int i;
226 	uint16_t sum;
227 
228 #ifdef CMOS_DEBUG
229 	printf("%s: from %d to %d and store %d\n", __func__, from, to, offset);
230 #endif
231 
232 	sum = 0;
233 	for (i = from; i < to; i++)
234 		sum += p[i];
235 	p[offset] = sum >> 8;
236 	p[offset + 1] = sum & 0xff;
237 }
238 
239 #ifdef CMOS_DEBUG
240 static void
241 cmos_dump(uint8_t *p)
242 {
243 	int i;
244 
245 	for (i = 0; i < CMOS_SIZE; i++) {
246 		if (i % 16 == 0)
247 			printf("%02x:", i);
248 		printf(" %02x", p[i]);
249 		if (i % 16 == 15)
250 			printf("\n", buf);
251 	}
252 	printf("\n");
253 }
254 #endif
255