Home | History | Annotate | Line # | Download | only in dev
rtclock.c revision 1.3.10.2
      1  1.3.10.2  minoura /*	$NetBSD: rtclock.c,v 1.3.10.2 1998/12/27 14:13:04 minoura 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.3.10.1  minoura #include <sys/device.h>
     47       1.1      oki 
     48  1.3.10.1  minoura #include <machine/bus.h>
     49  1.3.10.1  minoura 
     50  1.3.10.1  minoura #include <arch/x68k/dev/rtclock_var.h>
     51  1.3.10.1  minoura #include <arch/x68k/dev/intiovar.h>
     52       1.1      oki 
     53       1.1      oki static u_long rtgettod __P((void));
     54       1.1      oki static int  rtsettod __P((long));
     55       1.1      oki 
     56  1.3.10.1  minoura static int rtc_match __P((struct device *, struct cfdata *, void *));
     57  1.3.10.1  minoura static void rtc_attach __P((struct device *, struct device *, void *));
     58  1.3.10.1  minoura 
     59  1.3.10.1  minoura struct cfattach rtc_ca = {
     60  1.3.10.1  minoura 	sizeof(struct rtc_softc), rtc_match, rtc_attach
     61  1.3.10.1  minoura };
     62  1.3.10.1  minoura 
     63  1.3.10.1  minoura static int
     64  1.3.10.1  minoura rtc_match(parent, cf, aux)
     65  1.3.10.1  minoura 	struct device *parent;
     66  1.3.10.1  minoura 	struct cfdata *cf;
     67  1.3.10.1  minoura 	void *aux;
     68  1.3.10.1  minoura {
     69  1.3.10.1  minoura 	struct intio_attach_args *ia = aux;
     70  1.3.10.1  minoura 
     71  1.3.10.2  minoura 	if (strcmp (ia->ia_name, "rtc") != 0)
     72  1.3.10.2  minoura 		return (0);
     73  1.3.10.1  minoura 	if (cf->cf_unit != 0)
     74  1.3.10.1  minoura 		return (0);
     75  1.3.10.1  minoura 
     76  1.3.10.1  minoura 	/* fixed address */
     77  1.3.10.1  minoura 	if (ia->ia_addr != RTC_ADDR)
     78  1.3.10.1  minoura 		return (0);
     79  1.3.10.1  minoura 	if (ia->ia_intr != -1)
     80  1.3.10.1  minoura 		return (0);
     81  1.3.10.1  minoura 
     82  1.3.10.1  minoura 	return (1);
     83  1.3.10.1  minoura }
     84  1.3.10.1  minoura 
     85  1.3.10.1  minoura 
     86  1.3.10.1  minoura static struct rtc_softc *rtc;	/* XXX: softc cache */
     87  1.3.10.1  minoura 
     88  1.3.10.1  minoura static void
     89  1.3.10.1  minoura rtc_attach(parent, self, aux)
     90  1.3.10.1  minoura 	struct device *parent, *self;
     91  1.3.10.1  minoura 	void *aux;
     92  1.3.10.1  minoura {
     93  1.3.10.1  minoura 	struct rtc_softc *sc = (struct rtc_softc *)self;
     94  1.3.10.1  minoura 	struct intio_attach_args *ia = aux;
     95  1.3.10.1  minoura 	int r;
     96  1.3.10.1  minoura 
     97  1.3.10.1  minoura 	ia->ia_size = 0x20;
     98  1.3.10.1  minoura 	r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
     99  1.3.10.1  minoura #ifdef DIAGNOSTIC
    100  1.3.10.1  minoura 	if (r)
    101  1.3.10.1  minoura 		panic ("IO map for RTC corruption??");
    102  1.3.10.1  minoura #endif
    103  1.3.10.1  minoura 
    104  1.3.10.1  minoura 
    105  1.3.10.1  minoura 	sc->sc_bst = ia->ia_bst;
    106  1.3.10.1  minoura 	bus_space_map(sc->sc_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
    107  1.3.10.1  minoura 	rtc = sc;
    108  1.3.10.1  minoura 
    109  1.3.10.1  minoura 	rtclockinit();
    110  1.3.10.1  minoura 	printf (": RP5C15\n");
    111  1.3.10.1  minoura }
    112  1.3.10.1  minoura 
    113  1.3.10.1  minoura 
    114  1.3.10.1  minoura 
    115       1.1      oki /*
    116       1.1      oki  * x68k/clock.c calls thru this vector, if it is set, to read
    117       1.1      oki  * the realtime clock.
    118       1.1      oki  */
    119       1.1      oki u_long (*gettod) __P((void));
    120       1.3      oki int (*settod) __P((long));
    121       1.1      oki 
    122       1.3      oki int rtclockinit __P((void));
    123       1.1      oki 
    124       1.1      oki int
    125       1.1      oki rtclockinit()
    126       1.1      oki {
    127       1.1      oki 	if (rtgettod())	{
    128       1.1      oki 		gettod = rtgettod;
    129       1.1      oki 		settod = rtsettod;
    130       1.1      oki 	} else {
    131       1.1      oki 		return 0;
    132       1.1      oki 	}
    133       1.1      oki 	return 1;
    134       1.1      oki }
    135       1.1      oki 
    136       1.1      oki static int month_days[12] = {
    137       1.1      oki 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    138       1.1      oki };
    139       1.1      oki 
    140       1.1      oki static u_long
    141       1.1      oki rtgettod()
    142       1.1      oki {
    143       1.1      oki 	register int i;
    144       1.1      oki 	register u_long tmp;
    145       1.1      oki 	int year, month, day, hour, min, sec;
    146       1.1      oki 
    147       1.1      oki 	/* hold clock */
    148  1.3.10.1  minoura 	RTC_WRITE(RTC_MODE, RTC_HOLD_CLOCK);
    149       1.1      oki 
    150       1.1      oki 	/* read it */
    151  1.3.10.1  minoura 	sec   = RTC_REG(RTC_SEC10)  * 10 + RTC_REG(RTC_SEC);
    152  1.3.10.1  minoura 	min   = RTC_REG(RTC_MIN10)  * 10 + RTC_REG(RTC_MIN);
    153  1.3.10.1  minoura 	hour  = RTC_REG(RTC_HOUR10) * 10 + RTC_REG(RTC_HOUR);
    154  1.3.10.1  minoura 	day   = RTC_REG(RTC_DAY10)  * 10 + RTC_REG(RTC_DAY);
    155  1.3.10.1  minoura 	month = RTC_REG(RTC_MON10)  * 10 + RTC_REG(RTC_MON);
    156  1.3.10.1  minoura 	year  = RTC_REG(RTC_YEAR10) * 10 + RTC_REG(RTC_YEAR)  + 1980;
    157       1.1      oki 
    158       1.1      oki 	/* let it run again.. */
    159  1.3.10.1  minoura 	RTC_WRITE(RTC_MODE, RTC_FREE_CLOCK);
    160       1.1      oki 
    161       1.1      oki 	range_test(hour, 0, 23);
    162       1.1      oki 	range_test(day, 1, 31);
    163       1.1      oki 	range_test(month, 1, 12);
    164       1.1      oki 	range_test(year, STARTOFTIME, 2000);
    165       1.1      oki 
    166       1.1      oki 	tmp = 0;
    167       1.1      oki 
    168       1.1      oki 	for (i = STARTOFTIME; i < year; i++)
    169       1.1      oki 		tmp += days_in_year(i);
    170       1.1      oki 	if (leapyear(year) && month > FEBRUARY)
    171       1.1      oki 		tmp++;
    172       1.1      oki 
    173       1.1      oki 	for (i = 1; i < month; i++)
    174       1.1      oki 		tmp += days_in_month(i);
    175       1.1      oki 
    176       1.1      oki 	tmp += (day - 1);
    177       1.1      oki 
    178       1.2    perry 	tmp = ((tmp * 24 + hour) * 60 + min + rtc_offset) * 60 + sec;
    179       1.1      oki 
    180       1.1      oki 	return tmp;
    181       1.1      oki }
    182       1.1      oki 
    183       1.1      oki static int
    184       1.1      oki rtsettod (tim)
    185       1.1      oki 	long tim;
    186       1.1      oki {
    187       1.1      oki 	/*
    188       1.1      oki 	 * I don't know if setting the clock is analogous
    189       1.1      oki 	 * to reading it, I don't have demo-code for setting.
    190       1.1      oki 	 * just give it a try..
    191       1.1      oki 	 */
    192       1.1      oki 	register int i;
    193       1.1      oki 	register long hms, day;
    194       1.1      oki 	u_char sec1, sec2;
    195       1.1      oki 	u_char min1, min2;
    196       1.1      oki 	u_char hour1, hour2;
    197       1.1      oki 	u_char day1, day2;
    198       1.1      oki 	u_char mon1, mon2;
    199       1.1      oki 	u_char year1, year2;
    200       1.1      oki 
    201       1.2    perry 	tim -= (rtc_offset * 60);
    202       1.1      oki 
    203       1.1      oki 	/* prepare values to be written to clock */
    204       1.1      oki 	day = tim / SECDAY;
    205       1.1      oki 	hms = tim % SECDAY;
    206       1.1      oki 
    207       1.1      oki 	hour2 = hms / 3600;
    208       1.1      oki 	hour1 = hour2 / 10;
    209       1.1      oki 	hour2 %= 10;
    210       1.1      oki 
    211       1.1      oki 	min2 = (hms % 3600) / 60;
    212       1.1      oki 	min1 = min2 / 10;
    213       1.1      oki 	min2 %= 10;
    214       1.1      oki 
    215       1.1      oki 	sec2 = (hms % 3600) % 60;
    216       1.1      oki 	sec1 = sec2 / 10;
    217       1.1      oki 	sec2 %= 10;
    218       1.1      oki 
    219       1.1      oki 	/* Number of years in days */
    220       1.1      oki 	for (i = STARTOFTIME - 1980; day >= days_in_year(i); i++)
    221       1.1      oki 		day -= days_in_year(i);
    222       1.1      oki 	year1 = i / 10;
    223       1.1      oki 	year2 = i % 10;
    224       1.1      oki 
    225       1.1      oki 	/* Number of months in days left */
    226       1.1      oki 	if (leapyear(i))
    227       1.1      oki 		days_in_month(FEBRUARY) = 29;
    228       1.1      oki 	for (i = 1; day >= days_in_month(i); i++)
    229       1.1      oki 		day -= days_in_month(i);
    230       1.1      oki 	days_in_month(FEBRUARY) = 28;
    231       1.1      oki 
    232       1.1      oki 	mon1 = i / 10;
    233       1.1      oki 	mon2 = i % 10;
    234       1.1      oki 
    235       1.1      oki 	/* Days are what is left over (+1) from all that. */
    236       1.1      oki 	day ++;
    237       1.1      oki 	day1 = day / 10;
    238       1.1      oki 	day2 = day % 10;
    239       1.1      oki 
    240  1.3.10.1  minoura 	RTC_WRITE(RTC_MODE,   RTC_HOLD_CLOCK);
    241  1.3.10.1  minoura 	RTC_WRITE(RTC_SEC10,  sec1);
    242  1.3.10.1  minoura 	RTC_WRITE(RTC_SEC,    sec2);
    243  1.3.10.1  minoura 	RTC_WRITE(RTC_MIN10,  min1);
    244  1.3.10.1  minoura 	RTC_WRITE(RTC_MIN,    min2);
    245  1.3.10.1  minoura 	RTC_WRITE(RTC_HOUR10, hour1);
    246  1.3.10.1  minoura 	RTC_WRITE(RTC_HOUR,   hour2);
    247  1.3.10.1  minoura 	RTC_WRITE(RTC_DAY10,  day1);
    248  1.3.10.1  minoura 	RTC_WRITE(RTC_DAY,    day2);
    249  1.3.10.1  minoura 	RTC_WRITE(RTC_MON10,  mon1);
    250  1.3.10.1  minoura 	RTC_WRITE(RTC_MON,    mon2);
    251  1.3.10.1  minoura 	RTC_WRITE(RTC_YEAR10, year1);
    252  1.3.10.1  minoura 	RTC_WRITE(RTC_YEAR,   year2);
    253  1.3.10.1  minoura 	RTC_WRITE(RTC_MODE,   RTC_FREE_CLOCK);
    254       1.1      oki 
    255       1.1      oki 	return 1;
    256       1.1      oki }
    257