Home | History | Annotate | Line # | Download | only in ifpga
pl030_rtc.c revision 1.9
      1  1.9    dyoung /*	$NetBSD: pl030_rtc.c,v 1.9 2009/07/21 16:04:16 dyoung Exp $ */
      2  1.1  rearnsha 
      3  1.1  rearnsha /*
      4  1.1  rearnsha  * Copyright (c) 2001 ARM Ltd
      5  1.1  rearnsha  * All rights reserved.
      6  1.1  rearnsha  *
      7  1.1  rearnsha  * Redistribution and use in source and binary forms, with or without
      8  1.1  rearnsha  * modification, are permitted provided that the following conditions
      9  1.1  rearnsha  * are met:
     10  1.1  rearnsha  * 1. Redistributions of source code must retain the above copyright
     11  1.1  rearnsha  *    notice, this list of conditions and the following disclaimer.
     12  1.1  rearnsha  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  rearnsha  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  rearnsha  *    documentation and/or other materials provided with the distribution.
     15  1.1  rearnsha  * 3. The name of the company may not be used to endorse or promote
     16  1.1  rearnsha  *    products derived from this software without specific prior written
     17  1.1  rearnsha  *    permission.
     18  1.1  rearnsha  *
     19  1.1  rearnsha  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     20  1.1  rearnsha  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     21  1.1  rearnsha  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  rearnsha  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     23  1.1  rearnsha  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  1.1  rearnsha  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  1.1  rearnsha  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  rearnsha  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  rearnsha  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  rearnsha  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  rearnsha  * SUCH DAMAGE.
     30  1.1  rearnsha  */
     31  1.1  rearnsha 
     32  1.1  rearnsha /* Include header files */
     33  1.6     lukem 
     34  1.6     lukem #include <sys/cdefs.h>
     35  1.9    dyoung __KERNEL_RCSID(0, "$NetBSD: pl030_rtc.c,v 1.9 2009/07/21 16:04:16 dyoung Exp $");
     36  1.1  rearnsha 
     37  1.1  rearnsha #include <sys/types.h>
     38  1.1  rearnsha #include <sys/param.h>
     39  1.1  rearnsha #include <sys/systm.h>
     40  1.1  rearnsha #include <sys/kernel.h>
     41  1.1  rearnsha #include <sys/time.h>
     42  1.1  rearnsha #include <sys/device.h>
     43  1.1  rearnsha 
     44  1.8    briggs #include <dev/clock_subr.h>
     45  1.8    briggs 
     46  1.2   thorpej #include <arm/cpufunc.h>
     47  1.1  rearnsha #include <machine/intr.h>
     48  1.1  rearnsha #include <evbarm/ifpga/ifpgavar.h>
     49  1.1  rearnsha #include <evbarm/ifpga/ifpgamem.h>
     50  1.1  rearnsha #include <evbarm/ifpga/ifpgareg.h>
     51  1.1  rearnsha 
     52  1.1  rearnsha #define PL030_RTC_SIZE	0x14
     53  1.1  rearnsha 
     54  1.1  rearnsha struct plrtc_softc {
     55  1.1  rearnsha 	bus_space_tag_t		    sc_iot;
     56  1.1  rearnsha 	bus_space_handle_t	    sc_ioh;
     57  1.8    briggs 	struct todr_chip_handle     sc_todr;
     58  1.1  rearnsha };
     59  1.1  rearnsha 
     60  1.9    dyoung static int  plrtc_probe  (device_t, cfdata_t, void *);
     61  1.9    dyoung static void plrtc_attach (device_t, device_t, void *);
     62  1.1  rearnsha 
     63  1.9    dyoung CFATTACH_DECL_NEW(plrtc, sizeof(struct plrtc_softc),
     64  1.5   thorpej     plrtc_probe, plrtc_attach, NULL, NULL);
     65  1.1  rearnsha 
     66  1.8    briggs static int
     67  1.8    briggs plrtc_gettime(todr_chip_handle_t todr, volatile struct timeval *tv)
     68  1.8    briggs {
     69  1.8    briggs 	struct plrtc_softc *sc;
     70  1.8    briggs 
     71  1.8    briggs  	sc = (struct plrtc_softc *)todr->cookie;
     72  1.8    briggs 	tv->tv_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IFPGA_RTC_DR);
     73  1.8    briggs 	/* initialize tv_usec? */
     74  1.8    briggs 	return 0;
     75  1.8    briggs }
     76  1.8    briggs 
     77  1.8    briggs static int
     78  1.8    briggs plrtc_settime(todr_chip_handle_t todr, volatile struct timeval *tv)
     79  1.8    briggs {
     80  1.8    briggs 	struct plrtc_softc *sc;
     81  1.1  rearnsha 
     82  1.8    briggs  	sc = (struct plrtc_softc *)todr->cookie;
     83  1.8    briggs 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IFPGA_RTC_LR, tv->tv_sec);
     84  1.8    briggs 	return 0;
     85  1.8    briggs }
     86  1.1  rearnsha 
     87  1.1  rearnsha static int
     88  1.9    dyoung plrtc_probe(device_t parent, cfdata_t cf, void *aux)
     89  1.1  rearnsha {
     90  1.3   thorpej 	return 1;
     91  1.1  rearnsha }
     92  1.1  rearnsha 
     93  1.1  rearnsha static void
     94  1.9    dyoung plrtc_attach(device_t parent, device_t self, void *aux)
     95  1.1  rearnsha {
     96  1.1  rearnsha 	struct ifpga_attach_args *ifa = aux;
     97  1.9    dyoung 	struct plrtc_softc *sc = device_private(self);
     98  1.1  rearnsha 
     99  1.1  rearnsha 	sc->sc_iot = ifa->ifa_iot;
    100  1.1  rearnsha 	if (bus_space_map(ifa->ifa_iot, ifa->ifa_addr, PL030_RTC_SIZE, 0,
    101  1.1  rearnsha 	    &sc->sc_ioh)) {
    102  1.9    dyoung 		printf("%s: unable to map device\n", device_xname(self));
    103  1.1  rearnsha 		return;
    104  1.1  rearnsha 	}
    105  1.1  rearnsha 
    106  1.8    briggs 	memset(&sc->sc_todr, 0, sizeof(struct todr_chip_handle));
    107  1.8    briggs 	sc->sc_todr.cookie = sc;
    108  1.8    briggs 	sc->sc_todr.todr_gettime = plrtc_gettime;
    109  1.8    briggs 	sc->sc_todr.todr_settime = plrtc_settime;
    110  1.8    briggs 
    111  1.8    briggs 	todr_attach( &sc->sc_todr );
    112  1.1  rearnsha 
    113  1.1  rearnsha 	printf("\n");
    114  1.1  rearnsha }
    115