aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/iicbus/rtc/rtc8583.c
blob: 84e639f233c707391750e3351a3adb8857e75102 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2017 Hiroki Mori.  All rights reserved.
 * Copyright (c) 2017 Ian Lepore.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This code base on isl12xx.c
 */

#include <sys/cdefs.h>
/*
 * Driver for realtime clock EPSON RTC-8583
 */

#include "opt_platform.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/clock.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/module.h>

#ifdef FDT
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif

#include <dev/iicbus/iiconf.h>
#include <dev/iicbus/iicbus.h>

#include "clock_if.h"
#include "iicbus_if.h"

#define	RTC8583_SC_REG		0x01		/* RTC Seconds */
#define RTC8583_USERSRAM_REG	0x10		/* User SRAM register (first) */
#define	MAX_TRANSFER		16

/*
 * A struct laid out in the same order as the time registers in the chip.
 */
struct time_regs {
	uint8_t	msec, sec, min, hour, day, month;
};

struct rtc8583_softc {
	device_t	dev;
	device_t	busdev;
	struct intr_config_hook 
			init_hook;
};

#ifdef FDT
static struct ofw_compat_data compat_data[] = {
	{"epson,rtc8583", 1},
	{NULL,           0},
};
#endif

static void	rtc8583_init(void *arg);
static int	rtc8583_probe(device_t dev);
static int	rtc8583_attach(device_t dev);
static int	rtc8583_detach(device_t dev);

static int	rtc8583_gettime(device_t dev, struct timespec *ts);
static int	rtc8583_settime(device_t dev, struct timespec *ts);

static int	rtc8583_writeto(device_t slavedev, uint8_t regaddr, 
		    void *buffer, uint16_t buflen, int waithow);

/* Implementation */

static int 
rtc8583_writeto(device_t slavedev, uint8_t regaddr, void *buffer,
    uint16_t buflen, int waithow)
{
	struct iic_msg	msgs;
	uint8_t		slaveaddr;
	uint8_t		newbuf[MAX_TRANSFER];

	slaveaddr = iicbus_get_addr(slavedev);

	newbuf[0] = regaddr;
	memcpy(newbuf + 1, buffer, buflen);
	msgs.slave = slaveaddr;
	msgs.flags = IIC_M_WR;
	msgs.len   = 1 + buflen;
	msgs.buf   = newbuf;

	return (iicbus_transfer_excl(slavedev, &msgs, 1, waithow));
}

static inline int
rtc8583_read1(struct rtc8583_softc *sc, uint8_t reg, uint8_t *data) 
{

	return (iicdev_readfrom(sc->dev, reg, data, 1, IIC_WAIT));
}

static inline int
rtc8583_write1(struct rtc8583_softc *sc, uint8_t reg, uint8_t val) 
{

	return (rtc8583_writeto(sc->dev, reg, &val, 1, IIC_WAIT));
}

static void
rtc8583_init(void *arg)
{
	struct rtc8583_softc	*sc;
	
	sc = (struct rtc8583_softc*)arg;
	config_intrhook_disestablish(&sc->init_hook);

	/*
	 * Register as a system realtime clock.
	 */
	clock_register_flags(sc->dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
	clock_schedule(sc->dev, 1);
	return;
}

static int
rtc8583_probe(device_t dev)
{

#ifdef FDT
	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
		device_set_desc(dev, "EPSON RTC-8583");
		return (BUS_PROBE_DEFAULT);
	}
#endif
	return (ENXIO);
}

static int
rtc8583_attach(device_t dev)
{
	struct rtc8583_softc	*sc;
	
	sc = device_get_softc(dev);
	sc->dev = dev;
	sc->busdev = device_get_parent(sc->dev);

	/*
	 * Chip init must wait until interrupts are enabled.  Often i2c access
	 * works only when the interrupts are available.
	 */
	sc->init_hook.ich_func = rtc8583_init;
	sc->init_hook.ich_arg = sc;
	if (config_intrhook_establish(&sc->init_hook) != 0)
		return (ENOMEM);

	return (0);
}

static int
rtc8583_detach(device_t dev)
{

	clock_unregister(dev);
	return (0);
}

