Home | History | Annotate | Line # | Download | only in dev
rtclock.c revision 1.1.1.1.2.1
      1  1.1.1.1.2.1  thorpej /*	$NetBSD: rtclock.c,v 1.1.1.1.2.1 1997/01/18 04:29:18 thorpej Exp $	*/
      2          1.1      oki 
      3          1.1      oki /*
      4          1.1      oki  * Copyright 1993, 1994 Masaru Oki
      5          1.1      oki  * All rights reserved.
      6          1.1      oki  *
      7          1.1      oki  * Redistribution and use in source and binary forms, with or without
      8          1.1      oki  * modification, are permitted provided that the following conditions
      9          1.1      oki  * are met:
     10          1.1      oki  * 1. Redistributions of source code must retain the above copyright
     11          1.1      oki  *    notice, this list of conditions and the following disclaimer.
     12          1.1      oki  * 2. Redistributions in binary form must reproduce the above copyright
     13          1.1      oki  *    notice, this list of conditions and the following disclaimer in the
     14          1.1      oki  *    documentation and/or other materials provided with the distribution.
     15          1.1      oki  * 3. All advertising materials mentioning features or use of this software
     16          1.1      oki  *    must display the following acknowledgement:
     17          1.1      oki  *      This product includes software developed by Masaru Oki.
     18          1.1      oki  * 4. The name of the author may not be used to endorse or promote products
     19          1.1      oki  *    derived from this software without specific prior written permission
     20          1.1      oki  *
     21          1.1      oki  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22          1.1      oki  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23          1.1      oki  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24          1.1      oki  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25          1.1      oki  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26          1.1      oki  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27          1.1      oki  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28          1.1      oki  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29          1.1      oki  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30          1.1      oki  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31          1.1      oki  */
     32          1.1      oki 
     33          1.1      oki /*
     34          1.1      oki  * X680x0 internal real time clock interface
     35          1.1      oki  * alarm is not supported.
     36          1.1      oki  */
     37          1.1      oki 
     38          1.1      oki #include <sys/param.h>
     39          1.1      oki #include <sys/systm.h>
     40          1.1      oki #include <sys/buf.h>
     41          1.1      oki #include <sys/malloc.h>
     42          1.1      oki #include <sys/proc.h>
     43          1.1      oki #include <sys/reboot.h>
     44          1.1      oki #include <sys/file.h>
     45  1.1.1.1.2.1  thorpej #include <sys/kernel.h>
     46          1.1      oki 
     47          1.1      oki #include <x68k/dev/rtclock_var.h>
     48          1.1      oki #include <x68k/x68k/iodevice.h>
     49          1.1      oki 
     50          1.1      oki static u_long rtgettod __P((void));
     51          1.1      oki static int  rtsettod __P((long));
     52          1.1      oki 
     53          1.1      oki /*
     54          1.1      oki  * x68k/clock.c calls thru this vector, if it is set, to read
     55          1.1      oki  * the realtime clock.
     56          1.1      oki  */
     57          1.1      oki u_long (*gettod) __P((void));
     58          1.1      oki int (*settod)();
     59          1.1      oki 
     60          1.1      oki static volatile union rtc *rtc_addr = 0;
     61          1.1      oki 
     62          1.1      oki int
     63          1.1      oki rtclockinit()
     64          1.1      oki {
     65          1.1      oki 	rtc_addr = &IODEVbase->io_rtc;
     66          1.1      oki 
     67          1.1      oki 	if (rtgettod())	{
     68          1.1      oki 		gettod = rtgettod;
     69          1.1      oki 		settod = rtsettod;
     70          1.1      oki 	} else {
     71          1.1      oki 		return 0;
     72          1.1      oki 	}
     73          1.1      oki 	return 1;
     74          1.1      oki }
     75          1.1      oki 
     76          1.1      oki static int month_days[12] = {
     77          1.1      oki 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
     78          1.1      oki };
     79          1.1      oki 
     80          1.1      oki static u_long
     81          1.1      oki rtgettod()
     82          1.1      oki {
     83          1.1      oki 	register int i;
     84          1.1      oki 	register u_long tmp;
     85          1.1      oki 	int year, month, day, hour, min, sec;
     86          1.1      oki 
     87          1.1      oki 	/* hold clock */
     88          1.1      oki 	RTC_WRITE(rtc_addr, mode, RTC_HOLD_CLOCK);
     89          1.1      oki 
     90          1.1      oki 	/* read it */
     91          1.1      oki 	sec   = RTC_REG(sec10)  * 10 + RTC_REG(sec);
     92          1.1      oki 	min   = RTC_REG(min10)  * 10 + RTC_REG(min);
     93          1.1      oki 	hour  = RTC_REG(hour10) * 10 + RTC_REG(hour);
     94          1.1      oki 	day   = RTC_REG(day10)  * 10 + RTC_REG(day);
     95          1.1      oki 	month = RTC_REG(mon10)  * 10 + RTC_REG(mon);
     96          1.1      oki 	year  = RTC_REG(year10) * 10 + RTC_REG(year)  + 1980;
     97          1.1      oki 
     98          1.1      oki 	/* let it run again.. */
     99          1.1      oki 	RTC_WRITE(rtc_addr, mode, RTC_FREE_CLOCK);
    100          1.1      oki 
    101          1.1      oki 	range_test(hour, 0, 23);
    102          1.1      oki 	range_test(day, 1, 31);
    103          1.1      oki 	range_test(month, 1, 12);
    104          1.1      oki 	range_test(year, STARTOFTIME, 2000);
    105          1.1      oki 
    106          1.1      oki 	tmp = 0;
    107          1.1      oki 
    108          1.1      oki 	for (i = STARTOFTIME; i < year; i++)
    109          1.1      oki 		tmp += days_in_year(i);
    110          1.1      oki 	if (leapyear(year) && month > FEBRUARY)
    111          1.1      oki 		tmp++;
    112          1.1      oki 
    113          1.1      oki 	for (i = 1; i < month; i++)
    114          1.1      oki 		tmp += days_in_month(i);
    115          1.1      oki 
    116          1.1      oki 	tmp += (day - 1);
    117          1.1      oki 
    118  1.1.1.1.2.1  thorpej 	tmp = ((tmp * 24 + hour) * 60 + min + rtc_offset) * 60 + sec;
    119          1.1      oki 
    120          1.1      oki 	return tmp;
    121          1.1      oki }
    122          1.1      oki 
    123          1.1      oki static int
    124          1.1      oki rtsettod (tim)
    125          1.1      oki 	long tim;
    126          1.1      oki {
    127          1.1      oki 	/*
    128          1.1      oki 	 * I don't know if setting the clock is analogous
    129          1.1      oki 	 * to reading it, I don't have demo-code for setting.
    130          1.1      oki 	 * just give it a try..
    131          1.1      oki 	 */
    132          1.1      oki 	register int i;
    133          1.1      oki 	register long hms, day;
    134          1.1      oki 	u_char sec1, sec2;
    135          1.1      oki 	u_char min1, min2;
    136          1.1      oki 	u_char hour1, hour2;
    137          1.1      oki 	u_char day1, day2;
    138          1.1      oki 	u_char mon1, mon2;
    139          1.1      oki 	u_char year1, year2;
    140          1.1      oki 
    141          1.1      oki 	/*
    142          1.1      oki 	 * there seem to be problems with the bitfield addressing
    143          1.1      oki 	 * currently used..
    144          1.1      oki 	 */
    145          1.1      oki 	if (!rtc_addr)
    146          1.1      oki 		return 0;
    147          1.1      oki 
    148  1.1.1.1.2.1  thorpej 	tim -= (rtc_offset * 60);
    149          1.1      oki 
    150          1.1      oki 	/* prepare values to be written to clock */
    151          1.1      oki 	day = tim / SECDAY;
    152          1.1      oki 	hms = tim % SECDAY;
    153          1.1      oki 
    154          1.1      oki 	hour2 = hms / 3600;
    155          1.1      oki 	hour1 = hour2 / 10;
    156          1.1      oki 	hour2 %= 10;
    157          1.1      oki 
    158          1.1      oki 	min2 = (hms % 3600) / 60;
    159          1.1      oki 	min1 = min2 / 10;
    160          1.1      oki 	min2 %= 10;
    161          1.1      oki 
    162          1.1      oki 	sec2 = (hms % 3600) % 60;
    163          1.1      oki 	sec1 = sec2 / 10;
    164          1.1      oki 	sec2 %= 10;
    165          1.1      oki 
    166          1.1      oki 	/* Number of years in days */
    167          1.1      oki 	for (i = STARTOFTIME - 1980; day >= days_in_year(i); i++)
    168          1.1      oki 		day -= days_in_year(i);
    169          1.1      oki 	year1 = i / 10;
    170          1.1      oki 	year2 = i % 10;
    171          1.1      oki 
    172          1.1      oki 	/* Number of months in days left */
    173          1.1      oki 	if (leapyear(i))
    174          1.1      oki 		days_in_month(FEBRUARY) = 29;
    175          1.1      oki 	for (i = 1; day >= days_in_month(i); i++)
    176          1.1      oki 		day -= days_in_month(i);
    177          1.1      oki 	days_in_month(FEBRUARY) = 28;
    178          1.1      oki 
    179          1.1      oki 	mon1 = i / 10;
    180          1.1      oki 	mon2 = i % 10;
    181          1.1      oki 
    182          1.1      oki 	/* Days are what is left over (+1) from all that. */
    183          1.1      oki 	day ++;
    184          1.1      oki 	day1 = day / 10;
    185          1.1      oki 	day2 = day % 10;
    186          1.1      oki 
    187          1.1      oki 	RTC_WRITE(rtc_addr, mode,   RTC_HOLD_CLOCK);
    188          1.1      oki 	RTC_WRITE(rtc_addr, sec10,  sec1);
    189          1.1      oki 	RTC_WRITE(rtc_addr, sec,    sec2);
    190          1.1      oki 	RTC_WRITE(rtc_addr, min10,  min1);
    191          1.1      oki 	RTC_WRITE(rtc_addr, min,    min2);
    192          1.1      oki 	RTC_WRITE(rtc_addr, hour10, hour1);
    193          1.1      oki 	RTC_WRITE(rtc_addr, hour,   hour2);
    194          1.1      oki 	RTC_WRITE(rtc_addr, day10,  day1);
    195          1.1      oki 	RTC_WRITE(rtc_addr, day,    day2);
    196          1.1      oki 	RTC_WRITE(rtc_addr, mon10,  mon1);
    197          1.1      oki 	RTC_WRITE(rtc_addr, mon,    mon2);
    198          1.1      oki 	RTC_WRITE(rtc_addr, year10, year1);
    199          1.1      oki 	RTC_WRITE(rtc_addr, year,   year2);
    200          1.1      oki 	RTC_WRITE(rtc_addr, mode,   RTC_FREE_CLOCK);
    201          1.1      oki 
    202          1.1      oki 	return 1;
    203          1.1      oki }
    204