a2kbbc.c revision 1.11
11.11Saymeric/*	$NetBSD: a2kbbc.c,v 1.11 2002/01/26 13:40:52 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.1Sis
451.1Sis#include <sys/param.h>
461.1Sis#include <sys/kernel.h>
471.1Sis#include <sys/device.h>
481.1Sis#include <sys/systm.h>
491.1Sis#include <machine/psl.h>
501.1Sis#include <machine/cpu.h>
511.1Sis#include <amiga/amiga/device.h>
521.1Sis#include <amiga/amiga/custom.h>
531.1Sis#include <amiga/amiga/cia.h>
541.1Sis#include <amiga/dev/rtc.h>
551.1Sis#include <amiga/dev/zbusvar.h>
561.1Sis
571.1Sis#include <dev/clock_subr.h>
581.1Sis
591.11Saymericint a2kbbc_match(struct device *, struct cfdata *, void *);
601.11Saymericvoid a2kbbc_attach(struct device *, struct device *, void *);
611.1Sis
621.1Sisstruct cfattach a2kbbc_ca = {
631.1Sis        sizeof(struct device), a2kbbc_match, a2kbbc_attach
641.11Saymeric};
651.1Sis
661.1Sisvoid *a2kclockaddr;
671.11Saymericint a2kugettod(struct timeval *);
681.11Saymericint a2kusettod(struct timeval *);
691.1Sis
701.1Sisint
711.11Saymerica2kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
721.1Sis{
731.10Skleink	static int a2kbbc_matched = 0;
741.10Skleink
751.1Sis	if (!matchname("a2kbbc", auxp))
761.2Skleink		return (0);
771.1Sis
781.10Skleink	/* Allow only one instance. */
791.10Skleink	if (a2kbbc_matched)
801.10Skleink		return (0);
811.1Sis
821.11Saymeric	if (/* is_a1200() || */ is_a3000() || is_a4000()
831.3Sis#ifdef DRACO
841.3Sis	    || is_draco()
851.3Sis#endif
861.3Sis	    )
871.2Skleink		return (0);
881.1Sis
891.1Sis	a2kclockaddr = (void *)ztwomap(0xdc0000);
901.6Sis	if (a2kugettod(0) == 0)
911.2Skleink		return (0);
921.1Sis
931.10Skleink	a2kbbc_matched = 1;
941.2Skleink	return (1);
951.1Sis}
961.1Sis
971.1Sis/*
981.1Sis * Attach us to the rtc function pointers.
991.1Sis */
1001.1Sisvoid
1011.11Saymerica2kbbc_attach(struct device *pdp, struct device *dp, void *auxp)
1021.1Sis{
1031.1Sis	printf("\n");
1041.1Sis	a2kclockaddr = (void *)ztwomap(0xdc0000);
1051.1Sis
1061.6Sis	ugettod = a2kugettod;
1071.6Sis	usettod = a2kusettod;
1081.1Sis}
1091.1Sis
1101.6Sisint
1111.11Saymerica2kugettod(struct timeval *tvp)
1121.1Sis{
1131.1Sis	struct rtclock2000 *rt;
1141.1Sis	struct clock_ymdhms dt;
1151.1Sis	time_t secs;
1161.1Sis	int i;
1171.1Sis
1181.1Sis	rt = a2kclockaddr;
1191.1Sis
1201.1Sis	/*
1211.1Sis	 * hold clock
1221.1Sis	 */
1231.1Sis	rt->control1 |= A2CONTROL1_HOLD;
1241.1Sis	i = 0x1000;
1251.1Sis	while (rt->control1 & A2CONTROL1_BUSY && i--)
1261.1Sis		;
1271.1Sis	if (rt->control1 & A2CONTROL1_BUSY)
1281.1Sis		return (0);	/* Give up and say it's not there */
1291.1Sis
1301.1Sis	/* Copy the info.  Careful about the order! */
1311.1Sis	dt.dt_sec   = rt->second1 * 10 + rt->second2;
1321.1Sis	dt.dt_min   = rt->minute1 * 10 + rt->minute2;
1331.1Sis	dt.dt_hour  = (rt->hour1 & 3) * 10 + rt->hour2;
1341.1Sis	dt.dt_day   = rt->day1    * 10 + rt->day2;
1351.1Sis	dt.dt_mon   = rt->month1  * 10 + rt->month2;
1361.1Sis	dt.dt_year  = rt->year1   * 10 + rt->year2;
1371.1Sis	dt.dt_wday  = rt->weekday;
1381.1Sis
1391.1Sis	/*
1401.1Sis	 * The oki clock chip has a register to put the clock into
1411.1Sis	 * 12/24h mode.
1421.1Sis	 *
1431.1Sis	 *  clockmode   |    A2HOUR1_PM
1441.1Sis	 *  24h   12h   |  am = 0, pm = 1
1451.1Sis	 * ---------------------------------
1461.1Sis	 *   0    12    |       0
1471.1Sis	 *   1     1    |       0
1481.1Sis	 *  ..    ..    |       0
1491.1Sis	 *  11    11    |       0
1501.1Sis	 *  12    12    |       1
1511.1Sis	 *  13     1    |       1
1521.1Sis	 *  ..    ..    |       1
1531.1Sis	 *  23    11    |       1
1541.1Sis	 *
1551.1Sis	 */
1561.1Sis
1571.1Sis	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
1581.1Sis		if ((rt->hour1 & A2HOUR1_PM) == 0 && dt.dt_hour == 12)
1591.1Sis			dt.dt_hour = 0;
1601.1Sis		else if ((rt->hour1 & A2HOUR1_PM) && dt.dt_hour != 12)
1611.1Sis			dt.dt_hour += 12;
1621.1Sis	}
1631.1Sis
1641.11Saymeric	/*
1651.11Saymeric	 * release the clock
1661.1Sis	 */
1671.1Sis	rt->control1 &= ~A2CONTROL1_HOLD;
1681.1Sis
1691.1Sis	dt.dt_year += CLOCK_BASE_YEAR;
1701.8Sis	if (dt.dt_year < STARTOFTIME)
1711.8Sis		dt.dt_year += 100;
1721.1Sis
1731.1Sis	if ((dt.dt_hour > 23) ||
1741.11Saymeric	    (dt.dt_day  > 31) ||
1751.1Sis	    (dt.dt_mon  > 12) ||
1761.8Sis	    /* (dt.dt_year < STARTOFTIME) || */ (dt.dt_year > 2036))
1771.1Sis		return (0);
1781.11Saymeric
1791.1Sis	secs = clock_ymdhms_to_secs(&dt);
1801.7Sis	if (tvp) {
1811.7Sis		tvp->tv_sec = secs;
1821.7Sis		tvp->tv_usec = 0;
1831.7Sis	}
1841.6Sis	return (1);
1851.1Sis}
1861.1Sis
1871.1Sisint
1881.11Saymerica2kusettod(struct timeval *tvp)
1891.1Sis{
1901.1Sis	struct rtclock2000 *rt;
1911.1Sis	struct clock_ymdhms dt;
1921.1Sis	int ampm, i;
1931.6Sis	time_t secs;
1941.1Sis
1951.6Sis	secs = tvp->tv_sec;
1961.1Sis	rt = a2kclockaddr;
1971.11Saymeric	/*
1981.1Sis	 * there seem to be problems with the bitfield addressing
1991.1Sis	 * currently used..
2001.1Sis	 */
2011.1Sis	if (! rt)
2021.1Sis		return (0);
2031.1Sis
2041.1Sis	clock_secs_to_ymdhms(secs, &dt);
2051.1Sis
2061.1Sis	/*
2071.1Sis	 * hold clock
2081.1Sis	 */
2091.1Sis	rt->control1 |= A2CONTROL1_HOLD;
2101.1Sis	i = 0x1000;
2111.1Sis	while (rt->control1 & A2CONTROL1_BUSY && i--)
2121.1Sis		;
2131.1Sis	if (rt->control1 & A2CONTROL1_BUSY)
2141.1Sis		return (0);	/* Give up and say it's not there */
2151.1Sis
2161.1Sis	ampm = 0;
2171.1Sis	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
2181.1Sis		if (dt.dt_hour >= 12) {
2191.1Sis			ampm = A2HOUR1_PM;
2201.1Sis			if (dt.dt_hour != 12)
2211.1Sis				dt.dt_hour -= 12;
2221.1Sis		} else if (dt.dt_hour == 0) {
2231.1Sis			dt.dt_hour = 12;
2241.1Sis		}
2251.1Sis	}
2261.1Sis	rt->hour1   = (dt.dt_hour / 10) | ampm;
2271.1Sis	rt->hour2   = dt.dt_hour % 10;
2281.1Sis	rt->second1 = dt.dt_sec / 10;
2291.1Sis	rt->second2 = dt.dt_sec % 10;
2301.1Sis	rt->minute1 = dt.dt_min / 10;
2311.1Sis	rt->minute2 = dt.dt_min % 10;
2321.1Sis	rt->day1    = dt.dt_day / 10;
2331.1Sis	rt->day2    = dt.dt_day % 10;
2341.1Sis	rt->month1  = dt.dt_mon / 10;
2351.1Sis	rt->month2  = dt.dt_mon % 10;
2361.9Sis	rt->year1   = (dt.dt_year / 10) % 10;
2371.1Sis	rt->year2   = dt.dt_year % 10;
2381.1Sis	rt->weekday = dt.dt_wday;
2391.1Sis
2401.11Saymeric	/*
2411.11Saymeric	 * release the clock
2421.1Sis	 */
2431.1Sis	rt->control2 &= ~A2CONTROL1_HOLD;
2441.1Sis
2451.1Sis	return (1);
2461.1Sis}
247