xref: /openbsd/sys/dev/fdt/exrtc.c (revision 3cab2bb3)
1 /*	$OpenBSD: exrtc.c,v 1.3 2019/08/29 11:51:48 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 extern todr_chip_handle_t todr_handle;
32 
33 #define RTCCTRL		0x40
34 #define RTCCTRL_RTCEN	(1 << 0)
35 
36 #define RTCSEC		0x70
37 #define RTCMIN		0x74
38 #define RTCHOUR		0x78
39 #define RTCDAY		0x7c
40 #define RTCMON		0x84
41 #define RTCYEAR		0x88
42 
43 #define HREAD4(sc, reg)							\
44 	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
45 #define HWRITE4(sc, reg, val)						\
46 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
47 
48 struct exrtc_softc {
49 	struct device	sc_dev;
50 	bus_space_tag_t		sc_iot;
51 	bus_space_handle_t	sc_ioh;
52 	struct todr_chip_handle sc_todr;
53 };
54 
55 int	exrtc_match(struct device *, void *, void *);
56 void	exrtc_attach(struct device *, struct device *, void *);
57 
58 struct cfattach exrtc_ca = {
59 	sizeof(struct exrtc_softc), exrtc_match, exrtc_attach
60 };
61 
62 struct cfdriver exrtc_cd = {
63 	NULL, "exrtc", DV_DULL
64 };
65 
66 int	exrtc_gettime(todr_chip_handle_t, struct timeval *);
67 int	exrtc_settime(todr_chip_handle_t, struct timeval *);
68 
69 int
70 exrtc_match(struct device *parent, void *match, void *aux)
71 {
72 	struct fdt_attach_args *faa = aux;
73 
74 	return OF_is_compatible(faa->fa_node, "samsung,s3c6410-rtc");
75 }
76 
77 void
78 exrtc_attach(struct device *parent, struct device *self, void *aux)
79 {
80 	struct exrtc_softc *sc = (struct exrtc_softc *)self;
81 	struct fdt_attach_args *faa = aux;
82 
83 	sc->sc_iot = faa->fa_iot;
84 
85 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
86 	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
87 		panic("%s: bus_space_map failed!", __func__);
88 
89 	printf("\n");
90 
91 	sc->sc_todr.cookie = sc;
92 	sc->sc_todr.todr_gettime = exrtc_gettime;
93 	sc->sc_todr.todr_settime = exrtc_settime;
94 	todr_handle = &sc->sc_todr;
95 }
96 
97 int
98 exrtc_gettime(todr_chip_handle_t handle, struct timeval *tv)
99 {
100 	struct exrtc_softc *sc = handle->cookie;
101 	struct clock_ymdhms dt;
102 	int retried = 0;
103 
104 retry:
105 	dt.dt_sec = FROMBCD(HREAD4(sc, RTCSEC));
106 	dt.dt_min = FROMBCD(HREAD4(sc, RTCMIN));
107 	dt.dt_hour = FROMBCD(HREAD4(sc, RTCHOUR));
108 	dt.dt_day = FROMBCD(HREAD4(sc, RTCDAY));
109 	dt.dt_mon = FROMBCD(HREAD4(sc, RTCMON));
110 	dt.dt_year = FROMBCD(HREAD4(sc, RTCYEAR)) + 1900;
111 
112 	/* If the second counter rolled over, retry. */
113 	if (dt.dt_sec > FROMBCD(HREAD4(sc, RTCSEC)) && !retried) {
114 		retried = 1;
115 		goto retry;
116 	}
117 
118 	if (dt.dt_sec > 59 || dt.dt_min > 59 || dt.dt_hour > 23 ||
119 	    dt.dt_day > 31 || dt.dt_day == 0 ||
120 	    dt.dt_mon > 12 || dt.dt_mon == 0 ||
121 	    dt.dt_year < POSIX_BASE_YEAR)
122 		return 1;
123 
124 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
125 	tv->tv_usec = 0;
126 	return 0;
127 }
128 
129 int
130 exrtc_settime(todr_chip_handle_t handle, struct timeval *tv)
131 {
132 	struct exrtc_softc *sc = handle->cookie;
133 	struct clock_ymdhms dt;
134 	uint32_t val;
135 
136 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
137 
138 	HWRITE4(sc, RTCSEC, TOBCD(dt.dt_sec));
139 	HWRITE4(sc, RTCMIN, TOBCD(dt.dt_min));
140 	HWRITE4(sc, RTCHOUR, TOBCD(dt.dt_hour));
141 	HWRITE4(sc, RTCDAY, TOBCD(dt.dt_day));
142 	HWRITE4(sc, RTCMON, TOBCD(dt.dt_mon));
143 	HWRITE4(sc, RTCYEAR, TOBCD(dt.dt_year - 1900));
144 
145 	val = HREAD4(sc, RTCCTRL);
146 	HWRITE4(sc, RTCCTRL, val | RTCCTRL_RTCEN);
147 
148 	return 0;
149 }
150