nvram.c revision 1.22 1 1.22 tsutsui /* $NetBSD: nvram.c,v 1.22 2022/07/02 14:29:04 tsutsui Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Leo Weppelman.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo *
16 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 leo */
27 1.1 leo
28 1.1 leo /*
29 1.1 leo * Nvram driver
30 1.1 leo */
31 1.10 lukem
32 1.10 lukem #include <sys/cdefs.h>
33 1.22 tsutsui __KERNEL_RCSID(0, "$NetBSD: nvram.c,v 1.22 2022/07/02 14:29:04 tsutsui Exp $");
34 1.1 leo
35 1.1 leo #include <sys/param.h>
36 1.1 leo #include <sys/conf.h>
37 1.1 leo #include <sys/kernel.h>
38 1.1 leo #include <sys/proc.h>
39 1.1 leo #include <sys/systm.h>
40 1.1 leo #include <sys/device.h>
41 1.1 leo #include <sys/buf.h>
42 1.1 leo #include <sys/uio.h>
43 1.1 leo
44 1.1 leo #include <machine/iomap.h>
45 1.3 leo #include <machine/cpu.h>
46 1.1 leo
47 1.1 leo #include <atari/dev/clockreg.h>
48 1.1 leo #include <atari/dev/nvramvar.h>
49 1.1 leo
50 1.18 tsutsui #include "ioconf.h"
51 1.18 tsutsui
52 1.1 leo #include "nvr.h"
53 1.1 leo
54 1.20 christos #ifdef NVRAM_DEBUG
55 1.20 christos #define DPRINTF(a) printf a
56 1.20 christos #else
57 1.20 christos #define DPRINTF(a)
58 1.20 christos #endif
59 1.20 christos
60 1.1 leo #define MC_NVRAM_CSUM (MC_NVRAM_START + MC_NVRAM_SIZE - 2)
61 1.1 leo
62 1.1 leo #if NNVR > 0
63 1.14 dsl static void nvram_set_csum(u_char csum);
64 1.14 dsl static int nvram_csum_valid(u_char csum);
65 1.14 dsl static u_char nvram_csum(void);
66 1.1 leo
67 1.1 leo /*
68 1.1 leo * Auto config stuff....
69 1.1 leo */
70 1.19 tsutsui static void nvr_attach(device_t, device_t, void *);
71 1.19 tsutsui static int nvr_match(device_t, cfdata_t, void *);
72 1.1 leo
73 1.19 tsutsui CFATTACH_DECL_NEW(nvr, sizeof(struct nvr_softc),
74 1.9 thorpej nvr_match, nvr_attach, NULL, NULL);
75 1.2 thorpej
76 1.1 leo /*ARGSUSED*/
77 1.22 tsutsui static int
78 1.19 tsutsui nvr_match(device_t parent, cfdata_t cf, void *aux)
79 1.1 leo {
80 1.22 tsutsui
81 1.19 tsutsui if (!strcmp((char *)aux, "nvr"))
82 1.22 tsutsui return 1;
83 1.22 tsutsui return 0;
84 1.1 leo }
85 1.1 leo
86 1.1 leo /*ARGSUSED*/
87 1.1 leo static void
88 1.19 tsutsui nvr_attach(device_t parent, device_t self, void *aux)
89 1.1 leo {
90 1.22 tsutsui struct nvr_softc *sc;
91 1.22 tsutsui int nreg;
92 1.1 leo
93 1.1 leo /*
94 1.1 leo * Check the validity of the NVram contents
95 1.1 leo */
96 1.21 tsutsui /* XXX: Milan's firmware seems to use different check method */
97 1.21 tsutsui if ((machineid & ATARI_MILAN) == 0) {
98 1.21 tsutsui if (!nvram_csum_valid(nvram_csum())) {
99 1.22 tsutsui aprint_error(": Invalid checksum - re-initialized");
100 1.21 tsutsui for (nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM;
101 1.21 tsutsui nreg++)
102 1.21 tsutsui mc146818_write(RTC, nreg, 0);
103 1.21 tsutsui nvram_set_csum(nvram_csum());
104 1.21 tsutsui }
105 1.1 leo }
106 1.19 tsutsui sc = device_private(self);
107 1.19 tsutsui sc->sc_dev = self;
108 1.19 tsutsui sc->sc_flags = NVR_CONFIGURED;
109 1.22 tsutsui aprint_normal("\n");
110 1.1 leo }
111 1.1 leo /*
112 1.1 leo * End of auto config stuff....
113 1.1 leo */
114 1.1 leo #endif /* NNVR > 0 */
115 1.1 leo
116 1.1 leo /*
117 1.1 leo * Kernel internal interface
118 1.1 leo */
119 1.1 leo int
120 1.13 cegger nvr_get_byte(int byteno)
121 1.1 leo {
122 1.1 leo #if NNVR > 0
123 1.22 tsutsui struct nvr_softc *sc;
124 1.1 leo
125 1.19 tsutsui sc = device_lookup_private(&nvr_cd, 0);
126 1.22 tsutsui if ((sc->sc_flags & NVR_CONFIGURED) == 0)
127 1.22 tsutsui return NVR_INVALID;
128 1.22 tsutsui return mc146818_read(RTC, byteno + MC_NVRAM_START) & 0xff;
129 1.1 leo #else
130 1.22 tsutsui return NVR_INVALID;
131 1.1 leo #endif /* NNVR > 0 */
132 1.1 leo }
133 1.1 leo
134 1.1 leo #if NNVR > 0
135 1.1 leo
136 1.3 leo int
137 1.13 cegger nvram_uio(struct uio *uio)
138 1.1 leo {
139 1.22 tsutsui int i;
140 1.22 tsutsui off_t offset;
141 1.22 tsutsui int nleft;
142 1.22 tsutsui uint8_t buf[MC_NVRAM_CSUM - MC_NVRAM_START + 1];
143 1.22 tsutsui uint8_t *p;
144 1.22 tsutsui struct nvr_softc *sc;
145 1.1 leo
146 1.19 tsutsui sc = device_lookup_private(&nvr_cd,0);
147 1.22 tsutsui if ((sc->sc_flags & NVR_CONFIGURED) == 0)
148 1.1 leo return ENXIO;
149 1.1 leo
150 1.20 christos DPRINTF(("Request to transfer %d bytes offset: %d, %s nvram\n",
151 1.22 tsutsui (long)uio->uio_resid, (long)uio->uio_offset,
152 1.22 tsutsui (uio->uio_rw == UIO_READ) ? "from" : "to"));
153 1.1 leo
154 1.1 leo offset = uio->uio_offset + MC_NVRAM_START;
155 1.1 leo nleft = uio->uio_resid;
156 1.1 leo if (offset + nleft >= MC_NVRAM_CSUM) {
157 1.1 leo if (offset == MC_NVRAM_CSUM)
158 1.22 tsutsui return 0;
159 1.1 leo nleft = MC_NVRAM_CSUM - offset;
160 1.1 leo if (nleft <= 0)
161 1.22 tsutsui return EINVAL;
162 1.1 leo }
163 1.20 christos DPRINTF(("Translated: offset = %d, bytes: %d\n", (long)offset, nleft));
164 1.1 leo
165 1.1 leo if (uio->uio_rw == UIO_READ) {
166 1.1 leo for (i = 0, p = buf; i < nleft; i++, p++)
167 1.22 tsutsui *p = mc146818_read(RTC, offset + i);
168 1.1 leo }
169 1.3 leo if ((i = uiomove(buf, nleft, uio)) != 0)
170 1.22 tsutsui return i;
171 1.1 leo if (uio->uio_rw == UIO_WRITE) {
172 1.1 leo for (i = 0, p = buf; i < nleft; i++, p++)
173 1.1 leo mc146818_write(RTC, offset + i, *p);
174 1.1 leo nvram_set_csum(nvram_csum());
175 1.1 leo }
176 1.22 tsutsui return 0;
177 1.1 leo }
178 1.1 leo
179 1.1 leo static u_char
180 1.16 cegger nvram_csum(void)
181 1.1 leo {
182 1.22 tsutsui uint8_t csum;
183 1.22 tsutsui int nreg;
184 1.1 leo
185 1.1 leo for (csum = 0, nreg = MC_NVRAM_START; nreg < MC_NVRAM_CSUM; nreg++)
186 1.1 leo csum += mc146818_read(RTC, nreg);
187 1.22 tsutsui return csum;
188 1.1 leo }
189 1.1 leo
190 1.1 leo static int
191 1.15 dsl nvram_csum_valid(u_char csum)
192 1.1 leo {
193 1.22 tsutsui
194 1.1 leo if (((~csum & 0xff) != mc146818_read(RTC, MC_NVRAM_CSUM))
195 1.1 leo || (csum != mc146818_read(RTC, MC_NVRAM_CSUM + 1)))
196 1.1 leo return 0;
197 1.1 leo return 1;
198 1.1 leo }
199 1.1 leo
200 1.1 leo static void
201 1.15 dsl nvram_set_csum(u_char csum)
202 1.1 leo {
203 1.22 tsutsui
204 1.1 leo mc146818_write(RTC, MC_NVRAM_CSUM, ~csum);
205 1.1 leo mc146818_write(RTC, MC_NVRAM_CSUM + 1, csum);
206 1.1 leo }
207 1.1 leo #endif /* NNVR > 0 */
208