2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2017 Hiroki Mori. All rights reserved.
5 * Copyright (c) 2017 Ian Lepore.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
27 * This code base on isl12xx.c
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Driver for realtime clock EPSON RTC-8583
37 #include "opt_platform.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/clock.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/iicbus/iiconf.h>
53 #include <dev/iicbus/iicbus.h>
56 #include "iicbus_if.h"
58 #define RTC8583_SC_REG 0x01 /* RTC Seconds */
59 #define RTC8583_USERSRAM_REG 0x10 /* User SRAM register (first) */
60 #define MAX_TRANSFER 16
63 * A struct laid out in the same order as the time registers in the chip.
66 uint8_t msec, sec, min, hour, day, month;
69 struct rtc8583_softc {
72 struct intr_config_hook
77 static struct ofw_compat_data compat_data[] = {
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);
88 static int rtc8583_gettime(device_t dev, struct timespec *ts);
89 static int rtc8583_settime(device_t dev, struct timespec *ts);
91 static int rtc8583_writeto(device_t slavedev, uint8_t regaddr,
92 void *buffer, uint16_t buflen, int waithow);
97 rtc8583_writeto(device_t slavedev, uint8_t regaddr, void *buffer,
98 uint16_t buflen, int waithow)
102 uint8_t newbuf[MAX_TRANSFER];
104 slaveaddr = iicbus_get_addr(slavedev);
107 memcpy(newbuf + 1, buffer, buflen);
108 msgs.slave = slaveaddr;
109 msgs.flags = IIC_M_WR;
110 msgs.len = 1 + buflen;
113 return (iicbus_transfer_excl(slavedev, &msgs, 1, waithow));
117 rtc8583_read1(struct rtc8583_softc *sc, uint8_t reg, uint8_t *data)
120 return (iicdev_readfrom(sc->dev, reg, data, 1, IIC_WAIT));
124 rtc8583_write1(struct rtc8583_softc *sc, uint8_t reg, uint8_t val)
127 return (rtc8583_writeto(sc->dev, reg, &val, 1, IIC_WAIT));
131 rtc8583_init(void *arg)
133 struct rtc8583_softc *sc;
135 sc = (struct rtc8583_softc*)arg;
136 config_intrhook_disestablish(&sc->init_hook);
139 * Register as a system realtime clock.
141 clock_register_flags(sc->dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
142 clock_schedule(sc->dev, 1);
147 rtc8583_probe(device_t dev)
151 if (!ofw_bus_status_okay(dev))
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);
163 rtc8583_attach(device_t dev)
165 struct rtc8583_softc *sc;
167 sc = device_get_softc(dev);
169 sc->busdev = device_get_parent(sc->dev);
172 * Chip init must wait until interrupts are enabled. Often i2c access
173 * works only when the interrupts are available.
175 sc->init_hook.ich_func = rtc8583_init;
176 sc->init_hook.ich_arg = sc;
177 if (config_intrhook_establish(&sc->init_hook) != 0)
184 rtc8583_detach(device_t dev)
187 clock_unregister(dev);
192 rtc8583_gettime(device_t dev, struct timespec *ts)
194 struct rtc8583_softc *sc;
195 struct bcd_clocktime bct;
196 struct time_regs tregs;
197 uint8_t y, ytmp, sreg;
200 sc = device_get_softc(dev);
202 /* Read the bcd time registers. */
203 if ((err = iicdev_readfrom(sc->dev, RTC8583_SC_REG, &tregs, sizeof(tregs),
208 /* Get year from user SRAM */
209 rtc8583_read1(sc, RTC8583_USERSRAM_REG, &sreg);
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
219 /* shift according to difference */
222 /* check if overflow happened */
226 if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
228 rtc8583_write1(sc, RTC8583_USERSRAM_REG, sreg);
229 iicbus_release_bus(sc->busdev, sc->dev);
232 if (!validbcd(tregs.msec))
235 /* The 'msec' reg is actually 1/100ths, in bcd. */
236 bct.nsec = bcd2bin(tregs.msec) * 10 * 1000 * 1000;
239 bct.hour = tregs.hour & 0x3f;
240 bct.day = tregs.day & 0x3f;
241 bct.mon = tregs.month & 0x1f;
242 bct.year = bin2bcd(sreg % 100);
244 clock_dbgprint_bcd(sc->dev, CLOCK_DBG_READ, &bct);
245 return (clock_bcd_to_ts(&bct, ts, false));
249 rtc8583_settime(device_t dev, struct timespec *ts)
251 struct rtc8583_softc *sc;
252 struct bcd_clocktime bct;
253 struct time_regs tregs;
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);
262 /* The 'msec' reg is actually 1/100ths, in bcd. */
263 tregs.msec = bin2bcd(ts->tv_nsec / (10 * 1000 * 1000));
266 tregs.hour = bct.hour;
267 tregs.day = bct.day | (bct.year & 0x03 << 6);
268 tregs.month = bct.mon;
270 if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
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);
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),
288 /* clock_if methods */
289 DEVMETHOD(clock_gettime, rtc8583_gettime),
290 DEVMETHOD(clock_settime, rtc8583_settime),
295 static driver_t rtc8583_driver = {
298 sizeof(struct rtc8583_softc),
300 static devclass_t rtc8583_devclass;
302 DRIVER_MODULE(rtc8583, iicbus, rtc8583_driver, rtc8583_devclass, NULL, NULL);
303 MODULE_VERSION(rtc8583, 1);
304 MODULE_DEPEND(rtc8583, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
305 IICBUS_FDT_PNP_INFO(compat_data);