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