a2kbbc.c revision 1.18
1/*	$NetBSD: a2kbbc.c,v 1.18 2006/09/05 05:32:30 mhitch 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.18 2006/09/05 05:32:30 mhitch 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(todr_chip_handle_t, volatile struct timeval *);
105int a2kusettod(todr_chip_handle_t, volatile struct timeval *);
106static struct todr_chip_handle a2ktodr;
107
108int
109a2kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
110{
111	static int a2kbbc_matched = 0;
112
113	if (!matchname("a2kbbc", auxp))
114		return (0);
115
116	/* Allow only one instance. */
117	if (a2kbbc_matched)
118		return (0);
119
120	if (/* is_a1200() || */ is_a3000() || is_a4000()
121#ifdef DRACO
122	    || is_draco()
123#endif
124	    )
125		return (0);
126
127	a2kclockaddr = (void *)__UNVOLATILE(ztwomap(0xdc0000));
128	if (a2kugettod(&a2ktodr, 0) != 0)
129		return (0);
130
131	a2kbbc_matched = 1;
132	return (1);
133}
134
135/*
136 * Attach us to the rtc function pointers.
137 */
138void
139a2kbbc_attach(struct device *pdp, struct device *dp, void *auxp)
140{
141	printf("\n");
142	a2kclockaddr = (void *)__UNVOLATILE(ztwomap(0xdc0000));
143
144	a2ktodr.cookie = a2kclockaddr;
145	a2ktodr.todr_gettime = a2kugettod;
146	a2ktodr.todr_settime = a2kusettod;
147	todr_attach(&a2ktodr);
148}
149
150int
151a2kugettod(todr_chip_handle_t h, volatile struct timeval *tvp)
152{
153	struct rtclock2000 *rt;
154	struct clock_ymdhms dt;
155	time_t secs;
156	int i;
157
158	rt = a2kclockaddr;
159
160	/*
161	 * hold clock
162	 */
163	rt->control1 |= A2CONTROL1_HOLD;
164	i = 0x1000;
165	while (rt->control1 & A2CONTROL1_BUSY && i--)
166		;
167	if (rt->control1 & A2CONTROL1_BUSY)
168		return (0);	/* Give up and say it's not there */
169
170	/* Copy the info.  Careful about the order! */
171	dt.dt_sec   = rt->second1 * 10 + rt->second2;
172	dt.dt_min   = rt->minute1 * 10 + rt->minute2;
173	dt.dt_hour  = (rt->hour1 & 3) * 10 + rt->hour2;
174	dt.dt_day   = rt->day1    * 10 + rt->day2;
175	dt.dt_mon   = rt->month1  * 10 + rt->month2;
176	dt.dt_year  = rt->year1   * 10 + rt->year2;
177	dt.dt_wday  = rt->weekday;
178
179	/*
180	 * The oki clock chip has a register to put the clock into
181	 * 12/24h mode.
182	 *
183	 *  clockmode   |    A2HOUR1_PM
184	 *  24h   12h   |  am = 0, pm = 1
185	 * ---------------------------------
186	 *   0    12    |       0
187	 *   1     1    |       0
188	 *  ..    ..    |       0
189	 *  11    11    |       0
190	 *  12    12    |       1
191	 *  13     1    |       1
192	 *  ..    ..    |       1
193	 *  23    11    |       1
194	 *
195	 */
196
197	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
198		if ((rt->hour1 & A2HOUR1_PM) == 0 && dt.dt_hour == 12)
199			dt.dt_hour = 0;
200		else if ((rt->hour1 & A2HOUR1_PM) && dt.dt_hour != 12)
201			dt.dt_hour += 12;
202	}
203
204	/*
205	 * release the clock
206	 */
207	rt->control1 &= ~A2CONTROL1_HOLD;
208
209	dt.dt_year += CLOCK_BASE_YEAR;
210	if (dt.dt_year < STARTOFTIME)
211		dt.dt_year += 100;
212
213	if ((dt.dt_hour > 23) ||
214	    (dt.dt_day  > 31) ||
215	    (dt.dt_mon  > 12) ||
216	    /* (dt.dt_year < STARTOFTIME) || */ (dt.dt_year > 2036))
217		return (0);
218
219	secs = clock_ymdhms_to_secs(&dt);
220	if (tvp) {
221		tvp->tv_sec = secs;
222		tvp->tv_usec = 0;
223	}
224	return (0);
225}
226
227int
228a2kusettod(todr_chip_handle_t h, volatile struct timeval *tvp)
229{
230	struct rtclock2000 *rt;
231	struct clock_ymdhms dt;
232	int ampm, i;
233	time_t secs;
234
235	secs = tvp->tv_sec;
236	rt = a2kclockaddr;
237	/*
238	 * there seem to be problems with the bitfield addressing
239	 * currently used..
240	 */
241	if (! rt)
242		return (1);
243
244	clock_secs_to_ymdhms(secs, &dt);
245
246	/*
247	 * hold clock
248	 */
249	rt->control1 |= A2CONTROL1_HOLD;
250	i = 0x1000;
251	while (rt->control1 & A2CONTROL1_BUSY && i--)
252		;
253	if (rt->control1 & A2CONTROL1_BUSY)
254		return (0);	/* Give up and say it's not there */
255
256	ampm = 0;
257	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
258		if (dt.dt_hour >= 12) {
259			ampm = A2HOUR1_PM;
260			if (dt.dt_hour != 12)
261				dt.dt_hour -= 12;
262		} else if (dt.dt_hour == 0) {
263			dt.dt_hour = 12;
264		}
265	}
266	rt->hour1   = (dt.dt_hour / 10) | ampm;
267	rt->hour2   = dt.dt_hour % 10;
268	rt->second1 = dt.dt_sec / 10;
269	rt->second2 = dt.dt_sec % 10;
270	rt->minute1 = dt.dt_min / 10;
271	rt->minute2 = dt.dt_min % 10;
272	rt->day1    = dt.dt_day / 10;
273	rt->day2    = dt.dt_day % 10;
274	rt->month1  = dt.dt_mon / 10;
275	rt->month2  = dt.dt_mon % 10;
276	rt->year1   = (dt.dt_year / 10) % 10;
277	rt->year2   = dt.dt_year % 10;
278	rt->weekday = dt.dt_wday;
279
280	/*
281	 * release the clock
282	 */
283	rt->control2 &= ~A2CONTROL1_HOLD;
284
285	return (0);
286}
287