static int
rtc8583_gettime(device_t dev, struct timespec *ts)
{
	struct rtc8583_softc	*sc;
	struct bcd_clocktime	 bct;
	struct time_regs	 tregs;
	uint8_t			 y, ytmp, sreg;
	int 			 err;

	sc = device_get_softc(dev);

	/* Read the bcd time registers. */
	if ((err = iicdev_readfrom(sc->dev, RTC8583_SC_REG, &tregs, sizeof(tregs),
	    IIC_WAIT)) != 0)
		return (err);

	y = tregs.day >> 6;
	/* Get year from user SRAM */
	rtc8583_read1(sc, RTC8583_USERSRAM_REG,  &sreg);
	
	/* 
	 * Check if year adjustment is required.
	 * RTC has only 2 bits for year value (i.e. maximum is 4 years), so
	 * full year value is stored in user SRAM and updated manually or 
	 * by this code.
	 */
	ytmp = sreg & 0x03;
	if (ytmp != y) {
		/* shift according to difference */
		sreg += y - ytmp;
		
		/* check if overflow happened */
		if (ytmp > y)
			sreg += 4;
		
		if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
			return (err);
		rtc8583_write1(sc, RTC8583_USERSRAM_REG, sreg);
		iicbus_release_bus(sc->busdev, sc->dev);
	}

	if (!validbcd(tregs.msec))
		return (EINVAL);

        /* The 'msec' reg is actually 1/100ths, in bcd.  */
	bct.nsec = bcd2bin(tregs.msec) * 10 * 1000 * 1000;
	bct.sec  = tregs.sec;
	bct.min  = tregs.min;
	bct.hour = tregs.hour & 0x3f;
	bct.day  = tregs.day & 0x3f;
	bct.mon  = tregs.month & 0x1f;
	bct.year = bin2bcd(sreg % 100);

	clock_dbgprint_bcd(sc->dev, CLOCK_DBG_READ, &bct); 
	return (clock_bcd_to_ts(&bct, ts, false));
}

static int
rtc8583_settime(device_t dev, struct timespec *ts)
{
	struct rtc8583_softc	*sc;
	struct bcd_clocktime 	 bct;
	struct time_regs	 tregs;
	uint8_t			 sreg;
	int 			 err;

	sc = device_get_softc(dev);
	ts->tv_sec -= utc_offset();
	clock_ts_to_bcd(ts, &bct, false);
	clock_dbgprint_bcd(sc->dev, CLOCK_DBG_WRITE, &bct);

	/* The 'msec' reg is actually 1/100ths, in bcd.  */
	tregs.msec  = bin2bcd(ts->tv_nsec / (10 * 1000 * 1000));
	tregs.sec   = bct.sec;
	tregs.min   = bct.min;
	tregs.hour  = bct.hour;
	tregs.day   = bct.day | (bct.year & 0x03 << 6);
	tregs.month = bct.mon;

	if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
		return (err);
	err = rtc8583_writeto(sc->dev, RTC8583_SC_REG, &tregs,
	    sizeof(tregs), IIC_WAIT);
	sreg = bcd2bin(bct.year & 0xff);
	/* save to year to sram */
	rtc8583_write1(sc, RTC8583_USERSRAM_REG, sreg);
	iicbus_release_bus(sc->busdev, sc->dev);

	return (err);
}

static device_method_t rtc8583_methods[] = {
        /* device_if methods */
	DEVMETHOD(device_probe,		rtc8583_probe),
	DEVMETHOD(device_attach,	rtc8583_attach),
	DEVMETHOD(device_detach,	rtc8583_detach),

        /* clock_if methods */
	DEVMETHOD(clock_gettime,	rtc8583_gettime),
	DEVMETHOD(clock_settime,	rtc8583_settime),

	DEVMETHOD_END,
};

static driver_t rtc8583_driver = {
	"rtc8583",
	rtc8583_methods,
	sizeof(struct rtc8583_softc),
};

DRIVER_MODULE(rtc8583, iicbus, rtc8583_driver, NULL, NULL);
MODULE_VERSION(rtc8583, 1);
MODULE_DEPEND(rtc8583, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
IICBUS_FDT_PNP_INFO(compat_data);