xref: /freebsd/sys/dev/iicbus/rtc/rx8803.c (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2020 Alstom Group.
5  * Copyright (c) 2020 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/clock.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 
42 #include <dev/iicbus/iiconf.h>
43 #include <dev/iicbus/iicbus.h>
44 
45 #include "clock_if.h"
46 #include "iicbus_if.h"
47 
48 #define	BIT(x)			(1 << (x))
49 
50 #define RX8803_TIME		0x0
51 #define RX8803_FLAGS		0xE
52 #define RX8803_CTRL		0xF
53 
54 #define RX8803_FLAGS_V1F	BIT(0)
55 #define RX8803_FLAGS_V2F	BIT(1)
56 
57 #define RX8803_CTRL_DISABLE	BIT(0)
58 
59 #define HALF_OF_SEC_NS		500000000
60 #define MAX_WRITE_LEN		16
61 
62 struct rx8803_time {
63 	uint8_t sec;
64 	uint8_t min;
65 	uint8_t hour;
66 	uint8_t dow;
67 	uint8_t day;
68 	uint8_t mon;
69 	uint8_t year;
70 };
71 
72 static int rx8803_probe(device_t dev);
73 static int rx8803_attach(device_t dev);
74 static int rx8803_detach(device_t dev);
75 
76 static int rx8803_gettime(device_t dev, struct timespec *ts);
77 static int rx8803_settime(device_t dev, struct timespec *ts);
78 
79 static int rx8803_check_status(device_t dev);
80 
81 static int
82 rx8803_check_status(device_t dev)
83 {
84 	uint8_t flags;
85 	int rc;
86 
87 	rc = iicdev_readfrom(dev, RX8803_FLAGS, &flags, 1, IIC_WAIT);
88 	if (rc != 0)
89 		return (rc);
90 
91 	if (flags & RX8803_FLAGS_V2F) {
92 		device_printf(dev, "Low voltage flag set, date is incorrect\n");
93 		return (ENXIO);
94 	}
95 
96 	return (0);
97 }
98 
99 static int
100 rx8803_gettime(device_t dev, struct timespec *ts)
101 {
102 	struct rx8803_time data;
103 	struct bcd_clocktime bcd;
104 	int rc;
105 
106 	rc = rx8803_check_status(dev);
107 	if (rc != 0)
108 		return (rc);
109 
110 	rc = iicdev_readfrom(dev,
111 	    RX8803_TIME,
112 	    &data, sizeof(struct rx8803_time),
113 	    IIC_WAIT);
114 	if (rc != 0)
115 		return (rc);
116 
117 	bcd.nsec = 0;
118 	bcd.sec = data.sec & 0x7F;
119 	bcd.min = data.min & 0x7F;
120 	bcd.hour = data.hour & 0x3F;
121 	bcd.dow = flsl(data.dow & 0x7F) - 1;
122 	bcd.day = data.day & 0x3F;
123 	bcd.mon = (data.mon & 0x1F);
124 	bcd.year = data.year;
125 
126 	clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bcd);
127 
128 	rc = clock_bcd_to_ts(&bcd, ts, false);
129 	return (rc);
130 }
131 
132 static int
133 rx8803_settime(device_t dev, struct timespec *ts)
134 {
135 	struct rx8803_time data;
136 	struct bcd_clocktime bcd;
137 	uint8_t reg;
138 	int rc;
139 
140 	ts->tv_sec -= utc_offset();
141 	clock_ts_to_bcd(ts, &bcd, false);
142 	clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bcd);
143 
144 	data.sec = bcd.sec;
145 	data.min = bcd.min;
146 	data.hour = bcd.hour;
147 	data.dow = 1 << bcd.dow;
148 	data.day = bcd.day;
149 	data.mon = bcd.mon;
150 	data.year = bcd.year;
151 
152 	if (ts->tv_nsec > HALF_OF_SEC_NS)
153 		data.sec++;
154 
155 	/* First disable clock. */
156 	rc = iicdev_readfrom(dev, RX8803_CTRL, &reg, sizeof(uint8_t), IIC_WAIT);
157 	if (rc != 0)
158 		return (rc);
159 
160 	reg |= RX8803_CTRL_DISABLE;
161 
162 	rc = iicdev_writeto(dev, RX8803_CTRL, &reg, sizeof(uint8_t), IIC_WAIT);
163 	if (rc != 0)
164 		return (rc);
165 
166 	/* Update the date. */
167 	rc = iicdev_writeto(dev,
168 	    RX8803_TIME,
169 	    &data, sizeof(struct rx8803_time),
170 	    IIC_WAIT);
171 	if (rc != 0)
172 		return (rc);
173 
174 	/* Now restart it. */
175 	reg &= ~RX8803_CTRL_DISABLE;
176 	rc = iicdev_writeto(dev, RX8803_CTRL, &reg, sizeof(uint8_t), IIC_WAIT);
177 	if (rc != 0)
178 		return (rc);
179 
180 	/* Clear low voltage flags, as we have just updated the clock. */
181 	rc = iicdev_readfrom(dev, RX8803_FLAGS, &reg, sizeof(uint8_t), IIC_WAIT);
182 	if (rc != 0)
183 		return (rc);
184 
185 	reg &= ~(RX8803_FLAGS_V1F | RX8803_FLAGS_V2F);
186 	rc = iicdev_writeto(dev, RX8803_FLAGS, &reg, sizeof(uint8_t), IIC_WAIT);
187 	return (rc);
188 }
189 
190 static int
191 rx8803_probe(device_t dev)
192 {
193 
194 	if (!ofw_bus_is_compatible(dev, "epson,rx8803"))
195 		return (ENXIO);
196 
197 	device_set_desc(dev, "Epson RX8803 Real Time Clock");
198 
199 	return (BUS_PROBE_GENERIC);
200 }
201 
202 static int
203 rx8803_attach(device_t dev)
204 {
205 
206 	/* Set 1 sec resolution. */
207 	clock_register_flags(dev, 1000000, 0);
208 	clock_schedule(dev, 1);
209 
210 	return (0);
211 
212 }
213 
214 static int
215 rx8803_detach(device_t dev)
216 {
217 
218 	clock_unregister(dev);
219 
220 	return (0);
221 }
222 
223 static device_method_t rx8803_methods[] = {
224 	DEVMETHOD(device_probe, rx8803_probe),
225 	DEVMETHOD(device_attach, rx8803_attach),
226 	DEVMETHOD(device_detach, rx8803_detach),
227 
228 	DEVMETHOD(clock_gettime, rx8803_gettime),
229 	DEVMETHOD(clock_settime, rx8803_settime),
230 
231 	DEVMETHOD_END,
232 };
233 
234 static driver_t rx8803_driver = {
235 	"rx8803",
236 	rx8803_methods,
237 	0,			/* We don't need softc for this one. */
238 };
239 
240 DRIVER_MODULE(rx8803, iicbus, rx8803_driver, NULL, NULL);
241 MODULE_DEPEND(rx8803, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
242