xref: /netbsd-src/sys/arch/atari/dev/nvram.c (revision 81b108b45f75f89f1e3ffad9fb6f074e771c0935)
1 /*	$NetBSD: nvram.c,v 1.3 1996/04/18 08:52:08 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman.
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 Leo Weppelman.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Nvram driver
35  */
36 
37 #include <sys/param.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 #include <sys/buf.h>
44 #include <sys/uio.h>
45 
46 #include <machine/iomap.h>
47 #include <machine/cpu.h>
48 
49 #include <atari/dev/clockreg.h>
50 #include <atari/dev/nvramvar.h>
51 
52 #include "nvr.h"
53 
54 #define	MC_NVRAM_CSUM	(MC_NVRAM_START + MC_NVRAM_SIZE - 2)
55 
56 #if NNVR > 0
57 static void	nvram_set_csum __P((u_char csum));
58 static int	nvram_csum_valid __P((u_char csum));
59 static u_char	nvram_csum __P((void));
60 
61 /*
62  * Auto config stuff....
63  */
64 static void	nvr_attach __P((struct device *, struct device *, void *));
65 static int	nvr_match __P((struct device *, void *, void *));
66 
67 struct cfattach nvr_ca = {
68 	sizeof(struct nvr_softc), nvr_match, nvr_attach
69 };
70 
71 struct cfdriver nvr_cd = {
72 	NULL, "nvr", DV_DULL, NULL, 0
73 };
74 
75 /*ARGSUSED*/
76 static	int
77 nvr_match(pdp, match, auxp)
78 struct	device *pdp;
79 void	*match, *auxp;
80 {
81 	if (!strcmp((char *)auxp, "nvr"))
82 		return (1);
83 	return (0);
84 }
85 
86 /*ARGSUSED*/
87 static void
88 nvr_attach(pdp, dp, auxp)
89 struct	device *pdp, *dp;
90 void	*auxp;
91 {
92 	struct nvr_softc	*nvr_soft;
93 	int			nreg;
94 
95 	/*
96 	 * Check the validity of the NVram contents
97 	 */
98 	if (!nvram_csum_valid(nvram_csum())) {
99 		printf(": Invalid checksum - re-initialized");
100 		for (nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++)
101 			mc146818_write(RTC, nreg, 0);
102 		nvram_set_csum(nvram_csum());
103 	}
104 	nvr_soft = nvr_cd.cd_devs[0];
105 	nvr_soft->nvr_flags = NVR_CONFIGURED;
106 	printf("\n");
107 }
108 /*
109  * End of auto config stuff....
110  */
111 #endif /* NNVR > 0 */
112 
113 /*
114  * Kernel internal interface
115  */
116 int
117 nvr_get_byte(byteno)
118 int	byteno;
119 {
120 #if NNVR > 0
121 	struct nvr_softc	*nvr_soft;
122 
123 	nvr_soft = nvr_cd.cd_devs[0];
124 	if (!(nvr_soft->nvr_flags & NVR_CONFIGURED))
125 		return(NVR_INVALID);
126 	return (mc146818_read(RTC, byteno + MC_NVRAM_START) & 0xff);
127 #else
128 	return(NVR_INVALID);
129 #endif /* NNVR > 0 */
130 }
131 
132 #if NNVR > 0
133 
134 int
135 nvram_uio(uio)
136 struct uio	*uio;
137 {
138 	int			i;
139 	off_t			offset;
140 	int			nleft;
141 	u_char			buf[MC_NVRAM_CSUM - MC_NVRAM_START + 1];
142 	u_char			*p;
143 	struct nvr_softc	*nvr_soft;
144 
145 	nvr_soft = nvr_cd.cd_devs[0];
146 	if (!(nvr_soft->nvr_flags & NVR_CONFIGURED))
147 		return ENXIO;
148 
149 #ifdef NV_DEBUG
150 	printf("Request to transfer %d bytes offset: %d, %s nvram\n",
151 				(long)uio->uio_resid, (long)uio->uio_offset,
152 				(uio->uio_rw == UIO_READ) ? "from" : "to");
153 #endif /* NV_DEBUG */
154 
155 	offset = uio->uio_offset + MC_NVRAM_START;
156 	nleft  = uio->uio_resid;
157 	if (offset + nleft >= MC_NVRAM_CSUM) {
158 		if (offset == MC_NVRAM_CSUM)
159 			return (0);
160 		nleft = MC_NVRAM_CSUM - offset;
161 		if (nleft <= 0)
162 			return (EINVAL);
163 	}
164 #ifdef NV_DEBUG
165 	printf("Translated: offset = %d, bytes: %d\n", (long)offset, nleft);
166 #endif /* NV_DEBUG */
167 
168 	if (uio->uio_rw == UIO_READ) {
169 		for (i = 0, p = buf; i < nleft; i++, p++)
170 			*p =  mc146818_read(RTC, offset + i);
171 	}
172 	if ((i = uiomove(buf, nleft, uio)) != 0)
173 		return (i);
174 	if (uio->uio_rw == UIO_WRITE) {
175 		for (i = 0, p = buf; i < nleft; i++, p++)
176 			mc146818_write(RTC, offset + i, *p);
177 		nvram_set_csum(nvram_csum());
178 	}
179 	return(0);
180 }
181 
182 static u_char
183 nvram_csum()
184 {
185 	u_char	csum;
186 	int	nreg;
187 
188 	for (csum = 0, nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++)
189 		csum += mc146818_read(RTC, nreg);
190 	return(csum);
191 }
192 
193 static int
194 nvram_csum_valid(csum)
195 u_char	csum;
196 {
197 	if (((~csum & 0xff) != mc146818_read(RTC, MC_NVRAM_CSUM))
198 		|| (csum != mc146818_read(RTC, MC_NVRAM_CSUM + 1)))
199 		return 0;
200 	return 1;
201 }
202 
203 static void
204 nvram_set_csum(csum)
205 u_char	csum;
206 {
207 	mc146818_write(RTC, MC_NVRAM_CSUM,    ~csum);
208 	mc146818_write(RTC, MC_NVRAM_CSUM + 1, csum);
209 }
210 #endif /* NNVR > 0 */
211