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