a34kbbc.c revision 1.18 1 /* $NetBSD: a34kbbc.c,v 1.18 2006/09/07 20:59:47 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: a34kbbc.c,v 1.18 2006/09/07 20:59:47 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
97 int a34kbbc_match(struct device *, struct cfdata *, void *);
98 void a34kbbc_attach(struct device *, struct device *, void *);
99
100 CFATTACH_DECL(a34kbbc, sizeof(struct device),
101 a34kbbc_match, a34kbbc_attach, NULL, NULL);
102
103 void *a34kclockaddr;
104 int a34kugettod(todr_chip_handle_t, volatile struct timeval *);
105 int a34kusettod(todr_chip_handle_t, volatile struct timeval *);
106 static struct todr_chip_handle a34ktodr;
107
108 int
109 a34kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
110 {
111 static int a34kbbc_matched = 0;
112
113 if (!matchname("a34kbbc", auxp))
114 return(0);
115
116 /* Allow only one instance. */
117 if (a34kbbc_matched)
118 return(0);
119
120 if (!(is_a3000() || is_a4000()))
121 return(0);
122
123 a34kclockaddr = (void *)__UNVOLATILE(ztwomap(0xdc0000));
124 if (a34kugettod(&a34ktodr, 0) != 0)
125 return(0);
126
127 a34kbbc_matched = 1;
128 return(1);
129 }
130
131 /*
132 * Attach us to the rtc function pointers.
133 */
134 void
135 a34kbbc_attach(struct device *pdp, struct device *dp, void *auxp)
136 {
137 printf("\n");
138 a34kclockaddr = (void *)__UNVOLATILE(ztwomap(0xdc0000));
139
140 a34ktodr.cookie = a34kclockaddr;
141 a34ktodr.todr_gettime = a34kugettod;
142 a34ktodr.todr_settime = a34kusettod;
143 todr_attach(&a34ktodr);
144 }
145
146 int
147 a34kugettod(todr_chip_handle_t h, volatile struct timeval *tvp)
148 {
149 struct rtclock3000 *rt;
150 struct clock_ymdhms dt;
151 time_t secs;
152
153 rt = a34kclockaddr;
154
155 /* hold clock */
156 rt->control1 = A3CONTROL1_HOLD_CLOCK;
157
158 /* Copy the info. Careful about the order! */
159 dt.dt_sec = rt->second1 * 10 + rt->second2;
160 dt.dt_min = rt->minute1 * 10 + rt->minute2;
161 dt.dt_hour = rt->hour1 * 10 + rt->hour2;
162 dt.dt_wday = rt->weekday;
163 dt.dt_day = rt->day1 * 10 + rt->day2;
164 dt.dt_mon = rt->month1 * 10 + rt->month2;
165 dt.dt_year = rt->year1 * 10 + rt->year2;
166
167 dt.dt_year += CLOCK_BASE_YEAR;
168 /* let it run again.. */
169 rt->control1 = A3CONTROL1_FREE_CLOCK;
170
171 if (dt.dt_year < STARTOFTIME)
172 dt.dt_year += 100;
173
174
175 if ((dt.dt_hour > 23) ||
176 (dt.dt_wday > 6) ||
177 (dt.dt_day > 31) ||
178 (dt.dt_mon > 12) ||
179 /* (dt.dt_year < STARTOFTIME) || */ (dt.dt_year > 2036))
180 return (EINVAL);
181
182 secs = clock_ymdhms_to_secs(&dt);
183 if (tvp)
184 tvp->tv_sec = secs;
185 return (0);
186 }
187
188 int
189 a34kusettod(todr_chip_handle_t h, volatile struct timeval *tvp)
190 {
191 struct rtclock3000 *rt;
192 struct clock_ymdhms dt;
193 time_t secs;
194
195 rt = a34kclockaddr;
196 secs = tvp->tv_sec;
197 /*
198 * there seem to be problems with the bitfield addressing
199 * currently used..
200 */
201
202 if (! rt)
203 return (ENXIO);
204
205 clock_secs_to_ymdhms(secs, &dt);
206
207 rt->control1 = A3CONTROL1_HOLD_CLOCK; /* implies mode 0 */
208 rt->second1 = dt.dt_sec / 10;
209 rt->second2 = dt.dt_sec % 10;
210 rt->minute1 = dt.dt_min / 10;
211 rt->minute2 = dt.dt_min % 10;
212 rt->hour1 = dt.dt_hour / 10;
213 rt->hour2 = dt.dt_hour % 10;
214 rt->weekday = dt.dt_wday;
215 rt->day1 = dt.dt_day / 10;
216 rt->day2 = dt.dt_day % 10;
217 rt->month1 = dt.dt_mon / 10;
218 rt->month2 = dt.dt_mon % 10;
219 rt->year1 = (dt.dt_year / 10) % 10;
220 rt->year2 = dt.dt_year % 10;
221 rt->control1 = A3CONTROL1_HOLD_CLOCK | 1; /* mode 1 registers */
222 rt->leapyear = dt.dt_year; /* XXX implicit % 4 */
223 rt->control1 = A3CONTROL1_FREE_CLOCK; /* implies mode 1 */
224
225 return (0);
226 }
227