xref: /freebsd/sys/arm/mv/rtc.c (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
5  * All rights reserved.
6  *
7  * Developed by Semihalf.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of MARVELL nor the names of contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/lock.h>
37 #include <sys/time.h>
38 #include <sys/clock.h>
39 #include <sys/resource.h>
40 #include <sys/systm.h>
41 #include <sys/rman.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include "clock_if.h"
52 
53 #define MV_RTC_TIME_REG		0x00
54 #define MV_RTC_DATE_REG		0x04
55 #define YEAR_BASE		2000
56 
57 struct mv_rtc_softc {
58 	device_t dev;
59 	struct resource *res[1];
60 };
61 
62 static struct resource_spec res_spec[] = {
63 	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
64 	{ -1, 0 }
65 };
66 
67 static int mv_rtc_probe(device_t dev);
68 static int mv_rtc_attach(device_t dev);
69 
70 static int mv_rtc_gettime(device_t dev, struct timespec *ts);
71 static int mv_rtc_settime(device_t dev, struct timespec *ts);
72 
73 static uint32_t mv_rtc_reg_read(struct mv_rtc_softc *sc, bus_size_t off);
74 static int mv_rtc_reg_write(struct mv_rtc_softc *sc, bus_size_t off,
75     uint32_t val);
76 
77 static device_method_t mv_rtc_methods[] = {
78 	DEVMETHOD(device_probe,		mv_rtc_probe),
79 	DEVMETHOD(device_attach,	mv_rtc_attach),
80 
81 	DEVMETHOD(clock_gettime,	mv_rtc_gettime),
82 	DEVMETHOD(clock_settime,	mv_rtc_settime),
83 
84 	{ 0, 0 },
85 };
86 
87 static driver_t mv_rtc_driver = {
88 	"rtc",
89 	mv_rtc_methods,
90 	sizeof(struct mv_rtc_softc),
91 };
92 
93 DRIVER_MODULE(mv_rtc, simplebus, mv_rtc_driver, 0, 0);
94 
95 static int
96 mv_rtc_probe(device_t dev)
97 {
98 
99 	if (!ofw_bus_status_okay(dev))
100 		return (ENXIO);
101 
102 	if (!ofw_bus_is_compatible(dev, "mrvl,rtc"))
103 		return (ENXIO);
104 
105 	device_set_desc(dev, "Marvell Integrated RTC");
106 	return (BUS_PROBE_DEFAULT);
107 }
108 
109 static int
110 mv_rtc_attach(device_t dev)
111 {
112 	struct mv_rtc_softc *sc;
113 
114 	sc = device_get_softc(dev);
115 	sc->dev = dev;
116 
117 	clock_register(dev, 1000000);
118 
119 	if (bus_alloc_resources(dev, res_spec, sc->res)) {
120 		device_printf(dev, "could not allocate resources\n");
121 		return (ENXIO);
122 	}
123 
124 	return (0);
125 }
126 
127 static int
128 mv_rtc_gettime(device_t dev, struct timespec *ts)
129 {
130 	struct clocktime ct;
131 	struct mv_rtc_softc *sc;
132 	uint32_t val;
133 
134 	sc = device_get_softc(dev);
135 
136 	val = mv_rtc_reg_read(sc, MV_RTC_TIME_REG);
137 
138 	ct.nsec = 0;
139 	ct.sec = FROMBCD(val & 0x7f);
140 	ct.min = FROMBCD((val & 0x7f00) >> 8);
141 	ct.hour = FROMBCD((val & 0x3f0000) >> 16);
142 	ct.dow = FROMBCD((val & 0x7000000) >> 24) - 1;
143 
144 	val = mv_rtc_reg_read(sc, MV_RTC_DATE_REG);
145 
146 	ct.day = FROMBCD(val & 0x7f);
147 	ct.mon = FROMBCD((val & 0x1f00) >> 8);
148 	ct.year = YEAR_BASE + FROMBCD((val & 0xff0000) >> 16);
149 
150 	return (clock_ct_to_ts(&ct, ts));
151 }
152 
153 static int
154 mv_rtc_settime(device_t dev, struct timespec *ts)
155 {
156 	struct clocktime ct;
157 	struct mv_rtc_softc *sc;
158 	uint32_t val;
159 
160 	sc = device_get_softc(dev);
161 
162 	/* Resolution: 1 sec */
163 	if (ts->tv_nsec >= 500000000)
164 		ts->tv_sec++;
165 	ts->tv_nsec = 0;
166 	clock_ts_to_ct(ts, &ct);
167 
168 	val = TOBCD(ct.sec) | (TOBCD(ct.min) << 8) |
169 	    (TOBCD(ct.hour) << 16) | (TOBCD( ct.dow + 1) << 24);
170 	mv_rtc_reg_write(sc, MV_RTC_TIME_REG, val);
171 
172 	val = TOBCD(ct.day) | (TOBCD(ct.mon) << 8) |
173 	    (TOBCD(ct.year - YEAR_BASE) << 16);
174 	mv_rtc_reg_write(sc, MV_RTC_DATE_REG, val);
175 
176 	return (0);
177 }
178 
179 static uint32_t
180 mv_rtc_reg_read(struct mv_rtc_softc *sc, bus_size_t off)
181 {
182 
183 	return (bus_read_4(sc->res[0], off));
184 }
185 
186 static int
187 mv_rtc_reg_write(struct mv_rtc_softc *sc, bus_size_t off, uint32_t val)
188 {
189 
190 	bus_write_4(sc->res[0], off, val);
191 	return (0);
192 }
193