a2kbbc.c revision 1.16
11.16Sjmc/*	$NetBSD: a2kbbc.c,v 1.16 2005/06/13 21:34:16 jmc Exp $ */
21.1Sis
31.1Sis/*
41.1Sis * Copyright (c) 1982, 1990 The Regents of the University of California.
51.1Sis * All rights reserved.
61.1Sis *
71.1Sis * This code is derived from software contributed to Berkeley by
81.1Sis * the Systems Programming Group of the University of Utah Computer
91.1Sis * Science Department.
101.1Sis *
111.1Sis * Redistribution and use in source and binary forms, with or without
121.1Sis * modification, are permitted provided that the following conditions
131.1Sis * are met:
141.1Sis * 1. Redistributions of source code must retain the above copyright
151.1Sis *    notice, this list of conditions and the following disclaimer.
161.1Sis * 2. Redistributions in binary form must reproduce the above copyright
171.1Sis *    notice, this list of conditions and the following disclaimer in the
181.1Sis *    documentation and/or other materials provided with the distribution.
191.15Sagc * 3. Neither the name of the University nor the names of its contributors
201.15Sagc *    may be used to endorse or promote products derived from this software
211.15Sagc *    without specific prior written permission.
221.15Sagc *
231.15Sagc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241.15Sagc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251.15Sagc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261.15Sagc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271.15Sagc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281.15Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291.15Sagc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301.15Sagc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311.15Sagc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321.15Sagc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331.15Sagc * SUCH DAMAGE.
341.15Sagc *
351.15Sagc * from: Utah $Hdr: clock.c 1.18 91/01/21$
361.15Sagc *
371.15Sagc *	@(#)clock.c	7.6 (Berkeley) 5/7/91
381.15Sagc */
391.15Sagc
401.15Sagc/*
411.15Sagc * Copyright (c) 1988 University of Utah.
421.15Sagc *
431.15Sagc * This code is derived from software contributed to Berkeley by
441.15Sagc * the Systems Programming Group of the University of Utah Computer
451.15Sagc * Science Department.
461.15Sagc *
471.15Sagc * Redistribution and use in source and binary forms, with or without
481.15Sagc * modification, are permitted provided that the following conditions
491.15Sagc * are met:
501.15Sagc * 1. Redistributions of source code must retain the above copyright
511.15Sagc *    notice, this list of conditions and the following disclaimer.
521.15Sagc * 2. Redistributions in binary form must reproduce the above copyright
531.15Sagc *    notice, this list of conditions and the following disclaimer in the
541.15Sagc *    documentation and/or other materials provided with the distribution.
551.1Sis * 3. All advertising materials mentioning features or use of this software
561.1Sis *    must display the following acknowledgement:
571.1Sis *	This product includes software developed by the University of
581.1Sis *	California, Berkeley and its contributors.
591.1Sis * 4. Neither the name of the University nor the names of its contributors
601.1Sis *    may be used to endorse or promote products derived from this software
611.1Sis *    without specific prior written permission.
621.1Sis *
631.1Sis * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
641.1Sis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
651.1Sis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
661.1Sis * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
671.1Sis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
681.1Sis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
691.1Sis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
701.1Sis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
711.1Sis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
721.1Sis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
731.1Sis * SUCH DAMAGE.
741.1Sis *
751.1Sis * from: Utah $Hdr: clock.c 1.18 91/01/21$
761.1Sis *
771.1Sis *	@(#)clock.c	7.6 (Berkeley) 5/7/91
781.1Sis */
791.12Saymeric
801.12Saymeric#include <sys/cdefs.h>
811.16Sjmc__KERNEL_RCSID(0, "$NetBSD: a2kbbc.c,v 1.16 2005/06/13 21:34:16 jmc Exp $");
821.1Sis
831.1Sis#include <sys/param.h>
841.1Sis#include <sys/kernel.h>
851.1Sis#include <sys/device.h>
861.1Sis#include <sys/systm.h>
871.1Sis#include <machine/psl.h>
881.1Sis#include <machine/cpu.h>
891.1Sis#include <amiga/amiga/device.h>
901.1Sis#include <amiga/amiga/custom.h>
911.1Sis#include <amiga/amiga/cia.h>
921.1Sis#include <amiga/dev/rtc.h>
931.1Sis#include <amiga/dev/zbusvar.h>
941.1Sis
951.1Sis#include <dev/clock_subr.h>
961.1Sis
971.11Saymericint a2kbbc_match(struct device *, struct cfdata *, void *);
981.11Saymericvoid a2kbbc_attach(struct device *, struct device *, void *);
991.1Sis
1001.14SthorpejCFATTACH_DECL(a2kbbc, sizeof(struct device),
1011.14Sthorpej    a2kbbc_match, a2kbbc_attach, NULL, NULL);
1021.1Sis
1031.1Sisvoid *a2kclockaddr;
1041.11Saymericint a2kugettod(struct timeval *);
1051.11Saymericint a2kusettod(struct timeval *);
1061.1Sis
1071.1Sisint
1081.11Saymerica2kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
1091.1Sis{
1101.10Skleink	static int a2kbbc_matched = 0;
1111.10Skleink
1121.1Sis	if (!matchname("a2kbbc", auxp))
1131.2Skleink		return (0);
1141.1Sis
1151.10Skleink	/* Allow only one instance. */
1161.10Skleink	if (a2kbbc_matched)
1171.10Skleink		return (0);
1181.1Sis
1191.11Saymeric	if (/* is_a1200() || */ is_a3000() || is_a4000()
1201.3Sis#ifdef DRACO
1211.3Sis	    || is_draco()
1221.3Sis#endif
1231.3Sis	    )
1241.2Skleink		return (0);
1251.1Sis
1261.16Sjmc	a2kclockaddr = (void *)__UNVOLATILE(ztwomap(0xdc0000));
1271.6Sis	if (a2kugettod(0) == 0)
1281.2Skleink		return (0);
1291.1Sis
1301.10Skleink	a2kbbc_matched = 1;
1311.2Skleink	return (1);
1321.1Sis}
1331.1Sis
1341.1Sis/*
1351.1Sis * Attach us to the rtc function pointers.
1361.1Sis */
1371.1Sisvoid
1381.11Saymerica2kbbc_attach(struct device *pdp, struct device *dp, void *auxp)
1391.1Sis{
1401.1Sis	printf("\n");
1411.16Sjmc	a2kclockaddr = (void *)__UNVOLATILE(ztwomap(0xdc0000));
1421.1Sis
1431.6Sis	ugettod = a2kugettod;
1441.6Sis	usettod = a2kusettod;
1451.1Sis}
1461.1Sis
1471.6Sisint
1481.11Saymerica2kugettod(struct timeval *tvp)
1491.1Sis{
1501.1Sis	struct rtclock2000 *rt;
1511.1Sis	struct clock_ymdhms dt;
1521.1Sis	time_t secs;
1531.1Sis	int i;
1541.1Sis
1551.1Sis	rt = a2kclockaddr;
1561.1Sis
1571.1Sis	/*
1581.1Sis	 * hold clock
1591.1Sis	 */
1601.1Sis	rt->control1 |= A2CONTROL1_HOLD;
1611.1Sis	i = 0x1000;
1621.1Sis	while (rt->control1 & A2CONTROL1_BUSY && i--)
1631.1Sis		;
1641.1Sis	if (rt->control1 & A2CONTROL1_BUSY)
1651.1Sis		return (0);	/* Give up and say it's not there */
1661.1Sis
1671.1Sis	/* Copy the info.  Careful about the order! */
1681.1Sis	dt.dt_sec   = rt->second1 * 10 + rt->second2;
1691.1Sis	dt.dt_min   = rt->minute1 * 10 + rt->minute2;
1701.1Sis	dt.dt_hour  = (rt->hour1 & 3) * 10 + rt->hour2;
1711.1Sis	dt.dt_day   = rt->day1    * 10 + rt->day2;
1721.1Sis	dt.dt_mon   = rt->month1  * 10 + rt->month2;
1731.1Sis	dt.dt_year  = rt->year1   * 10 + rt->year2;
1741.1Sis	dt.dt_wday  = rt->weekday;
1751.1Sis
1761.1Sis	/*
1771.1Sis	 * The oki clock chip has a register to put the clock into
1781.1Sis	 * 12/24h mode.
1791.1Sis	 *
1801.1Sis	 *  clockmode   |    A2HOUR1_PM
1811.1Sis	 *  24h   12h   |  am = 0, pm = 1
1821.1Sis	 * ---------------------------------
1831.1Sis	 *   0    12    |       0
1841.1Sis	 *   1     1    |       0
1851.1Sis	 *  ..    ..    |       0
1861.1Sis	 *  11    11    |       0
1871.1Sis	 *  12    12    |       1
1881.1Sis	 *  13     1    |       1
1891.1Sis	 *  ..    ..    |       1
1901.1Sis	 *  23    11    |       1
1911.1Sis	 *
1921.1Sis	 */
1931.1Sis
1941.1Sis	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
1951.1Sis		if ((rt->hour1 & A2HOUR1_PM) == 0 && dt.dt_hour == 12)
1961.1Sis			dt.dt_hour = 0;
1971.1Sis		else if ((rt->hour1 & A2HOUR1_PM) && dt.dt_hour != 12)
1981.1Sis			dt.dt_hour += 12;
1991.1Sis	}
2001.1Sis
2011.11Saymeric	/*
2021.11Saymeric	 * release the clock
2031.1Sis	 */
2041.1Sis	rt->control1 &= ~A2CONTROL1_HOLD;
2051.1Sis
2061.1Sis	dt.dt_year += CLOCK_BASE_YEAR;
2071.8Sis	if (dt.dt_year < STARTOFTIME)
2081.8Sis		dt.dt_year += 100;
2091.1Sis
2101.1Sis	if ((dt.dt_hour > 23) ||
2111.11Saymeric	    (dt.dt_day  > 31) ||
2121.1Sis	    (dt.dt_mon  > 12) ||
2131.8Sis	    /* (dt.dt_year < STARTOFTIME) || */ (dt.dt_year > 2036))
2141.1Sis		return (0);
2151.11Saymeric
2161.1Sis	secs = clock_ymdhms_to_secs(&dt);
2171.7Sis	if (tvp) {
2181.7Sis		tvp->tv_sec = secs;
2191.7Sis		tvp->tv_usec = 0;
2201.7Sis	}
2211.6Sis	return (1);
2221.1Sis}
2231.1Sis
2241.1Sisint
2251.11Saymerica2kusettod(struct timeval *tvp)
2261.1Sis{
2271.1Sis	struct rtclock2000 *rt;
2281.1Sis	struct clock_ymdhms dt;
2291.1Sis	int ampm, i;
2301.6Sis	time_t secs;
2311.1Sis
2321.6Sis	secs = tvp->tv_sec;
2331.1Sis	rt = a2kclockaddr;
2341.11Saymeric	/*
2351.1Sis	 * there seem to be problems with the bitfield addressing
2361.1Sis	 * currently used..
2371.1Sis	 */
2381.1Sis	if (! rt)
2391.1Sis		return (0);
2401.1Sis
2411.1Sis	clock_secs_to_ymdhms(secs, &dt);
2421.1Sis
2431.1Sis	/*
2441.1Sis	 * hold clock
2451.1Sis	 */
2461.1Sis	rt->control1 |= A2CONTROL1_HOLD;
2471.1Sis	i = 0x1000;
2481.1Sis	while (rt->control1 & A2CONTROL1_BUSY && i--)
2491.1Sis		;
2501.1Sis	if (rt->control1 & A2CONTROL1_BUSY)
2511.1Sis		return (0);	/* Give up and say it's not there */
2521.1Sis
2531.1Sis	ampm = 0;
2541.1Sis	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
2551.1Sis		if (dt.dt_hour >= 12) {
2561.1Sis			ampm = A2HOUR1_PM;
2571.1Sis			if (dt.dt_hour != 12)
2581.1Sis				dt.dt_hour -= 12;
2591.1Sis		} else if (dt.dt_hour == 0) {
2601.1Sis			dt.dt_hour = 12;
2611.1Sis		}
2621.1Sis	}
2631.1Sis	rt->hour1   = (dt.dt_hour / 10) | ampm;
2641.1Sis	rt->hour2   = dt.dt_hour % 10;
2651.1Sis	rt->second1 = dt.dt_sec / 10;
2661.1Sis	rt->second2 = dt.dt_sec % 10;
2671.1Sis	rt->minute1 = dt.dt_min / 10;
2681.1Sis	rt->minute2 = dt.dt_min % 10;
2691.1Sis	rt->day1    = dt.dt_day / 10;
2701.1Sis	rt->day2    = dt.dt_day % 10;
2711.1Sis	rt->month1  = dt.dt_mon / 10;
2721.1Sis	rt->month2  = dt.dt_mon % 10;
2731.9Sis	rt->year1   = (dt.dt_year / 10) % 10;
2741.1Sis	rt->year2   = dt.dt_year % 10;
2751.1Sis	rt->weekday = dt.dt_wday;
2761.1Sis
2771.11Saymeric	/*
2781.11Saymeric	 * release the clock
2791.1Sis	 */
2801.1Sis	rt->control2 &= ~A2CONTROL1_HOLD;
2811.1Sis
2821.1Sis	return (1);
2831.1Sis}
284