Home | History | Annotate | Line # | Download | only in ifpga
pl030_rtc.c revision 1.4
      1 /*	$NetBSD: pl030_rtc.c,v 1.4 2002/09/27 20:31:26 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2001 ARM Ltd
      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. The name of the company may not be used to endorse or promote
     16  *    products derived from this software without specific prior written
     17  *    permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /* Include header files */
     33 
     34 #include <sys/types.h>
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/time.h>
     39 #include <sys/device.h>
     40 
     41 #include <arm/cpufunc.h>
     42 #include <machine/intr.h>
     43 #include <evbarm/ifpga/ifpgavar.h>
     44 #include <evbarm/ifpga/ifpgamem.h>
     45 #include <evbarm/ifpga/ifpgareg.h>
     46 
     47 #define PL030_RTC_SIZE	0x14
     48 
     49 struct plrtc_softc {
     50 	struct device		    sc_dev;
     51 	bus_space_tag_t		    sc_iot;
     52 	bus_space_handle_t	    sc_ioh;
     53 };
     54 
     55 static int  plrtc_probe  (struct device *, struct cfdata *, void *);
     56 static void plrtc_attach (struct device *, struct device *, void *);
     57 
     58 const struct cfattach plrtc_ca = {
     59 	sizeof(struct plrtc_softc), plrtc_probe, plrtc_attach
     60 };
     61 
     62 /* Remember our handle, since it isn't passed in by inittodr and
     63    resettodr.  */
     64 static struct plrtc_softc *plrtc_sc;
     65 
     66 static int timeset = 0;
     67 
     68 static int
     69 plrtc_probe(struct device *parent, struct cfdata *cf, void *aux)
     70 {
     71 	return 1;
     72 }
     73 
     74 static void
     75 plrtc_attach(struct device *parent, struct device *self, void *aux)
     76 {
     77 	struct ifpga_attach_args *ifa = aux;
     78 	struct plrtc_softc *sc = (struct plrtc_softc *)self;
     79 
     80 	sc->sc_iot = ifa->ifa_iot;
     81 	if (bus_space_map(ifa->ifa_iot, ifa->ifa_addr, PL030_RTC_SIZE, 0,
     82 	    &sc->sc_ioh)) {
     83 		printf("%s: unable to map device\n", sc->sc_dev.dv_xname);
     84 		return;
     85 	}
     86 
     87 	plrtc_sc = sc;
     88 
     89 	printf("\n");
     90 }
     91 
     92 void
     93 inittodr(time_t base)
     94 {
     95 	time_t rtc_time;
     96 	struct plrtc_softc *sc = plrtc_sc;
     97 
     98 	if (sc == NULL)
     99 		panic("RTC not attached");
    100 
    101 	/* Default to the suggested time, but replace that with one from the
    102 	   RTC if it seems more sensible.  */
    103 	rtc_time = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IFPGA_RTC_DR);
    104 
    105 	time.tv_usec = 0;
    106 	time.tv_sec = rtc_time;
    107 
    108 	timeset = 1;
    109 
    110 	if (base > rtc_time) {
    111 		printf("inittodr: rtc value suspect, ignoring it.\n");
    112 		time.tv_usec = 0;
    113 		time.tv_sec = base;
    114 		return;
    115 	}
    116 }
    117 
    118 void
    119 resettodr()
    120 {
    121 	struct plrtc_softc *sc = plrtc_sc;
    122 
    123 	/* The time hasn't been set yet, so avoid writing a dubious value
    124 	   to the RTC.  */
    125 	if (!timeset)
    126 		return;
    127 
    128 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IFPGA_RTC_LR, time.tv_sec);
    129 }
    130