Home | History | Annotate | Line # | Download | only in dev
rtclock.c revision 1.2.8.1
      1  1.2.8.1  thorpej /*	$NetBSD: rtclock.c,v 1.2.8.1 1997/10/14 10:20:35 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.2    perry #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.2.8.1  thorpej int (*settod) __P((long));
     59      1.1      oki 
     60      1.1      oki static volatile union rtc *rtc_addr = 0;
     61  1.2.8.1  thorpej int rtclockinit __P((void));
     62      1.1      oki 
     63      1.1      oki int
     64      1.1      oki rtclockinit()
     65      1.1      oki {
     66      1.1      oki 	rtc_addr = &IODEVbase->io_rtc;
     67      1.1      oki 
     68      1.1      oki 	if (rtgettod())	{
     69      1.1      oki 		gettod = rtgettod;
     70      1.1      oki 		settod = rtsettod;
     71      1.1      oki 	} else {
     72      1.1      oki 		return 0;
     73      1.1      oki 	}
     74      1.1      oki 	return 1;
     75      1.1      oki }
     76      1.1      oki 
     77      1.1      oki static int month_days[12] = {
     78      1.1      oki 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
     79      1.1      oki };
     80      1.1      oki 
     81      1.1      oki static u_long
     82      1.1      oki rtgettod()
     83      1.1      oki {
     84      1.1      oki 	register int i;
     85      1.1      oki 	register u_long tmp;
     86      1.1      oki 	int year, month, day, hour, min, sec;
     87      1.1      oki 
     88      1.1      oki 	/* hold clock */
     89      1.1      oki 	RTC_WRITE(rtc_addr, mode, RTC_HOLD_CLOCK);
     90      1.1      oki 
     91      1.1      oki 	/* read it */
     92      1.1      oki 	sec   = RTC_REG(sec10)  * 10 + RTC_REG(sec);
     93      1.1      oki 	min   = RTC_REG(min10)  * 10 + RTC_REG(min);
     94      1.1      oki 	hour  = RTC_REG(hour10) * 10 + RTC_REG(hour);
     95      1.1      oki 	day   = RTC_REG(day10)  * 10 + RTC_REG(day);
     96      1.1      oki 	month = RTC_REG(mon10)  * 10 + RTC_REG(mon);
     97      1.1      oki 	year  = RTC_REG(year10) * 10 + RTC_REG(year)  + 1980;
     98      1.1      oki 
     99      1.1      oki 	/* let it run again.. */
    100      1.1      oki 	RTC_WRITE(rtc_addr, mode, RTC_FREE_CLOCK);
    101      1.1      oki 
    102      1.1      oki 	range_test(hour, 0, 23);
    103      1.1      oki 	range_test(day, 1, 31);
    104      1.1      oki 	range_test(month, 1, 12);
    105      1.1      oki 	range_test(year, STARTOFTIME, 2000);
    106      1.1      oki 
    107      1.1      oki 	tmp = 0;
    108      1.1      oki 
    109      1.1      oki 	for (i = STARTOFTIME; i < year; i++)
    110      1.1      oki 		tmp += days_in_year(i);
    111      1.1      oki 	if (leapyear(year) && month > FEBRUARY)
    112      1.1      oki 		tmp++;
    113      1.1      oki 
    114      1.1      oki 	for (i = 1; i < month; i++)
    115      1.1      oki 		tmp += days_in_month(i);
    116      1.1      oki 
    117      1.1      oki 	tmp += (day - 1);
    118      1.1      oki 
    119      1.2    perry 	tmp = ((tmp * 24 + hour) * 60 + min + rtc_offset) * 60 + sec;
    120      1.1      oki 
    121      1.1      oki 	return tmp;
    122      1.1      oki }
    123      1.1      oki 
    124      1.1      oki static int
    125      1.1      oki rtsettod (tim)
    126      1.1      oki 	long tim;
    127      1.1      oki {
    128      1.1      oki 	/*
    129      1.1      oki 	 * I don't know if setting the clock is analogous
    130      1.1      oki 	 * to reading it, I don't have demo-code for setting.
    131      1.1      oki 	 * just give it a try..
    132      1.1      oki 	 */
    133      1.1      oki 	register int i;
    134      1.1      oki 	register long hms, day;
    135      1.1      oki 	u_char sec1, sec2;
    136      1.1      oki 	u_char min1, min2;
    137      1.1      oki 	u_char hour1, hour2;
    138      1.1      oki 	u_char day1, day2;
    139      1.1      oki 	u_char mon1, mon2;
    140      1.1      oki 	u_char year1, year2;
    141      1.1      oki 
    142      1.1      oki 	/*
    143      1.1      oki 	 * there seem to be problems with the bitfield addressing
    144      1.1      oki 	 * currently used..
    145      1.1      oki 	 */
    146      1.1      oki 	if (!rtc_addr)
    147      1.1      oki 		return 0;
    148      1.1      oki 
    149      1.2    perry 	tim -= (rtc_offset * 60);
    150      1.1      oki 
    151      1.1      oki 	/* prepare values to be written to clock */
    152      1.1      oki 	day = tim / SECDAY;
    153      1.1      oki 	hms = tim % SECDAY;
    154      1.1      oki 
    155      1.1      oki 	hour2 = hms / 3600;
    156      1.1      oki 	hour1 = hour2 / 10;
    157      1.1      oki 	hour2 %= 10;
    158      1.1      oki 
    159      1.1      oki 	min2 = (hms % 3600) / 60;
    160      1.1      oki 	min1 = min2 / 10;
    161      1.1      oki 	min2 %= 10;
    162      1.1      oki 
    163      1.1      oki 	sec2 = (hms % 3600) % 60;
    164      1.1      oki 	sec1 = sec2 / 10;
    165      1.1      oki 	sec2 %= 10;
    166      1.1      oki 
    167      1.1      oki 	/* Number of years in days */
    168      1.1      oki 	for (i = STARTOFTIME - 1980; day >= days_in_year(i); i++)
    169      1.1      oki 		day -= days_in_year(i);
    170      1.1      oki 	year1 = i / 10;
    171      1.1      oki 	year2 = i % 10;
    172      1.1      oki 
    173      1.1      oki 	/* Number of months in days left */
    174      1.1      oki 	if (leapyear(i))
    175      1.1      oki 		days_in_month(FEBRUARY) = 29;
    176      1.1      oki 	for (i = 1; day >= days_in_month(i); i++)
    177      1.1      oki 		day -= days_in_month(i);
    178      1.1      oki 	days_in_month(FEBRUARY) = 28;
    179      1.1      oki 
    180      1.1      oki 	mon1 = i / 10;
    181      1.1      oki 	mon2 = i % 10;
    182      1.1      oki 
    183      1.1      oki 	/* Days are what is left over (+1) from all that. */
    184      1.1      oki 	day ++;
    185      1.1      oki 	day1 = day / 10;
    186      1.1      oki 	day2 = day % 10;
    187      1.1      oki 
    188      1.1      oki 	RTC_WRITE(rtc_addr, mode,   RTC_HOLD_CLOCK);
    189      1.1      oki 	RTC_WRITE(rtc_addr, sec10,  sec1);
    190      1.1      oki 	RTC_WRITE(rtc_addr, sec,    sec2);
    191      1.1      oki 	RTC_WRITE(rtc_addr, min10,  min1);
    192      1.1      oki 	RTC_WRITE(rtc_addr, min,    min2);
    193      1.1      oki 	RTC_WRITE(rtc_addr, hour10, hour1);
    194      1.1      oki 	RTC_WRITE(rtc_addr, hour,   hour2);
    195      1.1      oki 	RTC_WRITE(rtc_addr, day10,  day1);
    196      1.1      oki 	RTC_WRITE(rtc_addr, day,    day2);
    197      1.1      oki 	RTC_WRITE(rtc_addr, mon10,  mon1);
    198      1.1      oki 	RTC_WRITE(rtc_addr, mon,    mon2);
    199      1.1      oki 	RTC_WRITE(rtc_addr, year10, year1);
    200      1.1      oki 	RTC_WRITE(rtc_addr, year,   year2);
    201      1.1      oki 	RTC_WRITE(rtc_addr, mode,   RTC_FREE_CLOCK);
    202      1.1      oki 
    203      1.1      oki 	return 1;
    204      1.1      oki }
    205