xref: /freebsd/sys/dev/iicbus/rtc/rx8803.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
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 	device_t bus;
138 	uint8_t reg;
139 	int rc;
140 
141 	bus = device_get_parent(dev);
142 
143 	ts->tv_sec -= utc_offset();
144 	clock_ts_to_bcd(ts, &bcd, false);
145 	clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bcd);
146 
147 	data.sec = bcd.sec;
148 	data.min = bcd.min;
149 	data.hour = bcd.hour;
150 	data.dow = 1 << bcd.dow;
151 	data.day = bcd.day;
152 	data.mon = bcd.mon;
153 	data.year = bcd.year;
154 
155 	if (ts->tv_nsec > HALF_OF_SEC_NS)
156 		data.sec++;
157 
158 	/* First disable clock. */
159 	rc = iicdev_readfrom(dev, RX8803_CTRL, &reg, sizeof(uint8_t), IIC_WAIT);
160 	if (rc != 0)
161 		return (rc);
162 
163 	reg |= RX8803_CTRL_DISABLE;
164 
165 	rc = iicdev_writeto(dev, RX8803_CTRL, &reg, sizeof(uint8_t), IIC_WAIT);
166 	if (rc != 0)
167 		return (rc);
168 
169 	/* Update the date. */
170 	rc = iicdev_writeto(dev,
171 	    RX8803_TIME,
172 	    &data, sizeof(struct rx8803_time),
173 	    IIC_WAIT);
174 	if (rc != 0)
175 		return (rc);
176 
177 	/* Now restart it. */
178 	reg &= ~RX8803_CTRL_DISABLE;
179 	rc = iicdev_writeto(dev, RX8803_CTRL, &reg, sizeof(uint8_t), IIC_WAIT);
180 	if (rc != 0)
181 		return (rc);
182 
183 	/* Clear low voltage flags, as we have just updated the clock. */
184 	rc = iicdev_readfrom(dev, RX8803_FLAGS, &reg, sizeof(uint8_t), IIC_WAIT);
185 	if (rc != 0)
186 		return (rc);
187 
188 	reg &= ~(RX8803_FLAGS_V1F | RX8803_FLAGS_V2F);
189 	rc = iicdev_writeto(dev, RX8803_FLAGS, &reg, sizeof(uint8_t), IIC_WAIT);
190 	return (rc);
191 }
192 
193 static int
194 rx8803_probe(device_t dev)
195 {
196 
197 	if (!ofw_bus_is_compatible(dev, "epson,rx8803"))
198 		return (ENXIO);
199 
200 	device_set_desc(dev, "Epson RX8803 Real Time Clock");
201 
202 	return (BUS_PROBE_GENERIC);
203 }
204 
205 static int
206 rx8803_attach(device_t dev)
207 {
208 
209 	/* Set 1 sec resolution. */
210 	clock_register_flags(dev, 1000000, 0);
211 	clock_schedule(dev, 1);
212 
213 	return (0);
214 
215 }
216 
217 static int
218 rx8803_detach(device_t dev)
219 {
220 
221 	clock_unregister(dev);
222 
223 	return (0);
224 }
225 
226 static device_method_t rx8803_methods[] = {
227 	DEVMETHOD(device_probe, rx8803_probe),
228 	DEVMETHOD(device_attach, rx8803_attach),
229 	DEVMETHOD(device_detach, rx8803_detach),
230 
231 	DEVMETHOD(clock_gettime, rx8803_gettime),
232 	DEVMETHOD(clock_settime, rx8803_settime),
233 
234 	DEVMETHOD_END,
235 };
236 
237 static driver_t rx8803_driver = {
238 	"rx8803",
239 	rx8803_methods,
240 	0,			/* We don't need softc for this one. */
241 };
242 
243 static devclass_t rx8803_devclass;
244 
245 DRIVER_MODULE(rx8803, iicbus, rx8803_driver, rx8803_devclass, NULL, NULL);
246 MODULE_DEPEND(rx8803, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
247