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