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