xref: /openbsd/sys/dev/fdt/exrtc.c (revision 62d244ed)
1 /*	$OpenBSD: exrtc.c,v 1.5 2022/10/17 19:09:46 kettenis Exp $	*/
2 /*
3  * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 #include <sys/time.h>
22 
23 #include <machine/bus.h>
24 #include <machine/fdt.h>
25 
26 #include <dev/ofw/openfirm.h>
27 #include <dev/ofw/fdt.h>
28 
29 #include <dev/clock_subr.h>
30 
31 #define RTCCTRL		0x40
32 #define RTCCTRL_RTCEN	(1 << 0)
33 
34 #define RTCSEC		0x70
35 #define RTCMIN		0x74
36 #define RTCHOUR		0x78
37 #define RTCDAY		0x7c
38 #define RTCMON		0x84
39 #define RTCYEAR		0x88
40 
41 #define HREAD4(sc, reg)							\
42 	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
43 #define HWRITE4(sc, reg, val)						\
44 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
45 
46 struct exrtc_softc {
47 	struct device	sc_dev;
48 	bus_space_tag_t		sc_iot;
49 	bus_space_handle_t	sc_ioh;
50 	struct todr_chip_handle sc_todr;
51 };
52 
53 int	exrtc_match(struct device *, void *, void *);
54 void	exrtc_attach(struct device *, struct device *, void *);
55 
56 const struct cfattach exrtc_ca = {
57 	sizeof(struct exrtc_softc), exrtc_match, exrtc_attach
58 };
59 
60 struct cfdriver exrtc_cd = {
61 	NULL, "exrtc", DV_DULL
62 };
63 
64 int	exrtc_gettime(todr_chip_handle_t, struct timeval *);
65 int	exrtc_settime(todr_chip_handle_t, struct timeval *);
66 
67 int
exrtc_match(struct device * parent,void * match,void * aux)68 exrtc_match(struct device *parent, void *match, void *aux)
69 {
70 	struct fdt_attach_args *faa = aux;
71 
72 	return OF_is_compatible(faa->fa_node, "samsung,s3c6410-rtc");
73 }
74 
75 void
exrtc_attach(struct device * parent,struct device * self,void * aux)76 exrtc_attach(struct device *parent, struct device *self, void *aux)
77 {
78 	struct exrtc_softc *sc = (struct exrtc_softc *)self;
79 	struct fdt_attach_args *faa = aux;
80 
81 	sc->sc_iot = faa->fa_iot;
82 
83 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
84 	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
85 		panic("%s: bus_space_map failed!", __func__);
86 
87 	printf("\n");
88 
89 	sc->sc_todr.cookie = sc;
90 	sc->sc_todr.todr_gettime = exrtc_gettime;
91 	sc->sc_todr.todr_settime = exrtc_settime;
92 	sc->sc_todr.todr_quality = 0;
93 	todr_attach(&sc->sc_todr);
94 }
95 
96 int
exrtc_gettime(todr_chip_handle_t handle,struct timeval * tv)97 exrtc_gettime(todr_chip_handle_t handle, struct timeval *tv)
98 {
99 	struct exrtc_softc *sc = handle->cookie;
100 	struct clock_ymdhms dt;
101 	int retried = 0;
102 
103 retry:
104 	dt.dt_sec = FROMBCD(HREAD4(sc, RTCSEC));
105 	dt.dt_min = FROMBCD(HREAD4(sc, RTCMIN));
106 	dt.dt_hour = FROMBCD(HREAD4(sc, RTCHOUR));
107 	dt.dt_day = FROMBCD(HREAD4(sc, RTCDAY));
108 	dt.dt_mon = FROMBCD(HREAD4(sc, RTCMON));
109 	dt.dt_year = FROMBCD(HREAD4(sc, RTCYEAR)) + 1900;
110 
111 	/* If the second counter rolled over, retry. */
112 	if (dt.dt_sec > FROMBCD(HREAD4(sc, RTCSEC)) && !retried) {
113 		retried = 1;
114 		goto retry;
115 	}
116 
117 	if (dt.dt_sec > 59 || dt.dt_min > 59 || dt.dt_hour > 23 ||
118 	    dt.dt_day > 31 || dt.dt_day == 0 ||
119 	    dt.dt_mon > 12 || dt.dt_mon == 0 ||
120 	    dt.dt_year < POSIX_BASE_YEAR)
121 		return 1;
122 
123 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
124 	tv->tv_usec = 0;
125 	return 0;
126 }
127 
128 int
exrtc_settime(todr_chip_handle_t handle,struct timeval * tv)129 exrtc_settime(todr_chip_handle_t handle, struct timeval *tv)
130 {
131 	struct exrtc_softc *sc = handle->cookie;
132 	struct clock_ymdhms dt;
133 	uint32_t val;
134 
135 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
136 
137 	HWRITE4(sc, RTCSEC, TOBCD(dt.dt_sec));
138 	HWRITE4(sc, RTCMIN, TOBCD(dt.dt_min));
139 	HWRITE4(sc, RTCHOUR, TOBCD(dt.dt_hour));
140 	HWRITE4(sc, RTCDAY, TOBCD(dt.dt_day));
141 	HWRITE4(sc, RTCMON, TOBCD(dt.dt_mon));
142 	HWRITE4(sc, RTCYEAR, TOBCD(dt.dt_year - 1900));
143 
144 	val = HREAD4(sc, RTCCTRL);
145 	HWRITE4(sc, RTCCTRL, val | RTCCTRL_RTCEN);
146 
147 	return 0;
148 }
149