a2kbbc.c revision 1.12
11.12Saymeric/* $NetBSD: a2kbbc.c,v 1.12 2002/01/28 09:56:50 aymeric Exp $ */ 21.1Sis 31.1Sis/* 41.1Sis * Copyright (c) 1988 University of Utah. 51.1Sis * Copyright (c) 1982, 1990 The Regents of the University of California. 61.1Sis * All rights reserved. 71.1Sis * 81.1Sis * This code is derived from software contributed to Berkeley by 91.1Sis * the Systems Programming Group of the University of Utah Computer 101.1Sis * Science Department. 111.1Sis * 121.1Sis * Redistribution and use in source and binary forms, with or without 131.1Sis * modification, are permitted provided that the following conditions 141.1Sis * are met: 151.1Sis * 1. Redistributions of source code must retain the above copyright 161.1Sis * notice, this list of conditions and the following disclaimer. 171.1Sis * 2. Redistributions in binary form must reproduce the above copyright 181.1Sis * notice, this list of conditions and the following disclaimer in the 191.1Sis * documentation and/or other materials provided with the distribution. 201.1Sis * 3. All advertising materials mentioning features or use of this software 211.1Sis * must display the following acknowledgement: 221.1Sis * This product includes software developed by the University of 231.1Sis * California, Berkeley and its contributors. 241.1Sis * 4. Neither the name of the University nor the names of its contributors 251.1Sis * may be used to endorse or promote products derived from this software 261.1Sis * without specific prior written permission. 271.1Sis * 281.1Sis * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 291.1Sis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 301.1Sis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 311.1Sis * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 321.1Sis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 331.1Sis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 341.1Sis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 351.1Sis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 361.1Sis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 371.1Sis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 381.1Sis * SUCH DAMAGE. 391.1Sis * 401.1Sis * from: Utah $Hdr: clock.c 1.18 91/01/21$ 411.1Sis * 421.1Sis * @(#)clock.c 7.6 (Berkeley) 5/7/91 431.1Sis */ 441.12Saymeric 451.12Saymeric#include <sys/cdefs.h> 461.12Saymeric__KERNEL_RCSID(0, "$NetBSD: a2kbbc.c,v 1.12 2002/01/28 09:56:50 aymeric Exp $"); 471.1Sis 481.1Sis#include <sys/param.h> 491.1Sis#include <sys/kernel.h> 501.1Sis#include <sys/device.h> 511.1Sis#include <sys/systm.h> 521.1Sis#include <machine/psl.h> 531.1Sis#include <machine/cpu.h> 541.1Sis#include <amiga/amiga/device.h> 551.1Sis#include <amiga/amiga/custom.h> 561.1Sis#include <amiga/amiga/cia.h> 571.1Sis#include <amiga/dev/rtc.h> 581.1Sis#include <amiga/dev/zbusvar.h> 591.1Sis 601.1Sis#include <dev/clock_subr.h> 611.1Sis 621.11Saymericint a2kbbc_match(struct device *, struct cfdata *, void *); 631.11Saymericvoid a2kbbc_attach(struct device *, struct device *, void *); 641.1Sis 651.1Sisstruct cfattach a2kbbc_ca = { 661.1Sis sizeof(struct device), a2kbbc_match, a2kbbc_attach 671.11Saymeric}; 681.1Sis 691.1Sisvoid *a2kclockaddr; 701.11Saymericint a2kugettod(struct timeval *); 711.11Saymericint a2kusettod(struct timeval *); 721.1Sis 731.1Sisint 741.11Saymerica2kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp) 751.1Sis{ 761.10Skleink static int a2kbbc_matched = 0; 771.10Skleink 781.1Sis if (!matchname("a2kbbc", auxp)) 791.2Skleink return (0); 801.1Sis 811.10Skleink /* Allow only one instance. */ 821.10Skleink if (a2kbbc_matched) 831.10Skleink return (0); 841.1Sis 851.11Saymeric if (/* is_a1200() || */ is_a3000() || is_a4000() 861.3Sis#ifdef DRACO 871.3Sis || is_draco() 881.3Sis#endif 891.3Sis ) 901.2Skleink return (0); 911.1Sis 921.1Sis a2kclockaddr = (void *)ztwomap(0xdc0000); 931.6Sis if (a2kugettod(0) == 0) 941.2Skleink return (0); 951.1Sis 961.10Skleink a2kbbc_matched = 1; 971.2Skleink return (1); 981.1Sis} 991.1Sis 1001.1Sis/* 1011.1Sis * Attach us to the rtc function pointers. 1021.1Sis */ 1031.1Sisvoid 1041.11Saymerica2kbbc_attach(struct device *pdp, struct device *dp, void *auxp) 1051.1Sis{ 1061.1Sis printf("\n"); 1071.1Sis a2kclockaddr = (void *)ztwomap(0xdc0000); 1081.1Sis 1091.6Sis ugettod = a2kugettod; 1101.6Sis usettod = a2kusettod; 1111.1Sis} 1121.1Sis 1131.6Sisint 1141.11Saymerica2kugettod(struct timeval *tvp) 1151.1Sis{ 1161.1Sis struct rtclock2000 *rt; 1171.1Sis struct clock_ymdhms dt; 1181.1Sis time_t secs; 1191.1Sis int i; 1201.1Sis 1211.1Sis rt = a2kclockaddr; 1221.1Sis 1231.1Sis /* 1241.1Sis * hold clock 1251.1Sis */ 1261.1Sis rt->control1 |= A2CONTROL1_HOLD; 1271.1Sis i = 0x1000; 1281.1Sis while (rt->control1 & A2CONTROL1_BUSY && i--) 1291.1Sis ; 1301.1Sis if (rt->control1 & A2CONTROL1_BUSY) 1311.1Sis return (0); /* Give up and say it's not there */ 1321.1Sis 1331.1Sis /* Copy the info. Careful about the order! */ 1341.1Sis dt.dt_sec = rt->second1 * 10 + rt->second2; 1351.1Sis dt.dt_min = rt->minute1 * 10 + rt->minute2; 1361.1Sis dt.dt_hour = (rt->hour1 & 3) * 10 + rt->hour2; 1371.1Sis dt.dt_day = rt->day1 * 10 + rt->day2; 1381.1Sis dt.dt_mon = rt->month1 * 10 + rt->month2; 1391.1Sis dt.dt_year = rt->year1 * 10 + rt->year2; 1401.1Sis dt.dt_wday = rt->weekday; 1411.1Sis 1421.1Sis /* 1431.1Sis * The oki clock chip has a register to put the clock into 1441.1Sis * 12/24h mode. 1451.1Sis * 1461.1Sis * clockmode | A2HOUR1_PM 1471.1Sis * 24h 12h | am = 0, pm = 1 1481.1Sis * --------------------------------- 1491.1Sis * 0 12 | 0 1501.1Sis * 1 1 | 0 1511.1Sis * .. .. | 0 1521.1Sis * 11 11 | 0 1531.1Sis * 12 12 | 1 1541.1Sis * 13 1 | 1 1551.1Sis * .. .. | 1 1561.1Sis * 23 11 | 1 1571.1Sis * 1581.1Sis */ 1591.1Sis 1601.1Sis if ((rt->control3 & A2CONTROL3_24HMODE) == 0) { 1611.1Sis if ((rt->hour1 & A2HOUR1_PM) == 0 && dt.dt_hour == 12) 1621.1Sis dt.dt_hour = 0; 1631.1Sis else if ((rt->hour1 & A2HOUR1_PM) && dt.dt_hour != 12) 1641.1Sis dt.dt_hour += 12; 1651.1Sis } 1661.1Sis 1671.11Saymeric /* 1681.11Saymeric * release the clock 1691.1Sis */ 1701.1Sis rt->control1 &= ~A2CONTROL1_HOLD; 1711.1Sis 1721.1Sis dt.dt_year += CLOCK_BASE_YEAR; 1731.8Sis if (dt.dt_year < STARTOFTIME) 1741.8Sis dt.dt_year += 100; 1751.1Sis 1761.1Sis if ((dt.dt_hour > 23) || 1771.11Saymeric (dt.dt_day > 31) || 1781.1Sis (dt.dt_mon > 12) || 1791.8Sis /* (dt.dt_year < STARTOFTIME) || */ (dt.dt_year > 2036)) 1801.1Sis return (0); 1811.11Saymeric 1821.1Sis secs = clock_ymdhms_to_secs(&dt); 1831.7Sis if (tvp) { 1841.7Sis tvp->tv_sec = secs; 1851.7Sis tvp->tv_usec = 0; 1861.7Sis } 1871.6Sis return (1); 1881.1Sis} 1891.1Sis 1901.1Sisint 1911.11Saymerica2kusettod(struct timeval *tvp) 1921.1Sis{ 1931.1Sis struct rtclock2000 *rt; 1941.1Sis struct clock_ymdhms dt; 1951.1Sis int ampm, i; 1961.6Sis time_t secs; 1971.1Sis 1981.6Sis secs = tvp->tv_sec; 1991.1Sis rt = a2kclockaddr; 2001.11Saymeric /* 2011.1Sis * there seem to be problems with the bitfield addressing 2021.1Sis * currently used.. 2031.1Sis */ 2041.1Sis if (! rt) 2051.1Sis return (0); 2061.1Sis 2071.1Sis clock_secs_to_ymdhms(secs, &dt); 2081.1Sis 2091.1Sis /* 2101.1Sis * hold clock 2111.1Sis */ 2121.1Sis rt->control1 |= A2CONTROL1_HOLD; 2131.1Sis i = 0x1000; 2141.1Sis while (rt->control1 & A2CONTROL1_BUSY && i--) 2151.1Sis ; 2161.1Sis if (rt->control1 & A2CONTROL1_BUSY) 2171.1Sis return (0); /* Give up and say it's not there */ 2181.1Sis 2191.1Sis ampm = 0; 2201.1Sis if ((rt->control3 & A2CONTROL3_24HMODE) == 0) { 2211.1Sis if (dt.dt_hour >= 12) { 2221.1Sis ampm = A2HOUR1_PM; 2231.1Sis if (dt.dt_hour != 12) 2241.1Sis dt.dt_hour -= 12; 2251.1Sis } else if (dt.dt_hour == 0) { 2261.1Sis dt.dt_hour = 12; 2271.1Sis } 2281.1Sis } 2291.1Sis rt->hour1 = (dt.dt_hour / 10) | ampm; 2301.1Sis rt->hour2 = dt.dt_hour % 10; 2311.1Sis rt->second1 = dt.dt_sec / 10; 2321.1Sis rt->second2 = dt.dt_sec % 10; 2331.1Sis rt->minute1 = dt.dt_min / 10; 2341.1Sis rt->minute2 = dt.dt_min % 10; 2351.1Sis rt->day1 = dt.dt_day / 10; 2361.1Sis rt->day2 = dt.dt_day % 10; 2371.1Sis rt->month1 = dt.dt_mon / 10; 2381.1Sis rt->month2 = dt.dt_mon % 10; 2391.9Sis rt->year1 = (dt.dt_year / 10) % 10; 2401.1Sis rt->year2 = dt.dt_year % 10; 2411.1Sis rt->weekday = dt.dt_wday; 2421.1Sis 2431.11Saymeric /* 2441.11Saymeric * release the clock 2451.1Sis */ 2461.1Sis rt->control2 &= ~A2CONTROL1_HOLD; 2471.1Sis 2481.1Sis return (1); 2491.1Sis} 250