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