timekeeper.c revision 1.1.6.2 1 1.1.6.2 bouyer /* $NetBSD: timekeeper.c,v 1.1.6.2 2000/11/20 20:10:27 bouyer Exp $ */
2 1.1.6.2 bouyer
3 1.1.6.2 bouyer /*-
4 1.1.6.2 bouyer * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1.6.2 bouyer * All rights reserved.
6 1.1.6.2 bouyer *
7 1.1.6.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1.6.2 bouyer * by Tohru Nishimura.
9 1.1.6.2 bouyer *
10 1.1.6.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1.6.2 bouyer * modification, are permitted provided that the following conditions
12 1.1.6.2 bouyer * are met:
13 1.1.6.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1.6.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1.6.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.6.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1.6.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.1.6.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.1.6.2 bouyer * must display the following acknowledgement:
20 1.1.6.2 bouyer * This product includes software developed by the NetBSD
21 1.1.6.2 bouyer * Foundation, Inc. and its contributors.
22 1.1.6.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.6.2 bouyer * contributors may be used to endorse or promote products derived
24 1.1.6.2 bouyer * from this software without specific prior written permission.
25 1.1.6.2 bouyer *
26 1.1.6.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.6.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.6.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.6.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.6.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.6.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.6.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.6.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.6.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.6.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.6.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.1.6.2 bouyer */
38 1.1.6.2 bouyer
39 1.1.6.2 bouyer #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
40 1.1.6.2 bouyer
41 1.1.6.2 bouyer __KERNEL_RCSID(0, "$NetBSD: timekeeper.c,v 1.1.6.2 2000/11/20 20:10:27 bouyer Exp $");
42 1.1.6.2 bouyer
43 1.1.6.2 bouyer #include <sys/param.h>
44 1.1.6.2 bouyer #include <sys/systm.h>
45 1.1.6.2 bouyer #include <sys/device.h>
46 1.1.6.2 bouyer #include <sys/kernel.h>
47 1.1.6.2 bouyer
48 1.1.6.2 bouyer #include <machine/cpu.h>
49 1.1.6.2 bouyer
50 1.1.6.2 bouyer #include <dev/clock_subr.h>
51 1.1.6.2 bouyer #include <luna68k/luna68k/clockvar.h>
52 1.1.6.2 bouyer #include <luna68k/dev/timekeeper.h>
53 1.1.6.2 bouyer #include <machine/autoconf.h>
54 1.1.6.2 bouyer
55 1.1.6.2 bouyer #define YEAR0 1970 /* year offset */
56 1.1.6.2 bouyer
57 1.1.6.2 bouyer struct timekeeper_softc {
58 1.1.6.2 bouyer struct device sc_dev;
59 1.1.6.2 bouyer void *sc_clock, *sc_nvram;
60 1.1.6.2 bouyer int sc_nvramsize;
61 1.1.6.2 bouyer u_int8_t sc_image[2040];
62 1.1.6.2 bouyer };
63 1.1.6.2 bouyer
64 1.1.6.2 bouyer /*
65 1.1.6.2 bouyer * BCD to decimal and decimal to BCD.
66 1.1.6.2 bouyer */
67 1.1.6.2 bouyer #define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf))
68 1.1.6.2 bouyer #define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
69 1.1.6.2 bouyer
70 1.1.6.2 bouyer static int clock_match __P((struct device *, struct cfdata *, void *));
71 1.1.6.2 bouyer static void clock_attach __P((struct device *, struct device *, void *));
72 1.1.6.2 bouyer
73 1.1.6.2 bouyer const struct cfattach clock_ca = {
74 1.1.6.2 bouyer sizeof (struct timekeeper_softc), clock_match, clock_attach
75 1.1.6.2 bouyer };
76 1.1.6.2 bouyer extern struct cfdriver clock_cd;
77 1.1.6.2 bouyer
78 1.1.6.2 bouyer static void mkclock_get __P((struct device *, time_t, struct clock_ymdhms *));
79 1.1.6.2 bouyer static void mkclock_set __P((struct device *, struct clock_ymdhms *));
80 1.1.6.2 bouyer static void dsclock_get __P((struct device *, time_t, struct clock_ymdhms *));
81 1.1.6.2 bouyer static void dsclock_set __P((struct device *, struct clock_ymdhms *));
82 1.1.6.2 bouyer
83 1.1.6.2 bouyer static struct clockfns mkclock_clockfns = {
84 1.1.6.2 bouyer NULL /* never used */, mkclock_get, mkclock_set,
85 1.1.6.2 bouyer };
86 1.1.6.2 bouyer
87 1.1.6.2 bouyer static struct clockfns dsclock_clockfns = {
88 1.1.6.2 bouyer NULL /* never used */, dsclock_get, dsclock_set,
89 1.1.6.2 bouyer };
90 1.1.6.2 bouyer
91 1.1.6.2 bouyer static int
92 1.1.6.2 bouyer clock_match(parent, match, aux)
93 1.1.6.2 bouyer struct device *parent;
94 1.1.6.2 bouyer struct cfdata *match;
95 1.1.6.2 bouyer void *aux;
96 1.1.6.2 bouyer {
97 1.1.6.2 bouyer struct mainbus_attach_args *ma = aux;
98 1.1.6.2 bouyer
99 1.1.6.2 bouyer if (strcmp(ma->ma_name, clock_cd.cd_name))
100 1.1.6.2 bouyer return 0;
101 1.1.6.2 bouyer return 1;
102 1.1.6.2 bouyer }
103 1.1.6.2 bouyer
104 1.1.6.2 bouyer static void
105 1.1.6.2 bouyer clock_attach(parent, self, aux)
106 1.1.6.2 bouyer struct device *parent, *self;
107 1.1.6.2 bouyer void *aux;
108 1.1.6.2 bouyer {
109 1.1.6.2 bouyer struct timekeeper_softc *sc = (void *)self;
110 1.1.6.2 bouyer struct mainbus_attach_args *ma = aux;
111 1.1.6.2 bouyer struct clockfns *clockwork;
112 1.1.6.2 bouyer
113 1.1.6.2 bouyer switch (machtype) {
114 1.1.6.2 bouyer default:
115 1.1.6.2 bouyer case LUNA_I: /* Mostek MK48T02 */
116 1.1.6.2 bouyer sc->sc_clock = (void *)(ma->ma_addr + 2040);
117 1.1.6.2 bouyer sc->sc_nvram = (void *)ma->ma_addr;
118 1.1.6.2 bouyer sc->sc_nvramsize = 2040;
119 1.1.6.2 bouyer clockwork = &mkclock_clockfns;
120 1.1.6.2 bouyer printf(": mk48t02\n");
121 1.1.6.2 bouyer break;
122 1.1.6.2 bouyer case LUNA_II: /* Dallas DS1287A */
123 1.1.6.2 bouyer sc->sc_clock = (void *)ma->ma_addr;
124 1.1.6.2 bouyer sc->sc_nvram = (void *)(ma->ma_addr + 50);
125 1.1.6.2 bouyer sc->sc_nvramsize = 50;
126 1.1.6.2 bouyer clockwork = &dsclock_clockfns;
127 1.1.6.2 bouyer printf(": ds1287a\n");
128 1.1.6.2 bouyer break;
129 1.1.6.2 bouyer }
130 1.1.6.2 bouyer clockattach(&sc->sc_dev, clockwork);
131 1.1.6.2 bouyer memcpy(sc->sc_image, sc->sc_nvram, sc->sc_nvramsize);
132 1.1.6.2 bouyer }
133 1.1.6.2 bouyer
134 1.1.6.2 bouyer /*
135 1.1.6.2 bouyer * Get the time of day, based on the clock's value and/or the base value.
136 1.1.6.2 bouyer */
137 1.1.6.2 bouyer static void
138 1.1.6.2 bouyer mkclock_get(dev, base, dt)
139 1.1.6.2 bouyer struct device *dev;
140 1.1.6.2 bouyer time_t base;
141 1.1.6.2 bouyer struct clock_ymdhms *dt;
142 1.1.6.2 bouyer {
143 1.1.6.2 bouyer struct timekeeper_softc *sc = (void *)dev;
144 1.1.6.2 bouyer volatile u_int8_t *chiptime = (void *)sc->sc_clock;
145 1.1.6.2 bouyer int s;
146 1.1.6.2 bouyer
147 1.1.6.2 bouyer s = splclock();
148 1.1.6.2 bouyer chiptime[MK_CSR] |= MK_CSR_READ; /* enable read (stop time) */
149 1.1.6.2 bouyer dt->dt_sec = FROMBCD(chiptime[MK_SEC]);
150 1.1.6.2 bouyer dt->dt_min = FROMBCD(chiptime[MK_MIN]);
151 1.1.6.2 bouyer dt->dt_hour = FROMBCD(chiptime[MK_HOUR]);
152 1.1.6.2 bouyer dt->dt_wday = FROMBCD(chiptime[MK_DOW]);
153 1.1.6.2 bouyer dt->dt_day = FROMBCD(chiptime[MK_DOM]);
154 1.1.6.2 bouyer dt->dt_mon = FROMBCD(chiptime[MK_MONTH]);
155 1.1.6.2 bouyer dt->dt_year = FROMBCD(chiptime[MK_YEAR]) + YEAR0;
156 1.1.6.2 bouyer chiptime[MK_CSR] &= ~MK_CSR_READ; /* time wears on */
157 1.1.6.2 bouyer splx(s);
158 1.1.6.2 bouyer #ifdef TIMEKEEPER_DEBUG
159 1.1.6.2 bouyer printf("get %d/%d/%d %d:%d:%d\n",
160 1.1.6.2 bouyer dt->dt_year, dt->dt_mon, dt->dt_day,
161 1.1.6.2 bouyer dt->dt_hour, dt->dt_min, dt->dt_sec);
162 1.1.6.2 bouyer #endif
163 1.1.6.2 bouyer }
164 1.1.6.2 bouyer
165 1.1.6.2 bouyer /*
166 1.1.6.2 bouyer * Reset the TODR based on the time value.
167 1.1.6.2 bouyer */
168 1.1.6.2 bouyer static void
169 1.1.6.2 bouyer mkclock_set(dev, dt)
170 1.1.6.2 bouyer struct device *dev;
171 1.1.6.2 bouyer struct clock_ymdhms *dt;
172 1.1.6.2 bouyer {
173 1.1.6.2 bouyer struct timekeeper_softc *sc = (void *)dev;
174 1.1.6.2 bouyer volatile u_int8_t *chiptime = (void *)sc->sc_clock;
175 1.1.6.2 bouyer volatile u_int8_t *stamp = (u_int8_t *)sc->sc_nvram + 0x10;
176 1.1.6.2 bouyer int s;
177 1.1.6.2 bouyer
178 1.1.6.2 bouyer s = splclock();
179 1.1.6.2 bouyer chiptime[MK_CSR] |= MK_CSR_WRITE; /* enable write */
180 1.1.6.2 bouyer chiptime[MK_SEC] = TOBCD(dt->dt_sec);
181 1.1.6.2 bouyer chiptime[MK_MIN] = TOBCD(dt->dt_min);
182 1.1.6.2 bouyer chiptime[MK_HOUR] = TOBCD(dt->dt_hour);
183 1.1.6.2 bouyer chiptime[MK_DOW] = TOBCD(dt->dt_wday);
184 1.1.6.2 bouyer chiptime[MK_DOM] = TOBCD(dt->dt_day);
185 1.1.6.2 bouyer chiptime[MK_MONTH] = TOBCD(dt->dt_mon);
186 1.1.6.2 bouyer chiptime[MK_YEAR] = TOBCD(dt->dt_year - YEAR0);
187 1.1.6.2 bouyer chiptime[MK_CSR] &= ~MK_CSR_WRITE; /* load them up */
188 1.1.6.2 bouyer splx(s);
189 1.1.6.2 bouyer #ifdef TIMEKEEPER_DEBUG
190 1.1.6.2 bouyer printf("set %d/%d/%d %d:%d:%d\n",
191 1.1.6.2 bouyer dt->dt_year, dt->dt_mon, dt->dt_day,
192 1.1.6.2 bouyer dt->dt_hour, dt->dt_min, dt->dt_sec);
193 1.1.6.2 bouyer #endif
194 1.1.6.2 bouyer
195 1.1.6.2 bouyer stamp[0] = 'R'; stamp[1] = 'T'; stamp[2] = 'C'; stamp[3] = '\0';
196 1.1.6.2 bouyer }
197 1.1.6.2 bouyer
198 1.1.6.2 bouyer /*
199 1.1.6.2 bouyer * Get the time of day, based on the clock's value and/or the base value.
200 1.1.6.2 bouyer */
201 1.1.6.2 bouyer static void
202 1.1.6.2 bouyer dsclock_get(dev, base, dt)
203 1.1.6.2 bouyer struct device *dev;
204 1.1.6.2 bouyer time_t base;
205 1.1.6.2 bouyer struct clock_ymdhms *dt;
206 1.1.6.2 bouyer {
207 1.1.6.2 bouyer struct timekeeper_softc *sc = (void *)dev;
208 1.1.6.2 bouyer volatile u_int8_t *chiptime = (void *)sc->sc_clock;
209 1.1.6.2 bouyer int s;
210 1.1.6.2 bouyer
211 1.1.6.2 bouyer s = splclock();
212 1.1.6.2 bouyer /* update in progress; spin loop */
213 1.1.6.2 bouyer while (chiptime[MC_REGA] & MC_REGA_UIP)
214 1.1.6.2 bouyer ;
215 1.1.6.2 bouyer dt->dt_sec = chiptime[MC_SEC];
216 1.1.6.2 bouyer dt->dt_min = chiptime[MC_MIN];
217 1.1.6.2 bouyer dt->dt_hour = chiptime[MC_HOUR];
218 1.1.6.2 bouyer dt->dt_wday = chiptime[MC_DOW];
219 1.1.6.2 bouyer dt->dt_day = chiptime[MC_DOM];
220 1.1.6.2 bouyer dt->dt_mon = chiptime[MC_MONTH];
221 1.1.6.2 bouyer dt->dt_year = chiptime[MC_YEAR] + YEAR0;
222 1.1.6.2 bouyer splx(s);
223 1.1.6.2 bouyer }
224 1.1.6.2 bouyer
225 1.1.6.2 bouyer /*
226 1.1.6.2 bouyer * Reset the TODR based on the time value.
227 1.1.6.2 bouyer */
228 1.1.6.2 bouyer static void
229 1.1.6.2 bouyer dsclock_set(dev, dt)
230 1.1.6.2 bouyer struct device *dev;
231 1.1.6.2 bouyer struct clock_ymdhms *dt;
232 1.1.6.2 bouyer {
233 1.1.6.2 bouyer struct timekeeper_softc *sc = (void *)dev;
234 1.1.6.2 bouyer volatile u_int8_t *chiptime = (void *)sc->sc_clock;
235 1.1.6.2 bouyer int s;
236 1.1.6.2 bouyer
237 1.1.6.2 bouyer s = splclock();
238 1.1.6.2 bouyer chiptime[MC_REGB] |= MC_REGB_SET; /* enable write */
239 1.1.6.2 bouyer chiptime[MC_SEC] = dt->dt_sec;
240 1.1.6.2 bouyer chiptime[MC_MIN] = dt->dt_min;
241 1.1.6.2 bouyer chiptime[MC_HOUR] = dt->dt_hour;
242 1.1.6.2 bouyer chiptime[MC_DOW] = dt->dt_wday;
243 1.1.6.2 bouyer chiptime[MC_DOM] = dt->dt_day;
244 1.1.6.2 bouyer chiptime[MC_MONTH] = dt->dt_mon;
245 1.1.6.2 bouyer chiptime[MC_YEAR] = dt->dt_year - YEAR0;
246 1.1.6.2 bouyer chiptime[MC_REGB] &= ~MC_REGB_SET; /* load them up */
247 1.1.6.2 bouyer splx(s);
248 1.1.6.2 bouyer }
249