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
588294e09SRichard Bean * Common Development and Distribution License (the "License").
688294e09SRichard Bean * 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 #include <sys/lom_priv.h>
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #define WDOG_ON 1
447c478bd9Sstevel@tonic-gate #define WDOG_OFF 0
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate static timestruc_t todbl_get(void);
477c478bd9Sstevel@tonic-gate static void todbl_set(timestruc_t);
487c478bd9Sstevel@tonic-gate static uint_t todbl_set_watchdog_timer(uint_t);
497c478bd9Sstevel@tonic-gate static uint_t todbl_clear_watchdog_timer(void);
507c478bd9Sstevel@tonic-gate static void todbl_set_power_alarm(timestruc_t);
517c478bd9Sstevel@tonic-gate static void todbl_clear_power_alarm(void);
527c478bd9Sstevel@tonic-gate static uint64_t todbl_get_cpufrequency(void);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static todinfo_t rtc_to_tod(struct rtc_t *);
557c478bd9Sstevel@tonic-gate static uint_t read_rtc(struct rtc_t *);
567c478bd9Sstevel@tonic-gate static void write_rtc_time(struct rtc_t *);
577c478bd9Sstevel@tonic-gate static uint_t configure_wdog(uint8_t new_state);
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate extern uint64_t find_cpufrequency(volatile uint8_t *);
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate * External variables
637c478bd9Sstevel@tonic-gate */
647c478bd9Sstevel@tonic-gate extern int watchdog_enable;
657c478bd9Sstevel@tonic-gate extern int watchdog_available;
667c478bd9Sstevel@tonic-gate extern int watchdog_activated;
677c478bd9Sstevel@tonic-gate extern uint_t watchdog_timeout_seconds;
687c478bd9Sstevel@tonic-gate extern int boothowto;
697c478bd9Sstevel@tonic-gate extern void (*bsc_drv_func_ptr)(struct bscv_idi_info *);
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * Global variables
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate int m5819_debug_flags;
757c478bd9Sstevel@tonic-gate uint8_t wdog_reset_on_timeout = 1;
767c478bd9Sstevel@tonic-gate static clock_t last_pat_lbt;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
8088294e09SRichard Bean &mod_miscops, "todblade module",
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
847c478bd9Sstevel@tonic-gate MODREV_1, &modlmisc, NULL
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate int
_init(void)897c478bd9Sstevel@tonic-gate _init(void)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate if (strcmp(tod_module_name, "todblade") == 0) {
927c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_B, (RTC_DM | RTC_HM));
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate tod_ops.tod_get = todbl_get;
957c478bd9Sstevel@tonic-gate tod_ops.tod_set = todbl_set;
96*8fc99e42STrevor Thompson tod_ops.tod_set_watchdog_timer = todbl_set_watchdog_timer;
97*8fc99e42STrevor Thompson tod_ops.tod_clear_watchdog_timer = todbl_clear_watchdog_timer;
987c478bd9Sstevel@tonic-gate tod_ops.tod_set_power_alarm = todbl_set_power_alarm;
997c478bd9Sstevel@tonic-gate tod_ops.tod_clear_power_alarm = todbl_clear_power_alarm;
1007c478bd9Sstevel@tonic-gate tod_ops.tod_get_cpufrequency = todbl_get_cpufrequency;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate if (watchdog_enable && (boothowto & RB_DEBUG)) {
1037c478bd9Sstevel@tonic-gate watchdog_available = 0;
1047c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todblade: kernel debugger "
1057c478bd9Sstevel@tonic-gate "detected: hardware watchdog disabled");
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage));
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate int
_fini(void)1127c478bd9Sstevel@tonic-gate _fini(void)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate if (strcmp(tod_module_name, "todblade") == 0) {
1157c478bd9Sstevel@tonic-gate return (EBUSY);
1167c478bd9Sstevel@tonic-gate } else {
1177c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage));
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * The loadable-module _info(9E) entry point
1237c478bd9Sstevel@tonic-gate */
1247c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1257c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate * Read the current time from the clock chip and convert to UNIX form.
1337c478bd9Sstevel@tonic-gate * Assumes that the year in the clock chip is valid.
1347c478bd9Sstevel@tonic-gate * Must be called with tod_lock held.
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate static timestruc_t
todbl_get(void)1377c478bd9Sstevel@tonic-gate todbl_get(void)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate int i;
1407c478bd9Sstevel@tonic-gate timestruc_t ts;
1417c478bd9Sstevel@tonic-gate struct rtc_t rtc;
1427c478bd9Sstevel@tonic-gate struct bscv_idi_info bscv_info;
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate * We must check that the value of watchdog enable hasnt changed
1487c478bd9Sstevel@tonic-gate * as its a user knob for turning it on and off
1497c478bd9Sstevel@tonic-gate */
1507c478bd9Sstevel@tonic-gate if (watchdog_available) {
1517c478bd9Sstevel@tonic-gate if (watchdog_activated && !watchdog_enable) {
1527c478bd9Sstevel@tonic-gate (void) configure_wdog(WDOG_OFF);
1537c478bd9Sstevel@tonic-gate } else if (!watchdog_activated && watchdog_enable) {
1547c478bd9Sstevel@tonic-gate (void) configure_wdog(WDOG_ON);
1557c478bd9Sstevel@tonic-gate } else if (watchdog_activated &&
156*8fc99e42STrevor Thompson (ddi_get_lbolt() - last_pat_lbt) >= SEC_TO_TICK(1)) {
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate * PAT THE WATCHDOG!!
1597c478bd9Sstevel@tonic-gate * We dont want to accelerate the pat frequency
1607c478bd9Sstevel@tonic-gate * when userland calls to the TOD_GET_DATE ioctl
1617c478bd9Sstevel@tonic-gate * pass through here.
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate bscv_info.type = BSCV_IDI_WDOG_PAT;
1647c478bd9Sstevel@tonic-gate bscv_info.data = NULL;
1657c478bd9Sstevel@tonic-gate bscv_info.size = 0;
1667c478bd9Sstevel@tonic-gate if (bsc_drv_func_ptr != NULL) {
1677c478bd9Sstevel@tonic-gate (*bsc_drv_func_ptr)(&bscv_info);
1687c478bd9Sstevel@tonic-gate last_pat_lbt = ddi_get_lbolt();
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate * Read from the tod, and if it isnt accessible wait
1757c478bd9Sstevel@tonic-gate * before retrying.
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate for (i = 0; i < TODM5819_UIP_RETRY_THRESH; i++) {
1787c478bd9Sstevel@tonic-gate if (read_rtc(&rtc))
1797c478bd9Sstevel@tonic-gate break;
1807c478bd9Sstevel@tonic-gate drv_usecwait(TODM5819_UIP_WAIT_USEC);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate if (i == TODM5819_UIP_RETRY_THRESH) {
1837c478bd9Sstevel@tonic-gate /*
184*8fc99e42STrevor Thompson * We couldn't read from the TOD.
1857c478bd9Sstevel@tonic-gate */
186*8fc99e42STrevor Thompson tod_status_set(TOD_GET_FAILED);
1877c478bd9Sstevel@tonic-gate return (hrestime);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate DPRINTF("todbl_get: century=%d year=%d dom=%d hrs=%d\n",
1917c478bd9Sstevel@tonic-gate rtc.rtc_century, rtc.rtc_year, rtc.rtc_dom, rtc.rtc_hrs);
1927c478bd9Sstevel@tonic-gate
193*8fc99e42STrevor Thompson /* read was successful so ensure failure flag is clear */
194*8fc99e42STrevor Thompson tod_status_clear(TOD_GET_FAILED);
195*8fc99e42STrevor Thompson
1967c478bd9Sstevel@tonic-gate ts.tv_sec = tod_to_utc(rtc_to_tod(&rtc));
1977c478bd9Sstevel@tonic-gate ts.tv_nsec = 0;
1987c478bd9Sstevel@tonic-gate return (ts);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate static todinfo_t
rtc_to_tod(struct rtc_t * rtc)2027c478bd9Sstevel@tonic-gate rtc_to_tod(struct rtc_t *rtc)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate todinfo_t tod;
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate * tod_year is base 1900 so this code needs to adjust the true
2087c478bd9Sstevel@tonic-gate * year retrieved from the rtc's century and year fields.
2097c478bd9Sstevel@tonic-gate */
2107c478bd9Sstevel@tonic-gate tod.tod_year = rtc->rtc_year + (rtc->rtc_century * 100) - 1900;
2117c478bd9Sstevel@tonic-gate tod.tod_month = rtc->rtc_mon;
2127c478bd9Sstevel@tonic-gate tod.tod_day = rtc->rtc_dom;
2137c478bd9Sstevel@tonic-gate tod.tod_dow = rtc->rtc_dow;
2147c478bd9Sstevel@tonic-gate tod.tod_hour = rtc->rtc_hrs;
2157c478bd9Sstevel@tonic-gate tod.tod_min = rtc->rtc_min;
2167c478bd9Sstevel@tonic-gate tod.tod_sec = rtc->rtc_sec;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate return (tod);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate static uint_t
read_rtc(struct rtc_t * rtc)2237c478bd9Sstevel@tonic-gate read_rtc(struct rtc_t *rtc)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate int s;
2267c478bd9Sstevel@tonic-gate uint_t rtc_readable = 0;
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate s = splhi();
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate * If UIP bit is not set we have at least 274us
2317c478bd9Sstevel@tonic-gate * to read the values.
2327c478bd9Sstevel@tonic-gate */
2337c478bd9Sstevel@tonic-gate if (!(RTC_GET8(RTC_A) & RTC_UIP)) {
2347c478bd9Sstevel@tonic-gate rtc_readable = 1;
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate rtc->rtc_sec = RTC_GET8(RTC_SEC);
2377c478bd9Sstevel@tonic-gate rtc->rtc_asec = RTC_GET8(RTC_ASEC);
2387c478bd9Sstevel@tonic-gate rtc->rtc_min = RTC_GET8(RTC_MIN);
2397c478bd9Sstevel@tonic-gate rtc->rtc_amin = RTC_GET8(RTC_AMIN);
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate rtc->rtc_hrs = RTC_GET8(RTC_HRS);
2427c478bd9Sstevel@tonic-gate rtc->rtc_ahrs = RTC_GET8(RTC_AHRS);
2437c478bd9Sstevel@tonic-gate rtc->rtc_dow = RTC_GET8(RTC_DOW);
2447c478bd9Sstevel@tonic-gate rtc->rtc_dom = RTC_GET8(RTC_DOM);
2457c478bd9Sstevel@tonic-gate rtc->rtc_adom = RTC_GET8(RTC_D) & 0x3f;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate rtc->rtc_mon = RTC_GET8(RTC_MON);
2487c478bd9Sstevel@tonic-gate rtc->rtc_year = RTC_GET8(RTC_YEAR);
2497c478bd9Sstevel@tonic-gate rtc->rtc_century = RTC_GET8(RTC_CENTURY);
2507c478bd9Sstevel@tonic-gate rtc->rtc_amon = 0;
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate /* Clear wakeup data */
2537c478bd9Sstevel@tonic-gate rtc->apc_wdwr = 0;
2547c478bd9Sstevel@tonic-gate rtc->apc_wdmr = 0;
2557c478bd9Sstevel@tonic-gate rtc->apc_wmr = 0;
2567c478bd9Sstevel@tonic-gate rtc->apc_wyr = 0;
2577c478bd9Sstevel@tonic-gate rtc->apc_wcr = 0;
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate splx(s);
2617c478bd9Sstevel@tonic-gate return (rtc_readable);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate * Write the specified time into the clock chip.
2667c478bd9Sstevel@tonic-gate * Must be called with tod_lock held.
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate static void
todbl_set(timestruc_t ts)2697c478bd9Sstevel@tonic-gate todbl_set(timestruc_t ts)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate struct rtc_t rtc;
2727c478bd9Sstevel@tonic-gate todinfo_t tod = utc_to_tod(ts.tv_sec);
2737c478bd9Sstevel@tonic-gate struct bscv_idi_info bscv_info;
2747c478bd9Sstevel@tonic-gate int year;
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate /* tod_year is base 1900 so this code needs to adjust */
2797c478bd9Sstevel@tonic-gate year = 1900 + tod.tod_year;
2807c478bd9Sstevel@tonic-gate rtc.rtc_year = year % 100;
2817c478bd9Sstevel@tonic-gate rtc.rtc_century = year / 100;
2827c478bd9Sstevel@tonic-gate rtc.rtc_mon = (uint8_t)tod.tod_month;
2837c478bd9Sstevel@tonic-gate rtc.rtc_dom = (uint8_t)tod.tod_day;
2847c478bd9Sstevel@tonic-gate rtc.rtc_dow = (uint8_t)tod.tod_dow;
2857c478bd9Sstevel@tonic-gate rtc.rtc_hrs = (uint8_t)tod.tod_hour;
2867c478bd9Sstevel@tonic-gate rtc.rtc_min = (uint8_t)tod.tod_min;
2877c478bd9Sstevel@tonic-gate rtc.rtc_sec = (uint8_t)tod.tod_sec;
2887c478bd9Sstevel@tonic-gate DPRINTF("todbl_set: century=%d year=%d dom=%d hrs=%d\n",
2897c478bd9Sstevel@tonic-gate rtc.rtc_century, rtc.rtc_year, rtc.rtc_dom, rtc.rtc_hrs);
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate write_rtc_time(&rtc);
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate * Because of a generic solaris problem where calls to stime()
2957c478bd9Sstevel@tonic-gate * starve calls to tod_get(), we need to check to see when the
2967c478bd9Sstevel@tonic-gate * watchdog was last patted and pat it if necessary.
2977c478bd9Sstevel@tonic-gate */
2987c478bd9Sstevel@tonic-gate if (watchdog_activated &&
2997c478bd9Sstevel@tonic-gate (ddi_get_lbolt() - last_pat_lbt) >= SEC_TO_TICK(1)) {
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate * Pat the watchdog!
3027c478bd9Sstevel@tonic-gate */
3037c478bd9Sstevel@tonic-gate bscv_info.type = BSCV_IDI_WDOG_PAT;
3047c478bd9Sstevel@tonic-gate bscv_info.data = NULL;
3057c478bd9Sstevel@tonic-gate bscv_info.size = 0;
3067c478bd9Sstevel@tonic-gate if (bsc_drv_func_ptr != NULL) {
3077c478bd9Sstevel@tonic-gate (*bsc_drv_func_ptr)(&bscv_info);
3087c478bd9Sstevel@tonic-gate last_pat_lbt = ddi_get_lbolt();
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate static void
write_rtc_time(struct rtc_t * rtc)3147c478bd9Sstevel@tonic-gate write_rtc_time(struct rtc_t *rtc)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate uint8_t regb;
317*8fc99e42STrevor Thompson int i;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate * Freeze
3217c478bd9Sstevel@tonic-gate */
3227c478bd9Sstevel@tonic-gate regb = RTC_GET8(RTC_B);
3237c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_B, (regb | RTC_SET));
3247c478bd9Sstevel@tonic-gate
325*8fc99e42STrevor Thompson /*
326*8fc99e42STrevor Thompson * If an update is in progress wait for the UIP flag to clear.
327*8fc99e42STrevor Thompson * If we write whilst UIP is still set there is a slight but real
328*8fc99e42STrevor Thompson * possibility of corrupting the RTC date and time registers.
329*8fc99e42STrevor Thompson *
330*8fc99e42STrevor Thompson * The expected wait is one internal cycle of the chip. We could
331*8fc99e42STrevor Thompson * simply spin but this may hang a CPU if we were to have a broken
332*8fc99e42STrevor Thompson * RTC chip where UIP is stuck, so we use a retry loop instead.
333*8fc99e42STrevor Thompson * No critical section is needed here as the UIP flag will not be
334*8fc99e42STrevor Thompson * re-asserted until we clear RTC_SET.
335*8fc99e42STrevor Thompson */
336*8fc99e42STrevor Thompson for (i = 0; i < TODM5819_UIP_RETRY_THRESH; i++) {
337*8fc99e42STrevor Thompson if (!(RTC_GET8(RTC_A) & RTC_UIP)) {
338*8fc99e42STrevor Thompson break;
339*8fc99e42STrevor Thompson }
340*8fc99e42STrevor Thompson drv_usecwait(TODM5819_UIP_WAIT_USEC);
341*8fc99e42STrevor Thompson }
342*8fc99e42STrevor Thompson if (i < TODM5819_UIP_RETRY_THRESH) {
3437c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_SEC, (rtc->rtc_sec));
3447c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_ASEC, (rtc->rtc_asec));
3457c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_MIN, (rtc->rtc_min));
3467c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_AMIN, (rtc->rtc_amin));
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_HRS, (rtc->rtc_hrs));
3497c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_AHRS, (rtc->rtc_ahrs));
3507c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_DOW, (rtc->rtc_dow));
3517c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_DOM, (rtc->rtc_dom));
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_MON, (rtc->rtc_mon));
3547c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_YEAR, (rtc->rtc_year));
3557c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_CENTURY, (rtc->rtc_century));
356*8fc99e42STrevor Thompson } else {
357*8fc99e42STrevor Thompson cmn_err(CE_WARN, "todblade: Could not write the RTC\n");
358*8fc99e42STrevor Thompson }
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate * Unfreeze
3627c478bd9Sstevel@tonic-gate */
3637c478bd9Sstevel@tonic-gate RTC_PUT8(RTC_B, regb);
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate * The TOD alarm functionality is not supported on our platform
3707c478bd9Sstevel@tonic-gate * as the interrupt is not wired, so do nothing.
3717c478bd9Sstevel@tonic-gate */
3727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3737c478bd9Sstevel@tonic-gate static void
todbl_set_power_alarm(timestruc_t ts)3747c478bd9Sstevel@tonic-gate todbl_set_power_alarm(timestruc_t ts)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate * clear alarm interrupt
3817c478bd9Sstevel@tonic-gate */
3827c478bd9Sstevel@tonic-gate static void
todbl_clear_power_alarm(void)3837c478bd9Sstevel@tonic-gate todbl_clear_power_alarm(void)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate * Determine the cpu frequency by watching the TOD chip rollover twice.
3907c478bd9Sstevel@tonic-gate * Cpu clock rate is determined by computing the ticks added (in tick register)
3917c478bd9Sstevel@tonic-gate * during one second interval on TOD.
3927c478bd9Sstevel@tonic-gate */
3937c478bd9Sstevel@tonic-gate uint64_t
todbl_get_cpufrequency(void)3947c478bd9Sstevel@tonic-gate todbl_get_cpufrequency(void)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
3977c478bd9Sstevel@tonic-gate M5819_ADDR_REG = RTC_SEC;
3987c478bd9Sstevel@tonic-gate return (find_cpufrequency(v_rtc_data_reg));
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate static uint_t
todbl_set_watchdog_timer(uint_t timeoutval)4037c478bd9Sstevel@tonic-gate todbl_set_watchdog_timer(uint_t timeoutval)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate * We get started during kernel intilaisation only
4077c478bd9Sstevel@tonic-gate * if watchdog_enable is set.
4087c478bd9Sstevel@tonic-gate */
4097c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate if (watchdog_available && (!watchdog_activated ||
4127c478bd9Sstevel@tonic-gate (watchdog_activated && (timeoutval != watchdog_timeout_seconds)))) {
4137c478bd9Sstevel@tonic-gate watchdog_timeout_seconds = timeoutval;
4147c478bd9Sstevel@tonic-gate if (configure_wdog(WDOG_ON))
4157c478bd9Sstevel@tonic-gate return (watchdog_timeout_seconds);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate return (0);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate static uint_t
todbl_clear_watchdog_timer(void)4217c478bd9Sstevel@tonic-gate todbl_clear_watchdog_timer(void)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate * The core kernel will call us here to disable the wdog when:
4257c478bd9Sstevel@tonic-gate * 1. we're panicing
4267c478bd9Sstevel@tonic-gate * 2. we're entering debug
4277c478bd9Sstevel@tonic-gate * 3. we're rebooting
4287c478bd9Sstevel@tonic-gate */
4297c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate if (watchdog_available && watchdog_activated) {
4327c478bd9Sstevel@tonic-gate watchdog_enable = 0;
4337c478bd9Sstevel@tonic-gate if (!configure_wdog(WDOG_OFF))
4347c478bd9Sstevel@tonic-gate return (0);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate return (watchdog_timeout_seconds);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate static uint_t
configure_wdog(uint8_t new_state)4407c478bd9Sstevel@tonic-gate configure_wdog(uint8_t new_state)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate bscv_wdog_t wdog_cmd;
4437c478bd9Sstevel@tonic-gate struct bscv_idi_info bscv_info;
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate if (new_state == WDOG_ON || new_state == WDOG_OFF) {
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate wdog_cmd.enable_wdog = new_state;
4487c478bd9Sstevel@tonic-gate wdog_cmd.wdog_timeout_s = watchdog_timeout_seconds;
4497c478bd9Sstevel@tonic-gate wdog_cmd.reset_system_on_timeout = wdog_reset_on_timeout;
4507c478bd9Sstevel@tonic-gate bscv_info.type = BSCV_IDI_WDOG_CFG;
4517c478bd9Sstevel@tonic-gate bscv_info.data = &wdog_cmd;
4527c478bd9Sstevel@tonic-gate bscv_info.size = sizeof (wdog_cmd);
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate if (bsc_drv_func_ptr != NULL) {
4557c478bd9Sstevel@tonic-gate watchdog_activated = new_state;
4567c478bd9Sstevel@tonic-gate (*bsc_drv_func_ptr)(&bscv_info);
4577c478bd9Sstevel@tonic-gate return (1);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate return (0);
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate }
463