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