xref: /illumos-gate/usr/src/uts/sun4u/io/todm5819.c (revision 8fc99e42)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*8fc99e42STrevor Thompson  * Common Development and Distribution License (the "License").
6*8fc99e42STrevor Thompson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*8fc99e42STrevor Thompson  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/conf.h>
287c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
297c478bd9Sstevel@tonic-gate #include <sys/open.h>
307c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/todm5819.h>
347c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/clock.h>
377c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
387c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
397c478bd9Sstevel@tonic-gate #include <sys/poll.h>
407c478bd9Sstevel@tonic-gate #include <sys/pbio.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static timestruc_t	todm5819_get(void);
437c478bd9Sstevel@tonic-gate static void		todm5819_set(timestruc_t);
447c478bd9Sstevel@tonic-gate static uint_t		todm5819_set_watchdog_timer(uint_t);
457c478bd9Sstevel@tonic-gate static uint_t		todm5819_clear_watchdog_timer(void);
467c478bd9Sstevel@tonic-gate static void		todm5819_set_power_alarm(timestruc_t);
477c478bd9Sstevel@tonic-gate static void		todm5819_clear_power_alarm(void);
487c478bd9Sstevel@tonic-gate static uint64_t		todm5819_get_cpufrequency(void);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate extern uint64_t		find_cpufrequency(volatile uint8_t *);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * External variables
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate extern int		watchdog_enable;
567c478bd9Sstevel@tonic-gate extern int		watchdog_available;
577c478bd9Sstevel@tonic-gate extern int		boothowto;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Global variables
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate int m5819_debug_flags;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static todinfo_t	rtc_to_tod(struct rtc_t *);
657c478bd9Sstevel@tonic-gate static uint_t		read_rtc(struct rtc_t *);
667c478bd9Sstevel@tonic-gate static void		write_rtc_time(struct rtc_t *);
677c478bd9Sstevel@tonic-gate static void		write_rtc_alarm(struct rtc_t *);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
717c478bd9Sstevel@tonic-gate 	&mod_miscops, "tod module for ALI M5819",
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
757c478bd9Sstevel@tonic-gate 	MODREV_1, &modlmisc, NULL
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate int
_init(void)807c478bd9Sstevel@tonic-gate _init(void)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todm5819") == 0 ||
83*8fc99e42STrevor Thompson 	    strcmp(tod_module_name, "m5819") == 0) {
847c478bd9Sstevel@tonic-gate 		RTC_PUT8(RTC_B, (RTC_DM | RTC_HM));
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		tod_ops.tod_get = todm5819_get;
877c478bd9Sstevel@tonic-gate 		tod_ops.tod_set = todm5819_set;
88*8fc99e42STrevor Thompson 		tod_ops.tod_set_watchdog_timer = todm5819_set_watchdog_timer;
897c478bd9Sstevel@tonic-gate 		tod_ops.tod_clear_watchdog_timer =
90*8fc99e42STrevor Thompson 		    todm5819_clear_watchdog_timer;
917c478bd9Sstevel@tonic-gate 		tod_ops.tod_set_power_alarm = todm5819_set_power_alarm;
927c478bd9Sstevel@tonic-gate 		tod_ops.tod_clear_power_alarm = todm5819_clear_power_alarm;
937c478bd9Sstevel@tonic-gate 		tod_ops.tod_get_cpufrequency = todm5819_get_cpufrequency;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		/*
967c478bd9Sstevel@tonic-gate 		 * check if hardware watchdog timer is available and user
977c478bd9Sstevel@tonic-gate 		 * enabled it.
987c478bd9Sstevel@tonic-gate 		 */
997c478bd9Sstevel@tonic-gate 		if (watchdog_enable) {
1007c478bd9Sstevel@tonic-gate 			if (!watchdog_available) {
1017c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "m5819: Hardware watchdog "
1027c478bd9Sstevel@tonic-gate 				    "unavailable");
1037c478bd9Sstevel@tonic-gate 			} else if (boothowto & RB_DEBUG) {
1047c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "m5819: Hardware watchdog "
1057c478bd9Sstevel@tonic-gate 				    "disabled [debugger]");
1067c478bd9Sstevel@tonic-gate 			}
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate int
_fini(void)1147c478bd9Sstevel@tonic-gate _fini(void)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	if (strcmp(tod_module_name, "m5819") == 0 ||
117*8fc99e42STrevor Thompson 	    strcmp(tod_module_name, "todm5819") == 0) {
1187c478bd9Sstevel@tonic-gate 		return (EBUSY);
1197c478bd9Sstevel@tonic-gate 	} else {
1207c478bd9Sstevel@tonic-gate 		return (mod_remove(&modlinkage));
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * The loadable-module _info(9E) entry point
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1287c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Read the current time from the clock chip and convert to UNIX form.
1367c478bd9Sstevel@tonic-gate  * Assumes that the year in the clock chip is valid.
1377c478bd9Sstevel@tonic-gate  * Must be called with tod_lock held.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate static timestruc_t
todm5819_get(void)1407c478bd9Sstevel@tonic-gate todm5819_get(void)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	int i;
1437c478bd9Sstevel@tonic-gate 	timestruc_t ts;
1447c478bd9Sstevel@tonic-gate 	struct rtc_t rtc;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * Read from the tod, and if it isnt accessible wait
1507c478bd9Sstevel@tonic-gate 	 * before retrying.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	for (i = 0; i < TODM5819_UIP_RETRY_THRESH; i++) {
1537c478bd9Sstevel@tonic-gate 		if (read_rtc(&rtc))
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		drv_usecwait(TODM5819_UIP_WAIT_USEC);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	if (i == TODM5819_UIP_RETRY_THRESH) {
1587c478bd9Sstevel@tonic-gate 		/*
159*8fc99e42STrevor Thompson 		 * We couldn't read from the TOD.
1607c478bd9Sstevel@tonic-gate 		 */
161*8fc99e42STrevor Thompson 		tod_status_set(TOD_GET_FAILED);
1627c478bd9Sstevel@tonic-gate 		return (hrestime);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	DPRINTF("todm5819_get: century=%d year=%d dom=%d hrs=%d\n",
166*8fc99e42STrevor Thompson 	    rtc.rtc_century, rtc.rtc_year, rtc.rtc_dom, rtc.rtc_hrs);
167*8fc99e42STrevor Thompson 
168*8fc99e42STrevor Thompson 	/* read was successful so ensure failure flag is clear */
169*8fc99e42STrevor Thompson 	tod_status_clear(TOD_GET_FAILED);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	ts.tv_sec = tod_to_utc(rtc_to_tod(&rtc));
1727c478bd9Sstevel@tonic-gate 	ts.tv_nsec = 0;
1737c478bd9Sstevel@tonic-gate 	return (ts);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static todinfo_t
rtc_to_tod(struct rtc_t * rtc)1777c478bd9Sstevel@tonic-gate rtc_to_tod(struct rtc_t *rtc)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	todinfo_t tod;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * tod_year is base 1900 so this code needs to adjust the true
1837c478bd9Sstevel@tonic-gate 	 * year retrieved from the rtc's century and year fields.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	tod.tod_year	= rtc->rtc_year + (rtc->rtc_century * 100) - 1900;
1867c478bd9Sstevel@tonic-gate 	tod.tod_month	= rtc->rtc_mon;
1877c478bd9Sstevel@tonic-gate 	tod.tod_day	= rtc->rtc_dom;
1887c478bd9Sstevel@tonic-gate 	tod.tod_dow	= rtc->rtc_dow;
1897c478bd9Sstevel@tonic-gate 	tod.tod_hour	= rtc->rtc_hrs;
1907c478bd9Sstevel@tonic-gate 	tod.tod_min	= rtc->rtc_min;
1917c478bd9Sstevel@tonic-gate 	tod.tod_sec	= rtc->rtc_sec;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	return (tod);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate uint_t
read_rtc(struct rtc_t * rtc)1977c478bd9Sstevel@tonic-gate read_rtc(struct rtc_t *rtc)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	int s;
2007c478bd9Sstevel@tonic-gate 	uint_t rtc_readable = 0;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	s = splhi();
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * If UIP bit is not set we have at least 274us
2057c478bd9Sstevel@tonic-gate 	 * to read the values. Otherwise we have up to
2067c478bd9Sstevel@tonic-gate 	 * 336us to wait before we can read it
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	if (!(RTC_GET8(RTC_A) & RTC_UIP)) {
2097c478bd9Sstevel@tonic-gate 		rtc_readable = 1;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		rtc->rtc_sec = RTC_GET8(RTC_SEC);
2127c478bd9Sstevel@tonic-gate 		rtc->rtc_asec = RTC_GET8(RTC_ASEC);
2137c478bd9Sstevel@tonic-gate 		rtc->rtc_min = RTC_GET8(RTC_MIN);
2147c478bd9Sstevel@tonic-gate 		rtc->rtc_amin = RTC_GET8(RTC_AMIN);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		rtc->rtc_hrs = RTC_GET8(RTC_HRS);
2177c478bd9Sstevel@tonic-gate 		rtc->rtc_ahrs = RTC_GET8(RTC_AHRS);
2187c478bd9Sstevel@tonic-gate 		rtc->rtc_dow = RTC_GET8(RTC_DOW);
2197c478bd9Sstevel@tonic-gate 		rtc->rtc_dom = RTC_GET8(RTC_DOM);
2207c478bd9Sstevel@tonic-gate 		rtc->rtc_adom = RTC_GET8(RTC_D) & 0x3f;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		rtc->rtc_mon = RTC_GET8(RTC_MON);
2237c478bd9Sstevel@tonic-gate 		rtc->rtc_year = RTC_GET8(RTC_YEAR);
2247c478bd9Sstevel@tonic-gate 		rtc->rtc_century = RTC_GET8(RTC_CENTURY);
2257c478bd9Sstevel@tonic-gate 		rtc->rtc_amon = 0;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		/* Clear wakeup data */
2287c478bd9Sstevel@tonic-gate 		rtc->apc_wdwr = 0;
2297c478bd9Sstevel@tonic-gate 		rtc->apc_wdmr = 0;
2307c478bd9Sstevel@tonic-gate 		rtc->apc_wmr = 0;
2317c478bd9Sstevel@tonic-gate 		rtc->apc_wyr = 0;
2327c478bd9Sstevel@tonic-gate 		rtc->apc_wcr = 0;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	splx(s);
2357c478bd9Sstevel@tonic-gate 	return (rtc_readable);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * Write the specified time into the clock chip.
2407c478bd9Sstevel@tonic-gate  * Must be called with tod_lock held.
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate static void
todm5819_set(timestruc_t ts)2437c478bd9Sstevel@tonic-gate todm5819_set(timestruc_t ts)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	struct rtc_t	rtc;
2467c478bd9Sstevel@tonic-gate 	todinfo_t tod = utc_to_tod(ts.tv_sec);
2477c478bd9Sstevel@tonic-gate 	int year;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/* tod_year is base 1900 so this code needs to adjust */
2527c478bd9Sstevel@tonic-gate 	year = 1900 + tod.tod_year;
2537c478bd9Sstevel@tonic-gate 	rtc.rtc_year	= year % 100;
2547c478bd9Sstevel@tonic-gate 	rtc.rtc_century = year / 100;
2557c478bd9Sstevel@tonic-gate 	rtc.rtc_mon	= (uint8_t)tod.tod_month;
2567c478bd9Sstevel@tonic-gate 	rtc.rtc_dom	= (uint8_t)tod.tod_day;
2577c478bd9Sstevel@tonic-gate 	rtc.rtc_dow	= (uint8_t)tod.tod_dow;
2587c478bd9Sstevel@tonic-gate 	rtc.rtc_hrs	= (uint8_t)tod.tod_hour;
2597c478bd9Sstevel@tonic-gate 	rtc.rtc_min	= (uint8_t)tod.tod_min;
2607c478bd9Sstevel@tonic-gate 	rtc.rtc_sec	= (uint8_t)tod.tod_sec;
2617c478bd9Sstevel@tonic-gate 	DPRINTF("todm5819_set: century=%d year=%d dom=%d hrs=%d\n",
2627c478bd9Sstevel@tonic-gate 	    rtc.rtc_century, rtc.rtc_year, rtc.rtc_dom, rtc.rtc_hrs);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	write_rtc_time(&rtc);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate void
write_rtc_time(struct rtc_t * rtc)2687c478bd9Sstevel@tonic-gate write_rtc_time(struct rtc_t *rtc)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	uint8_t	regb;
271*8fc99e42STrevor Thompson 	int	i;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * Freeze
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	regb = RTC_GET8(RTC_B);
2777c478bd9Sstevel@tonic-gate 	RTC_PUT8(RTC_B, (regb | RTC_SET));
2787c478bd9Sstevel@tonic-gate 
279*8fc99e42STrevor Thompson 	/*
280*8fc99e42STrevor Thompson 	 * If an update is in progress wait for the UIP flag to clear.
281*8fc99e42STrevor Thompson 	 * If we write whilst UIP is still set there is a slight but real
282*8fc99e42STrevor Thompson 	 * possibility of corrupting the RTC date and time registers.
283*8fc99e42STrevor Thompson 	 *
284*8fc99e42STrevor Thompson 	 * The expected wait is one internal cycle of the chip.  We could
285*8fc99e42STrevor Thompson 	 * simply spin but this may hang a CPU if we were to have a broken
286*8fc99e42STrevor Thompson 	 * RTC chip where UIP is stuck, so we use a retry loop instead.
287*8fc99e42STrevor Thompson 	 * No critical section is needed here as the UIP flag will not be
288*8fc99e42STrevor Thompson 	 * re-asserted until we clear RTC_SET.
289*8fc99e42STrevor Thompson 	 */
290*8fc99e42STrevor Thompson 	for (i = 0; i < TODM5819_UIP_RETRY_THRESH; i++) {
291*8fc99e42STrevor Thompson 		if (!(RTC_GET8(RTC_A) & RTC_UIP)) {
292*8fc99e42STrevor Thompson 			break;
293*8fc99e42STrevor Thompson 		}
294*8fc99e42STrevor Thompson 		drv_usecwait(TODM5819_UIP_WAIT_USEC);
295*8fc99e42STrevor Thompson 	}
296*8fc99e42STrevor Thompson 	if (i < TODM5819_UIP_RETRY_THRESH) {
297*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_SEC, (rtc->rtc_sec));
298*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_ASEC, (rtc->rtc_asec));
299*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_MIN, (rtc->rtc_min));
300*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_AMIN, (rtc->rtc_amin));
301*8fc99e42STrevor Thompson 
302*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_HRS, (rtc->rtc_hrs));
303*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_AHRS, (rtc->rtc_ahrs));
304*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_DOW, (rtc->rtc_dow));
305*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_DOM, (rtc->rtc_dom));
306*8fc99e42STrevor Thompson 
307*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_MON, (rtc->rtc_mon));
308*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_YEAR, (rtc->rtc_year));
309*8fc99e42STrevor Thompson 		RTC_PUT8(RTC_CENTURY, (rtc->rtc_century));
310*8fc99e42STrevor Thompson 	} else {
311*8fc99e42STrevor Thompson 		cmn_err(CE_WARN, "todm5819: Could not write the RTC\n");
312*8fc99e42STrevor Thompson 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Unfreeze
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	RTC_PUT8(RTC_B, regb);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate void
write_rtc_alarm(struct rtc_t * rtc)3227c478bd9Sstevel@tonic-gate write_rtc_alarm(struct rtc_t *rtc)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	RTC_PUT8(RTC_ASEC, (rtc->rtc_asec));
3257c478bd9Sstevel@tonic-gate 	RTC_PUT8(RTC_AMIN, (rtc->rtc_amin));
3267c478bd9Sstevel@tonic-gate 	RTC_PUT8(RTC_AHRS, (rtc->rtc_ahrs));
3277c478bd9Sstevel@tonic-gate 	RTC_PUT8(RTC_D, (rtc->rtc_adom));
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * program the rtc registers for alarm to go off at the specified time
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate static void
todm5819_set_power_alarm(timestruc_t ts)3347c478bd9Sstevel@tonic-gate todm5819_set_power_alarm(timestruc_t ts)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	todinfo_t	tod;
3377c478bd9Sstevel@tonic-gate 	uint8_t		regb;
3387c478bd9Sstevel@tonic-gate 	struct rtc_t	rtc;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
3417c478bd9Sstevel@tonic-gate 	tod = utc_to_tod(ts.tv_sec);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * disable alarms
3457c478bd9Sstevel@tonic-gate 	 */
3467c478bd9Sstevel@tonic-gate 	regb = RTC_GET8(RTC_B);
3477c478bd9Sstevel@tonic-gate 	RTC_PUT8(RTC_B, (regb & ~RTC_AIE));
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	rtc.rtc_asec = (uint8_t)tod.tod_sec;
3517c478bd9Sstevel@tonic-gate 	rtc.rtc_amin = (uint8_t)tod.tod_min;
3527c478bd9Sstevel@tonic-gate 	rtc.rtc_ahrs = (uint8_t)tod.tod_hour;
3537c478bd9Sstevel@tonic-gate 	rtc.rtc_adom = (uint8_t)tod.tod_day;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	write_rtc_alarm(&rtc);
3567c478bd9Sstevel@tonic-gate 	/*
3577c478bd9Sstevel@tonic-gate 	 * Enable alarm.
3587c478bd9Sstevel@tonic-gate 	 */
3597c478bd9Sstevel@tonic-gate 	RTC_PUT8(RTC_B, (regb | RTC_AIE));
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * clear alarm interrupt
3647c478bd9Sstevel@tonic-gate  */
3657c478bd9Sstevel@tonic-gate static void
todm5819_clear_power_alarm(void)3667c478bd9Sstevel@tonic-gate todm5819_clear_power_alarm(void)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	uint8_t regb;
3697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	regb = RTC_GET8(RTC_B);
3727c478bd9Sstevel@tonic-gate 	RTC_PUT8(RTC_B, (regb & ~RTC_AIE));
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * Determine the cpu frequency by watching the TOD chip rollover twice.
3777c478bd9Sstevel@tonic-gate  * Cpu clock rate is determined by computing the ticks added (in tick register)
3787c478bd9Sstevel@tonic-gate  * during one second interval on TOD.
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate uint64_t
todm5819_get_cpufrequency(void)3817c478bd9Sstevel@tonic-gate todm5819_get_cpufrequency(void)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
3847c478bd9Sstevel@tonic-gate 	M5819_ADDR_REG = RTC_SEC;
3857c478bd9Sstevel@tonic-gate 	return (find_cpufrequency(v_rtc_data_reg));
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3907c478bd9Sstevel@tonic-gate static uint_t
todm5819_set_watchdog_timer(uint_t timeoutval)3917c478bd9Sstevel@tonic-gate todm5819_set_watchdog_timer(uint_t timeoutval)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
3947c478bd9Sstevel@tonic-gate 	return (0);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate static uint_t
todm5819_clear_watchdog_timer(void)3987c478bd9Sstevel@tonic-gate todm5819_clear_watchdog_timer(void)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4017c478bd9Sstevel@tonic-gate 	return (0);
4027c478bd9Sstevel@tonic-gate }
403