summaryrefslogtreecommitdiffstats
path: root/sys/dev/iicbus/s35390a.c
blob: 59369b3f23802d0b4587a48db57e7b0993e8d0b2 (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*-
 * Copyright (c) 2012 Yusuke Tanaka
 * All rights reserved.
 *
 * 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.
 */

/*-
 * Copyright (c) 2011 Frank Wille.
 * All rights reserved.
 *
 * Written by Frank Wille for The NetBSD Project.
 *
 * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * Driver for Seiko Instruments S-35390A Real-time Clock
 */

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

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

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

#define S390_DEVNAME		"s35390a_rtc"
#define S390_DEVCODE		0x6	/* 0110 */
/*
 * S-35390A uses 4-bit device code + 3-bit command in the slave address
 * field.  The possible combination is 0x60-0x6f including the R/W bit.
 * 0x60 means an write access to status register 1.
 */
#define S390_ADDR		(S390_DEVCODE << 4)

/* Registers are encoded into the slave address */
#define S390_STATUS1		(0 << 1)
#define S390_STATUS2		(1 << 1)
#define S390_REALTIME1		(2 << 1)
#define S390_REALTIME2		(3 << 1)
#define S390_INT1_1		(4 << 1)
#define S390_INT1_2		(5 << 1)
#define S390_CLOCKADJ		(6 << 1)
#define S390_FREE		(7 << 1)

/* Status1 bits */
#define S390_ST1_POC		(1 << 7)
#define S390_ST1_BLD		(1 << 6)
#define S390_ST1_24H		(1 << 1)
#define S390_ST1_RESET		(1 << 0)

/* Status2 bits */
#define S390_ST2_TEST		(1 << 7)

/* Realtime1 data bytes */
#define S390_RT1_NBYTES		7
#define S390_RT1_YEAR		0
#define S390_RT1_MONTH		1
#define S390_RT1_DAY		2
#define S390_RT1_WDAY		3
#define S390_RT1_HOUR		4
#define S390_RT1_MINUTE		5
#define S390_RT1_SECOND		6

struct s390rtc_softc {
	device_t	sc_dev;
	uint16_t	sc_addr;
};

/*
 * S-35390A interprets bits in each byte on SDA in reverse order.
 * bitreverse() reverses the bits in uint8_t.
 */
static const uint8_t nibbletab[] = {
	/* 0x0 0000 -> 0000 */	0x0,
	/* 0x1 0001 -> 1000 */	0x8,
	/* 0x2 0010 -> 0100 */	0x4,
	/* 0x3 0011 -> 1100 */	0xc,
	/* 0x4 0100 -> 0010 */	0x2,
	/* 0x5 0101 -> 1010 */	0xa,
	/* 0x6 0110 -> 0110 */	0x6,
	/* 0x7 0111 -> 1110 */	0xe,
	/* 0x8 1000 -> 0001 */	0x1,
	/* 0x9 1001 -> 1001 */	0x9,
	/* 0xa 1010 -> 0101 */	0x5,
	/* 0xb 1011 -> 1101 */	0xd,
	/* 0xc 1100 -> 0011 */	0x3,
	/* 0xd 1101 -> 1011 */	0xb,
	/* 0xe 1110 -> 0111 */	0x7,
	/* 0xf 1111 -> 1111 */	0xf, };

static uint8_t
bitreverse(uint8_t x)
{

	return (nibbletab[x & 0xf] << 4) | nibbletab[x >> 4];
}

static int
s390rtc_read(device_t dev, uint8_t reg, uint8_t *buf, size_t len)
{
	struct s390rtc_softc *sc = device_get_softc(dev);
	struct iic_msg msg[] = {
		{
			.slave = sc->sc_addr | reg,
			.flags = IIC_M_RD,
			.len = len,
			.buf = buf,
		},
	};
	int i;
	int error;

	error = iicbus_transfer(dev, msg, 1);
	if (error)
		return (error);

	/* this chip returns each byte in reverse order */
	for (i = 0; i < len; ++i)
		buf[i] = bitreverse(buf[i]);

	return (0);
}

static int
s390rtc_write(device_t dev, uint8_t reg, uint8_t *buf, size_t len)
{
	struct s390rtc_softc *sc = device_get_softc(dev);
	struct iic_msg msg[] = {
		{
			.slave = sc->sc_addr | reg,
			.flags = IIC_M_WR,
			.len = len,
			.buf = buf,
		},
	};
	int i;

	/* this chip expects each byte in reverse order */
	for (i = 0; i < len; ++i)
		buf[i] = bitreverse(buf[i]);

	return (iicbus_transfer(dev, msg, 1));
}

static int
s390rtc_probe(device_t dev)
{

	if (iicbus_get_addr(dev) != S390_ADDR) {
		if (bootverbose)
			device_printf(dev, "slave address mismatch. "
			    "(%02x != %02x)\n", iicbus_get_addr(dev),
			    S390_ADDR);
		return (ENXIO);
	}
	device_set_desc(dev, "Seiko Instruments S-35390A Real-time Clock");

	return (BUS_PROBE_SPECIFIC);
}

static int
s390rtc_attach(device_t dev)
{
	struct s390rtc_softc *sc;
	uint8_t reg;
	int error;

	sc = device_get_softc(dev);
	sc->sc_dev = dev;
	sc->sc_addr = iicbus_get_addr(dev);

	/* Reset the chip and turn on 24h mode, after power-off or battery. */
	error = s390rtc_read(dev, S390_STATUS1, &reg, 1);
	if (error) {
		device_printf(dev, "%s: cannot read status1 register\n",
		     __func__);
		return (error);
	}
	if (reg & (S390_ST1_POC | S390_ST1_BLD)) {
		reg |= S390_ST1_24H | S390_ST1_RESET;
		error = s390rtc_write(dev, S390_STATUS1, &reg, 1);
		if (error) {
			device_printf(dev, "%s: cannot initialize\n", __func__);
			return (error);
		}
	}

	/* Disable the test mode, when enabled. */
	error = s390rtc_read(dev, S390_STATUS2, &reg, 1);
	if (error) {
		device_printf(dev, "%s: cannot read status2 register\n",
		    __func__);
		return (error);
	}
	if (reg & S390_ST2_TEST) {
		reg &= ~S390_ST2_TEST;
		error = s390rtc_write(dev, S390_STATUS2, &reg, 1);
		if (error) {
			device_printf(dev,
			    "%s: cannot disable the test mode\n", __func__);
			return (error);
		}
	}

	clock_register(dev, 1000000);   /* 1 second resolution */
	return (0);
}

static int
s390rtc_gettime(device_t dev, struct timespec *ts)
{
	uint8_t bcd[S390_RT1_NBYTES];
	struct clocktime ct;
	int error;

	error = s390rtc_read(dev, S390_REALTIME1, bcd, S390_RT1_NBYTES);
	if (error) {
		device_printf(dev, "%s: cannot read realtime1 register\n",
		    __func__);
		return (error);
	}

	/*
	 * Convert the register values into something useable.
	 */
	ct.nsec = 0;
	ct.sec = FROMBCD(bcd[S390_RT1_SECOND]);
	ct.min = FROMBCD(bcd[S390_RT1_MINUTE]);
	ct.hour = FROMBCD(bcd[S390_RT1_HOUR] & 0x3f);
	ct.day = FROMBCD(bcd[S390_RT1_DAY]);
	ct.dow = bcd[S390_RT1_WDAY] & 0x07;
	ct.mon = FROMBCD(bcd[S390_RT1_MONTH]);
	ct.year = FROMBCD(bcd[S390_RT1_YEAR]) + 2000;

	return (clock_ct_to_ts(&ct, ts));
}

static int
s390rtc_settime(device_t dev, struct timespec *ts)
{
	uint8_t bcd[S390_RT1_NBYTES];
	struct clocktime ct;

	clock_ts_to_ct(ts, &ct);

	/*
	 * Convert our time representation into something the S-xx390
	 * can understand.
	 */
	bcd[S390_RT1_SECOND] = TOBCD(ct.sec);
	bcd[S390_RT1_MINUTE] = TOBCD(ct.min);
	bcd[S390_RT1_HOUR] = TOBCD(ct.hour);
	bcd[S390_RT1_DAY] = TOBCD(ct.day);
	bcd[S390_RT1_WDAY] = ct.dow;
	bcd[S390_RT1_MONTH] = TOBCD(ct.mon);
	bcd[S390_RT1_YEAR] = TOBCD(ct.year % 100);

	return (s390rtc_write(dev, S390_REALTIME1, bcd, S390_RT1_NBYTES));
}

static device_method_t s390rtc_methods[] = {
	DEVMETHOD(device_probe,		s390rtc_probe),
	DEVMETHOD(device_attach,	s390rtc_attach),

	DEVMETHOD(clock_gettime,	s390rtc_gettime),
	DEVMETHOD(clock_settime,	s390rtc_settime),

	DEVMETHOD_END
};

static driver_t s390rtc_driver = {
	S390_DEVNAME,
	s390rtc_methods,
	sizeof(struct s390rtc_softc),
};
static devclass_t s390rtc_devclass;

DRIVER_MODULE(s35390a, iicbus, s390rtc_driver, s390rtc_devclass, NULL, NULL);
MODULE_VERSION(s35390a, 1);
MODULE_DEPEND(s35390a, iicbus, 1, 1, 1);
OpenPOWER on IntegriCloud