1 /* $NetBSD: mcclock.c,v 1.23 2009/05/12 14:18:16 cegger Exp $ */
4 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
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.
47 #define MC_DFEAULTHZ 1024
50 #define MC_DEFAULTHZ 256
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
= {
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)
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
);
87 mcclock_init(device_t dev
)
89 struct mcclock_softc
*sc
= (struct mcclock_softc
*)dev
;
95 rate
= MC_BASE_32_KHz
| MC_RATE_32_Hz
;
98 rate
= MC_BASE_32_KHz
| MC_RATE_64_Hz
;
101 rate
= MC_BASE_32_KHz
| MC_RATE_128_Hz
;
104 rate
= MC_BASE_32_KHz
| MC_RATE_256_Hz
;
107 rate
= MC_BASE_32_KHz
| MC_RATE_512_Hz
;
110 rate
= MC_BASE_32_KHz
| MC_RATE_1024_Hz
;
113 rate
= MC_BASE_32_KHz
| MC_RATE_2048_Hz
;
116 rate
= MC_BASE_32_KHz
| MC_RATE_4096_Hz
;
119 rate
= MC_BASE_32_KHz
| MC_RATE_8192_Hz
;
122 rate
= MC_BASE_4_MHz
| MC_RATE_1
;
125 rate
= MC_BASE_4_MHz
| MC_RATE_2
;
128 printf("%s: Cannot get %d Hz clock; using %d Hz\n",
129 device_xname(&sc
->sc_dev
), hz
, MC_DEFAULTHZ
);
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
;
158 struct clock_ymdhms dt
;
161 MC146818_GETTOD(sc
, ®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
];
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 ... */
184 tvp
->tv_sec
= yearsecs
+ clock_ymdhms_to_secs(&dt
);
185 if (tvp
->tv_sec
> tch
->base_time
- 2 * SECDAY
)
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
;
207 * calculate seconds relative to this year
209 clock_secs_to_ymdhms(tvp
->tv_sec
, &dt
); /* get the year */
215 yearsecs
= tvp
->tv_sec
- clock_ymdhms_to_secs(&dt
);
217 #define first72 ((72 - 70) * SECYR)
218 clock_secs_to_ymdhms(first72
+ yearsecs
, &dt
);
221 if (dt
.dt_year
!= 1972)
222 printf("resettodr: botch (%d, %ld)\n", yearsecs
, time_second
);
226 MC146818_GETTOD(sc
, ®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
;
238 MC146818_PUTTOD(sc
, ®s
);