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