xref: /freebsd/sys/arm/mv/rtc.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/lock.h>
39 #include <sys/time.h>
40 #include <sys/clock.h>
41 #include <sys/resource.h>
42 #include <sys/systm.h>
43 #include <sys/rman.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 
53 #include "clock_if.h"
54 
55 #define MV_RTC_TIME_REG		0x00
56 #define MV_RTC_DATE_REG		0x04
57 #define YEAR_BASE		2000
58 
59 struct mv_rtc_softc {
60 	device_t dev;
61 	struct resource *res[1];
62 };
63 
64 static struct resource_spec res_spec[] = {
65 	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
66 	{ -1, 0 }
67 };
68 
69 static int mv_rtc_probe(device_t dev);
70 static int mv_rtc_attach(device_t dev);
71 
72 static int mv_rtc_gettime(device_t dev, struct timespec *ts);
73 static int mv_rtc_settime(device_t dev, struct timespec *ts);
74 
75 static uint32_t mv_rtc_reg_read(struct mv_rtc_softc *sc, bus_size_t off);
76 static int mv_rtc_reg_write(struct mv_rtc_softc *sc, bus_size_t off,
77     uint32_t val);
78 
79 static device_method_t mv_rtc_methods[] = {
80 	DEVMETHOD(device_probe,		mv_rtc_probe),
81 	DEVMETHOD(device_attach,	mv_rtc_attach),
82 
83 	DEVMETHOD(clock_gettime,	mv_rtc_gettime),
84 	DEVMETHOD(clock_settime,	mv_rtc_settime),
85 
86 	{ 0, 0 },
87 };
88 
89 static driver_t mv_rtc_driver = {
90 	"rtc",
91 	mv_rtc_methods,
92 	sizeof(struct mv_rtc_softc),
93 };
94 
95 DRIVER_MODULE(mv_rtc, simplebus, mv_rtc_driver, 0, 0);
96 
97 static int
98 mv_rtc_probe(device_t dev)
99 {
100 
101 	if (!ofw_bus_status_okay(dev))
102 		return (ENXIO);
103 
104 	if (!ofw_bus_is_compatible(dev, "mrvl,rtc"))
105 		return (ENXIO);
106 
107 	device_set_desc(dev, "Marvell Integrated RTC");
108 	return (BUS_PROBE_DEFAULT);
109 }
110 
111 static int
112 mv_rtc_attach(device_t dev)
113 {
114 	struct mv_rtc_softc *sc;
115 
116 	sc = device_get_softc(dev);
117 	sc->dev = dev;
118 
119 	clock_register(dev, 1000000);
120 
121 	if (bus_alloc_resources(dev, res_spec, sc->res)) {
122 		device_printf(dev, "could not allocate resources\n");
123 		return (ENXIO);
124 	}
125 
126 	return (0);
127 }
128 
129 static int
130 mv_rtc_gettime(device_t dev, struct timespec *ts)
131 {
132 	struct clocktime ct;
133 	struct mv_rtc_softc *sc;
134 	uint32_t val;
135 
136 	sc = device_get_softc(dev);
137 
138 	val = mv_rtc_reg_read(sc, MV_RTC_TIME_REG);
139 
140 	ct.nsec = 0;
141 	ct.sec = FROMBCD(val & 0x7f);
142 	ct.min = FROMBCD((val & 0x7f00) >> 8);
143 	ct.hour = FROMBCD((val & 0x3f0000) >> 16);
144 	ct.dow = FROMBCD((val & 0x7000000) >> 24) - 1;
145 
146 	val = mv_rtc_reg_read(sc, MV_RTC_DATE_REG);
147 
148 	ct.day = FROMBCD(val & 0x7f);
149 	ct.mon = FROMBCD((val & 0x1f00) >> 8);
150 	ct.year = YEAR_BASE + FROMBCD((val & 0xff0000) >> 16);
151 
152 	return (clock_ct_to_ts(&ct, ts));
153 }
154 
155 static int
156 mv_rtc_settime(device_t dev, struct timespec *ts)
157 {
158 	struct clocktime ct;
159 	struct mv_rtc_softc *sc;
160 	uint32_t val;
161 
162 	sc = device_get_softc(dev);
163 
164 	/* Resolution: 1 sec */
165 	if (ts->tv_nsec >= 500000000)
166 		ts->tv_sec++;
167 	ts->tv_nsec = 0;
168 	clock_ts_to_ct(ts, &ct);
169 
170 	val = TOBCD(ct.sec) | (TOBCD(ct.min) << 8) |
171 	    (TOBCD(ct.hour) << 16) | (TOBCD( ct.dow + 1) << 24);
172 	mv_rtc_reg_write(sc, MV_RTC_TIME_REG, val);
173 
174 	val = TOBCD(ct.day) | (TOBCD(ct.mon) << 8) |
175 	    (TOBCD(ct.year - YEAR_BASE) << 16);
176 	mv_rtc_reg_write(sc, MV_RTC_DATE_REG, val);
177 
178 	return (0);
179 }
180 
181 static uint32_t
182 mv_rtc_reg_read(struct mv_rtc_softc *sc, bus_size_t off)
183 {
184 
185 	return (bus_read_4(sc->res[0], off));
186 }
187 
188 static int
189 mv_rtc_reg_write(struct mv_rtc_softc *sc, bus_size_t off, uint32_t val)
190 {
191 
192 	bus_write_4(sc->res[0], off, val);
193 	return (0);
194 }
195