Home | History | Annotate | Line # | Download | only in dev
rtclock.c revision 1.5.8.2
      1  1.5.8.1   bouyer /*	$NetBSD: rtclock.c,v 1.5.8.2 2001/01/18 09:23:10 bouyer 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.4  minoura #include <sys/device.h>
     47      1.1      oki 
     48      1.4  minoura #include <machine/bus.h>
     49      1.4  minoura 
     50  1.5.8.2   bouyer #include <dev/clock_subr.h>
     51  1.5.8.2   bouyer 
     52      1.4  minoura #include <arch/x68k/dev/rtclock_var.h>
     53      1.4  minoura #include <arch/x68k/dev/intiovar.h>
     54      1.1      oki 
     55  1.5.8.2   bouyer static time_t rtgettod __P((void));
     56      1.1      oki static int  rtsettod __P((long));
     57      1.1      oki 
     58      1.4  minoura static int rtc_match __P((struct device *, struct cfdata *, void *));
     59      1.4  minoura static void rtc_attach __P((struct device *, struct device *, void *));
     60      1.4  minoura 
     61      1.5  minoura int rtclockinit __P((void));
     62      1.5  minoura 
     63      1.4  minoura struct cfattach rtc_ca = {
     64      1.4  minoura 	sizeof(struct rtc_softc), rtc_match, rtc_attach
     65      1.4  minoura };
     66      1.4  minoura 
     67      1.4  minoura static int
     68      1.4  minoura rtc_match(parent, cf, aux)
     69      1.4  minoura 	struct device *parent;
     70      1.4  minoura 	struct cfdata *cf;
     71      1.4  minoura 	void *aux;
     72      1.4  minoura {
     73      1.4  minoura 	struct intio_attach_args *ia = aux;
     74      1.4  minoura 
     75      1.4  minoura 	if (strcmp (ia->ia_name, "rtc") != 0)
     76      1.4  minoura 		return (0);
     77      1.4  minoura 	if (cf->cf_unit != 0)
     78      1.4  minoura 		return (0);
     79      1.4  minoura 
     80      1.4  minoura 	/* fixed address */
     81      1.4  minoura 	if (ia->ia_addr != RTC_ADDR)
     82      1.4  minoura 		return (0);
     83      1.4  minoura 	if (ia->ia_intr != -1)
     84      1.4  minoura 		return (0);
     85      1.4  minoura 
     86      1.4  minoura 	return (1);
     87      1.4  minoura }
     88      1.4  minoura 
     89      1.4  minoura 
     90      1.4  minoura static struct rtc_softc *rtc;	/* XXX: softc cache */
     91      1.4  minoura 
     92      1.4  minoura static void
     93      1.4  minoura rtc_attach(parent, self, aux)
     94      1.4  minoura 	struct device *parent, *self;
     95      1.4  minoura 	void *aux;
     96      1.4  minoura {
     97      1.4  minoura 	struct rtc_softc *sc = (struct rtc_softc *)self;
     98      1.4  minoura 	struct intio_attach_args *ia = aux;
     99      1.4  minoura 	int r;
    100      1.4  minoura 
    101      1.4  minoura 	ia->ia_size = 0x20;
    102      1.4  minoura 	r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
    103      1.4  minoura #ifdef DIAGNOSTIC
    104      1.4  minoura 	if (r)
    105      1.4  minoura 		panic ("IO map for RTC corruption??");
    106      1.4  minoura #endif
    107      1.4  minoura 
    108      1.4  minoura 
    109      1.4  minoura 	sc->sc_bst = ia->ia_bst;
    110      1.4  minoura 	bus_space_map(sc->sc_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
    111      1.4  minoura 	rtc = sc;
    112      1.4  minoura 
    113      1.4  minoura 	rtclockinit();
    114      1.4  minoura 	printf (": RP5C15\n");
    115      1.4  minoura }
    116      1.4  minoura 
    117      1.4  minoura 
    118      1.4  minoura 
    119      1.1      oki /*
    120      1.1      oki  * x68k/clock.c calls thru this vector, if it is set, to read
    121      1.1      oki  * the realtime clock.
    122      1.1      oki  */
    123  1.5.8.2   bouyer time_t (*gettod) __P((void));
    124      1.3      oki int (*settod) __P((long));
    125      1.1      oki 
    126      1.1      oki int
    127      1.1      oki rtclockinit()
    128      1.1      oki {
    129      1.1      oki 	if (rtgettod())	{
    130      1.1      oki 		gettod = rtgettod;
    131      1.1      oki 		settod = rtsettod;
    132      1.1      oki 	} else {
    133      1.1      oki 		return 0;
    134      1.1      oki 	}
    135      1.1      oki 	return 1;
    136      1.1      oki }
    137      1.1      oki 
    138  1.5.8.2   bouyer static time_t
    139      1.1      oki rtgettod()
    140      1.1      oki {
    141  1.5.8.2   bouyer 	struct clock_ymdhms dt;
    142      1.1      oki 
    143      1.1      oki 	/* hold clock */
    144      1.4  minoura 	RTC_WRITE(RTC_MODE, RTC_HOLD_CLOCK);
    145      1.1      oki 
    146      1.1      oki 	/* read it */
    147  1.5.8.2   bouyer 	dt.dt_sec  = RTC_REG(RTC_SEC10)  * 10 + RTC_REG(RTC_SEC);
    148  1.5.8.2   bouyer 	dt.dt_min  = RTC_REG(RTC_MIN10)  * 10 + RTC_REG(RTC_MIN);
    149  1.5.8.2   bouyer 	dt.dt_hour = RTC_REG(RTC_HOUR10) * 10 + RTC_REG(RTC_HOUR);
    150  1.5.8.2   bouyer 	dt.dt_day  = RTC_REG(RTC_DAY10)  * 10 + RTC_REG(RTC_DAY);
    151  1.5.8.2   bouyer 	dt.dt_mon  = RTC_REG(RTC_MON10)  * 10 + RTC_REG(RTC_MON);
    152  1.5.8.2   bouyer 	dt.dt_year = RTC_REG(RTC_YEAR10) * 10 + RTC_REG(RTC_YEAR)
    153  1.5.8.2   bouyer 							+RTC_BASE_YEAR;
    154      1.1      oki 
    155      1.1      oki 	/* let it run again.. */
    156      1.4  minoura 	RTC_WRITE(RTC_MODE, RTC_FREE_CLOCK);
    157      1.1      oki 
    158  1.5.8.2   bouyer #ifdef DIAGNOSTIC
    159  1.5.8.2   bouyer 	range_test0(dt.dt_hour, 23);
    160  1.5.8.2   bouyer 	range_test(dt.dt_day, 1, 31);
    161  1.5.8.2   bouyer 	range_test(dt.dt_mon, 1, 12);
    162  1.5.8.2   bouyer 	range_test(dt.dt_year, RTC_BASE_YEAR, RTC_BASE_YEAR+100-1);
    163  1.5.8.2   bouyer #endif
    164      1.1      oki 
    165  1.5.8.2   bouyer 	return clock_ymdhms_to_secs (&dt);
    166      1.1      oki }
    167      1.1      oki 
    168      1.1      oki static int
    169      1.1      oki rtsettod (tim)
    170  1.5.8.2   bouyer 	time_t tim;
    171      1.1      oki {
    172  1.5.8.2   bouyer 	struct clock_ymdhms dt;
    173      1.1      oki 	u_char sec1, sec2;
    174      1.1      oki 	u_char min1, min2;
    175      1.1      oki 	u_char hour1, hour2;
    176      1.1      oki 	u_char day1, day2;
    177      1.1      oki 	u_char mon1, mon2;
    178      1.1      oki 	u_char year1, year2;
    179      1.1      oki 
    180  1.5.8.2   bouyer 	clock_secs_to_ymdhms (tim, &dt);
    181      1.1      oki 
    182      1.1      oki 	/* prepare values to be written to clock */
    183  1.5.8.2   bouyer 	sec1  = dt.dt_sec  / 10;
    184  1.5.8.2   bouyer 	sec2  = dt.dt_sec  % 10;
    185  1.5.8.2   bouyer 	min1  = dt.dt_min  / 10;
    186  1.5.8.2   bouyer 	min2  = dt.dt_min  % 10;
    187  1.5.8.2   bouyer 	hour1 = dt.dt_hour / 10;
    188  1.5.8.2   bouyer 	hour2 = dt.dt_hour % 10;
    189  1.5.8.2   bouyer 
    190  1.5.8.2   bouyer 	day1  = dt.dt_day  / 10;
    191  1.5.8.2   bouyer 	day2  = dt.dt_day  % 10;
    192  1.5.8.2   bouyer 	mon1  = dt.dt_mon  / 10;
    193  1.5.8.2   bouyer 	mon2  = dt.dt_mon  % 10;
    194  1.5.8.2   bouyer 	year1 = (dt.dt_year - RTC_BASE_YEAR) / 10;
    195  1.5.8.2   bouyer 	year2 = dt.dt_year % 10;
    196      1.1      oki 
    197      1.4  minoura 	RTC_WRITE(RTC_MODE,   RTC_HOLD_CLOCK);
    198      1.4  minoura 	RTC_WRITE(RTC_SEC10,  sec1);
    199      1.4  minoura 	RTC_WRITE(RTC_SEC,    sec2);
    200      1.4  minoura 	RTC_WRITE(RTC_MIN10,  min1);
    201      1.4  minoura 	RTC_WRITE(RTC_MIN,    min2);
    202      1.4  minoura 	RTC_WRITE(RTC_HOUR10, hour1);
    203      1.4  minoura 	RTC_WRITE(RTC_HOUR,   hour2);
    204      1.4  minoura 	RTC_WRITE(RTC_DAY10,  day1);
    205      1.4  minoura 	RTC_WRITE(RTC_DAY,    day2);
    206      1.4  minoura 	RTC_WRITE(RTC_MON10,  mon1);
    207      1.4  minoura 	RTC_WRITE(RTC_MON,    mon2);
    208      1.4  minoura 	RTC_WRITE(RTC_YEAR10, year1);
    209      1.4  minoura 	RTC_WRITE(RTC_YEAR,   year2);
    210      1.4  minoura 	RTC_WRITE(RTC_MODE,   RTC_FREE_CLOCK);
    211      1.1      oki 
    212      1.1      oki 	return 1;
    213      1.1      oki }
    214