FreeBSD kernel IICBUS device code
rtc8583.c
Go to the documentation of this file.
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 */
65struct time_regs {
66 uint8_t msec, sec, min, hour, day, month;
67};
68
70 device_t dev;
71 device_t busdev;
72 struct intr_config_hook
74};
75
76#ifdef FDT
77static struct ofw_compat_data compat_data[] = {
78 {"epson,rtc8583", 1},
79 {NULL, 0},
80};
81#endif
82
83static void rtc8583_init(void *arg);
84static int rtc8583_probe(device_t dev);
85static int rtc8583_attach(device_t dev);
86static int rtc8583_detach(device_t dev);
87
88static int rtc8583_gettime(device_t dev, struct timespec *ts);
89static int rtc8583_settime(device_t dev, struct timespec *ts);
90
91static int rtc8583_writeto(device_t slavedev, uint8_t regaddr,
92 void *buffer, uint16_t buflen, int waithow);
93
94/* Implementation */
95
96static int
97rtc8583_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;
110 msgs.len = 1 + buflen;
111 msgs.buf = newbuf;
112
113 return (iicbus_transfer_excl(slavedev, &msgs, 1, waithow));
114}
115
116static inline int
117rtc8583_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
123static inline int
124rtc8583_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
130static void
131rtc8583_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
146static int
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
162static int
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
183static int
185{
186
187 clock_unregister(dev);
188 return (0);
189}
190
191static int
192rtc8583_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 */
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);
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
248static int
249rtc8583_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 */
277 iicbus_release_bus(sc->busdev, sc->dev);
278
279 return (err);
280}
281
282static 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
295static driver_t rtc8583_driver = {
296 "rtc8583",
298 sizeof(struct rtc8583_softc),
299};
300static devclass_t rtc8583_devclass;
301
303MODULE_VERSION(rtc8583, 1);
#define IIC_M_WR
Definition: ad7418.c:45
static ds13_compat_data compat_data[]
Definition: ds13rtc.c:187
caddr_t data
Definition: iicbb_if.m:61
int val
Definition: iicbb_if.m:83
struct iic_msg * msgs
Definition: iicbus_if.m:134
INTERFACE iicbus
Definition: iicbus_if.m:32
int iicbus_request_bus(device_t bus, device_t dev, int how)
Definition: iiconf.c:138
int iicbus_release_bus(device_t bus, device_t dev)
Definition: iiconf.c:206
int iicdev_readfrom(device_t slavedev, uint8_t regaddr, void *buffer, uint16_t buflen, int waithow)
Definition: iiconf.c:524
int iicbus_transfer_excl(device_t dev, struct iic_msg *msgs, uint32_t nmsgs, int how)
Definition: iiconf.c:449
#define IICBUS_MINVER
Definition: iiconf.h:171
#define IIC_WAIT
Definition: iiconf.h:46
#define IICBUS_MAXVER
Definition: iiconf.h:172
#define IICBUS_PREFVER
Definition: iiconf.h:173
device_t dev
Definition: ofw_iicbus_if.m:38
MODULE_DEPEND(rtc8583, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER)
static devclass_t rtc8583_devclass
Definition: rtc8583.c:300
static int rtc8583_attach(device_t dev)
Definition: rtc8583.c:163
static device_method_t rtc8583_methods[]
Definition: rtc8583.c:282
static int rtc8583_write1(struct rtc8583_softc *sc, uint8_t reg, uint8_t val)
Definition: rtc8583.c:124
static int rtc8583_writeto(device_t slavedev, uint8_t regaddr, void *buffer, uint16_t buflen, int waithow)
Definition: rtc8583.c:97
#define MAX_TRANSFER
Definition: rtc8583.c:60
static void rtc8583_init(void *arg)
Definition: rtc8583.c:131
DRIVER_MODULE(rtc8583, iicbus, rtc8583_driver, rtc8583_devclass, NULL, NULL)
__FBSDID("$FreeBSD$")
#define RTC8583_USERSRAM_REG
Definition: rtc8583.c:59
static driver_t rtc8583_driver
Definition: rtc8583.c:295
#define RTC8583_SC_REG
Definition: rtc8583.c:58
IICBUS_FDT_PNP_INFO(compat_data)
MODULE_VERSION(rtc8583, 1)
static int rtc8583_read1(struct rtc8583_softc *sc, uint8_t reg, uint8_t *data)
Definition: rtc8583.c:117
static int rtc8583_detach(device_t dev)
Definition: rtc8583.c:184
static int rtc8583_settime(device_t dev, struct timespec *ts)
Definition: rtc8583.c:249
static int rtc8583_gettime(device_t dev, struct timespec *ts)
Definition: rtc8583.c:192
static int rtc8583_probe(device_t dev)
Definition: rtc8583.c:147
Definition: iic.h:38
uint16_t slave
Definition: iic.h:39
uint16_t len
Definition: iic.h:45
uint8_t * buf
Definition: iic.h:46
uint16_t flags
Definition: iic.h:40
device_t dev
Definition: rtc8583.c:70
struct intr_config_hook init_hook
Definition: rtc8583.c:72
device_t busdev
Definition: rtc8583.c:71
uint8_t sec
Definition: ds13rtc.c:160
uint8_t month
Definition: ds13rtc.c:160
uint8_t min
Definition: ds13rtc.c:160
uint8_t msec
Definition: rtc8583.c:66
uint8_t hour
Definition: ds13rtc.c:160
uint8_t day
Definition: ds13rtc.c:160