Home | History | Annotate | Line # | Download | only in boot
      1 /*      $NetBSD: rtc.c,v 1.8 2023/02/12 08:25:09 tsutsui Exp $        */
      2 /*
      3  * Copyright (c) 1997 Rolf Grossmann
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Rolf Grossmann.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <next68k/dev/clockreg.h>
     34 #include <machine/cpu.h>
     35 #include <lib/libsa/stand.h>
     36 #include <lib/libsa/net.h>
     37 
     38 #include "samachdep.h"
     39 
     40 u_char rtc_read(u_char);
     41 
     42 static volatile u_int *scr2 = (u_int *)NEXT_P_SCR2_CON;
     43 static u_char new_clock;
     44 
     45 u_char
     46 rtc_read(u_char reg)
     47 {
     48 	int i;
     49 	u_int tmp;
     50 	u_char val;
     51 
     52 	*scr2 = (*scr2 & ~(SCR2_RTDATA | SCR2_RTCLK)) | SCR2_RTCE;
     53 	DELAY(1);
     54 
     55 	val = reg;
     56 	for (i=0; i<8; i++) {
     57 		tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK);
     58 		if (val & 0x80)
     59 			tmp |= SCR2_RTDATA;
     60 
     61 		*scr2 = tmp;
     62 		DELAY(1);
     63 		*scr2 = tmp | SCR2_RTCLK;
     64 		DELAY(1);
     65 		*scr2 = tmp;
     66 		DELAY(1);
     67 
     68 		val <<= 1;
     69 	}
     70 
     71 	val = 0;			/* should be anyway */
     72 	for (i=0; i<8; i++) {
     73 		val <<= 1;
     74 
     75 		tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK);
     76 
     77 		*scr2 = tmp | SCR2_RTCLK;
     78 		DELAY(1);
     79 		*scr2 = tmp;
     80 		DELAY(1);
     81 
     82 		if (*scr2 & SCR2_RTDATA)
     83 			val |= 1;
     84 	}
     85 
     86 	*scr2 &= ~(SCR2_RTDATA|SCR2_RTCLK|SCR2_RTCE);
     87 	DELAY(1);
     88 
     89 	return val;
     90 }
     91 
     92 void
     93 rtc_init(void)
     94 {
     95 	u_char val;
     96 
     97 	val = rtc_read(RTC_STATUS);
     98 	new_clock = (val & RTC_NEW_CLOCK) ? 1 : 0;
     99 }
    100 
    101 satime_t
    102 getsecs(void)
    103 {
    104 	u_int secs;
    105 
    106 	if (new_clock) {
    107 		secs = rtc_read(RTC_CNTR0) << 24 |
    108 		       rtc_read(RTC_CNTR1) << 16 |
    109 		       rtc_read(RTC_CNTR2) << 8  |
    110 		       rtc_read(RTC_CNTR3);
    111 	} else {
    112 		u_char d,h,m,s;
    113 #define BCD_DECODE(x) (((x) >> 4) * 10 + ((x) & 0xf))
    114 
    115 		d = rtc_read(RTC_DAY);
    116 		h = rtc_read(RTC_HRS);
    117 		m = rtc_read(RTC_MIN);
    118 		s = rtc_read(RTC_SEC);
    119 		secs = BCD_DECODE(d) * (60*60*24) +
    120 		       BCD_DECODE(h) * (60*60) +
    121 		       BCD_DECODE(m) * 60 +
    122 		       BCD_DECODE(s);
    123 	}
    124 
    125 	return secs;
    126 }
    127