Home | History | Annotate | Line # | Download | only in dev
rtclock.c revision 1.3.10.1
      1 /*	$NetBSD: rtclock.c,v 1.3.10.1 1998/12/23 16:47:31 minoura Exp $	*/
      2 
      3 /*
      4  * Copyright 1993, 1994 Masaru Oki
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Masaru Oki.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * X680x0 internal real time clock interface
     35  * alarm is not supported.
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/buf.h>
     41 #include <sys/malloc.h>
     42 #include <sys/proc.h>
     43 #include <sys/reboot.h>
     44 #include <sys/file.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.h>
     47 
     48 #include <machine/bus.h>
     49 
     50 #include <arch/x68k/dev/rtclock_var.h>
     51 #include <arch/x68k/dev/intiovar.h>
     52 
     53 static u_long rtgettod __P((void));
     54 static int  rtsettod __P((long));
     55 
     56 static int rtc_match __P((struct device *, struct cfdata *, void *));
     57 static void rtc_attach __P((struct device *, struct device *, void *));
     58 
     59 struct cfattach rtc_ca = {
     60 	sizeof(struct rtc_softc), rtc_match, rtc_attach
     61 };
     62 
     63 static int
     64 rtc_match(parent, cf, aux)
     65 	struct device *parent;
     66 	struct cfdata *cf;
     67 	void *aux;
     68 {
     69 	struct intio_attach_args *ia = aux;
     70 
     71 	if (cf->cf_unit != 0)
     72 		return (0);
     73 
     74 	/* fixed address */
     75 	if (ia->ia_addr != RTC_ADDR)
     76 		return (0);
     77 	if (ia->ia_intr != -1)
     78 		return (0);
     79 
     80 	return (1);
     81 }
     82 
     83 
     84 static struct rtc_softc *rtc;	/* XXX: softc cache */
     85 
     86 static void
     87 rtc_attach(parent, self, aux)
     88 	struct device *parent, *self;
     89 	void *aux;
     90 {
     91 	struct rtc_softc *sc = (struct rtc_softc *)self;
     92 	struct intio_attach_args *ia = aux;
     93 	int r;
     94 
     95 	ia->ia_size = 0x20;
     96 	r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
     97 #ifdef DIAGNOSTIC
     98 	if (r)
     99 		panic ("IO map for RTC corruption??");
    100 #endif
    101 
    102 
    103 	sc->sc_bst = ia->ia_bst;
    104 	bus_space_map(sc->sc_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
    105 	rtc = sc;
    106 
    107 	rtclockinit();
    108 	printf (": RP5C15\n");
    109 }
    110 
    111 
    112 
    113 /*
    114  * x68k/clock.c calls thru this vector, if it is set, to read
    115  * the realtime clock.
    116  */
    117 u_long (*gettod) __P((void));
    118 int (*settod) __P((long));
    119 
    120 int rtclockinit __P((void));
    121 
    122 int
    123 rtclockinit()
    124 {
    125 	if (rtgettod())	{
    126 		gettod = rtgettod;
    127 		settod = rtsettod;
    128 	} else {
    129 		return 0;
    130 	}
    131 	return 1;
    132 }
    133 
    134 static int month_days[12] = {
    135 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    136 };
    137 
    138 static u_long
    139 rtgettod()
    140 {
    141 	register int i;
    142 	register u_long tmp;
    143 	int year, month, day, hour, min, sec;
    144 
    145 	/* hold clock */
    146 	RTC_WRITE(RTC_MODE, RTC_HOLD_CLOCK);
    147 
    148 	/* read it */
    149 	sec   = RTC_REG(RTC_SEC10)  * 10 + RTC_REG(RTC_SEC);
    150 	min   = RTC_REG(RTC_MIN10)  * 10 + RTC_REG(RTC_MIN);
    151 	hour  = RTC_REG(RTC_HOUR10) * 10 + RTC_REG(RTC_HOUR);
    152 	day   = RTC_REG(RTC_DAY10)  * 10 + RTC_REG(RTC_DAY);
    153 	month = RTC_REG(RTC_MON10)  * 10 + RTC_REG(RTC_MON);
    154 	year  = RTC_REG(RTC_YEAR10) * 10 + RTC_REG(RTC_YEAR)  + 1980;
    155 
    156 	/* let it run again.. */
    157 	RTC_WRITE(RTC_MODE, RTC_FREE_CLOCK);
    158 
    159 	range_test(hour, 0, 23);
    160 	range_test(day, 1, 31);
    161 	range_test(month, 1, 12);
    162 	range_test(year, STARTOFTIME, 2000);
    163 
    164 	tmp = 0;
    165 
    166 	for (i = STARTOFTIME; i < year; i++)
    167 		tmp += days_in_year(i);
    168 	if (leapyear(year) && month > FEBRUARY)
    169 		tmp++;
    170 
    171 	for (i = 1; i < month; i++)
    172 		tmp += days_in_month(i);
    173 
    174 	tmp += (day - 1);
    175 
    176 	tmp = ((tmp * 24 + hour) * 60 + min + rtc_offset) * 60 + sec;
    177 
    178 	return tmp;
    179 }
    180 
    181 static int
    182 rtsettod (tim)
    183 	long tim;
    184 {
    185 	/*
    186 	 * I don't know if setting the clock is analogous
    187 	 * to reading it, I don't have demo-code for setting.
    188 	 * just give it a try..
    189 	 */
    190 	register int i;
    191 	register long hms, day;
    192 	u_char sec1, sec2;
    193 	u_char min1, min2;
    194 	u_char hour1, hour2;
    195 	u_char day1, day2;
    196 	u_char mon1, mon2;
    197 	u_char year1, year2;
    198 
    199 	tim -= (rtc_offset * 60);
    200 
    201 	/* prepare values to be written to clock */
    202 	day = tim / SECDAY;
    203 	hms = tim % SECDAY;
    204 
    205 	hour2 = hms / 3600;
    206 	hour1 = hour2 / 10;
    207 	hour2 %= 10;
    208 
    209 	min2 = (hms % 3600) / 60;
    210 	min1 = min2 / 10;
    211 	min2 %= 10;
    212 
    213 	sec2 = (hms % 3600) % 60;
    214 	sec1 = sec2 / 10;
    215 	sec2 %= 10;
    216 
    217 	/* Number of years in days */
    218 	for (i = STARTOFTIME - 1980; day >= days_in_year(i); i++)
    219 		day -= days_in_year(i);
    220 	year1 = i / 10;
    221 	year2 = i % 10;
    222 
    223 	/* Number of months in days left */
    224 	if (leapyear(i))
    225 		days_in_month(FEBRUARY) = 29;
    226 	for (i = 1; day >= days_in_month(i); i++)
    227 		day -= days_in_month(i);
    228 	days_in_month(FEBRUARY) = 28;
    229 
    230 	mon1 = i / 10;
    231 	mon2 = i % 10;
    232 
    233 	/* Days are what is left over (+1) from all that. */
    234 	day ++;
    235 	day1 = day / 10;
    236 	day2 = day % 10;
    237 
    238 	RTC_WRITE(RTC_MODE,   RTC_HOLD_CLOCK);
    239 	RTC_WRITE(RTC_SEC10,  sec1);
    240 	RTC_WRITE(RTC_SEC,    sec2);
    241 	RTC_WRITE(RTC_MIN10,  min1);
    242 	RTC_WRITE(RTC_MIN,    min2);
    243 	RTC_WRITE(RTC_HOUR10, hour1);
    244 	RTC_WRITE(RTC_HOUR,   hour2);
    245 	RTC_WRITE(RTC_DAY10,  day1);
    246 	RTC_WRITE(RTC_DAY,    day2);
    247 	RTC_WRITE(RTC_MON10,  mon1);
    248 	RTC_WRITE(RTC_MON,    mon2);
    249 	RTC_WRITE(RTC_YEAR10, year1);
    250 	RTC_WRITE(RTC_YEAR,   year2);
    251 	RTC_WRITE(RTC_MODE,   RTC_FREE_CLOCK);
    252 
    253 	return 1;
    254 }
    255