Home | History | Annotate | Line # | Download | only in ic
      1 /* $NetBSD: pl031.c,v 1.3 2025/09/08 13:06:16 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: pl031.c,v 1.3 2025/09/08 13:06:16 thorpej Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/kmem.h>
     36 #include <sys/bus.h>
     37 
     38 #include <dev/clock_subr.h>
     39 
     40 #include <dev/ic/pl031var.h>
     41 
     42 #define	RTCDR	0x000
     43 #define	RTCLR	0x008
     44 #define	RTCCR	0x00c
     45 #define	 RTCCR_START	__BIT(0)
     46 
     47 #define	RTC_READ(sc, reg)			\
     48 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     49 #define	RTC_WRITE(sc, reg, val)		\
     50 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     51 
     52 static int
     53 plrtc_gettime(todr_chip_handle_t tch, struct timeval *tv)
     54 {
     55 	struct plrtc_softc * const sc = device_private(tch->todr_dev);
     56 
     57 	tv->tv_sec = RTC_READ(sc, RTCDR);
     58 	tv->tv_usec = 0;
     59 
     60 	return 0;
     61 }
     62 
     63 static int
     64 plrtc_settime(todr_chip_handle_t tch, struct timeval *tv)
     65 {
     66 	struct plrtc_softc * const sc = device_private(tch->todr_dev);
     67 
     68 	RTC_WRITE(sc, RTCLR, tv->tv_sec);
     69 
     70 	return 0;
     71 }
     72 
     73 void
     74 plrtc_attach(struct plrtc_softc *sc)
     75 {
     76 	aprint_naive("\n");
     77 	aprint_normal(": RTC\n");
     78 
     79 	sc->sc_todr.todr_gettime = plrtc_gettime;
     80 	sc->sc_todr.todr_settime = plrtc_settime;
     81 	sc->sc_todr.todr_dev = sc->sc_dev;
     82 	todr_attach(&sc->sc_todr);
     83 
     84 	RTC_WRITE(sc, RTCCR, RTCCR_START);
     85 }
     86