clock.c revision 1.27.6.5 1 1.27.6.5 nathanw /* $NetBSD: clock.c,v 1.27.6.5 2002/09/17 21:13:42 nathanw Exp $ */
2 1.27.6.2 scw
3 1.27.6.2 scw /*
4 1.27.6.2 scw * Copyright (c) 1988 University of Utah.
5 1.27.6.2 scw * Copyright (c) 1982, 1990 The Regents of the University of California.
6 1.27.6.2 scw * All rights reserved.
7 1.27.6.2 scw *
8 1.27.6.2 scw * This code is derived from software contributed to Berkeley by
9 1.27.6.2 scw * the Systems Programming Group of the University of Utah Computer
10 1.27.6.2 scw * Science Department.
11 1.27.6.2 scw *
12 1.27.6.2 scw * Redistribution and use in source and binary forms, with or without
13 1.27.6.2 scw * modification, are permitted provided that the following conditions
14 1.27.6.2 scw * are met:
15 1.27.6.2 scw * 1. Redistributions of source code must retain the above copyright
16 1.27.6.2 scw * notice, this list of conditions and the following disclaimer.
17 1.27.6.2 scw * 2. Redistributions in binary form must reproduce the above copyright
18 1.27.6.2 scw * notice, this list of conditions and the following disclaimer in the
19 1.27.6.2 scw * documentation and/or other materials provided with the distribution.
20 1.27.6.2 scw * 3. All advertising materials mentioning features or use of this software
21 1.27.6.2 scw * must display the following acknowledgement:
22 1.27.6.2 scw * This product includes software developed by the University of
23 1.27.6.2 scw * California, Berkeley and its contributors.
24 1.27.6.2 scw * 4. Neither the name of the University nor the names of its contributors
25 1.27.6.2 scw * may be used to endorse or promote products derived from this software
26 1.27.6.2 scw * without specific prior written permission.
27 1.27.6.2 scw *
28 1.27.6.2 scw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.27.6.2 scw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.27.6.2 scw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.27.6.2 scw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.27.6.2 scw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.27.6.2 scw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.27.6.2 scw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.27.6.2 scw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.27.6.2 scw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.27.6.2 scw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.27.6.2 scw * SUCH DAMAGE.
39 1.27.6.2 scw *
40 1.27.6.2 scw * from: Utah $Hdr: clock.c 1.18 91/01/21$
41 1.27.6.2 scw *
42 1.27.6.2 scw * @(#)clock.c 7.6 (Berkeley) 5/7/91
43 1.27.6.2 scw */
44 1.27.6.2 scw
45 1.27.6.2 scw #include <sys/param.h>
46 1.27.6.2 scw #include <sys/kernel.h>
47 1.27.6.2 scw #include <sys/systm.h>
48 1.27.6.2 scw #include <sys/device.h>
49 1.27.6.2 scw #include <sys/uio.h>
50 1.27.6.2 scw #include <sys/conf.h>
51 1.27.6.2 scw #include <sys/proc.h>
52 1.27.6.2 scw
53 1.27.6.2 scw #include <dev/clock_subr.h>
54 1.27.6.2 scw
55 1.27.6.2 scw #include <machine/psl.h>
56 1.27.6.2 scw #include <machine/cpu.h>
57 1.27.6.2 scw #include <machine/iomap.h>
58 1.27.6.2 scw #include <machine/mfp.h>
59 1.27.6.2 scw #include <atari/dev/clockreg.h>
60 1.27.6.2 scw #include <atari/atari/device.h>
61 1.27.6.2 scw
62 1.27.6.2 scw #if defined(GPROF) && defined(PROFTIMER)
63 1.27.6.2 scw #include <machine/profile.h>
64 1.27.6.2 scw #endif
65 1.27.6.2 scw
66 1.27.6.2 scw /*
67 1.27.6.2 scw * The MFP clock runs at 2457600Hz. We use a {system,stat,prof}clock divider
68 1.27.6.2 scw * of 200. Therefore the timer runs at an effective rate of:
69 1.27.6.2 scw * 2457600/200 = 12288Hz.
70 1.27.6.2 scw */
71 1.27.6.2 scw #define CLOCK_HZ 12288
72 1.27.6.2 scw
73 1.27.6.2 scw /*
74 1.27.6.2 scw * Machine-dependent clock routines.
75 1.27.6.2 scw *
76 1.27.6.2 scw * Inittodr initializes the time of day hardware which provides
77 1.27.6.2 scw * date functions.
78 1.27.6.2 scw *
79 1.27.6.2 scw * Resettodr restores the time of day hardware after a time change.
80 1.27.6.2 scw */
81 1.27.6.2 scw
82 1.27.6.2 scw struct clock_softc {
83 1.27.6.2 scw struct device sc_dev;
84 1.27.6.2 scw int sc_flags;
85 1.27.6.2 scw };
86 1.27.6.2 scw
87 1.27.6.2 scw /*
88 1.27.6.2 scw * 'sc_flags' state info. Only used by the rtc-device functions.
89 1.27.6.2 scw */
90 1.27.6.2 scw #define RTC_OPEN 1
91 1.27.6.2 scw
92 1.27.6.2 scw dev_type_open(rtcopen);
93 1.27.6.2 scw dev_type_close(rtcclose);
94 1.27.6.2 scw dev_type_read(rtcread);
95 1.27.6.2 scw dev_type_write(rtcwrite);
96 1.27.6.2 scw
97 1.27.6.2 scw static void clockattach __P((struct device *, struct device *, void *));
98 1.27.6.2 scw static int clockmatch __P((struct device *, struct cfdata *, void *));
99 1.27.6.2 scw
100 1.27.6.2 scw struct cfattach clock_ca = {
101 1.27.6.2 scw sizeof(struct clock_softc), clockmatch, clockattach
102 1.27.6.2 scw };
103 1.27.6.2 scw
104 1.27.6.2 scw extern struct cfdriver clock_cd;
105 1.27.6.2 scw
106 1.27.6.5 nathanw const struct cdevsw rtc_cdevsw = {
107 1.27.6.5 nathanw rtcopen, rtcclose, rtcread, rtcwrite, noioctl,
108 1.27.6.5 nathanw nostop, notty, nopoll, nommap,
109 1.27.6.5 nathanw };
110 1.27.6.5 nathanw
111 1.27.6.2 scw void statintr __P((struct clockframe));
112 1.27.6.2 scw
113 1.27.6.2 scw static u_long gettod __P((void));
114 1.27.6.2 scw static int twodigits __P((char *, int));
115 1.27.6.2 scw
116 1.27.6.2 scw static int divisor; /* Systemclock divisor */
117 1.27.6.2 scw
118 1.27.6.2 scw /*
119 1.27.6.2 scw * Statistics and profile clock intervals and variances. Variance must
120 1.27.6.2 scw * be a power of 2. Since this gives us an even number, not an odd number,
121 1.27.6.2 scw * we discard one case and compensate. That is, a variance of 64 would
122 1.27.6.2 scw * give us offsets in [0..63]. Instead, we take offsets in [1..63].
123 1.27.6.2 scw * This is symmetric around the point 32, or statvar/2, and thus averages
124 1.27.6.2 scw * to that value (assuming uniform random numbers).
125 1.27.6.2 scw */
126 1.27.6.2 scw #ifdef STATCLOCK
127 1.27.6.2 scw static int statvar = 32; /* {stat,prof}clock variance */
128 1.27.6.2 scw static int statmin; /* statclock divisor - variance/2 */
129 1.27.6.2 scw static int profmin; /* profclock divisor - variance/2 */
130 1.27.6.2 scw static int clk2min; /* current, from above choices */
131 1.27.6.2 scw #endif
132 1.27.6.2 scw
133 1.27.6.2 scw int
134 1.27.6.2 scw clockmatch(pdp, cfp, auxp)
135 1.27.6.2 scw struct device *pdp;
136 1.27.6.2 scw struct cfdata *cfp;
137 1.27.6.2 scw void *auxp;
138 1.27.6.2 scw {
139 1.27.6.2 scw if (!atari_realconfig) {
140 1.27.6.2 scw /*
141 1.27.6.2 scw * Initialize Timer-B in the ST-MFP. This timer is used by
142 1.27.6.2 scw * the 'delay' function below. This timer is setup to be
143 1.27.6.2 scw * continueously counting from 255 back to zero at a
144 1.27.6.2 scw * frequency of 614400Hz. We do this *early* in the
145 1.27.6.2 scw * initialisation process.
146 1.27.6.2 scw */
147 1.27.6.2 scw MFP->mf_tbcr = 0; /* Stop timer */
148 1.27.6.2 scw MFP->mf_iera &= ~IA_TIMB; /* Disable timer interrupts */
149 1.27.6.2 scw MFP->mf_tbdr = 0;
150 1.27.6.2 scw MFP->mf_tbcr = T_Q004; /* Start timer */
151 1.27.6.2 scw
152 1.27.6.2 scw /*
153 1.27.6.2 scw * Initialize the time structure
154 1.27.6.2 scw */
155 1.27.6.2 scw time.tv_sec = 0;
156 1.27.6.2 scw time.tv_usec = 0;
157 1.27.6.2 scw
158 1.27.6.2 scw return 0;
159 1.27.6.2 scw }
160 1.27.6.2 scw if(!strcmp("clock", auxp))
161 1.27.6.2 scw return(1);
162 1.27.6.2 scw return(0);
163 1.27.6.2 scw }
164 1.27.6.2 scw
165 1.27.6.2 scw /*
166 1.27.6.2 scw * Start the real-time clock.
167 1.27.6.2 scw */
168 1.27.6.2 scw void clockattach(pdp, dp, auxp)
169 1.27.6.2 scw struct device *pdp, *dp;
170 1.27.6.2 scw void *auxp;
171 1.27.6.2 scw {
172 1.27.6.2 scw struct clock_softc *sc = (void *)dp;
173 1.27.6.2 scw
174 1.27.6.2 scw sc->sc_flags = 0;
175 1.27.6.2 scw
176 1.27.6.2 scw /*
177 1.27.6.2 scw * Initialize Timer-A in the ST-MFP. We use a divisor of 200.
178 1.27.6.2 scw * The MFP clock runs at 2457600Hz. Therefore the timer runs
179 1.27.6.2 scw * at an effective rate of: 2457600/200 = 12288Hz. The
180 1.27.6.2 scw * following expression works for 48, 64 or 96 hz.
181 1.27.6.2 scw */
182 1.27.6.2 scw divisor = CLOCK_HZ/hz;
183 1.27.6.2 scw MFP->mf_tacr = 0; /* Stop timer */
184 1.27.6.2 scw MFP->mf_iera &= ~IA_TIMA; /* Disable timer interrupts */
185 1.27.6.2 scw MFP->mf_tadr = divisor; /* Set divisor */
186 1.27.6.2 scw
187 1.27.6.2 scw if (hz != 48 && hz != 64 && hz != 96) { /* XXX */
188 1.27.6.2 scw printf (": illegal value %d for systemclock, reset to %d\n\t",
189 1.27.6.2 scw hz, 64);
190 1.27.6.2 scw hz = 64;
191 1.27.6.2 scw }
192 1.27.6.2 scw printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor);
193 1.27.6.2 scw
194 1.27.6.2 scw #ifdef STATCLOCK
195 1.27.6.2 scw if ((stathz == 0) || (stathz > hz) || (CLOCK_HZ % stathz))
196 1.27.6.2 scw stathz = hz;
197 1.27.6.2 scw if ((profhz == 0) || (profhz > (hz << 1)) || (CLOCK_HZ % profhz))
198 1.27.6.2 scw profhz = hz << 1;
199 1.27.6.2 scw
200 1.27.6.2 scw MFP->mf_tcdcr &= 0x7; /* Stop timer */
201 1.27.6.2 scw MFP->mf_ierb &= ~IB_TIMC; /* Disable timer inter. */
202 1.27.6.2 scw MFP->mf_tcdr = CLOCK_HZ/stathz; /* Set divisor */
203 1.27.6.2 scw
204 1.27.6.2 scw statmin = (CLOCK_HZ/stathz) - (statvar >> 1);
205 1.27.6.2 scw profmin = (CLOCK_HZ/profhz) - (statvar >> 1);
206 1.27.6.2 scw clk2min = statmin;
207 1.27.6.2 scw #endif /* STATCLOCK */
208 1.27.6.2 scw
209 1.27.6.2 scw }
210 1.27.6.2 scw
211 1.27.6.2 scw void cpu_initclocks()
212 1.27.6.2 scw {
213 1.27.6.2 scw MFP->mf_tacr = T_Q200; /* Start timer */
214 1.27.6.2 scw MFP->mf_ipra = (u_int8_t)~IA_TIMA;/* Clear pending interrupts */
215 1.27.6.2 scw MFP->mf_iera |= IA_TIMA; /* Enable timer interrupts */
216 1.27.6.2 scw MFP->mf_imra |= IA_TIMA; /* ..... */
217 1.27.6.2 scw
218 1.27.6.2 scw #ifdef STATCLOCK
219 1.27.6.2 scw MFP->mf_tcdcr = (MFP->mf_tcdcr & 0x7) | (T_Q200<<4); /* Start */
220 1.27.6.2 scw MFP->mf_iprb = (u_int8_t)~IB_TIMC;/* Clear pending interrupts */
221 1.27.6.2 scw MFP->mf_ierb |= IB_TIMC; /* Enable timer interrupts */
222 1.27.6.2 scw MFP->mf_imrb |= IB_TIMC; /* ..... */
223 1.27.6.2 scw #endif /* STATCLOCK */
224 1.27.6.2 scw }
225 1.27.6.2 scw
226 1.27.6.2 scw void
227 1.27.6.2 scw setstatclockrate(newhz)
228 1.27.6.2 scw int newhz;
229 1.27.6.2 scw {
230 1.27.6.2 scw #ifdef STATCLOCK
231 1.27.6.2 scw if (newhz == stathz)
232 1.27.6.2 scw clk2min = statmin;
233 1.27.6.2 scw else clk2min = profmin;
234 1.27.6.2 scw #endif /* STATCLOCK */
235 1.27.6.2 scw }
236 1.27.6.2 scw
237 1.27.6.2 scw #ifdef STATCLOCK
238 1.27.6.2 scw void
239 1.27.6.2 scw statintr(frame)
240 1.27.6.2 scw struct clockframe frame;
241 1.27.6.2 scw {
242 1.27.6.2 scw register int var, r;
243 1.27.6.2 scw
244 1.27.6.2 scw var = statvar - 1;
245 1.27.6.2 scw do {
246 1.27.6.2 scw r = random() & var;
247 1.27.6.2 scw } while(r == 0);
248 1.27.6.2 scw
249 1.27.6.2 scw /*
250 1.27.6.2 scw * Note that we are always lagging behind as the new divisor
251 1.27.6.2 scw * value will not be loaded until the next interrupt. This
252 1.27.6.2 scw * shouldn't disturb the median frequency (I think ;-) ) as
253 1.27.6.2 scw * only the value used when switching frequencies is used
254 1.27.6.2 scw * twice. This shouldn't happen very often.
255 1.27.6.2 scw */
256 1.27.6.2 scw MFP->mf_tcdr = clk2min + r;
257 1.27.6.2 scw
258 1.27.6.2 scw statclock(&frame);
259 1.27.6.2 scw }
260 1.27.6.2 scw #endif /* STATCLOCK */
261 1.27.6.2 scw
262 1.27.6.2 scw /*
263 1.27.6.2 scw * Returns number of usec since last recorded clock "tick"
264 1.27.6.2 scw * (i.e. clock interrupt).
265 1.27.6.2 scw */
266 1.27.6.2 scw long
267 1.27.6.2 scw clkread()
268 1.27.6.2 scw {
269 1.27.6.2 scw u_int delta;
270 1.27.6.2 scw u_char ipra, tadr;
271 1.27.6.2 scw
272 1.27.6.2 scw /*
273 1.27.6.2 scw * Note: Order is important!
274 1.27.6.2 scw * By reading 'ipra' before 'tadr' and caching the data, I try to avoid
275 1.27.6.2 scw * the situation that very low value in 'tadr' is read (== a big delta)
276 1.27.6.2 scw * while also acccounting for a full 'tick' because the counter went
277 1.27.6.2 scw * through zero during the calculations.
278 1.27.6.2 scw */
279 1.27.6.2 scw ipra = MFP->mf_ipra; tadr = MFP->mf_tadr;
280 1.27.6.2 scw
281 1.27.6.2 scw delta = ((divisor - tadr) * tick) / divisor;
282 1.27.6.2 scw /*
283 1.27.6.2 scw * Account for pending clock interrupts
284 1.27.6.2 scw */
285 1.27.6.2 scw if(ipra & IA_TIMA)
286 1.27.6.2 scw return(delta + tick);
287 1.27.6.2 scw return(delta);
288 1.27.6.2 scw }
289 1.27.6.2 scw
290 1.27.6.2 scw #define TIMB_FREQ 614400
291 1.27.6.2 scw #define TIMB_LIMIT 256
292 1.27.6.2 scw
293 1.27.6.2 scw /*
294 1.27.6.2 scw * Wait "n" microseconds.
295 1.27.6.2 scw * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz.
296 1.27.6.2 scw * Note: timer had better have been programmed before this is first used!
297 1.27.6.2 scw */
298 1.27.6.2 scw void
299 1.27.6.2 scw delay(n)
300 1.27.6.2 scw int n;
301 1.27.6.2 scw {
302 1.27.6.2 scw int tick, otick;
303 1.27.6.2 scw
304 1.27.6.2 scw /*
305 1.27.6.2 scw * Read the counter first, so that the rest of the setup overhead is
306 1.27.6.2 scw * counted.
307 1.27.6.2 scw */
308 1.27.6.2 scw otick = MFP->mf_tbdr;
309 1.27.6.2 scw
310 1.27.6.2 scw /*
311 1.27.6.2 scw * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler code so
312 1.27.6.2 scw * we can take advantage of the intermediate 64-bit quantity to prevent
313 1.27.6.2 scw * loss of significance.
314 1.27.6.2 scw */
315 1.27.6.2 scw n -= 5;
316 1.27.6.2 scw if(n < 0)
317 1.27.6.2 scw return;
318 1.27.6.2 scw {
319 1.27.6.2 scw u_int temp;
320 1.27.6.2 scw
321 1.27.6.2 scw __asm __volatile ("mulul %2,%1:%0" : "=d" (n), "=d" (temp)
322 1.27.6.2 scw : "d" (TIMB_FREQ), "d" (n));
323 1.27.6.2 scw __asm __volatile ("divul %1,%2:%0" : "=d" (n)
324 1.27.6.2 scw : "d"(1000000),"d"(temp),"0"(n));
325 1.27.6.2 scw }
326 1.27.6.2 scw
327 1.27.6.2 scw while(n > 0) {
328 1.27.6.2 scw tick = MFP->mf_tbdr;
329 1.27.6.2 scw if(tick > otick)
330 1.27.6.2 scw n -= TIMB_LIMIT - (tick - otick);
331 1.27.6.2 scw else n -= otick - tick;
332 1.27.6.2 scw otick = tick;
333 1.27.6.2 scw }
334 1.27.6.2 scw }
335 1.27.6.2 scw
336 1.27.6.2 scw #ifdef GPROF
337 1.27.6.2 scw /*
338 1.27.6.2 scw * profclock() is expanded in line in lev6intr() unless profiling kernel.
339 1.27.6.2 scw * Assumes it is called with clock interrupts blocked.
340 1.27.6.2 scw */
341 1.27.6.2 scw profclock(pc, ps)
342 1.27.6.2 scw caddr_t pc;
343 1.27.6.2 scw int ps;
344 1.27.6.2 scw {
345 1.27.6.2 scw /*
346 1.27.6.2 scw * Came from user mode.
347 1.27.6.2 scw * If this process is being profiled record the tick.
348 1.27.6.2 scw */
349 1.27.6.2 scw if (USERMODE(ps)) {
350 1.27.6.2 scw if (p->p_stats.p_prof.pr_scale)
351 1.27.6.3 nathanw addupc(pc, &curproc->p_stats.p_prof, 1);
352 1.27.6.2 scw }
353 1.27.6.2 scw /*
354 1.27.6.2 scw * Came from kernel (supervisor) mode.
355 1.27.6.2 scw * If we are profiling the kernel, record the tick.
356 1.27.6.2 scw */
357 1.27.6.2 scw else if (profiling < 2) {
358 1.27.6.2 scw register int s = pc - s_lowpc;
359 1.27.6.2 scw
360 1.27.6.2 scw if (s < s_textsize)
361 1.27.6.2 scw kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
362 1.27.6.2 scw }
363 1.27.6.2 scw /*
364 1.27.6.2 scw * Kernel profiling was on but has been disabled.
365 1.27.6.2 scw * Mark as no longer profiling kernel and if all profiling done,
366 1.27.6.2 scw * disable the clock.
367 1.27.6.2 scw */
368 1.27.6.2 scw if (profiling && (profon & PRF_KERNEL)) {
369 1.27.6.2 scw profon &= ~PRF_KERNEL;
370 1.27.6.2 scw if (profon == PRF_NONE)
371 1.27.6.2 scw stopprofclock();
372 1.27.6.2 scw }
373 1.27.6.2 scw }
374 1.27.6.2 scw #endif
375 1.27.6.2 scw
376 1.27.6.2 scw /***********************************************************************
377 1.27.6.2 scw * Real Time Clock support *
378 1.27.6.2 scw ***********************************************************************/
379 1.27.6.2 scw
380 1.27.6.2 scw u_int mc146818_read(rtc, regno)
381 1.27.6.2 scw void *rtc;
382 1.27.6.2 scw u_int regno;
383 1.27.6.2 scw {
384 1.27.6.2 scw ((struct rtc *)rtc)->rtc_regno = regno;
385 1.27.6.2 scw return(((struct rtc *)rtc)->rtc_data & 0377);
386 1.27.6.2 scw }
387 1.27.6.2 scw
388 1.27.6.2 scw void mc146818_write(rtc, regno, value)
389 1.27.6.2 scw void *rtc;
390 1.27.6.2 scw u_int regno, value;
391 1.27.6.2 scw {
392 1.27.6.2 scw ((struct rtc *)rtc)->rtc_regno = regno;
393 1.27.6.2 scw ((struct rtc *)rtc)->rtc_data = value;
394 1.27.6.2 scw }
395 1.27.6.2 scw
396 1.27.6.2 scw /*
397 1.27.6.2 scw * Initialize the time of day register, assuming the RTC runs in UTC.
398 1.27.6.2 scw * Since we've got the 'rtc' device, this functionality should be removed
399 1.27.6.2 scw * from the kernel. The only problem to be solved before that can happen
400 1.27.6.2 scw * is the possibility of init(1) providing a way (rc.boot?) to set
401 1.27.6.2 scw * the RTC before single-user mode is entered.
402 1.27.6.2 scw */
403 1.27.6.2 scw void
404 1.27.6.2 scw inittodr(base)
405 1.27.6.2 scw time_t base;
406 1.27.6.2 scw {
407 1.27.6.2 scw /* Battery clock does not store usec's, so forget about it. */
408 1.27.6.2 scw time.tv_sec = gettod();
409 1.27.6.2 scw time.tv_usec = 0;
410 1.27.6.2 scw }
411 1.27.6.2 scw
412 1.27.6.2 scw /*
413 1.27.6.2 scw * Function turned into a No-op. Use /dev/rtc to update the RTC.
414 1.27.6.2 scw */
415 1.27.6.2 scw void
416 1.27.6.2 scw resettodr()
417 1.27.6.2 scw {
418 1.27.6.2 scw return;
419 1.27.6.2 scw }
420 1.27.6.2 scw
421 1.27.6.2 scw static u_long
422 1.27.6.2 scw gettod()
423 1.27.6.2 scw {
424 1.27.6.2 scw int sps;
425 1.27.6.2 scw mc_todregs clkregs;
426 1.27.6.2 scw u_int regb;
427 1.27.6.2 scw struct clock_ymdhms dt;
428 1.27.6.2 scw
429 1.27.6.2 scw sps = splhigh();
430 1.27.6.2 scw regb = mc146818_read(RTC, MC_REGB);
431 1.27.6.2 scw MC146818_GETTOD(RTC, &clkregs);
432 1.27.6.2 scw splx(sps);
433 1.27.6.2 scw
434 1.27.6.2 scw regb &= MC_REGB_24HR|MC_REGB_BINARY;
435 1.27.6.2 scw if (regb != (MC_REGB_24HR|MC_REGB_BINARY)) {
436 1.27.6.2 scw printf("Error: Nonstandard RealTimeClock Configuration -"
437 1.27.6.2 scw " value ignored\n"
438 1.27.6.2 scw " A write to /dev/rtc will correct this.\n");
439 1.27.6.2 scw return(0);
440 1.27.6.2 scw }
441 1.27.6.2 scw if(clkregs[MC_SEC] > 59)
442 1.27.6.2 scw return(0);
443 1.27.6.2 scw if(clkregs[MC_MIN] > 59)
444 1.27.6.2 scw return(0);
445 1.27.6.2 scw if(clkregs[MC_HOUR] > 23)
446 1.27.6.2 scw return(0);
447 1.27.6.2 scw if(range_test(clkregs[MC_DOM], 1, 31))
448 1.27.6.2 scw return(0);
449 1.27.6.2 scw if (range_test(clkregs[MC_MONTH], 1, 12))
450 1.27.6.2 scw return(0);
451 1.27.6.2 scw if(clkregs[MC_YEAR] > 99)
452 1.27.6.2 scw return(0);
453 1.27.6.2 scw
454 1.27.6.2 scw dt.dt_year = clkregs[MC_YEAR] + GEMSTARTOFTIME;
455 1.27.6.2 scw dt.dt_mon = clkregs[MC_MONTH];
456 1.27.6.2 scw dt.dt_day = clkregs[MC_DOM];
457 1.27.6.2 scw dt.dt_hour = clkregs[MC_HOUR];
458 1.27.6.2 scw dt.dt_min = clkregs[MC_MIN];
459 1.27.6.2 scw dt.dt_sec = clkregs[MC_SEC];
460 1.27.6.2 scw
461 1.27.6.2 scw return(clock_ymdhms_to_secs(&dt));
462 1.27.6.2 scw }
463 1.27.6.2 scw /***********************************************************************
464 1.27.6.2 scw * RTC-device support *
465 1.27.6.2 scw ***********************************************************************/
466 1.27.6.2 scw int
467 1.27.6.2 scw rtcopen(dev, flag, mode, p)
468 1.27.6.2 scw dev_t dev;
469 1.27.6.2 scw int flag, mode;
470 1.27.6.2 scw struct proc *p;
471 1.27.6.2 scw {
472 1.27.6.2 scw int unit = minor(dev);
473 1.27.6.2 scw struct clock_softc *sc;
474 1.27.6.2 scw
475 1.27.6.2 scw if (unit >= clock_cd.cd_ndevs)
476 1.27.6.2 scw return ENXIO;
477 1.27.6.2 scw sc = clock_cd.cd_devs[unit];
478 1.27.6.2 scw if (!sc)
479 1.27.6.2 scw return ENXIO;
480 1.27.6.2 scw if (sc->sc_flags & RTC_OPEN)
481 1.27.6.2 scw return EBUSY;
482 1.27.6.2 scw
483 1.27.6.2 scw sc->sc_flags = RTC_OPEN;
484 1.27.6.2 scw return 0;
485 1.27.6.2 scw }
486 1.27.6.2 scw
487 1.27.6.2 scw int
488 1.27.6.2 scw rtcclose(dev, flag, mode, p)
489 1.27.6.2 scw dev_t dev;
490 1.27.6.2 scw int flag;
491 1.27.6.2 scw int mode;
492 1.27.6.2 scw struct proc *p;
493 1.27.6.2 scw {
494 1.27.6.2 scw int unit = minor(dev);
495 1.27.6.2 scw struct clock_softc *sc = clock_cd.cd_devs[unit];
496 1.27.6.2 scw
497 1.27.6.2 scw sc->sc_flags = 0;
498 1.27.6.2 scw return 0;
499 1.27.6.2 scw }
500 1.27.6.2 scw
501 1.27.6.2 scw int
502 1.27.6.2 scw rtcread(dev, uio, flags)
503 1.27.6.2 scw dev_t dev;
504 1.27.6.2 scw struct uio *uio;
505 1.27.6.2 scw int flags;
506 1.27.6.2 scw {
507 1.27.6.2 scw struct clock_softc *sc;
508 1.27.6.2 scw mc_todregs clkregs;
509 1.27.6.2 scw int s, length;
510 1.27.6.2 scw char buffer[16];
511 1.27.6.2 scw
512 1.27.6.2 scw sc = clock_cd.cd_devs[minor(dev)];
513 1.27.6.2 scw
514 1.27.6.2 scw s = splhigh();
515 1.27.6.2 scw MC146818_GETTOD(RTC, &clkregs);
516 1.27.6.2 scw splx(s);
517 1.27.6.2 scw
518 1.27.6.2 scw sprintf(buffer, "%4d%02d%02d%02d%02d.%02d\n",
519 1.27.6.2 scw clkregs[MC_YEAR] + GEMSTARTOFTIME,
520 1.27.6.2 scw clkregs[MC_MONTH], clkregs[MC_DOM],
521 1.27.6.2 scw clkregs[MC_HOUR], clkregs[MC_MIN], clkregs[MC_SEC]);
522 1.27.6.2 scw
523 1.27.6.2 scw if (uio->uio_offset > strlen(buffer))
524 1.27.6.2 scw return 0;
525 1.27.6.2 scw
526 1.27.6.2 scw length = strlen(buffer) - uio->uio_offset;
527 1.27.6.2 scw if (length > uio->uio_resid)
528 1.27.6.2 scw length = uio->uio_resid;
529 1.27.6.2 scw
530 1.27.6.2 scw return(uiomove((caddr_t)buffer, length, uio));
531 1.27.6.2 scw }
532 1.27.6.2 scw
533 1.27.6.2 scw static int
534 1.27.6.2 scw twodigits(buffer, pos)
535 1.27.6.2 scw char *buffer;
536 1.27.6.2 scw int pos;
537 1.27.6.2 scw {
538 1.27.6.2 scw int result = 0;
539 1.27.6.2 scw
540 1.27.6.2 scw if (buffer[pos] >= '0' && buffer[pos] <= '9')
541 1.27.6.2 scw result = (buffer[pos] - '0') * 10;
542 1.27.6.2 scw if (buffer[pos+1] >= '0' && buffer[pos+1] <= '9')
543 1.27.6.2 scw result += (buffer[pos+1] - '0');
544 1.27.6.2 scw return(result);
545 1.27.6.2 scw }
546 1.27.6.2 scw
547 1.27.6.2 scw int
548 1.27.6.2 scw rtcwrite(dev, uio, flags)
549 1.27.6.2 scw dev_t dev;
550 1.27.6.2 scw struct uio *uio;
551 1.27.6.2 scw int flags;
552 1.27.6.2 scw {
553 1.27.6.2 scw mc_todregs clkregs;
554 1.27.6.2 scw int s, length, error;
555 1.27.6.2 scw char buffer[16];
556 1.27.6.2 scw
557 1.27.6.2 scw /*
558 1.27.6.2 scw * We require atomic updates!
559 1.27.6.2 scw */
560 1.27.6.2 scw length = uio->uio_resid;
561 1.27.6.2 scw if (uio->uio_offset || (length != sizeof(buffer)
562 1.27.6.2 scw && length != sizeof(buffer - 1)))
563 1.27.6.2 scw return(EINVAL);
564 1.27.6.2 scw
565 1.27.6.2 scw if ((error = uiomove((caddr_t)buffer, sizeof(buffer), uio)))
566 1.27.6.2 scw return(error);
567 1.27.6.2 scw
568 1.27.6.2 scw if (length == sizeof(buffer) && buffer[sizeof(buffer) - 1] != '\n')
569 1.27.6.2 scw return(EINVAL);
570 1.27.6.2 scw
571 1.27.6.2 scw s = splclock();
572 1.27.6.2 scw mc146818_write(RTC, MC_REGB,
573 1.27.6.2 scw mc146818_read(RTC, MC_REGB) | MC_REGB_24HR | MC_REGB_BINARY);
574 1.27.6.2 scw MC146818_GETTOD(RTC, &clkregs);
575 1.27.6.2 scw splx(s);
576 1.27.6.2 scw
577 1.27.6.2 scw clkregs[MC_SEC] = twodigits(buffer, 13);
578 1.27.6.2 scw clkregs[MC_MIN] = twodigits(buffer, 10);
579 1.27.6.2 scw clkregs[MC_HOUR] = twodigits(buffer, 8);
580 1.27.6.2 scw clkregs[MC_DOM] = twodigits(buffer, 6);
581 1.27.6.2 scw clkregs[MC_MONTH] = twodigits(buffer, 4);
582 1.27.6.2 scw s = twodigits(buffer, 0) * 100 + twodigits(buffer, 2);
583 1.27.6.2 scw clkregs[MC_YEAR] = s - GEMSTARTOFTIME;
584 1.27.6.2 scw
585 1.27.6.2 scw s = splclock();
586 1.27.6.2 scw MC146818_PUTTOD(RTC, &clkregs);
587 1.27.6.2 scw splx(s);
588 1.27.6.2 scw
589 1.27.6.2 scw return(0);
590 1.27.6.2 scw }
591