xref: /netbsd-src/sys/dev/i2c/s390.c (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /*	$NetBSD: s390.c,v 1.1 2011/04/04 17:58:40 phx Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 Frank Wille.
5  * All rights reserved.
6  *
7  * Written by Frank Wille for The NetBSD Project.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: s390.c,v 1.1 2011/04/04 17:58:40 phx Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/conf.h>
38 
39 #include <dev/clock_subr.h>
40 
41 #include <dev/i2c/i2cvar.h>
42 #include <dev/i2c/s390reg.h>
43 
44 struct s390rtc_softc {
45 	device_t sc_dev;
46 	i2c_tag_t sc_tag;
47 	i2c_addr_t sc_addr;
48 	struct todr_chip_handle sc_todr;
49 };
50 
51 static int s390rtc_match(device_t, cfdata_t, void *);
52 static void s390rtc_attach(device_t, device_t, void *);
53 
54 CFATTACH_DECL_NEW(s390rtc, sizeof(struct s390rtc_softc),
55     s390rtc_match, s390rtc_attach, NULL, NULL);
56 
57 static int s390rtc_gettime(struct todr_chip_handle *, struct timeval *);
58 static int s390rtc_settime(struct todr_chip_handle *, struct timeval *);
59 static int s390rtc_clock_read(struct s390rtc_softc *, struct clock_ymdhms *);
60 static int s390rtc_clock_write(struct s390rtc_softc *, struct clock_ymdhms *);
61 static int s390rtc_read(struct s390rtc_softc *, int, uint8_t *, size_t);
62 static int s390rtc_write(struct s390rtc_softc *, int, uint8_t *, size_t);
63 static uint8_t bitreverse(uint8_t);
64 
65 static int
66 s390rtc_match(device_t parent, cfdata_t cf, void *arg)
67 {
68 	struct i2c_attach_args *ia = arg;
69 
70 	if (ia->ia_addr == S390_ADDR)
71 		return 1;
72 	return 0;
73 }
74 
75 static void
76 s390rtc_attach(device_t parent, device_t self, void *arg)
77 {
78 	struct s390rtc_softc *sc = device_private(self);
79 	struct i2c_attach_args *ia = arg;
80 	uint8_t reg[1];
81 
82 	aprint_naive(": Real-time Clock\n");
83 	aprint_normal(": Seiko Instruments 35390A Real-time Clock\n");
84 
85 	sc->sc_tag = ia->ia_tag;
86 	sc->sc_addr = ia->ia_addr;
87 	sc->sc_dev = self;
88 
89 	/* Reset the chip and turn on 24h mode, after power-off or battery. */
90 	if (!s390rtc_read(sc, S390_STATUS1, reg, sizeof(reg)))
91 		return;
92 	if (reg[0] & (S390_ST1_POC | S390_ST1_BLD)) {
93 		reg[0] |= S390_ST1_24H | S390_ST1_RESET;
94 		if (!s390rtc_write(sc, S390_STATUS1, reg, sizeof(reg)))
95 			return;
96 	}
97 
98 	/* Disable the test mode, when enabled. */
99 	if (!s390rtc_read(sc, S390_STATUS2, reg, sizeof(reg)))
100 		return;
101 	if ((reg[0] & S390_ST2_TEST)) {
102 		reg[0] &= ~S390_ST2_TEST;
103 		if (!s390rtc_write(sc, S390_STATUS2, reg, sizeof(reg)))
104 			return;
105 	}
106 
107 	sc->sc_todr.cookie = sc;
108 	sc->sc_todr.todr_gettime = s390rtc_gettime;
109 	sc->sc_todr.todr_settime = s390rtc_settime;
110 	sc->sc_todr.todr_setwen = NULL;
111 	todr_attach(&sc->sc_todr);
112 }
113 
114 static int
115 s390rtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
116 {
117 	struct s390rtc_softc *sc = ch->cookie;
118 	struct clock_ymdhms dt;
119 
120 	memset(&dt, 0, sizeof(dt));
121 
122 	if (!s390rtc_clock_read(sc, &dt))
123 		return -1;
124 
125 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
126 	tv->tv_usec = 0;
127 
128 	return 0;
129 }
130 
131 static int
132 s390rtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
133 {
134 	struct s390rtc_softc *sc = ch->cookie;
135 	struct clock_ymdhms dt;
136 
137 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
138 
139 	if (!s390rtc_clock_write(sc, &dt))
140 		return -1;
141 
142 	return 0;
143 }
144 
145 static int
146 s390rtc_clock_read(struct s390rtc_softc *sc, struct clock_ymdhms *dt)
147 {
148 	uint8_t bcd[S390_RT1_NBYTES];
149 
150 	if (!s390rtc_read(sc, S390_REALTIME1, bcd, S390_RT1_NBYTES))
151 		return 0;
152 
153 	/*
154 	 * Convert the register values into something useable.
155 	 */
156 	dt->dt_sec = FROMBCD(bcd[S390_RT1_SECOND]);
157 	dt->dt_min = FROMBCD(bcd[S390_RT1_MINUTE]);
158 	dt->dt_hour = FROMBCD(bcd[S390_RT1_HOUR] & 0x3f);
159 	dt->dt_day = FROMBCD(bcd[S390_RT1_DAY]);
160 	dt->dt_mon = FROMBCD(bcd[S390_RT1_MONTH]);
161 	dt->dt_year = FROMBCD(bcd[S390_RT1_YEAR]) + 2000;
162 
163 	return 1;
164 }
165 
166 static int
167 s390rtc_clock_write(struct s390rtc_softc *sc, struct clock_ymdhms *dt)
168 {
169 	uint8_t bcd[S390_RT1_NBYTES];
170 
171 	/*
172 	 * Convert our time representation into something the S-xx390
173 	 * can understand.
174 	 */
175 	bcd[S390_RT1_SECOND] = TOBCD(dt->dt_sec);
176 	bcd[S390_RT1_MINUTE] = TOBCD(dt->dt_min);
177 	bcd[S390_RT1_HOUR] = TOBCD(dt->dt_hour);
178 	bcd[S390_RT1_DAY] = TOBCD(dt->dt_day);
179 	bcd[S390_RT1_WDAY] = TOBCD(dt->dt_wday);
180 	bcd[S390_RT1_MONTH] = TOBCD(dt->dt_mon);
181 	bcd[S390_RT1_YEAR] = TOBCD(dt->dt_year % 100);
182 
183 	return s390rtc_write(sc, S390_REALTIME1, bcd, S390_RT1_NBYTES);
184 }
185 
186 static int
187 s390rtc_read(struct s390rtc_softc *sc, int reg, uint8_t *buf, size_t len)
188 {
189 	int i;
190 
191 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
192 		aprint_error_dev(sc->sc_dev,
193 		    "%s: failed to acquire I2C bus\n", __func__);
194 		return 0;
195 	}
196 
197 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr + reg,
198 	    NULL, 0, buf, len, I2C_F_POLL)) {
199 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
200 		aprint_error_dev(sc->sc_dev,
201 		    "%s: failed to read reg%d\n", __func__, reg);
202 		return 0;
203 	}
204 
205 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
206 
207 	/* this chip returns each byte in reverse order */
208 	for (i = 0; i < len; i++)
209 		buf[i] = bitreverse(buf[i]);
210 
211 	return 1;
212 }
213 
214 static int
215 s390rtc_write(struct s390rtc_softc *sc, int reg, uint8_t *buf, size_t len)
216 {
217 	int i;
218 
219 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
220 		aprint_error_dev(sc->sc_dev,
221 		    "%s: failed to acquire I2C bus\n", __func__);
222 		return 0;
223 	}
224 
225 	/* this chip expects each byte in reverse order */
226 	for (i = 0; i < len; i++)
227 		buf[i] = bitreverse(buf[i]);
228 
229 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr + reg,
230 	    NULL, 0, buf, len, I2C_F_POLL)) {
231 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
232 		aprint_error_dev(sc->sc_dev,
233 		    "%s: failed to write reg%d\n", __func__, reg);
234 		return 0;
235 	}
236 
237 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
238 	return 1;
239 }
240 
241 static uint8_t
242 bitreverse(uint8_t x)
243 {
244 	static unsigned char nibbletab[16] = {
245 		0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
246 	};
247 
248 	return (nibbletab[x & 15] << 4) | nibbletab[x >> 4];
249 }
250