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;
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 ct.nsec = FROMBCD(tregs.msec) * 10 * 1000 * 1000;
231 ct.sec = FROMBCD(tregs.sec);
232 ct.min = FROMBCD(tregs.min);
233 ct.hour = FROMBCD(tregs.hour & 0x3f);
234 ct.day = FROMBCD(tregs.day & 0x3f);
235 ct.mon = FROMBCD(tregs.month & 0x1f);
236 ct.year = 2000 + sreg;
238 return (clock_ct_to_ts(&ct, ts));
242 rtc8583_settime(device_t dev, struct timespec *ts)
244 struct rtc8583_softc *sc;
246 struct time_regs tregs;
250 sc = device_get_softc(dev);
251 ts->tv_sec -= utc_offset();
253 clock_ts_to_ct(ts, &ct);
255 tregs.sec = TOBCD(ct.sec);
256 tregs.min = TOBCD(ct.min);
257 tregs.hour = TOBCD(ct.hour);
258 tregs.day = TOBCD(ct.day) | ((ct.year & 0x03) << 6);
259 tregs.month = TOBCD(ct.mon);
261 if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
263 err = rtc8583_writeto(sc->dev, RTC8583_SC_REG, &tregs,
264 sizeof(tregs), IIC_WAIT);
265 sreg = ct.year - 2000;
266 /* save to year to sram */
267 rtc8583_write1(sc, RTC8583_USERSRAM_REG, sreg);
268 iicbus_release_bus(sc->busdev, sc->dev);
273 static device_method_t rtc8583_methods[] = {
274 /* device_if methods */
275 DEVMETHOD(device_probe, rtc8583_probe),
276 DEVMETHOD(device_attach, rtc8583_attach),
277 DEVMETHOD(device_detach, rtc8583_detach),
279 /* clock_if methods */
280 DEVMETHOD(clock_gettime, rtc8583_gettime),
281 DEVMETHOD(clock_settime, rtc8583_settime),
286 static driver_t rtc8583_driver = {
289 sizeof(struct rtc8583_softc),
291 static devclass_t rtc8583_devclass;
293 DRIVER_MODULE(rtc8583, iicbus, rtc8583_driver, rtc8583_devclass, NULL, NULL);
294 MODULE_VERSION(rtc8583, 1);
295 MODULE_DEPEND(rtc8583, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);