xref: /netbsd/sys/arch/amiga/dev/a2kbbc.c (revision c4a72b64)
1 /*	$NetBSD: a2kbbc.c,v 1.14 2002/10/02 04:55:48 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/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: a2kbbc.c,v 1.14 2002/10/02 04:55:48 thorpej Exp $");
47 
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/device.h>
51 #include <sys/systm.h>
52 #include <machine/psl.h>
53 #include <machine/cpu.h>
54 #include <amiga/amiga/device.h>
55 #include <amiga/amiga/custom.h>
56 #include <amiga/amiga/cia.h>
57 #include <amiga/dev/rtc.h>
58 #include <amiga/dev/zbusvar.h>
59 
60 #include <dev/clock_subr.h>
61 
62 int a2kbbc_match(struct device *, struct cfdata *, void *);
63 void a2kbbc_attach(struct device *, struct device *, void *);
64 
65 CFATTACH_DECL(a2kbbc, sizeof(struct device),
66     a2kbbc_match, a2kbbc_attach, NULL, NULL);
67 
68 void *a2kclockaddr;
69 int a2kugettod(struct timeval *);
70 int a2kusettod(struct timeval *);
71 
72 int
73 a2kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
74 {
75 	static int a2kbbc_matched = 0;
76 
77 	if (!matchname("a2kbbc", auxp))
78 		return (0);
79 
80 	/* Allow only one instance. */
81 	if (a2kbbc_matched)
82 		return (0);
83 
84 	if (/* is_a1200() || */ is_a3000() || is_a4000()
85 #ifdef DRACO
86 	    || is_draco()
87 #endif
88 	    )
89 		return (0);
90 
91 	a2kclockaddr = (void *)ztwomap(0xdc0000);
92 	if (a2kugettod(0) == 0)
93 		return (0);
94 
95 	a2kbbc_matched = 1;
96 	return (1);
97 }
98 
99 /*
100  * Attach us to the rtc function pointers.
101  */
102 void
103 a2kbbc_attach(struct device *pdp, struct device *dp, void *auxp)
104 {
105 	printf("\n");
106 	a2kclockaddr = (void *)ztwomap(0xdc0000);
107 
108 	ugettod = a2kugettod;
109 	usettod = a2kusettod;
110 }
111 
112 int
113 a2kugettod(struct timeval *tvp)
114 {
115 	struct rtclock2000 *rt;
116 	struct clock_ymdhms dt;
117 	time_t secs;
118 	int i;
119 
120 	rt = a2kclockaddr;
121 
122 	/*
123 	 * hold clock
124 	 */
125 	rt->control1 |= A2CONTROL1_HOLD;
126 	i = 0x1000;
127 	while (rt->control1 & A2CONTROL1_BUSY && i--)
128 		;
129 	if (rt->control1 & A2CONTROL1_BUSY)
130 		return (0);	/* Give up and say it's not there */
131 
132 	/* Copy the info.  Careful about the order! */
133 	dt.dt_sec   = rt->second1 * 10 + rt->second2;
134 	dt.dt_min   = rt->minute1 * 10 + rt->minute2;
135 	dt.dt_hour  = (rt->hour1 & 3) * 10 + rt->hour2;
136 	dt.dt_day   = rt->day1    * 10 + rt->day2;
137 	dt.dt_mon   = rt->month1  * 10 + rt->month2;
138 	dt.dt_year  = rt->year1   * 10 + rt->year2;
139 	dt.dt_wday  = rt->weekday;
140 
141 	/*
142 	 * The oki clock chip has a register to put the clock into
143 	 * 12/24h mode.
144 	 *
145 	 *  clockmode   |    A2HOUR1_PM
146 	 *  24h   12h   |  am = 0, pm = 1
147 	 * ---------------------------------
148 	 *   0    12    |       0
149 	 *   1     1    |       0
150 	 *  ..    ..    |       0
151 	 *  11    11    |       0
152 	 *  12    12    |       1
153 	 *  13     1    |       1
154 	 *  ..    ..    |       1
155 	 *  23    11    |       1
156 	 *
157 	 */
158 
159 	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
160 		if ((rt->hour1 & A2HOUR1_PM) == 0 && dt.dt_hour == 12)
161 			dt.dt_hour = 0;
162 		else if ((rt->hour1 & A2HOUR1_PM) && dt.dt_hour != 12)
163 			dt.dt_hour += 12;
164 	}
165 
166 	/*
167 	 * release the clock
168 	 */
169 	rt->control1 &= ~A2CONTROL1_HOLD;
170 
171 	dt.dt_year += CLOCK_BASE_YEAR;
172 	if (dt.dt_year < STARTOFTIME)
173 		dt.dt_year += 100;
174 
175 	if ((dt.dt_hour > 23) ||
176 	    (dt.dt_day  > 31) ||
177 	    (dt.dt_mon  > 12) ||
178 	    /* (dt.dt_year < STARTOFTIME) || */ (dt.dt_year > 2036))
179 		return (0);
180 
181 	secs = clock_ymdhms_to_secs(&dt);
182 	if (tvp) {
183 		tvp->tv_sec = secs;
184 		tvp->tv_usec = 0;
185 	}
186 	return (1);
187 }
188 
189 int
190 a2kusettod(struct timeval *tvp)
191 {
192 	struct rtclock2000 *rt;
193 	struct clock_ymdhms dt;
194 	int ampm, i;
195 	time_t secs;
196 
197 	secs = tvp->tv_sec;
198 	rt = a2kclockaddr;
199 	/*
200 	 * there seem to be problems with the bitfield addressing
201 	 * currently used..
202 	 */
203 	if (! rt)
204 		return (0);
205 
206 	clock_secs_to_ymdhms(secs, &dt);
207 
208 	/*
209 	 * hold clock
210 	 */
211 	rt->control1 |= A2CONTROL1_HOLD;
212 	i = 0x1000;
213 	while (rt->control1 & A2CONTROL1_BUSY && i--)
214 		;
215 	if (rt->control1 & A2CONTROL1_BUSY)
216 		return (0);	/* Give up and say it's not there */
217 
218 	ampm = 0;
219 	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
220 		if (dt.dt_hour >= 12) {
221 			ampm = A2HOUR1_PM;
222 			if (dt.dt_hour != 12)
223 				dt.dt_hour -= 12;
224 		} else if (dt.dt_hour == 0) {
225 			dt.dt_hour = 12;
226 		}
227 	}
228 	rt->hour1   = (dt.dt_hour / 10) | ampm;
229 	rt->hour2   = dt.dt_hour % 10;
230 	rt->second1 = dt.dt_sec / 10;
231 	rt->second2 = dt.dt_sec % 10;
232 	rt->minute1 = dt.dt_min / 10;
233 	rt->minute2 = dt.dt_min % 10;
234 	rt->day1    = dt.dt_day / 10;
235 	rt->day2    = dt.dt_day % 10;
236 	rt->month1  = dt.dt_mon / 10;
237 	rt->month2  = dt.dt_mon % 10;
238 	rt->year1   = (dt.dt_year / 10) % 10;
239 	rt->year2   = dt.dt_year % 10;
240 	rt->weekday = dt.dt_wday;
241 
242 	/*
243 	 * release the clock
244 	 */
245 	rt->control2 &= ~A2CONTROL1_HOLD;
246 
247 	return (1);
248 }
249