a2kbbc.c revision 1.14
11.14Sthorpej/*	$NetBSD: a2kbbc.c,v 1.14 2002/10/02 04:55:48 thorpej 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.14Sthorpej__KERNEL_RCSID(0, "$NetBSD: a2kbbc.c,v 1.14 2002/10/02 04:55:48 thorpej 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.14SthorpejCFATTACH_DECL(a2kbbc, sizeof(struct device),
661.14Sthorpej    a2kbbc_match, a2kbbc_attach, NULL, NULL);
671.1Sis
681.1Sisvoid *a2kclockaddr;
691.11Saymericint a2kugettod(struct timeval *);
701.11Saymericint a2kusettod(struct timeval *);
711.1Sis
721.1Sisint
731.11Saymerica2kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
741.1Sis{
751.10Skleink	static int a2kbbc_matched = 0;
761.10Skleink
771.1Sis	if (!matchname("a2kbbc", auxp))
781.2Skleink		return (0);
791.1Sis
801.10Skleink	/* Allow only one instance. */
811.10Skleink	if (a2kbbc_matched)
821.10Skleink		return (0);
831.1Sis
841.11Saymeric	if (/* is_a1200() || */ is_a3000() || is_a4000()
851.3Sis#ifdef DRACO
861.3Sis	    || is_draco()
871.3Sis#endif
881.3Sis	    )
891.2Skleink		return (0);
901.1Sis
911.1Sis	a2kclockaddr = (void *)ztwomap(0xdc0000);
921.6Sis	if (a2kugettod(0) == 0)
931.2Skleink		return (0);
941.1Sis
951.10Skleink	a2kbbc_matched = 1;
961.2Skleink	return (1);
971.1Sis}
981.1Sis
991.1Sis/*
1001.1Sis * Attach us to the rtc function pointers.
1011.1Sis */
1021.1Sisvoid
1031.11Saymerica2kbbc_attach(struct device *pdp, struct device *dp, void *auxp)
1041.1Sis{
1051.1Sis	printf("\n");
1061.1Sis	a2kclockaddr = (void *)ztwomap(0xdc0000);
1071.1Sis
1081.6Sis	ugettod = a2kugettod;
1091.6Sis	usettod = a2kusettod;
1101.1Sis}
1111.1Sis
1121.6Sisint
1131.11Saymerica2kugettod(struct timeval *tvp)
1141.1Sis{
1151.1Sis	struct rtclock2000 *rt;
1161.1Sis	struct clock_ymdhms dt;
1171.1Sis	time_t secs;
1181.1Sis	int i;
1191.1Sis
1201.1Sis	rt = a2kclockaddr;
1211.1Sis
1221.1Sis	/*
1231.1Sis	 * hold clock
1241.1Sis	 */
1251.1Sis	rt->control1 |= A2CONTROL1_HOLD;
1261.1Sis	i = 0x1000;
1271.1Sis	while (rt->control1 & A2CONTROL1_BUSY && i--)
1281.1Sis		;
1291.1Sis	if (rt->control1 & A2CONTROL1_BUSY)
1301.1Sis		return (0);	/* Give up and say it's not there */
1311.1Sis
1321.1Sis	/* Copy the info.  Careful about the order! */
1331.1Sis	dt.dt_sec   = rt->second1 * 10 + rt->second2;
1341.1Sis	dt.dt_min   = rt->minute1 * 10 + rt->minute2;
1351.1Sis	dt.dt_hour  = (rt->hour1 & 3) * 10 + rt->hour2;
1361.1Sis	dt.dt_day   = rt->day1    * 10 + rt->day2;
1371.1Sis	dt.dt_mon   = rt->month1  * 10 + rt->month2;
1381.1Sis	dt.dt_year  = rt->year1   * 10 + rt->year2;
1391.1Sis	dt.dt_wday  = rt->weekday;
1401.1Sis
1411.1Sis	/*
1421.1Sis	 * The oki clock chip has a register to put the clock into
1431.1Sis	 * 12/24h mode.
1441.1Sis	 *
1451.1Sis	 *  clockmode   |    A2HOUR1_PM
1461.1Sis	 *  24h   12h   |  am = 0, pm = 1
1471.1Sis	 * ---------------------------------
1481.1Sis	 *   0    12    |       0
1491.1Sis	 *   1     1    |       0
1501.1Sis	 *  ..    ..    |       0
1511.1Sis	 *  11    11    |       0
1521.1Sis	 *  12    12    |       1
1531.1Sis	 *  13     1    |       1
1541.1Sis	 *  ..    ..    |       1
1551.1Sis	 *  23    11    |       1
1561.1Sis	 *
1571.1Sis	 */
1581.1Sis
1591.1Sis	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
1601.1Sis		if ((rt->hour1 & A2HOUR1_PM) == 0 && dt.dt_hour == 12)
1611.1Sis			dt.dt_hour = 0;
1621.1Sis		else if ((rt->hour1 & A2HOUR1_PM) && dt.dt_hour != 12)
1631.1Sis			dt.dt_hour += 12;
1641.1Sis	}
1651.1Sis
1661.11Saymeric	/*
1671.11Saymeric	 * release the clock
1681.1Sis	 */
1691.1Sis	rt->control1 &= ~A2CONTROL1_HOLD;
1701.1Sis
1711.1Sis	dt.dt_year += CLOCK_BASE_YEAR;
1721.8Sis	if (dt.dt_year < STARTOFTIME)
1731.8Sis		dt.dt_year += 100;
1741.1Sis
1751.1Sis	if ((dt.dt_hour > 23) ||
1761.11Saymeric	    (dt.dt_day  > 31) ||
1771.1Sis	    (dt.dt_mon  > 12) ||
1781.8Sis	    /* (dt.dt_year < STARTOFTIME) || */ (dt.dt_year > 2036))
1791.1Sis		return (0);
1801.11Saymeric
1811.1Sis	secs = clock_ymdhms_to_secs(&dt);
1821.7Sis	if (tvp) {
1831.7Sis		tvp->tv_sec = secs;
1841.7Sis		tvp->tv_usec = 0;
1851.7Sis	}
1861.6Sis	return (1);
1871.1Sis}
1881.1Sis
1891.1Sisint
1901.11Saymerica2kusettod(struct timeval *tvp)
1911.1Sis{
1921.1Sis	struct rtclock2000 *rt;
1931.1Sis	struct clock_ymdhms dt;
1941.1Sis	int ampm, i;
1951.6Sis	time_t secs;
1961.1Sis
1971.6Sis	secs = tvp->tv_sec;
1981.1Sis	rt = a2kclockaddr;
1991.11Saymeric	/*
2001.1Sis	 * there seem to be problems with the bitfield addressing
2011.1Sis	 * currently used..
2021.1Sis	 */
2031.1Sis	if (! rt)
2041.1Sis		return (0);
2051.1Sis
2061.1Sis	clock_secs_to_ymdhms(secs, &dt);
2071.1Sis
2081.1Sis	/*
2091.1Sis	 * hold clock
2101.1Sis	 */
2111.1Sis	rt->control1 |= A2CONTROL1_HOLD;
2121.1Sis	i = 0x1000;
2131.1Sis	while (rt->control1 & A2CONTROL1_BUSY && i--)
2141.1Sis		;
2151.1Sis	if (rt->control1 & A2CONTROL1_BUSY)
2161.1Sis		return (0);	/* Give up and say it's not there */
2171.1Sis
2181.1Sis	ampm = 0;
2191.1Sis	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
2201.1Sis		if (dt.dt_hour >= 12) {
2211.1Sis			ampm = A2HOUR1_PM;
2221.1Sis			if (dt.dt_hour != 12)
2231.1Sis				dt.dt_hour -= 12;
2241.1Sis		} else if (dt.dt_hour == 0) {
2251.1Sis			dt.dt_hour = 12;
2261.1Sis		}
2271.1Sis	}
2281.1Sis	rt->hour1   = (dt.dt_hour / 10) | ampm;
2291.1Sis	rt->hour2   = dt.dt_hour % 10;
2301.1Sis	rt->second1 = dt.dt_sec / 10;
2311.1Sis	rt->second2 = dt.dt_sec % 10;
2321.1Sis	rt->minute1 = dt.dt_min / 10;
2331.1Sis	rt->minute2 = dt.dt_min % 10;
2341.1Sis	rt->day1    = dt.dt_day / 10;
2351.1Sis	rt->day2    = dt.dt_day % 10;
2361.1Sis	rt->month1  = dt.dt_mon / 10;
2371.1Sis	rt->month2  = dt.dt_mon % 10;
2381.9Sis	rt->year1   = (dt.dt_year / 10) % 10;
2391.1Sis	rt->year2   = dt.dt_year % 10;
2401.1Sis	rt->weekday = dt.dt_wday;
2411.1Sis
2421.11Saymeric	/*
2431.11Saymeric	 * release the clock
2441.1Sis	 */
2451.1Sis	rt->control2 &= ~A2CONTROL1_HOLD;
2461.1Sis
2471.1Sis	return (1);
2481.1Sis}
249