clock.c revision 1.27.6.4 1 1.27.6.4 nathanw /* $NetBSD: clock.c,v 1.27.6.4 2002/07/12 01:39:26 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 /* {b,c}devsw[] function prototypes for rtc functions */
93 1.27.6.2 scw dev_type_open(rtcopen);
94 1.27.6.2 scw dev_type_close(rtcclose);
95 1.27.6.2 scw dev_type_read(rtcread);
96 1.27.6.2 scw dev_type_write(rtcwrite);
97 1.27.6.2 scw
98 1.27.6.2 scw static void clockattach __P((struct device *, struct device *, void *));
99 1.27.6.2 scw static int clockmatch __P((struct device *, struct cfdata *, void *));
100 1.27.6.2 scw
101 1.27.6.2 scw struct cfattach clock_ca = {
102 1.27.6.2 scw sizeof(struct clock_softc), clockmatch, clockattach
103 1.27.6.2 scw };
104 1.27.6.2 scw
105 1.27.6.2 scw extern struct cfdriver clock_cd;
106 1.27.6.2 scw
107 1.27.6.2 scw void statintr __P((struct clockframe));
108 1.27.6.2 scw
109 1.27.6.2 scw static u_long gettod __P((void));
110 1.27.6.2 scw static int twodigits __P((char *, int));
111 1.27.6.2 scw
112 1.27.6.2 scw static int divisor; /* Systemclock divisor */
113 1.27.6.2 scw
114 1.27.6.2 scw /*
115 1.27.6.2 scw * Statistics and profile clock intervals and variances. Variance must
116 1.27.6.2 scw * be a power of 2. Since this gives us an even number, not an odd number,
117 1.27.6.2 scw * we discard one case and compensate. That is, a variance of 64 would
118 1.27.6.2 scw * give us offsets in [0..63]. Instead, we take offsets in [1..63].
119 1.27.6.2 scw * This is symmetric around the point 32, or statvar/2, and thus averages
120 1.27.6.2 scw * to that value (assuming uniform random numbers).
121 1.27.6.2 scw */
122 1.27.6.2 scw #ifdef STATCLOCK
123 1.27.6.2 scw static int statvar = 32; /* {stat,prof}clock variance */
124 1.27.6.2 scw static int statmin; /* statclock divisor - variance/2 */
125 1.27.6.2 scw static int profmin; /* profclock divisor - variance/2 */
126 1.27.6.2 scw static int clk2min; /* current, from above choices */
127 1.27.6.2 scw #endif
128 1.27.6.2 scw
129 1.27.6.2 scw int
130 1.27.6.2 scw clockmatch(pdp, cfp, auxp)
131 1.27.6.2 scw struct device *pdp;
132 1.27.6.2 scw struct cfdata *cfp;
133 1.27.6.2 scw void *auxp;
134 1.27.6.2 scw {
135 1.27.6.2 scw if (!atari_realconfig) {
136 1.27.6.2 scw /*
137 1.27.6.2 scw * Initialize Timer-B in the ST-MFP. This timer is used by
138 1.27.6.2 scw * the 'delay' function below. This timer is setup to be
139 1.27.6.2 scw * continueously counting from 255 back to zero at a
140 1.27.6.2 scw * frequency of 614400Hz. We do this *early* in the
141 1.27.6.2 scw * initialisation process.
142 1.27.6.2 scw */
143 1.27.6.2 scw MFP->mf_tbcr = 0; /* Stop timer */
144 1.27.6.2 scw MFP->mf_iera &= ~IA_TIMB; /* Disable timer interrupts */
145 1.27.6.2 scw MFP->mf_tbdr = 0;
146 1.27.6.2 scw MFP->mf_tbcr = T_Q004; /* Start timer */
147 1.27.6.2 scw
148 1.27.6.2 scw /*
149 1.27.6.2 scw * Initialize the time structure
150 1.27.6.2 scw */
151 1.27.6.2 scw time.tv_sec = 0;
152 1.27.6.2 scw time.tv_usec = 0;
153 1.27.6.2 scw
154 1.27.6.2 scw return 0;
155 1.27.6.2 scw }
156 1.27.6.2 scw if(!strcmp("clock", auxp))
157 1.27.6.2 scw return(1);
158 1.27.6.2 scw return(0);
159 1.27.6.2 scw }
160 1.27.6.2 scw
161 1.27.6.2 scw /*
162 1.27.6.2 scw * Start the real-time clock.
163 1.27.6.2 scw */
164 1.27.6.2 scw void clockattach(pdp, dp, auxp)
165 1.27.6.2 scw struct device *pdp, *dp;
166 1.27.6.2 scw void *auxp;
167 1.27.6.2 scw {
168 1.27.6.2 scw struct clock_softc *sc = (void *)dp;
169 1.27.6.2 scw
170 1.27.6.2 scw sc->sc_flags = 0;
171 1.27.6.2 scw
172 1.27.6.2 scw /*
173 1.27.6.2 scw * Initialize Timer-A in the ST-MFP. We use a divisor of 200.
174 1.27.6.2 scw * The MFP clock runs at 2457600Hz. Therefore the timer runs
175 1.27.6.2 scw * at an effective rate of: 2457600/200 = 12288Hz. The
176 1.27.6.2 scw * following expression works for 48, 64 or 96 hz.
177 1.27.6.2 scw */
178 1.27.6.2 scw divisor = CLOCK_HZ/hz;
179 1.27.6.2 scw MFP->mf_tacr = 0; /* Stop timer */
180 1.27.6.2 scw MFP->mf_iera &= ~IA_TIMA; /* Disable timer interrupts */
181 1.27.6.2 scw MFP->mf_tadr = divisor; /* Set divisor */
182 1.27.6.2 scw
183 1.27.6.2 scw if (hz != 48 && hz != 64 && hz != 96) { /* XXX */
184 1.27.6.2 scw printf (": illegal value %d for systemclock, reset to %d\n\t",
185 1.27.6.2 scw hz, 64);
186 1.27.6.2 scw hz = 64;
187 1.27.6.2 scw }
188 1.27.6.2 scw printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor);
189 1.27.6.2 scw
190 1.27.6.2 scw #ifdef STATCLOCK
191 1.27.6.2 scw if ((stathz == 0) || (stathz > hz) || (CLOCK_HZ % stathz))
192 1.27.6.2 scw stathz = hz;
193 1.27.6.2 scw if ((profhz == 0) || (profhz > (hz << 1)) || (CLOCK_HZ % profhz))
194 1.27.6.2 scw profhz = hz << 1;
195 1.27.6.2 scw
196 1.27.6.2 scw MFP->mf_tcdcr &= 0x7; /* Stop timer */
197 1.27.6.2 scw MFP->mf_ierb &= ~IB_TIMC; /* Disable timer inter. */
198 1.27.6.2 scw MFP->mf_tcdr = CLOCK_HZ/stathz; /* Set divisor */
199 1.27.6.2 scw
200 1.27.6.2 scw statmin = (CLOCK_HZ/stathz) - (statvar >> 1);
201 1.27.6.2 scw profmin = (CLOCK_HZ/profhz) - (statvar >> 1);
202 1.27.6.2 scw clk2min = statmin;
203 1.27.6.2 scw #endif /* STATCLOCK */
204 1.27.6.2 scw
205 1.27.6.2 scw }
206 1.27.6.2 scw
207 1.27.6.2 scw void cpu_initclocks()
208 1.27.6.2 scw {
209 1.27.6.2 scw MFP->mf_tacr = T_Q200; /* Start timer */
210 1.27.6.2 scw MFP->mf_ipra = (u_int8_t)~IA_TIMA;/* Clear pending interrupts */
211 1.27.6.2 scw MFP->mf_iera |= IA_TIMA; /* Enable timer interrupts */
212 1.27.6.2 scw MFP->mf_imra |= IA_TIMA; /* ..... */
213 1.27.6.2 scw
214 1.27.6.2 scw #ifdef STATCLOCK
215 1.27.6.2 scw MFP->mf_tcdcr = (MFP->mf_tcdcr & 0x7) | (T_Q200<<4); /* Start */
216 1.27.6.2 scw MFP->mf_iprb = (u_int8_t)~IB_TIMC;/* Clear pending interrupts */
217 1.27.6.2 scw MFP->mf_ierb |= IB_TIMC; /* Enable timer interrupts */
218 1.27.6.2 scw MFP->mf_imrb |= IB_TIMC; /* ..... */
219 1.27.6.2 scw #endif /* STATCLOCK */
220 1.27.6.2 scw }
221 1.27.6.2 scw
222 1.27.6.2 scw void
223 1.27.6.2 scw setstatclockrate(newhz)
224 1.27.6.2 scw int newhz;
225 1.27.6.2 scw {
226 1.27.6.2 scw #ifdef STATCLOCK
227 1.27.6.2 scw if (newhz == stathz)
228 1.27.6.2 scw clk2min = statmin;
229 1.27.6.2 scw else clk2min = profmin;
230 1.27.6.2 scw #endif /* STATCLOCK */
231 1.27.6.2 scw }
232 1.27.6.2 scw
233 1.27.6.2 scw #ifdef STATCLOCK
234 1.27.6.2 scw void
235 1.27.6.2 scw statintr(frame)
236 1.27.6.2 scw struct clockframe frame;
237 1.27.6.2 scw {
238 1.27.6.2 scw register int var, r;
239 1.27.6.2 scw
240 1.27.6.2 scw var = statvar - 1;
241 1.27.6.2 scw do {
242 1.27.6.2 scw r = random() & var;
243 1.27.6.2 scw } while(r == 0);
244 1.27.6.2 scw
245 1.27.6.2 scw /*
246 1.27.6.2 scw * Note that we are always lagging behind as the new divisor
247 1.27.6.2 scw * value will not be loaded until the next interrupt. This
248 1.27.6.2 scw * shouldn't disturb the median frequency (I think ;-) ) as
249 1.27.6.2 scw * only the value used when switching frequencies is used
250 1.27.6.2 scw * twice. This shouldn't happen very often.
251 1.27.6.2 scw */
252 1.27.6.2 scw MFP->mf_tcdr = clk2min + r;
253 1.27.6.2 scw
254 1.27.6.2 scw statclock(&frame);
255 1.27.6.2 scw }
256 1.27.6.2 scw #endif /* STATCLOCK */
257 1.27.6.2 scw
258 1.27.6.2 scw /*
259 1.27.6.2 scw * Returns number of usec since last recorded clock "tick"
260 1.27.6.2 scw * (i.e. clock interrupt).
261 1.27.6.2 scw */
262 1.27.6.2 scw long
263 1.27.6.2 scw clkread()
264 1.27.6.2 scw {
265 1.27.6.2 scw u_int delta;
266 1.27.6.2 scw u_char ipra, tadr;
267 1.27.6.2 scw
268 1.27.6.2 scw /*
269 1.27.6.2 scw * Note: Order is important!
270 1.27.6.2 scw * By reading 'ipra' before 'tadr' and caching the data, I try to avoid
271 1.27.6.2 scw * the situation that very low value in 'tadr' is read (== a big delta)
272 1.27.6.2 scw * while also acccounting for a full 'tick' because the counter went
273 1.27.6.2 scw * through zero during the calculations.
274 1.27.6.2 scw */
275 1.27.6.2 scw ipra = MFP->mf_ipra; tadr = MFP->mf_tadr;
276 1.27.6.2 scw
277 1.27.6.2 scw delta = ((divisor - tadr) * tick) / divisor;
278 1.27.6.2 scw /*
279 1.27.6.2 scw * Account for pending clock interrupts
280 1.27.6.2 scw */
281 1.27.6.2 scw if(ipra & IA_TIMA)
282 1.27.6.2 scw return(delta + tick);
283 1.27.6.2 scw return(delta);
284 1.27.6.2 scw }
285 1.27.6.2 scw
286 1.27.6.2 scw #define TIMB_FREQ 614400
287 1.27.6.2 scw #define TIMB_LIMIT 256
288 1.27.6.2 scw
289 1.27.6.2 scw /*
290 1.27.6.2 scw * Wait "n" microseconds.
291 1.27.6.2 scw * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz.
292 1.27.6.2 scw * Note: timer had better have been programmed before this is first used!
293 1.27.6.2 scw */
294 1.27.6.2 scw void
295 1.27.6.2 scw delay(n)
296 1.27.6.2 scw int n;
297 1.27.6.2 scw {
298 1.27.6.2 scw int tick, otick;
299 1.27.6.2 scw
300 1.27.6.2 scw /*
301 1.27.6.2 scw * Read the counter first, so that the rest of the setup overhead is
302 1.27.6.2 scw * counted.
303 1.27.6.2 scw */
304 1.27.6.2 scw otick = MFP->mf_tbdr;
305 1.27.6.2 scw
306 1.27.6.2 scw /*
307 1.27.6.2 scw * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler code so
308 1.27.6.2 scw * we can take advantage of the intermediate 64-bit quantity to prevent
309 1.27.6.2 scw * loss of significance.
310 1.27.6.2 scw */
311 1.27.6.2 scw n -= 5;
312 1.27.6.2 scw if(n < 0)
313 1.27.6.2 scw return;
314 1.27.6.2 scw {
315 1.27.6.2 scw u_int temp;
316 1.27.6.2 scw
317 1.27.6.2 scw __asm __volatile ("mulul %2,%1:%0" : "=d" (n), "=d" (temp)
318 1.27.6.2 scw : "d" (TIMB_FREQ), "d" (n));
319 1.27.6.2 scw __asm __volatile ("divul %1,%2:%0" : "=d" (n)
320 1.27.6.2 scw : "d"(1000000),"d"(temp),"0"(n));
321 1.27.6.2 scw }
322 1.27.6.2 scw
323 1.27.6.2 scw while(n > 0) {
324 1.27.6.2 scw tick = MFP->mf_tbdr;
325 1.27.6.2 scw if(tick > otick)
326 1.27.6.2 scw n -= TIMB_LIMIT - (tick - otick);
327 1.27.6.2 scw else n -= otick - tick;
328 1.27.6.2 scw otick = tick;
329 1.27.6.2 scw }
330 1.27.6.2 scw }
331 1.27.6.2 scw
332 1.27.6.2 scw #ifdef GPROF
333 1.27.6.2 scw /*
334 1.27.6.2 scw * profclock() is expanded in line in lev6intr() unless profiling kernel.
335 1.27.6.2 scw * Assumes it is called with clock interrupts blocked.
336 1.27.6.2 scw */
337 1.27.6.2 scw profclock(pc, ps)
338 1.27.6.2 scw caddr_t pc;
339 1.27.6.2 scw int ps;
340 1.27.6.2 scw {
341 1.27.6.2 scw /*
342 1.27.6.2 scw * Came from user mode.
343 1.27.6.2 scw * If this process is being profiled record the tick.
344 1.27.6.2 scw */
345 1.27.6.2 scw if (USERMODE(ps)) {
346 1.27.6.2 scw if (p->p_stats.p_prof.pr_scale)
347 1.27.6.3 nathanw addupc(pc, &curproc->p_stats.p_prof, 1);
348 1.27.6.2 scw }
349 1.27.6.2 scw /*
350 1.27.6.2 scw * Came from kernel (supervisor) mode.
351 1.27.6.2 scw * If we are profiling the kernel, record the tick.
352 1.27.6.2 scw */
353 1.27.6.2 scw else if (profiling < 2) {
354 1.27.6.2 scw register int s = pc - s_lowpc;
355 1.27.6.2 scw
356 1.27.6.2 scw if (s < s_textsize)
357 1.27.6.2 scw kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
358 1.27.6.2 scw }
359 1.27.6.2 scw /*
360 1.27.6.2 scw * Kernel profiling was on but has been disabled.
361 1.27.6.2 scw * Mark as no longer profiling kernel and if all profiling done,
362 1.27.6.2 scw * disable the clock.
363 1.27.6.2 scw */
364 1.27.6.2 scw if (profiling && (profon & PRF_KERNEL)) {
365 1.27.6.2 scw profon &= ~PRF_KERNEL;
366 1.27.6.2 scw if (profon == PRF_NONE)
367 1.27.6.2 scw stopprofclock();
368 1.27.6.2 scw }
369 1.27.6.2 scw }
370 1.27.6.2 scw #endif
371 1.27.6.2 scw
372 1.27.6.2 scw /***********************************************************************
373 1.27.6.2 scw * Real Time Clock support *
374 1.27.6.2 scw ***********************************************************************/
375 1.27.6.2 scw
376 1.27.6.2 scw u_int mc146818_read(rtc, regno)
377 1.27.6.2 scw void *rtc;
378 1.27.6.2 scw u_int regno;
379 1.27.6.2 scw {
380 1.27.6.2 scw ((struct rtc *)rtc)->rtc_regno = regno;
381 1.27.6.2 scw return(((struct rtc *)rtc)->rtc_data & 0377);
382 1.27.6.2 scw }
383 1.27.6.2 scw
384 1.27.6.2 scw void mc146818_write(rtc, regno, value)
385 1.27.6.2 scw void *rtc;
386 1.27.6.2 scw u_int regno, value;
387 1.27.6.2 scw {
388 1.27.6.2 scw ((struct rtc *)rtc)->rtc_regno = regno;
389 1.27.6.2 scw ((struct rtc *)rtc)->rtc_data = value;
390 1.27.6.2 scw }
391 1.27.6.2 scw
392 1.27.6.2 scw /*
393 1.27.6.2 scw * Initialize the time of day register, assuming the RTC runs in UTC.
394 1.27.6.2 scw * Since we've got the 'rtc' device, this functionality should be removed
395 1.27.6.2 scw * from the kernel. The only problem to be solved before that can happen
396 1.27.6.2 scw * is the possibility of init(1) providing a way (rc.boot?) to set
397 1.27.6.2 scw * the RTC before single-user mode is entered.
398 1.27.6.2 scw */
399 1.27.6.2 scw void
400 1.27.6.2 scw inittodr(base)
401 1.27.6.2 scw time_t base;
402 1.27.6.2 scw {
403 1.27.6.2 scw /* Battery clock does not store usec's, so forget about it. */
404 1.27.6.2 scw time.tv_sec = gettod();
405 1.27.6.2 scw time.tv_usec = 0;
406 1.27.6.2 scw }
407 1.27.6.2 scw
408 1.27.6.2 scw /*
409 1.27.6.2 scw * Function turned into a No-op. Use /dev/rtc to update the RTC.
410 1.27.6.2 scw */
411 1.27.6.2 scw void
412 1.27.6.2 scw resettodr()
413 1.27.6.2 scw {
414 1.27.6.2 scw return;
415 1.27.6.2 scw }
416 1.27.6.2 scw
417 1.27.6.2 scw static u_long
418 1.27.6.2 scw gettod()
419 1.27.6.2 scw {
420 1.27.6.2 scw int sps;
421 1.27.6.2 scw mc_todregs clkregs;
422 1.27.6.2 scw u_int regb;
423 1.27.6.2 scw struct clock_ymdhms dt;
424 1.27.6.2 scw
425 1.27.6.2 scw sps = splhigh();
426 1.27.6.2 scw regb = mc146818_read(RTC, MC_REGB);
427 1.27.6.2 scw MC146818_GETTOD(RTC, &clkregs);
428 1.27.6.2 scw splx(sps);
429 1.27.6.2 scw
430 1.27.6.2 scw regb &= MC_REGB_24HR|MC_REGB_BINARY;
431 1.27.6.2 scw if (regb != (MC_REGB_24HR|MC_REGB_BINARY)) {
432 1.27.6.2 scw printf("Error: Nonstandard RealTimeClock Configuration -"
433 1.27.6.2 scw " value ignored\n"
434 1.27.6.2 scw " A write to /dev/rtc will correct this.\n");
435 1.27.6.2 scw return(0);
436 1.27.6.2 scw }
437 1.27.6.2 scw if(clkregs[MC_SEC] > 59)
438 1.27.6.2 scw return(0);
439 1.27.6.2 scw if(clkregs[MC_MIN] > 59)
440 1.27.6.2 scw return(0);
441 1.27.6.2 scw if(clkregs[MC_HOUR] > 23)
442 1.27.6.2 scw return(0);
443 1.27.6.2 scw if(range_test(clkregs[MC_DOM], 1, 31))
444 1.27.6.2 scw return(0);
445 1.27.6.2 scw if (range_test(clkregs[MC_MONTH], 1, 12))
446 1.27.6.2 scw return(0);
447 1.27.6.2 scw if(clkregs[MC_YEAR] > 99)
448 1.27.6.2 scw return(0);
449 1.27.6.2 scw
450 1.27.6.2 scw dt.dt_year = clkregs[MC_YEAR] + GEMSTARTOFTIME;
451 1.27.6.2 scw dt.dt_mon = clkregs[MC_MONTH];
452 1.27.6.2 scw dt.dt_day = clkregs[MC_DOM];
453 1.27.6.2 scw dt.dt_hour = clkregs[MC_HOUR];
454 1.27.6.2 scw dt.dt_min = clkregs[MC_MIN];
455 1.27.6.2 scw dt.dt_sec = clkregs[MC_SEC];
456 1.27.6.2 scw
457 1.27.6.2 scw return(clock_ymdhms_to_secs(&dt));
458 1.27.6.2 scw }
459 1.27.6.2 scw /***********************************************************************
460 1.27.6.2 scw * RTC-device support *
461 1.27.6.2 scw ***********************************************************************/
462 1.27.6.2 scw int
463 1.27.6.2 scw rtcopen(dev, flag, mode, p)
464 1.27.6.2 scw dev_t dev;
465 1.27.6.2 scw int flag, mode;
466 1.27.6.2 scw struct proc *p;
467 1.27.6.2 scw {
468 1.27.6.2 scw int unit = minor(dev);
469 1.27.6.2 scw struct clock_softc *sc;
470 1.27.6.2 scw
471 1.27.6.2 scw if (unit >= clock_cd.cd_ndevs)
472 1.27.6.2 scw return ENXIO;
473 1.27.6.2 scw sc = clock_cd.cd_devs[unit];
474 1.27.6.2 scw if (!sc)
475 1.27.6.2 scw return ENXIO;
476 1.27.6.2 scw if (sc->sc_flags & RTC_OPEN)
477 1.27.6.2 scw return EBUSY;
478 1.27.6.2 scw
479 1.27.6.2 scw sc->sc_flags = RTC_OPEN;
480 1.27.6.2 scw return 0;
481 1.27.6.2 scw }
482 1.27.6.2 scw
483 1.27.6.2 scw int
484 1.27.6.2 scw rtcclose(dev, flag, mode, p)
485 1.27.6.2 scw dev_t dev;
486 1.27.6.2 scw int flag;
487 1.27.6.2 scw int mode;
488 1.27.6.2 scw struct proc *p;
489 1.27.6.2 scw {
490 1.27.6.2 scw int unit = minor(dev);
491 1.27.6.2 scw struct clock_softc *sc = clock_cd.cd_devs[unit];
492 1.27.6.2 scw
493 1.27.6.2 scw sc->sc_flags = 0;
494 1.27.6.2 scw return 0;
495 1.27.6.2 scw }
496 1.27.6.2 scw
497 1.27.6.2 scw int
498 1.27.6.2 scw rtcread(dev, uio, flags)
499 1.27.6.2 scw dev_t dev;
500 1.27.6.2 scw struct uio *uio;
501 1.27.6.2 scw int flags;
502 1.27.6.2 scw {
503 1.27.6.2 scw struct clock_softc *sc;
504 1.27.6.2 scw mc_todregs clkregs;
505 1.27.6.2 scw int s, length;
506 1.27.6.2 scw char buffer[16];
507 1.27.6.2 scw
508 1.27.6.2 scw sc = clock_cd.cd_devs[minor(dev)];
509 1.27.6.2 scw
510 1.27.6.2 scw s = splhigh();
511 1.27.6.2 scw MC146818_GETTOD(RTC, &clkregs);
512 1.27.6.2 scw splx(s);
513 1.27.6.2 scw
514 1.27.6.2 scw sprintf(buffer, "%4d%02d%02d%02d%02d.%02d\n",
515 1.27.6.2 scw clkregs[MC_YEAR] + GEMSTARTOFTIME,
516 1.27.6.2 scw clkregs[MC_MONTH], clkregs[MC_DOM],
517 1.27.6.2 scw clkregs[MC_HOUR], clkregs[MC_MIN], clkregs[MC_SEC]);
518 1.27.6.2 scw
519 1.27.6.2 scw if (uio->uio_offset > strlen(buffer))
520 1.27.6.2 scw return 0;
521 1.27.6.2 scw
522 1.27.6.2 scw length = strlen(buffer) - uio->uio_offset;
523 1.27.6.2 scw if (length > uio->uio_resid)
524 1.27.6.2 scw length = uio->uio_resid;
525 1.27.6.2 scw
526 1.27.6.2 scw return(uiomove((caddr_t)buffer, length, uio));
527 1.27.6.2 scw }
528 1.27.6.2 scw
529 1.27.6.2 scw static int
530 1.27.6.2 scw twodigits(buffer, pos)
531 1.27.6.2 scw char *buffer;
532 1.27.6.2 scw int pos;
533 1.27.6.2 scw {
534 1.27.6.2 scw int result = 0;
535 1.27.6.2 scw
536 1.27.6.2 scw if (buffer[pos] >= '0' && buffer[pos] <= '9')
537 1.27.6.2 scw result = (buffer[pos] - '0') * 10;
538 1.27.6.2 scw if (buffer[pos+1] >= '0' && buffer[pos+1] <= '9')
539 1.27.6.2 scw result += (buffer[pos+1] - '0');
540 1.27.6.2 scw return(result);
541 1.27.6.2 scw }
542 1.27.6.2 scw
543 1.27.6.2 scw int
544 1.27.6.2 scw rtcwrite(dev, uio, flags)
545 1.27.6.2 scw dev_t dev;
546 1.27.6.2 scw struct uio *uio;
547 1.27.6.2 scw int flags;
548 1.27.6.2 scw {
549 1.27.6.2 scw mc_todregs clkregs;
550 1.27.6.2 scw int s, length, error;
551 1.27.6.2 scw char buffer[16];
552 1.27.6.2 scw
553 1.27.6.2 scw /*
554 1.27.6.2 scw * We require atomic updates!
555 1.27.6.2 scw */
556 1.27.6.2 scw length = uio->uio_resid;
557 1.27.6.2 scw if (uio->uio_offset || (length != sizeof(buffer)
558 1.27.6.2 scw && length != sizeof(buffer - 1)))
559 1.27.6.2 scw return(EINVAL);
560 1.27.6.2 scw
561 1.27.6.2 scw if ((error = uiomove((caddr_t)buffer, sizeof(buffer), uio)))
562 1.27.6.2 scw return(error);
563 1.27.6.2 scw
564 1.27.6.2 scw if (length == sizeof(buffer) && buffer[sizeof(buffer) - 1] != '\n')
565 1.27.6.2 scw return(EINVAL);
566 1.27.6.2 scw
567 1.27.6.2 scw s = splclock();
568 1.27.6.2 scw mc146818_write(RTC, MC_REGB,
569 1.27.6.2 scw mc146818_read(RTC, MC_REGB) | MC_REGB_24HR | MC_REGB_BINARY);
570 1.27.6.2 scw MC146818_GETTOD(RTC, &clkregs);
571 1.27.6.2 scw splx(s);
572 1.27.6.2 scw
573 1.27.6.2 scw clkregs[MC_SEC] = twodigits(buffer, 13);
574 1.27.6.2 scw clkregs[MC_MIN] = twodigits(buffer, 10);
575 1.27.6.2 scw clkregs[MC_HOUR] = twodigits(buffer, 8);
576 1.27.6.2 scw clkregs[MC_DOM] = twodigits(buffer, 6);
577 1.27.6.2 scw clkregs[MC_MONTH] = twodigits(buffer, 4);
578 1.27.6.2 scw s = twodigits(buffer, 0) * 100 + twodigits(buffer, 2);
579 1.27.6.2 scw clkregs[MC_YEAR] = s - GEMSTARTOFTIME;
580 1.27.6.2 scw
581 1.27.6.2 scw s = splclock();
582 1.27.6.2 scw MC146818_PUTTOD(RTC, &clkregs);
583 1.27.6.2 scw splx(s);
584 1.27.6.2 scw
585 1.27.6.2 scw return(0);
586 1.27.6.2 scw }
587