kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / misc / dec / mcclock.c
1 /* $FreeBSD: src/sys/dev/dec/mcclock.c,v 1.5.2.2 2001/12/17 14:03:15 gallatin Exp $ */
2 /* $DragonFly: src/sys/dev/misc/dec/Attic/mcclock.c,v 1.3 2003/08/07 21:16:56 dillon Exp $ */
3 /* $NetBSD: mcclock.c,v 1.11 1998/04/19 07:50:25 jonathan Exp $ */
4
5 /*
6  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
7  * All rights reserved.
8  *
9  * Author: Chris G. Demetriou
10  *
11  * Permission to use, copy, modify and distribute this software and
12  * its documentation is hereby granted, provided that both the copyright
13  * notice and this permission notice appear in all copies of the
14  * software, derivative works or modified versions, and any portions
15  * thereof, and that both notices appear in supporting documentation.
16  *
17  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
18  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
19  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20  *
21  * Carnegie Mellon requests users of this software to return to
22  *
23  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
24  *  School of Computer Science
25  *  Carnegie Mellon University
26  *  Pittsburgh PA 15213-3890
27  *
28  * any improvements or extensions that they make and grant Carnegie the
29  * rights to redistribute these changes.
30  */
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36
37 #include <machine/clockvar.h>
38 #include "mcclockvar.h"
39 #include "mc146818reg.h"
40
41 /*
42  * XXX rate is machine-dependent.
43  */
44 #ifdef __alpha__
45 #define MC_DEFAULTRATE MC_RATE_1024_Hz
46 #endif
47 #ifdef __pmax__
48 #define MC_DEFAULTRATE MC_RATE_256_Hz
49 #endif
50
51 void
52 mcclock_attach(device_t dev)
53 {
54         /* Turn interrupts off, just in case. */
55         MCCLOCK_WRITE(dev, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
56
57         clockattach(dev);
58 }
59
60 void
61 mcclock_init(device_t dev)
62 {
63         MCCLOCK_WRITE(dev, MC_REGA, MC_BASE_32_KHz | MC_DEFAULTRATE);
64         MCCLOCK_WRITE(dev, MC_REGB,
65             MC_REGB_PIE | MC_REGB_SQWE | MC_REGB_BINARY | MC_REGB_24HR);
66 }
67
68 /*
69  * Get the time of day, based on the clock's value and/or the base value.
70  */
71 void
72 mcclock_get(device_t dev, time_t base, struct clocktime *ct)
73 {
74         mc_todregs regs;
75         int s;
76
77         s = splclock();
78         MC146818_GETTOD(dev, &regs)
79         splx(s);
80
81         ct->sec = regs[MC_SEC];
82         ct->min = regs[MC_MIN];
83         ct->hour = regs[MC_HOUR];
84         ct->dow = regs[MC_DOW];
85         ct->day = regs[MC_DOM];
86         ct->mon = regs[MC_MONTH];
87         ct->year = regs[MC_YEAR];
88 }
89
90 /*
91  * Reset the TODR based on the time value.
92  */
93 void
94 mcclock_set(device_t dev, struct clocktime *ct)
95 {
96         mc_todregs regs;
97         int s;
98
99         s = splclock();
100         MC146818_GETTOD(dev, &regs);
101         splx(s);
102
103         regs[MC_SEC] = ct->sec;
104         regs[MC_MIN] = ct->min;
105         regs[MC_HOUR] = ct->hour;
106         regs[MC_DOW] = ct->dow;
107         regs[MC_DOM] = ct->day;
108         regs[MC_MONTH] = ct->mon;
109         regs[MC_YEAR] = ct->year;
110
111         s = splclock();
112         MC146818_PUTTOD(dev, &regs);
113         splx(s);
114 }
115
116 int
117 mcclock_getsecs(device_t dev, int *secp)
118 {
119         int timeout = 100000000;
120         int sec;
121         int s;
122
123         s = splclock();
124         for (;;) {
125                 if (!(MCCLOCK_READ(dev, MC_REGA) & MC_REGA_UIP)) {
126                         sec = MCCLOCK_READ(dev, MC_SEC);
127                         break;
128                 }
129                 if (--timeout == 0)
130                         goto fail;
131         }
132
133         splx(s);
134         *secp = sec;
135         return 0;
136
137  fail:
138         splx(s);
139         return ETIMEDOUT;
140 }