s390.c revision 1.3 1 /* $NetBSD: s390.c,v 1.3 2014/11/20 16:34:26 christos 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.3 2014/11/20 16:34:26 christos 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_name) {
71 /* direct config - check name */
72 if (strcmp(ia->ia_name, "s390rtc") == 0)
73 return 1;
74 } else {
75 /* indirect config - check typical address */
76 if (ia->ia_addr == S390_ADDR)
77 return 1;
78 }
79 return 0;
80 }
81
82 static void
83 s390rtc_attach(device_t parent, device_t self, void *arg)
84 {
85 struct s390rtc_softc *sc = device_private(self);
86 struct i2c_attach_args *ia = arg;
87 uint8_t reg[1];
88
89 aprint_naive(": Real-time Clock\n");
90 aprint_normal(": Seiko Instruments 35390A Real-time Clock\n");
91
92 sc->sc_tag = ia->ia_tag;
93 sc->sc_addr = ia->ia_addr;
94 sc->sc_dev = self;
95
96 /* Reset the chip and turn on 24h mode, after power-off or battery. */
97 if (!s390rtc_read(sc, S390_STATUS1, reg, sizeof(reg)))
98 return;
99 if (reg[0] & (S390_ST1_POC | S390_ST1_BLD)) {
100 reg[0] |= S390_ST1_24H | S390_ST1_RESET;
101 if (!s390rtc_write(sc, S390_STATUS1, reg, sizeof(reg)))
102 return;
103 }
104
105 /* Disable the test mode, when enabled. */
106 if (!s390rtc_read(sc, S390_STATUS2, reg, sizeof(reg)))
107 return;
108 if ((reg[0] & S390_ST2_TEST)) {
109 reg[0] &= ~S390_ST2_TEST;
110 if (!s390rtc_write(sc, S390_STATUS2, reg, sizeof(reg)))
111 return;
112 }
113
114 sc->sc_todr.cookie = sc;
115 sc->sc_todr.todr_gettime = s390rtc_gettime;
116 sc->sc_todr.todr_settime = s390rtc_settime;
117 sc->sc_todr.todr_setwen = NULL;
118 todr_attach(&sc->sc_todr);
119 }
120
121 static int
122 s390rtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
123 {
124 struct s390rtc_softc *sc = ch->cookie;
125 struct clock_ymdhms dt;
126
127 memset(&dt, 0, sizeof(dt));
128
129 if (!s390rtc_clock_read(sc, &dt))
130 return -1;
131
132 tv->tv_sec = clock_ymdhms_to_secs(&dt);
133 tv->tv_usec = 0;
134
135 return 0;
136 }
137
138 static int
139 s390rtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
140 {
141 struct s390rtc_softc *sc = ch->cookie;
142 struct clock_ymdhms dt;
143
144 clock_secs_to_ymdhms(tv->tv_sec, &dt);
145
146 if (!s390rtc_clock_write(sc, &dt))
147 return -1;
148
149 return 0;
150 }
151
152 static int
153 s390rtc_clock_read(struct s390rtc_softc *sc, struct clock_ymdhms *dt)
154 {
155 uint8_t bcd[S390_RT1_NBYTES];
156
157 if (!s390rtc_read(sc, S390_REALTIME1, bcd, S390_RT1_NBYTES))
158 return 0;
159
160 /*
161 * Convert the register values into something useable.
162 */
163 dt->dt_sec = bcdtobin(bcd[S390_RT1_SECOND]);
164 dt->dt_min = bcdtobin(bcd[S390_RT1_MINUTE]);
165 dt->dt_hour = bcdtobin(bcd[S390_RT1_HOUR] & 0x3f);
166 dt->dt_day = bcdtobin(bcd[S390_RT1_DAY]);
167 dt->dt_mon = bcdtobin(bcd[S390_RT1_MONTH]);
168 dt->dt_year = bcdtobin(bcd[S390_RT1_YEAR]) + 2000;
169
170 return 1;
171 }
172
173 static int
174 s390rtc_clock_write(struct s390rtc_softc *sc, struct clock_ymdhms *dt)
175 {
176 uint8_t bcd[S390_RT1_NBYTES];
177
178 /*
179 * Convert our time representation into something the S-xx390
180 * can understand.
181 */
182 bcd[S390_RT1_SECOND] = bintobcd(dt->dt_sec);
183 bcd[S390_RT1_MINUTE] = bintobcd(dt->dt_min);
184 bcd[S390_RT1_HOUR] = bintobcd(dt->dt_hour);
185 bcd[S390_RT1_DAY] = bintobcd(dt->dt_day);
186 bcd[S390_RT1_WDAY] = bintobcd(dt->dt_wday);
187 bcd[S390_RT1_MONTH] = bintobcd(dt->dt_mon);
188 bcd[S390_RT1_YEAR] = bintobcd(dt->dt_year % 100);
189
190 return s390rtc_write(sc, S390_REALTIME1, bcd, S390_RT1_NBYTES);
191 }
192
193 static int
194 s390rtc_read(struct s390rtc_softc *sc, int reg, uint8_t *buf, size_t len)
195 {
196 int i;
197
198 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
199 aprint_error_dev(sc->sc_dev,
200 "%s: failed to acquire I2C bus\n", __func__);
201 return 0;
202 }
203
204 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr + reg,
205 NULL, 0, buf, len, I2C_F_POLL)) {
206 iic_release_bus(sc->sc_tag, I2C_F_POLL);
207 aprint_error_dev(sc->sc_dev,
208 "%s: failed to read reg%d\n", __func__, reg);
209 return 0;
210 }
211
212 iic_release_bus(sc->sc_tag, I2C_F_POLL);
213
214 /* this chip returns each byte in reverse order */
215 for (i = 0; i < len; i++)
216 buf[i] = bitreverse(buf[i]);
217
218 return 1;
219 }
220
221 static int
222 s390rtc_write(struct s390rtc_softc *sc, int reg, uint8_t *buf, size_t len)
223 {
224 int i;
225
226 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
227 aprint_error_dev(sc->sc_dev,
228 "%s: failed to acquire I2C bus\n", __func__);
229 return 0;
230 }
231
232 /* this chip expects each byte in reverse order */
233 for (i = 0; i < len; i++)
234 buf[i] = bitreverse(buf[i]);
235
236 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr + reg,
237 NULL, 0, buf, len, I2C_F_POLL)) {
238 iic_release_bus(sc->sc_tag, I2C_F_POLL);
239 aprint_error_dev(sc->sc_dev,
240 "%s: failed to write reg%d\n", __func__, reg);
241 return 0;
242 }
243
244 iic_release_bus(sc->sc_tag, I2C_F_POLL);
245 return 1;
246 }
247
248 static uint8_t
249 bitreverse(uint8_t x)
250 {
251 static unsigned char nibbletab[16] = {
252 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
253 };
254
255 return (nibbletab[x & 15] << 4) | nibbletab[x >> 4];
256 }
257