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