Home | History | Annotate | Line # | Download | only in ifpga
      1 /*	$NetBSD: pl030_rtc.c,v 1.11 2025/09/07 21:45:13 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/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: pl030_rtc.c,v 1.11 2025/09/07 21:45:13 thorpej Exp $");
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/time.h>
     42 #include <sys/device.h>
     43 
     44 #include <dev/clock_subr.h>
     45 
     46 #include <arm/cpufunc.h>
     47 #include <machine/intr.h>
     48 #include <evbarm/ifpga/ifpgavar.h>
     49 #include <evbarm/ifpga/ifpgamem.h>
     50 #include <evbarm/ifpga/ifpgareg.h>
     51 
     52 #define PL030_RTC_SIZE	0x14
     53 
     54 struct plrtc_softc {
     55 	device_t		    sc_dev;
     56 	bus_space_tag_t		    sc_iot;
     57 	bus_space_handle_t	    sc_ioh;
     58 	struct todr_chip_handle     sc_todr;
     59 };
     60 
     61 static int  plrtc_probe  (device_t, cfdata_t, void *);
     62 static void plrtc_attach (device_t, device_t, void *);
     63 
     64 CFATTACH_DECL_NEW(plrtc, sizeof(struct plrtc_softc),
     65     plrtc_probe, plrtc_attach, NULL, NULL);
     66 
     67 static int
     68 plrtc_gettime(todr_chip_handle_t todr, struct timeval *tv)
     69 {
     70 	struct plrtc_softc *sc = device_private(todr->todr_dev);
     71 
     72 	tv->tv_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IFPGA_RTC_DR);
     73 	/* initialize tv_usec? */
     74 	return 0;
     75 }
     76 
     77 static int
     78 plrtc_settime(todr_chip_handle_t todr, struct timeval *tv)
     79 {
     80 	struct plrtc_softc *sc = device_private(todr->todr_dev);
     81 
     82 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IFPGA_RTC_LR, tv->tv_sec);
     83 	return 0;
     84 }
     85 
     86 static int
     87 plrtc_probe(device_t parent, cfdata_t cf, void *aux)
     88 {
     89 	return 1;
     90 }
     91 
     92 static void
     93 plrtc_attach(device_t parent, device_t self, void *aux)
     94 {
     95 	struct ifpga_attach_args *ifa = aux;
     96 	struct plrtc_softc *sc = device_private(self);
     97 
     98 	sc->sc_dev = self;
     99 	sc->sc_iot = ifa->ifa_iot;
    100 	if (bus_space_map(ifa->ifa_iot, ifa->ifa_addr, PL030_RTC_SIZE, 0,
    101 	    &sc->sc_ioh)) {
    102 		printf("%s: unable to map device\n", device_xname(self));
    103 		return;
    104 	}
    105 
    106 	memset(&sc->sc_todr, 0, sizeof(struct todr_chip_handle));
    107 	sc->sc_todr.todr_dev = self;
    108 	sc->sc_todr.todr_gettime = plrtc_gettime;
    109 	sc->sc_todr.todr_settime = plrtc_settime;
    110 
    111 	todr_attach( &sc->sc_todr );
    112 
    113 	printf("\n");
    114 }
    115