xref: /netbsd-src/sys/arch/amiga/dev/a34kbbc.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: a34kbbc.c,v 1.10 2002/01/28 09:56:50 aymeric 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: a34kbbc.c,v 1.10 2002/01/28 09:56:50 aymeric 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 a34kbbc_match(struct device *, struct cfdata *, void *);
63 void a34kbbc_attach(struct device *, struct device *, void *);
64 
65 struct cfattach a34kbbc_ca = {
66 	sizeof(struct device), a34kbbc_match, a34kbbc_attach
67 };
68 
69 void *a34kclockaddr;
70 int a34kugettod(struct timeval *);
71 int a34kusettod(struct timeval *);
72 
73 int
74 a34kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
75 {
76 	static int a34kbbc_matched = 0;
77 
78 	if (!matchname("a34kbbc", auxp))
79 		return(0);
80 
81 	/* Allow only once instance. */
82 	if (a34kbbc_matched)
83 		return(0);
84 
85 	if (!(is_a3000() || is_a4000()))
86 		return(0);
87 
88 	a34kclockaddr = (void *)ztwomap(0xdc0000);
89 	if (a34kugettod(0) == 0)
90 		return(0);
91 
92 	a34kbbc_matched = 1;
93 	return(1);
94 }
95 
96 /*
97  * Attach us to the rtc function pointers.
98  */
99 void
100 a34kbbc_attach(struct device *pdp, struct device *dp, void *auxp)
101 {
102 	printf("\n");
103 	a34kclockaddr = (void *)ztwomap(0xdc0000);
104 
105 	ugettod = a34kugettod;
106 	usettod = a34kusettod;
107 }
108 
109 int
110 a34kugettod(struct timeval *tvp)
111 {
112 	struct rtclock3000 *rt;
113 	struct clock_ymdhms dt;
114 	time_t secs;
115 
116 	rt = a34kclockaddr;
117 
118 	/* hold clock */
119 	rt->control1 = A3CONTROL1_HOLD_CLOCK;
120 
121 	/* Copy the info.  Careful about the order! */
122 	dt.dt_sec   = rt->second1 * 10 + rt->second2;
123 	dt.dt_min   = rt->minute1 * 10 + rt->minute2;
124 	dt.dt_hour  = rt->hour1   * 10 + rt->hour2;
125 	dt.dt_wday  = rt->weekday;
126 	dt.dt_day   = rt->day1    * 10 + rt->day2;
127 	dt.dt_mon   = rt->month1  * 10 + rt->month2;
128 	dt.dt_year  = rt->year1   * 10 + rt->year2;
129 
130 	dt.dt_year += CLOCK_BASE_YEAR;
131 	/* let it run again.. */
132 	rt->control1 = A3CONTROL1_FREE_CLOCK;
133 
134 	if (dt.dt_year < STARTOFTIME)
135 		dt.dt_year += 100;
136 
137 
138 	if ((dt.dt_hour > 23) ||
139 	    (dt.dt_wday > 6) ||
140 	    (dt.dt_day  > 31) ||
141 	    (dt.dt_mon  > 12) ||
142 	    /* (dt.dt_year < STARTOFTIME) || */ (dt.dt_year > 2036))
143 		return (0);
144 
145 	secs = clock_ymdhms_to_secs(&dt);
146 	if (tvp)
147 		tvp->tv_sec = secs;
148 	return (1);
149 }
150 
151 int
152 a34kusettod(struct timeval *tvp)
153 {
154 	struct rtclock3000 *rt;
155 	struct clock_ymdhms dt;
156 	time_t secs;
157 
158 	rt = a34kclockaddr;
159 	secs = tvp->tv_sec;
160 	/*
161 	 * there seem to be problems with the bitfield addressing
162 	 * currently used..
163 	 */
164 
165 	if (! rt)
166 		return (0);
167 
168 	clock_secs_to_ymdhms(secs, &dt);
169 
170 	rt->control1 = A3CONTROL1_HOLD_CLOCK;		/* implies mode 0 */
171 	rt->second1 = dt.dt_sec / 10;
172 	rt->second2 = dt.dt_sec % 10;
173 	rt->minute1 = dt.dt_min / 10;
174 	rt->minute2 = dt.dt_min % 10;
175 	rt->hour1   = dt.dt_hour / 10;
176 	rt->hour2   = dt.dt_hour % 10;
177 	rt->weekday = dt.dt_wday;
178 	rt->day1    = dt.dt_day / 10;
179 	rt->day2    = dt.dt_day % 10;
180 	rt->month1  = dt.dt_mon / 10;
181 	rt->month2  = dt.dt_mon % 10;
182 	rt->year1   = (dt.dt_year / 10) % 10;
183 	rt->year2   = dt.dt_year % 10;
184 	rt->control1 = A3CONTROL1_HOLD_CLOCK | 1;	/* mode 1 registers */
185 	rt->leapyear = dt.dt_year; 		/* XXX implicit % 4 */
186 	rt->control1 = A3CONTROL1_FREE_CLOCK;		/* implies mode 1 */
187 
188 	return (1);
189 }
190