a2kbbc.c revision 1.15
1/*	$NetBSD: a2kbbc.c,v 1.15 2003/08/07 16:26:39 agc Exp $ */
2
3/*
4 * Copyright (c) 1982, 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * from: Utah $Hdr: clock.c 1.18 91/01/21$
36 *
37 *	@(#)clock.c	7.6 (Berkeley) 5/7/91
38 */
39
40/*
41 * Copyright (c) 1988 University of Utah.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * the Systems Programming Group of the University of Utah Computer
45 * Science Department.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 *    notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 *    notice, this list of conditions and the following disclaimer in the
54 *    documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 *    must display the following acknowledgement:
57 *	This product includes software developed by the University of
58 *	California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 *    may be used to endorse or promote products derived from this software
61 *    without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * from: Utah $Hdr: clock.c 1.18 91/01/21$
76 *
77 *	@(#)clock.c	7.6 (Berkeley) 5/7/91
78 */
79
80#include <sys/cdefs.h>
81__KERNEL_RCSID(0, "$NetBSD: a2kbbc.c,v 1.15 2003/08/07 16:26:39 agc Exp $");
82
83#include <sys/param.h>
84#include <sys/kernel.h>
85#include <sys/device.h>
86#include <sys/systm.h>
87#include <machine/psl.h>
88#include <machine/cpu.h>
89#include <amiga/amiga/device.h>
90#include <amiga/amiga/custom.h>
91#include <amiga/amiga/cia.h>
92#include <amiga/dev/rtc.h>
93#include <amiga/dev/zbusvar.h>
94
95#include <dev/clock_subr.h>
96
97int a2kbbc_match(struct device *, struct cfdata *, void *);
98void a2kbbc_attach(struct device *, struct device *, void *);
99
100CFATTACH_DECL(a2kbbc, sizeof(struct device),
101    a2kbbc_match, a2kbbc_attach, NULL, NULL);
102
103void *a2kclockaddr;
104int a2kugettod(struct timeval *);
105int a2kusettod(struct timeval *);
106
107int
108a2kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
109{
110	static int a2kbbc_matched = 0;
111
112	if (!matchname("a2kbbc", auxp))
113		return (0);
114
115	/* Allow only one instance. */
116	if (a2kbbc_matched)
117		return (0);
118
119	if (/* is_a1200() || */ is_a3000() || is_a4000()
120#ifdef DRACO
121	    || is_draco()
122#endif
123	    )
124		return (0);
125
126	a2kclockaddr = (void *)ztwomap(0xdc0000);
127	if (a2kugettod(0) == 0)
128		return (0);
129
130	a2kbbc_matched = 1;
131	return (1);
132}
133
134/*
135 * Attach us to the rtc function pointers.
136 */
137void
138a2kbbc_attach(struct device *pdp, struct device *dp, void *auxp)
139{
140	printf("\n");
141	a2kclockaddr = (void *)ztwomap(0xdc0000);
142
143	ugettod = a2kugettod;
144	usettod = a2kusettod;
145}
146
147int
148a2kugettod(struct timeval *tvp)
149{
150	struct rtclock2000 *rt;
151	struct clock_ymdhms dt;
152	time_t secs;
153	int i;
154
155	rt = a2kclockaddr;
156
157	/*
158	 * hold clock
159	 */
160	rt->control1 |= A2CONTROL1_HOLD;
161	i = 0x1000;
162	while (rt->control1 & A2CONTROL1_BUSY && i--)
163		;
164	if (rt->control1 & A2CONTROL1_BUSY)
165		return (0);	/* Give up and say it's not there */
166
167	/* Copy the info.  Careful about the order! */
168	dt.dt_sec   = rt->second1 * 10 + rt->second2;
169	dt.dt_min   = rt->minute1 * 10 + rt->minute2;
170	dt.dt_hour  = (rt->hour1 & 3) * 10 + rt->hour2;
171	dt.dt_day   = rt->day1    * 10 + rt->day2;
172	dt.dt_mon   = rt->month1  * 10 + rt->month2;
173	dt.dt_year  = rt->year1   * 10 + rt->year2;
174	dt.dt_wday  = rt->weekday;
175
176	/*
177	 * The oki clock chip has a register to put the clock into
178	 * 12/24h mode.
179	 *
180	 *  clockmode   |    A2HOUR1_PM
181	 *  24h   12h   |  am = 0, pm = 1
182	 * ---------------------------------
183	 *   0    12    |       0
184	 *   1     1    |       0
185	 *  ..    ..    |       0
186	 *  11    11    |       0
187	 *  12    12    |       1
188	 *  13     1    |       1
189	 *  ..    ..    |       1
190	 *  23    11    |       1
191	 *
192	 */
193
194	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
195		if ((rt->hour1 & A2HOUR1_PM) == 0 && dt.dt_hour == 12)
196			dt.dt_hour = 0;
197		else if ((rt->hour1 & A2HOUR1_PM) && dt.dt_hour != 12)
198			dt.dt_hour += 12;
199	}
200
201	/*
202	 * release the clock
203	 */
204	rt->control1 &= ~A2CONTROL1_HOLD;
205
206	dt.dt_year += CLOCK_BASE_YEAR;
207	if (dt.dt_year < STARTOFTIME)
208		dt.dt_year += 100;
209
210	if ((dt.dt_hour > 23) ||
211	    (dt.dt_day  > 31) ||
212	    (dt.dt_mon  > 12) ||
213	    /* (dt.dt_year < STARTOFTIME) || */ (dt.dt_year > 2036))
214		return (0);
215
216	secs = clock_ymdhms_to_secs(&dt);
217	if (tvp) {
218		tvp->tv_sec = secs;
219		tvp->tv_usec = 0;
220	}
221	return (1);
222}
223
224int
225a2kusettod(struct timeval *tvp)
226{
227	struct rtclock2000 *rt;
228	struct clock_ymdhms dt;
229	int ampm, i;
230	time_t secs;
231
232	secs = tvp->tv_sec;
233	rt = a2kclockaddr;
234	/*
235	 * there seem to be problems with the bitfield addressing
236	 * currently used..
237	 */
238	if (! rt)
239		return (0);
240
241	clock_secs_to_ymdhms(secs, &dt);
242
243	/*
244	 * hold clock
245	 */
246	rt->control1 |= A2CONTROL1_HOLD;
247	i = 0x1000;
248	while (rt->control1 & A2CONTROL1_BUSY && i--)
249		;
250	if (rt->control1 & A2CONTROL1_BUSY)
251		return (0);	/* Give up and say it's not there */
252
253	ampm = 0;
254	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
255		if (dt.dt_hour >= 12) {
256			ampm = A2HOUR1_PM;
257			if (dt.dt_hour != 12)
258				dt.dt_hour -= 12;
259		} else if (dt.dt_hour == 0) {
260			dt.dt_hour = 12;
261		}
262	}
263	rt->hour1   = (dt.dt_hour / 10) | ampm;
264	rt->hour2   = dt.dt_hour % 10;
265	rt->second1 = dt.dt_sec / 10;
266	rt->second2 = dt.dt_sec % 10;
267	rt->minute1 = dt.dt_min / 10;
268	rt->minute2 = dt.dt_min % 10;
269	rt->day1    = dt.dt_day / 10;
270	rt->day2    = dt.dt_day % 10;
271	rt->month1  = dt.dt_mon / 10;
272	rt->month2  = dt.dt_mon % 10;
273	rt->year1   = (dt.dt_year / 10) % 10;
274	rt->year2   = dt.dt_year % 10;
275	rt->weekday = dt.dt_wday;
276
277	/*
278	 * release the clock
279	 */
280	rt->control2 &= ~A2CONTROL1_HOLD;
281
282	return (1);
283}
284