summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/exrtc.c
blob: 20a2039304a1fdac727a2af940ce0d2fa1281a97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*	$OpenBSD: exrtc.c,v 1.3 2019/08/29 11:51:48 kettenis Exp $	*/
/*
 * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/time.h>

#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>

#include <dev/clock_subr.h>

extern todr_chip_handle_t todr_handle;

#define RTCCTRL		0x40
#define RTCCTRL_RTCEN	(1 << 0)

#define RTCSEC		0x70
#define RTCMIN		0x74
#define RTCHOUR		0x78
#define RTCDAY		0x7c
#define RTCMON		0x84
#define RTCYEAR		0x88

#define HREAD4(sc, reg)							\
	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
#define HWRITE4(sc, reg, val)						\
	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))

struct exrtc_softc {
	struct device	sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
	struct todr_chip_handle sc_todr;
};

int	exrtc_match(struct device *, void *, void *);
void	exrtc_attach(struct device *, struct device *, void *);

struct cfattach exrtc_ca = {
	sizeof(struct exrtc_softc), exrtc_match, exrtc_attach
};

struct cfdriver exrtc_cd = {
	NULL, "exrtc", DV_DULL
};

int	exrtc_gettime(todr_chip_handle_t, struct timeval *);
int	exrtc_settime(todr_chip_handle_t, struct timeval *);

int
exrtc_match(struct device *parent, void *match, void *aux)
{
	struct fdt_attach_args *faa = aux;

	return OF_is_compatible(faa->fa_node, "samsung,s3c6410-rtc");
}

void
exrtc_attach(struct device *parent, struct device *self, void *aux)
{
	struct exrtc_softc *sc = (struct exrtc_softc *)self;
	struct fdt_attach_args *faa = aux;

	sc->sc_iot = faa->fa_iot;

	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
		panic("%s: bus_space_map failed!", __func__);

	printf("\n");

	sc->sc_todr.cookie = sc;
	sc->sc_todr.todr_gettime = exrtc_gettime;
	sc->sc_todr.todr_settime = exrtc_settime;
	todr_handle = &sc->sc_todr;
}

int
exrtc_gettime(todr_chip_handle_t handle, struct timeval *tv)
{
	struct exrtc_softc *sc = handle->cookie;
	struct clock_ymdhms dt;
	int retried = 0;

retry:
	dt.dt_sec = FROMBCD(HREAD4(sc, RTCSEC));
	dt.dt_min = FROMBCD(HREAD4(sc, RTCMIN));
	dt.dt_hour = FROMBCD(HREAD4(sc, RTCHOUR));
	dt.dt_day = FROMBCD(HREAD4(sc, RTCDAY));
	dt.dt_mon = FROMBCD(HREAD4(sc, RTCMON));
	dt.dt_year = FROMBCD(HREAD4(sc, RTCYEAR)) + 1900;

	/* If the second counter rolled over, retry. */
	if (dt.dt_sec > FROMBCD(HREAD4(sc, RTCSEC)) && !retried) {
		retried = 1;
		goto retry;
	}

	if (dt.dt_sec > 59 || dt.dt_min > 59 || dt.dt_hour > 23 ||
	    dt.dt_day > 31 || dt.dt_day == 0 ||
	    dt.dt_mon > 12 || dt.dt_mon == 0 ||
	    dt.dt_year < POSIX_BASE_YEAR)
		return 1;

	tv->tv_sec = clock_ymdhms_to_secs(&dt);
	tv->tv_usec = 0;
	return 0;
}

int
exrtc_settime(todr_chip_handle_t handle, struct timeval *tv)
{
	struct exrtc_softc *sc = handle->cookie;
	struct clock_ymdhms dt;
	uint32_t val;

	clock_secs_to_ymdhms(tv->tv_sec, &dt);

	HWRITE4(sc, RTCSEC, TOBCD(dt.dt_sec));
	HWRITE4(sc, RTCMIN, TOBCD(dt.dt_min));
	HWRITE4(sc, RTCHOUR, TOBCD(dt.dt_hour));
	HWRITE4(sc, RTCDAY, TOBCD(dt.dt_day));
	HWRITE4(sc, RTCMON, TOBCD(dt.dt_mon));
	HWRITE4(sc, RTCYEAR, TOBCD(dt.dt_year - 1900));

	val = HREAD4(sc, RTCCTRL);
	HWRITE4(sc, RTCCTRL, val | RTCCTRL_RTCEN);

	return 0;
}