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