xref: /netbsd-src/sys/dev/ic/pl031.c (revision 25cf81196f30df7354411f724ff9d67d9c4f19f1)
1*25cf8119Sjmcneill /* $NetBSD: pl031.c,v 1.1 2017/06/08 21:00:43 jmcneill Exp $ */
2*25cf8119Sjmcneill 
3*25cf8119Sjmcneill /*-
4*25cf8119Sjmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5*25cf8119Sjmcneill  * All rights reserved.
6*25cf8119Sjmcneill  *
7*25cf8119Sjmcneill  * Redistribution and use in source and binary forms, with or without
8*25cf8119Sjmcneill  * modification, are permitted provided that the following conditions
9*25cf8119Sjmcneill  * are met:
10*25cf8119Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11*25cf8119Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12*25cf8119Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13*25cf8119Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14*25cf8119Sjmcneill  *    documentation and/or other materials provided with the distribution.
15*25cf8119Sjmcneill  *
16*25cf8119Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*25cf8119Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*25cf8119Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*25cf8119Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*25cf8119Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*25cf8119Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*25cf8119Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*25cf8119Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*25cf8119Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*25cf8119Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*25cf8119Sjmcneill  * SUCH DAMAGE.
27*25cf8119Sjmcneill  */
28*25cf8119Sjmcneill 
29*25cf8119Sjmcneill #include <sys/cdefs.h>
30*25cf8119Sjmcneill __KERNEL_RCSID(0, "$NetBSD: pl031.c,v 1.1 2017/06/08 21:00:43 jmcneill Exp $");
31*25cf8119Sjmcneill 
32*25cf8119Sjmcneill #include <sys/param.h>
33*25cf8119Sjmcneill #include <sys/systm.h>
34*25cf8119Sjmcneill #include <sys/device.h>
35*25cf8119Sjmcneill #include <sys/kmem.h>
36*25cf8119Sjmcneill #include <sys/bus.h>
37*25cf8119Sjmcneill 
38*25cf8119Sjmcneill #include <dev/clock_subr.h>
39*25cf8119Sjmcneill 
40*25cf8119Sjmcneill #include <dev/ic/pl031var.h>
41*25cf8119Sjmcneill 
42*25cf8119Sjmcneill #define	RTCDR	0x000
43*25cf8119Sjmcneill #define	RTCLR	0x008
44*25cf8119Sjmcneill #define	RTCCR	0x00c
45*25cf8119Sjmcneill #define	 RTCCR_START	__BIT(0)
46*25cf8119Sjmcneill 
47*25cf8119Sjmcneill #define	RTC_READ(sc, reg)			\
48*25cf8119Sjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
49*25cf8119Sjmcneill #define	RTC_WRITE(sc, reg, val)		\
50*25cf8119Sjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
51*25cf8119Sjmcneill 
52*25cf8119Sjmcneill static int
plrtc_gettime(todr_chip_handle_t tch,struct timeval * tv)53*25cf8119Sjmcneill plrtc_gettime(todr_chip_handle_t tch, struct timeval *tv)
54*25cf8119Sjmcneill {
55*25cf8119Sjmcneill 	struct plrtc_softc * const sc = tch->cookie;
56*25cf8119Sjmcneill 
57*25cf8119Sjmcneill 	tv->tv_sec = RTC_READ(sc, RTCDR);
58*25cf8119Sjmcneill 	tv->tv_usec = 0;
59*25cf8119Sjmcneill 
60*25cf8119Sjmcneill 	return 0;
61*25cf8119Sjmcneill }
62*25cf8119Sjmcneill 
63*25cf8119Sjmcneill static int
plrtc_settime(todr_chip_handle_t tch,struct timeval * tv)64*25cf8119Sjmcneill plrtc_settime(todr_chip_handle_t tch, struct timeval *tv)
65*25cf8119Sjmcneill {
66*25cf8119Sjmcneill 	struct plrtc_softc * const sc = tch->cookie;
67*25cf8119Sjmcneill 
68*25cf8119Sjmcneill 	RTC_WRITE(sc, RTCLR, tv->tv_sec);
69*25cf8119Sjmcneill 
70*25cf8119Sjmcneill 	return 0;
71*25cf8119Sjmcneill }
72*25cf8119Sjmcneill 
73*25cf8119Sjmcneill void
plrtc_attach(struct plrtc_softc * sc)74*25cf8119Sjmcneill plrtc_attach(struct plrtc_softc *sc)
75*25cf8119Sjmcneill {
76*25cf8119Sjmcneill 	aprint_naive("\n");
77*25cf8119Sjmcneill 	aprint_normal(": RTC\n");
78*25cf8119Sjmcneill 
79*25cf8119Sjmcneill 	sc->sc_todr.todr_gettime = plrtc_gettime;
80*25cf8119Sjmcneill 	sc->sc_todr.todr_settime = plrtc_settime;
81*25cf8119Sjmcneill 	sc->sc_todr.cookie = sc;
82*25cf8119Sjmcneill 
83*25cf8119Sjmcneill 	RTC_WRITE(sc, RTCCR, RTCCR_START);
84*25cf8119Sjmcneill }
85