xref: /titanic_44/usr/src/uts/sun4u/io/todmostek.c (revision 88294e09b5c27cbb12b6735e2fb247a86b76666d)
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*88294e09SRichard Bean  * Common Development and Distribution License (the "License").
6*88294e09SRichard 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*88294e09SRichard Bean  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * tod driver module for Mostek M48T59 part
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
367c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include <sys/clock.h>
397c478bd9Sstevel@tonic-gate #include <sys/todmostek.h>
407c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
417c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
427c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static timestruc_t	todm_get(void);
457c478bd9Sstevel@tonic-gate static void		todm_set(timestruc_t);
467c478bd9Sstevel@tonic-gate static uint_t		todm_set_watchdog_timer(uint_t);
477c478bd9Sstevel@tonic-gate static uint_t		todm_clear_watchdog_timer(void);
487c478bd9Sstevel@tonic-gate static void		todm_set_power_alarm(timestruc_t);
497c478bd9Sstevel@tonic-gate static void		todm_clear_power_alarm(void);
507c478bd9Sstevel@tonic-gate static uint64_t		todm_get_cpufrequency(void);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static uchar_t watchdog_bits = 0;
537c478bd9Sstevel@tonic-gate static uint_t watchdog_timeout;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate extern uint64_t find_cpufrequency(volatile uchar_t *);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
61*88294e09SRichard Bean 	&mod_miscops, "tod module for Mostek M48T59"
627c478bd9Sstevel@tonic-gate };
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
657c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int
697c478bd9Sstevel@tonic-gate _init(void)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todmostek") == 0) {
727c478bd9Sstevel@tonic-gate 		tod_ops.tod_get = todm_get;
737c478bd9Sstevel@tonic-gate 		tod_ops.tod_set = todm_set;
747c478bd9Sstevel@tonic-gate 		tod_ops.tod_set_watchdog_timer = todm_set_watchdog_timer;
757c478bd9Sstevel@tonic-gate 		tod_ops.tod_clear_watchdog_timer = todm_clear_watchdog_timer;
767c478bd9Sstevel@tonic-gate 		tod_ops.tod_set_power_alarm = todm_set_power_alarm;
777c478bd9Sstevel@tonic-gate 		tod_ops.tod_clear_power_alarm = todm_clear_power_alarm;
787c478bd9Sstevel@tonic-gate 		tod_ops.tod_get_cpufrequency = todm_get_cpufrequency;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		/*
817c478bd9Sstevel@tonic-gate 		 * check if hardware watchdog timer is available and user
827c478bd9Sstevel@tonic-gate 		 * enabled it.
837c478bd9Sstevel@tonic-gate 		 */
847c478bd9Sstevel@tonic-gate 		if (watchdog_enable) {
857c478bd9Sstevel@tonic-gate 			if (!watchdog_available) {
867c478bd9Sstevel@tonic-gate 			    cmn_err(CE_WARN, "Hardware watchdog unavailable");
877c478bd9Sstevel@tonic-gate 			} else if (boothowto & RB_DEBUG) {
887c478bd9Sstevel@tonic-gate 			    cmn_err(CE_WARN, "Hardware watchdog disabled"
897c478bd9Sstevel@tonic-gate 				" [debugger]");
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 		}
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate int
987c478bd9Sstevel@tonic-gate _fini(void)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todmostek") == 0)
1017c478bd9Sstevel@tonic-gate 		return (EBUSY);
1027c478bd9Sstevel@tonic-gate 	else
1037c478bd9Sstevel@tonic-gate 		return (mod_remove(&modlinkage));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate int
1077c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Read the current time from the clock chip and convert to UNIX form.
1147c478bd9Sstevel@tonic-gate  * Assumes that the year in the clock chip is valid.
1157c478bd9Sstevel@tonic-gate  * Must be called with tod_lock held.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate static timestruc_t
1187c478bd9Sstevel@tonic-gate todm_get(void)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	timestruc_t ts;
1217c478bd9Sstevel@tonic-gate #ifndef	MPSAS
1227c478bd9Sstevel@tonic-gate 	todinfo_t tod;
1237c478bd9Sstevel@tonic-gate 	int s;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	s = splhi();
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	CLOCK->clk_ctrl |= CLK_CTRL_READ;
1307c478bd9Sstevel@tonic-gate 	tod.tod_year	= BCD_TO_BYTE(CLOCK->clk_year) + YRBASE;
1317c478bd9Sstevel@tonic-gate 	tod.tod_month	= BCD_TO_BYTE(CLOCK->clk_month & 0x1f);
1327c478bd9Sstevel@tonic-gate 	tod.tod_day	= BCD_TO_BYTE(CLOCK->clk_day & 0x3f);
1337c478bd9Sstevel@tonic-gate 	tod.tod_dow	= BCD_TO_BYTE(CLOCK->clk_weekday & 0x7);
1347c478bd9Sstevel@tonic-gate 	tod.tod_hour	= BCD_TO_BYTE(CLOCK->clk_hour & 0x3f);
1357c478bd9Sstevel@tonic-gate 	tod.tod_min	= BCD_TO_BYTE(CLOCK->clk_min & 0x7f);
1367c478bd9Sstevel@tonic-gate 	tod.tod_sec	= BCD_TO_BYTE(CLOCK->clk_sec & 0x7f);
1377c478bd9Sstevel@tonic-gate 	CLOCK->clk_ctrl &= ~CLK_CTRL_READ;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	splx(s);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * Apparently the m48t59 doesn't quite do what the spec sheet says.
1437c478bd9Sstevel@tonic-gate 	 * The spec says reading WRD will reset the timer but that doesn't work.
1447c478bd9Sstevel@tonic-gate 	 * So we need to reload timeout each time we want to reset the timer.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	CLOCK->clk_watchdog = watchdog_bits;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	ts.tv_sec = tod_to_utc(tod);
1497c478bd9Sstevel@tonic-gate 	ts.tv_nsec = 0;
1507c478bd9Sstevel@tonic-gate #else
1517c478bd9Sstevel@tonic-gate 	ts.tv_sec = 0;
1527c478bd9Sstevel@tonic-gate 	ts.tv_nsec = 0;
1537c478bd9Sstevel@tonic-gate #endif
1547c478bd9Sstevel@tonic-gate 	return (ts);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Write the specified time into the clock chip.
1597c478bd9Sstevel@tonic-gate  * Must be called with tod_lock held.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate /* ARGSUSED */
1627c478bd9Sstevel@tonic-gate static void
1637c478bd9Sstevel@tonic-gate todm_set(timestruc_t ts)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate #ifndef	MPSAS
1667c478bd9Sstevel@tonic-gate 	todinfo_t tod = utc_to_tod(ts.tv_sec);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	CLOCK->clk_ctrl |= CLK_CTRL_WRITE;	/* allow writes */
1717c478bd9Sstevel@tonic-gate 	CLOCK->clk_year		= BYTE_TO_BCD(tod.tod_year - YRBASE);
1727c478bd9Sstevel@tonic-gate 	CLOCK->clk_month	= BYTE_TO_BCD(tod.tod_month);
1737c478bd9Sstevel@tonic-gate 	CLOCK->clk_day		= BYTE_TO_BCD(tod.tod_day);
1747c478bd9Sstevel@tonic-gate 	CLOCK->clk_weekday	= BYTE_TO_BCD(tod.tod_dow);
1757c478bd9Sstevel@tonic-gate 	CLOCK->clk_hour		= BYTE_TO_BCD(tod.tod_hour);
1767c478bd9Sstevel@tonic-gate 	CLOCK->clk_min		= BYTE_TO_BCD(tod.tod_min);
1777c478bd9Sstevel@tonic-gate 	CLOCK->clk_sec		= BYTE_TO_BCD(tod.tod_sec);
1787c478bd9Sstevel@tonic-gate 	CLOCK->clk_ctrl &= ~CLK_CTRL_WRITE;	/* load values */
1797c478bd9Sstevel@tonic-gate #endif
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * Program the watchdog timer shadow register with the specified value.
1857c478bd9Sstevel@tonic-gate  * Setting the timer to zero value means no watchdog timeout.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate static uint_t
1887c478bd9Sstevel@tonic-gate todm_set_watchdog_timer(uint_t timeoutval)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (watchdog_enable == 0 || watchdog_available == 0 ||
1937c478bd9Sstevel@tonic-gate 		(boothowto & RB_DEBUG))
1947c478bd9Sstevel@tonic-gate 			return (0);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	watchdog_timeout = timeoutval;
1977c478bd9Sstevel@tonic-gate 	watchdog_bits = CLK_WATCHDOG_BITS(timeoutval);
1987c478bd9Sstevel@tonic-gate 	watchdog_activated = 1;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	return (timeoutval);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * Clear the hardware timer register. Also zero out the watchdog timer
2057c478bd9Sstevel@tonic-gate  * shadow register.
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate static uint_t
2087c478bd9Sstevel@tonic-gate todm_clear_watchdog_timer(void)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (watchdog_activated == 0)
2137c478bd9Sstevel@tonic-gate 		return (0);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate #ifndef	MPSAS
2167c478bd9Sstevel@tonic-gate 	CLOCK->clk_watchdog = 0;
2177c478bd9Sstevel@tonic-gate #endif /* MPSAS */
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	watchdog_bits = 0;
2207c478bd9Sstevel@tonic-gate 	watchdog_activated = 0;
2217c478bd9Sstevel@tonic-gate 	return (watchdog_timeout);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * program the tod registers for alarm to go off at the specified time
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate static void
2287c478bd9Sstevel@tonic-gate todm_set_power_alarm(timestruc_t ts)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate #ifndef	MPSAS
2317c478bd9Sstevel@tonic-gate 	todinfo_t	tod;
2327c478bd9Sstevel@tonic-gate 	uchar_t	c;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
2357c478bd9Sstevel@tonic-gate 	tod = utc_to_tod(ts.tv_sec);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	c = CLOCK->clk_flags; /* clear alarm intr flag by reading the reg */
2387c478bd9Sstevel@tonic-gate #ifdef lint
2397c478bd9Sstevel@tonic-gate 	CLOCK->clk_flags = c;
2407c478bd9Sstevel@tonic-gate #endif
2417c478bd9Sstevel@tonic-gate 	CLOCK->clk_interrupts &= ~CLK_ALARM_ENABLE; /* disable alarm intr */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	CLOCK->clk_day &= ~CLK_FREQT; /* keep Freqency Test bit cleared */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	CLOCK->clk_alm_day = BYTE_TO_BCD(tod.tod_day);
2467c478bd9Sstevel@tonic-gate 	CLOCK->clk_alm_hours = BYTE_TO_BCD(tod.tod_hour);
2477c478bd9Sstevel@tonic-gate 	CLOCK->clk_alm_mins = BYTE_TO_BCD(tod.tod_min);
2487c478bd9Sstevel@tonic-gate 	CLOCK->clk_alm_secs = BYTE_TO_BCD(tod.tod_sec);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	CLOCK->clk_interrupts |= CLK_ALARM_ENABLE; /* enable alarm intr */
2517c478bd9Sstevel@tonic-gate #endif /* MPSAS */
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * clear alarm interrupt
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate static void
2587c478bd9Sstevel@tonic-gate todm_clear_power_alarm()
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate #ifndef	MPSAS
2617c478bd9Sstevel@tonic-gate 	uchar_t	c;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	c = CLOCK->clk_flags; /* clear alarm intr flag by reading the reg */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate #ifdef lint
2687c478bd9Sstevel@tonic-gate 	CLOCK->clk_flags = c;
2697c478bd9Sstevel@tonic-gate #endif
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	CLOCK->clk_interrupts &= ~CLK_ALARM_ENABLE; /* disable alarm intr */
2727c478bd9Sstevel@tonic-gate #endif /* MPSAS */
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Determine the cpu frequency by watching the TOD chip rollover twice.
2777c478bd9Sstevel@tonic-gate  * Cpu clock rate is determined by computing the ticks added (in tick register)
2787c478bd9Sstevel@tonic-gate  * during one second interval on TOD.
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate uint64_t
2817c478bd9Sstevel@tonic-gate todm_get_cpufrequency(void)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate #ifndef	MPSAS
2847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	return (find_cpufrequency(&(TIMECHECK_CLOCK->clk_sec)));
2877c478bd9Sstevel@tonic-gate #else
2887c478bd9Sstevel@tonic-gate 	return (cpunodes[CPU->cpu_id].clock_freq);
2897c478bd9Sstevel@tonic-gate #endif /* MPSAS */
2907c478bd9Sstevel@tonic-gate }
291