2 * Copyright (c) 2017 Hiroki Mori. All rights reserved.
3 * Copyright (c) 2017 Ian Lepore. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * This code base on isl12xx.c
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Driver for realtime clock EPSON RTC-8583
35 #include "opt_platform.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/clock.h>
41 #include <sys/kernel.h>
43 #include <sys/module.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/iicbus/iiconf.h>
51 #include <dev/iicbus/iicbus.h>
54 #include "iicbus_if.h"
56 #define RTC8583_SC_REG 0x01 /* RTC Seconds */
57 #define RTC8583_USERSRAM_REG 0x10 /* User SRAM register (first) */
58 #define MAX_TRANSFER 16
61 * A struct laid out in the same order as the time registers in the chip.
64 uint8_t msec, sec, min, hour, day, month;
67 struct rtc8583_softc {
70 struct intr_config_hook
75 static struct ofw_compat_data compat_data[] = {
81 static void rtc8583_init(void *arg);
82 static int rtc8583_probe(device_t dev);
83 static int rtc8583_attach(device_t dev);
84 static int rtc8583_detach(device_t dev);
86 static int rtc8583_gettime(device_t dev, struct timespec *ts);
87 static int rtc8583_settime(device_t dev, struct timespec *ts);
89 static int rtc8583_writeto(device_t slavedev, uint8_t regaddr,
90 void *buffer, uint16_t buflen, int waithow);
95 rtc8583_writeto(device_t slavedev, uint8_t regaddr, void *buffer,
96 uint16_t buflen, int waithow)
100 uint8_t newbuf[MAX_TRANSFER];
102 slaveaddr = iicbus_get_addr(slavedev);
105 memcpy(newbuf + 1, buffer, buflen);
106 msgs.slave = slaveaddr;
107 msgs.flags = IIC_M_WR;
108 msgs.len = 1 + buflen;
111 return (iicbus_transfer_excl(slavedev, &msgs, 1, waithow));
115 rtc8583_read1(struct rtc8583_softc *sc, uint8_t reg, uint8_t *data)
118 return (iicdev_readfrom(sc->dev, reg, data, 1, IIC_WAIT));
122 rtc8583_write1(struct rtc8583_softc *sc, uint8_t reg, uint8_t val)
125 return (rtc8583_writeto(sc->dev, reg, &val, 1, IIC_WAIT));
129 rtc8583_init(void *arg)
131 struct rtc8583_softc *sc;
133 sc = (struct rtc8583_softc*)arg;
134 config_intrhook_disestablish(&sc->init_hook);
137 * Register as a system realtime clock.
139 clock_register_flags(sc->dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
140 clock_schedule(sc->dev, 1);
145 rtc8583_probe(device_t dev)
149 if (!ofw_bus_status_okay(dev))
152 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
153 device_set_desc(dev, "EPSON RTC-8583");
154 return (BUS_PROBE_DEFAULT);
161 rtc8583_attach(device_t dev)
163 struct rtc8583_softc *sc;
165 sc = device_get_softc(dev);
167 sc->busdev = device_get_parent(sc->dev);
170 * Chip init must wait until interrupts are enabled. Often i2c access
171 * works only when the interrupts are available.
173 sc->init_hook.ich_func = rtc8583_init;
174 sc->init_hook.ich_arg = sc;
175 if (config_intrhook_establish(&sc->init_hook) != 0)
182 rtc8583_detach(device_t dev)
185 clock_unregister(dev);
190 rtc8583_gettime(device_t dev, struct timespec *ts)
192 struct rtc8583_softc *sc;
193 struct bcd_clocktime bct;
194 struct time_regs tregs;
195 uint8_t y, ytmp, sreg;
198 sc = device_get_softc(dev);
200 /* Read the bcd time registers. */
201 if ((err = iicdev_readfrom(sc->dev, RTC8583_SC_REG, &tregs, sizeof(tregs),
206 /* Get year from user SRAM */
207 rtc8583_read1(sc, RTC8583_USERSRAM_REG, &sreg);
210 * Check if year adjustment is required.
211 * RTC has only 2 bits for year value (i.e. maximum is 4 years), so
212 * full year value is stored in user SRAM and updated manually or
217 /* shift according to difference */
220 /* check if overflow happened */
224 if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
226 rtc8583_write1(sc, RTC8583_USERSRAM_REG, sreg);
227 iicbus_release_bus(sc->busdev, sc->dev);
230 if (!validbcd(tregs.msec))
233 /* The 'msec' reg is actually 1/100ths, in bcd. */
234 bct.nsec = bcd2bin(tregs.msec) * 10 * 1000 * 1000;
237 bct.hour = tregs.hour & 0x3f;
238 bct.day = tregs.day & 0x3f;
239 bct.mon = tregs.month & 0x1f;
240 bct.year = bin2bcd(sreg % 100);
242 clock_dbgprint_bcd(sc->dev, CLOCK_DBG_READ, &bct);
243 return (clock_bcd_to_ts(&bct, ts, false));
247 rtc8583_settime(device_t dev, struct timespec *ts)
249 struct rtc8583_softc *sc;
250 struct bcd_clocktime bct;
251 struct time_regs tregs;
255 sc = device_get_softc(dev);
256 ts->tv_sec -= utc_offset();
257 clock_ts_to_bcd(ts, &bct, false);
258 clock_dbgprint_bcd(sc->dev, CLOCK_DBG_WRITE, &bct);
260 /* The 'msec' reg is actually 1/100ths, in bcd. */
261 tregs.msec = bin2bcd(ts->tv_nsec / (10 * 1000 * 1000));
264 tregs.hour = bct.hour;
265 tregs.day = bct.day | (bct.year & 0x03 << 6);
266 tregs.month = bct.mon;
268 if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
270 err = rtc8583_writeto(sc->dev, RTC8583_SC_REG, &tregs,
271 sizeof(tregs), IIC_WAIT);
272 sreg = bcd2bin(bct.year & 0xff);
273 /* save to year to sram */
274 rtc8583_write1(sc, RTC8583_USERSRAM_REG, sreg);
275 iicbus_release_bus(sc->busdev, sc->dev);
280 static device_method_t rtc8583_methods[] = {
281 /* device_if methods */
282 DEVMETHOD(device_probe, rtc8583_probe),
283 DEVMETHOD(device_attach, rtc8583_attach),
284 DEVMETHOD(device_detach, rtc8583_detach),
286 /* clock_if methods */
287 DEVMETHOD(clock_gettime, rtc8583_gettime),
288 DEVMETHOD(clock_settime, rtc8583_settime),
293 static driver_t rtc8583_driver = {
296 sizeof(struct rtc8583_softc),
298 static devclass_t rtc8583_devclass;
300 DRIVER_MODULE(rtc8583, iicbus, rtc8583_driver, rtc8583_devclass, NULL, NULL);
301 MODULE_VERSION(rtc8583, 1);
302 MODULE_DEPEND(rtc8583, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);