Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / dec / mcclock.c
blobc02531ab4df63683b6afac9235d963d93331b773
1 /* $NetBSD: mcclock.c,v 1.23 2009/05/12 14:18:16 cegger Exp $ */
3 /*
4 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
7 * Author: Chris G. Demetriou
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: mcclock.c,v 1.23 2009/05/12 14:18:16 cegger Exp $");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <dev/clock_subr.h>
39 #include <dev/dec/clockvar.h>
40 #include <dev/dec/mcclockvar.h>
41 #include <dev/ic/mc146818reg.h>
44 * XXX default rate is machine-dependent.
46 #ifdef __alpha__
47 #define MC_DFEAULTHZ 1024
48 #endif
49 #ifdef pmax
50 #define MC_DEFAULTHZ 256
51 #endif
54 void mcclock_init(device_t);
55 int mcclock_get(todr_chip_handle_t, struct timeval *);
56 int mcclock_set(todr_chip_handle_t, struct timeval *);
58 const struct clockfns mcclock_clockfns = {
59 mcclock_init,
62 #define mc146818_write(dev, reg, datum) \
63 (*(dev)->sc_busfns->mc_bf_write)(dev, reg, datum)
64 #define mc146818_read(dev, reg) \
65 (*(dev)->sc_busfns->mc_bf_read)(dev, reg)
67 void
68 mcclock_attach(struct mcclock_softc *sc, const struct mcclock_busfns *busfns)
71 printf(": mc146818 or compatible");
73 sc->sc_busfns = busfns;
75 /* Turn interrupts off, just in case. */
76 mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
78 clockattach(&sc->sc_dev, &mcclock_clockfns);
80 sc->sc_todr.todr_gettime = mcclock_get;
81 sc->sc_todr.todr_settime = mcclock_set;
82 sc->sc_todr.cookie = sc;
83 todr_attach(&sc->sc_todr);
86 void
87 mcclock_init(device_t dev)
89 struct mcclock_softc *sc = (struct mcclock_softc *)dev;
90 int rate;
92 again:
93 switch (hz) {
94 case 32:
95 rate = MC_BASE_32_KHz | MC_RATE_32_Hz;
96 break;
97 case 64:
98 rate = MC_BASE_32_KHz | MC_RATE_64_Hz;
99 break;
100 case 128:
101 rate = MC_BASE_32_KHz | MC_RATE_128_Hz;
102 break;
103 case 256:
104 rate = MC_BASE_32_KHz | MC_RATE_256_Hz;
105 break;
106 case 512:
107 rate = MC_BASE_32_KHz | MC_RATE_512_Hz;
108 break;
109 case 1024:
110 rate = MC_BASE_32_KHz | MC_RATE_1024_Hz;
111 break;
112 case 2048:
113 rate = MC_BASE_32_KHz | MC_RATE_2048_Hz;
114 break;
115 case 4096:
116 rate = MC_BASE_32_KHz | MC_RATE_4096_Hz;
117 break;
118 case 8192:
119 rate = MC_BASE_32_KHz | MC_RATE_8192_Hz;
120 break;
121 case 16384:
122 rate = MC_BASE_4_MHz | MC_RATE_1;
123 break;
124 case 32768:
125 rate = MC_BASE_4_MHz | MC_RATE_2;
126 break;
127 default:
128 printf("%s: Cannot get %d Hz clock; using %d Hz\n",
129 device_xname(&sc->sc_dev), hz, MC_DEFAULTHZ);
130 hz = MC_DEFAULTHZ;
131 goto again;
133 mc146818_write(sc, MC_REGA, rate);
134 mc146818_write(sc, MC_REGB,
135 MC_REGB_PIE | MC_REGB_SQWE | MC_REGB_BINARY | MC_REGB_24HR);
139 * Experiments (and passing years) show that Decstation PROMS
140 * assume the kernel uses the clock chip as a time-of-year clock.
141 * The PROM assumes the clock is always set to 1972 or 1973, and contains
142 * time-of-year in seconds. The PROM checks the clock at boot time,
143 * and if it's outside that range, sets it to 1972-01-01.
145 * XXX should be at the mc146818 layer?
149 * Get the time of day, based on the clock's value and/or the base value.
152 mcclock_get(todr_chip_handle_t tch, struct timeval *tvp)
154 struct mcclock_softc *sc = (struct mcclock_softc *)tch->cookie;
155 uint32_t yearsecs;
156 mc_todregs regs;
157 int s;
158 struct clock_ymdhms dt;
160 s = splclock();
161 MC146818_GETTOD(sc, &regs)
162 splx(s);
164 dt.dt_sec = regs[MC_SEC];
165 dt.dt_min = regs[MC_MIN];
166 dt.dt_hour = regs[MC_HOUR];
167 dt.dt_day = regs[MC_DOM];
168 dt.dt_mon = regs[MC_MONTH];
169 dt.dt_year = 1972;
171 yearsecs = clock_ymdhms_to_secs(&dt) - (72 - 70) * SECYR;
174 * Take the actual year from the filesystem if possible;
175 * allow for 2 days of clock loss and 363 days of clock gain.
177 dt.dt_year = 1972; /* or MINYEAR or base/SECYR+1970 ... */
178 dt.dt_mon = 1;
179 dt.dt_day = 1;
180 dt.dt_hour = 0;
181 dt.dt_min = 0;
182 dt.dt_sec = 0;
183 for(;;) {
184 tvp->tv_sec = yearsecs + clock_ymdhms_to_secs(&dt);
185 if (tvp->tv_sec > tch->base_time - 2 * SECDAY)
186 break;
187 dt.dt_year++;
190 tvp->tv_usec = 0;
191 return 0;
195 * Reset the TODR based on the time value.
198 mcclock_set(todr_chip_handle_t tch, struct timeval *tvp)
200 struct mcclock_softc *sc = (struct mcclock_softc *)tch->cookie;
201 struct clock_ymdhms dt;
202 uint32_t yearsecs;
203 mc_todregs regs;
204 int s;
207 * calculate seconds relative to this year
209 clock_secs_to_ymdhms(tvp->tv_sec, &dt); /* get the year */
210 dt.dt_mon = 1;
211 dt.dt_day = 1;
212 dt.dt_hour = 0;
213 dt.dt_min = 0;
214 dt.dt_sec = 0;
215 yearsecs = tvp->tv_sec - clock_ymdhms_to_secs(&dt);
217 #define first72 ((72 - 70) * SECYR)
218 clock_secs_to_ymdhms(first72 + yearsecs, &dt);
220 #ifdef DEBUG
221 if (dt.dt_year != 1972)
222 printf("resettodr: botch (%d, %ld)\n", yearsecs, time_second);
223 #endif
225 s = splclock();
226 MC146818_GETTOD(sc, &regs);
227 splx(s);
229 regs[MC_SEC] = dt.dt_sec;
230 regs[MC_MIN] = dt.dt_min;
231 regs[MC_HOUR] = dt.dt_hour;
232 regs[MC_DOW] = dt.dt_wday;
233 regs[MC_DOM] = dt.dt_day;
234 regs[MC_MONTH] = dt.dt_mon;
235 regs[MC_YEAR] = dt.dt_year;
237 s = splclock();
238 MC146818_PUTTOD(sc, &regs);
239 splx(s);
241 return 0;