clpsrtc.c revision 1.3
1/*      $NetBSD: clpsrtc.c,v 1.3 2025/09/07 21:45:11 thorpej Exp $      */
2/*
3 * Copyright (c) 2013 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: clpsrtc.c,v 1.3 2025/09/07 21:45:11 thorpej Exp $");
29
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/device.h>
33#include <sys/errno.h>
34
35#include <dev/clock_subr.h>
36
37#include <arm/clps711x/clps711xreg.h>
38#include <arm/clps711x/clpssocvar.h>
39
40#include "locators.h"
41
42struct clpsrtc_softc {
43	device_t sc_dev;
44	bus_space_tag_t sc_iot;
45	bus_space_handle_t sc_ioh;
46
47	struct todr_chip_handle sc_todr;
48};
49
50static int clpsrtc_match(device_t, cfdata_t, void *);
51static void clpsrtc_attach(device_t, device_t, void *);
52
53/* todr(9) interfaces */
54static int clpsrtc_todr_gettime(todr_chip_handle_t, struct timeval *);
55static int clpsrtc_todr_settime(todr_chip_handle_t, struct timeval *);
56
57CFATTACH_DECL_NEW(clpsrtc, sizeof(struct clpsrtc_softc),
58    clpsrtc_match, clpsrtc_attach, NULL, NULL);
59
60
61/* ARGSUSED */
62static int
63clpsrtc_match(device_t parent, cfdata_t match, void *aux)
64{
65
66	return 1;
67}
68
69/* ARGSUSED */
70static void
71clpsrtc_attach(device_t parent, device_t self, void *aux)
72{
73	struct clpsrtc_softc *sc = device_private(self);
74	struct clpssoc_attach_args *aa = aux;
75
76	aprint_naive("\n");
77	aprint_normal("\n");
78
79	sc->sc_dev = self;
80	sc->sc_iot = aa->aa_iot;
81	sc->sc_ioh = *aa->aa_ioh;
82
83	/* XXXX: reset to compare registers */
84	bus_space_write_4(sc->sc_iot, sc->sc_ioh, PS711X_RTCMR, 0);
85
86	sc->sc_todr.todr_dev = self;
87	sc->sc_todr.todr_gettime = clpsrtc_todr_gettime;
88	sc->sc_todr.todr_settime = clpsrtc_todr_settime;
89	todr_attach(&sc->sc_todr);
90}
91
92static int
93clpsrtc_todr_gettime(todr_chip_handle_t ch, struct timeval *tv)
94{
95	struct clpsrtc_softc *sc = device_private(ch->todr_dev);
96
97	tv->tv_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, PS711X_RTCDR);
98	tv->tv_usec = 0;
99	return 0;
100}
101
102static int
103clpsrtc_todr_settime(todr_chip_handle_t ch, struct timeval *tv)
104{
105	struct clpsrtc_softc *sc = device_private(ch->todr_dev);
106
107	bus_space_write_4(sc->sc_iot, sc->sc_ioh, PS711X_RTCDR, tv->tv_sec);
108	return 0;
109}
110