1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2017 Hiroki Mori. All rights reserved.
5 * Copyright (c) 2017 Ian Lepore.
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 * This code base on isl12xx.c
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34 * Driver for realtime clock EPSON RTC-8583
35 */
36
37 #include "opt_platform.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/clock.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/module.h>
46
47 #ifdef FDT
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 #endif
51
52 #include <dev/iicbus/iiconf.h>
53 #include <dev/iicbus/iicbus.h>
54
55 #include "clock_if.h"
56 #include "iicbus_if.h"
57
58 #define RTC8583_SC_REG 0x01 /* RTC Seconds */
59 #define RTC8583_USERSRAM_REG 0x10 /* User SRAM register (first) */
60 #define MAX_TRANSFER 16
61
62 /*
63 * A struct laid out in the same order as the time registers in the chip.
64 */
65 struct time_regs {
66 uint8_t msec, sec, min, hour, day, month;
67 };
68
69 struct rtc8583_softc {
70 device_t dev;
71 device_t busdev;
72 struct intr_config_hook
73 init_hook;
74 };
75
76 #ifdef FDT
77 static struct ofw_compat_data compat_data[] = {
78 {"epson,rtc8583", 1},
79 {NULL, 0},
80 };
81 #endif
82
83 static void rtc8583_init(void *arg);
84 static int rtc8583_probe(device_t dev);
85 static int rtc8583_attach(device_t dev);
86 static int rtc8583_detach(device_t dev);
87
88 static int rtc8583_gettime(device_t dev, struct timespec *ts);
89 static int rtc8583_settime(device_t dev, struct timespec *ts);
90
91 static int rtc8583_writeto(device_t slavedev, uint8_t regaddr,
92 void *buffer, uint16_t buflen, int waithow);
93
94 /* Implementation */
95
96 static int
97 rtc8583_writeto(device_t slavedev, uint8_t regaddr, void *buffer,
98 uint16_t buflen, int waithow)
99 {
100 struct iic_msg msgs;
101 uint8_t slaveaddr;
102 uint8_t newbuf[MAX_TRANSFER];
103
104 slaveaddr = iicbus_get_addr(slavedev);
105
106 newbuf[0] = regaddr;
107 memcpy(newbuf + 1, buffer, buflen);
108 msgs.slave = slaveaddr;
109 msgs.flags = IIC_M_WR;
110 msgs.len = 1 + buflen;
111 msgs.buf = newbuf;
112
113 return (iicbus_transfer_excl(slavedev, &msgs, 1, waithow));
114 }
115
116 static inline int
117 rtc8583_read1(struct rtc8583_softc *sc, uint8_t reg, uint8_t *data)
118 {
119
120 return (iicdev_readfrom(sc->dev, reg, data, 1, IIC_WAIT));
121 }
122
123 static inline int
124 rtc8583_write1(struct rtc8583_softc *sc, uint8_t reg, uint8_t val)
125 {
126
127 return (rtc8583_writeto(sc->dev, reg, &val, 1, IIC_WAIT));
128 }
129
130 static void
131 rtc8583_init(void *arg)
132 {
133 struct rtc8583_softc *sc;
134
135 sc = (struct rtc8583_softc*)arg;
136 config_intrhook_disestablish(&sc->init_hook);
137
138 /*
139 * Register as a system realtime clock.
140 */
141 clock_register_flags(sc->dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
142 clock_schedule(sc->dev, 1);
143 return;
144 }
145
146 static int
147 rtc8583_probe(device_t dev)
148 {
149
150 #ifdef FDT
151 if (!ofw_bus_status_okay(dev))
152 return (ENXIO);
153
154 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
155 device_set_desc(dev, "EPSON RTC-8583");
156 return (BUS_PROBE_DEFAULT);
157 }
158 #endif
159 return (ENXIO);
160 }
161
162 static int
163 rtc8583_attach(device_t dev)
164 {
165 struct rtc8583_softc *sc;
166
167 sc = device_get_softc(dev);
168 sc->dev = dev;
169 sc->busdev = device_get_parent(sc->dev);
170
171 /*
172 * Chip init must wait until interrupts are enabled. Often i2c access
173 * works only when the interrupts are available.
174 */
175 sc->init_hook.ich_func = rtc8583_init;
176 sc->init_hook.ich_arg = sc;
177 if (config_intrhook_establish(&sc->init_hook) != 0)
178 return (ENOMEM);
179
180 return (0);
181 }
182
183 static int
184 rtc8583_detach(device_t dev)
185 {
186
187 clock_unregister(dev);
188 return (0);
189 }
190
191 static int
192 rtc8583_gettime(device_t dev, struct timespec *ts)
193 {
194 struct rtc8583_softc *sc;
195 struct bcd_clocktime bct;
196 struct time_regs tregs;
197 uint8_t y, ytmp, sreg;
198 int err;
199
200 sc = device_get_softc(dev);
201
202 /* Read the bcd time registers. */
203 if ((err = iicdev_readfrom(sc->dev, RTC8583_SC_REG, &tregs, sizeof(tregs),
204 IIC_WAIT)) != 0)
205 return (err);
206
207 y = tregs.day >> 6;
208 /* Get year from user SRAM */
209 rtc8583_read1(sc, RTC8583_USERSRAM_REG, &sreg);
210
211 /*
212 * Check if year adjustment is required.
213 * RTC has only 2 bits for year value (i.e. maximum is 4 years), so
214 * full year value is stored in user SRAM and updated manually or
215 * by this code.
216 */
217 ytmp = sreg & 0x03;
218 if (ytmp != y) {
219 /* shift according to difference */
220 sreg += y - ytmp;
221
222 /* check if overflow happened */
223 if (ytmp > y)
224 sreg += 4;
225
226 if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
227 return (err);
228 rtc8583_write1(sc, RTC8583_USERSRAM_REG, sreg);
229 iicbus_release_bus(sc->busdev, sc->dev);
230 }
231
232 if (!validbcd(tregs.msec))
233 return (EINVAL);
234
235 /* The 'msec' reg is actually 1/100ths, in bcd. */
236 bct.nsec = bcd2bin(tregs.msec) * 10 * 1000 * 1000;
237 bct.sec = tregs.sec;
238 bct.min = tregs.min;
239 bct.hour = tregs.hour & 0x3f;
240 bct.day = tregs.day & 0x3f;
241 bct.mon = tregs.month & 0x1f;
242 bct.year = bin2bcd(sreg % 100);
243
244 clock_dbgprint_bcd(sc->dev, CLOCK_DBG_READ, &bct);
245 return (clock_bcd_to_ts(&bct, ts, false));
246 }
247
248 static int
249 rtc8583_settime(device_t dev, struct timespec *ts)
250 {
251 struct rtc8583_softc *sc;
252 struct bcd_clocktime bct;
253 struct time_regs tregs;
254 uint8_t sreg;
255 int err;
256
257 sc = device_get_softc(dev);
258 ts->tv_sec -= utc_offset();
259 clock_ts_to_bcd(ts, &bct, false);
260 clock_dbgprint_bcd(sc->dev, CLOCK_DBG_WRITE, &bct);
261
262 /* The 'msec' reg is actually 1/100ths, in bcd. */
263 tregs.msec = bin2bcd(ts->tv_nsec / (10 * 1000 * 1000));
264 tregs.sec = bct.sec;
265 tregs.min = bct.min;
266 tregs.hour = bct.hour;
267 tregs.day = bct.day | (bct.year & 0x03 << 6);
268 tregs.month = bct.mon;
269
270 if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
271 return (err);
272 err = rtc8583_writeto(sc->dev, RTC8583_SC_REG, &tregs,
273 sizeof(tregs), IIC_WAIT);
274 sreg = bcd2bin(bct.year & 0xff);
275 /* save to year to sram */
276 rtc8583_write1(sc, RTC8583_USERSRAM_REG, sreg);
277 iicbus_release_bus(sc->busdev, sc->dev);
278
279 return (err);
280 }
281
282 static device_method_t rtc8583_methods[] = {
283 /* device_if methods */
284 DEVMETHOD(device_probe, rtc8583_probe),
285 DEVMETHOD(device_attach, rtc8583_attach),
286 DEVMETHOD(device_detach, rtc8583_detach),
287
288 /* clock_if methods */
289 DEVMETHOD(clock_gettime, rtc8583_gettime),
290 DEVMETHOD(clock_settime, rtc8583_settime),
291
292 DEVMETHOD_END,
293 };
294
295 static driver_t rtc8583_driver = {
296 "rtc8583",
297 rtc8583_methods,
298 sizeof(struct rtc8583_softc),
299 };
300
301 DRIVER_MODULE(rtc8583, iicbus, rtc8583_driver, NULL, NULL);
302 MODULE_VERSION(rtc8583, 1);
303 MODULE_DEPEND(rtc8583, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
304 IICBUS_FDT_PNP_INFO(compat_data);
Cache object: 2f6b3c3791937960ba25dde76b78d77a
|