msm6242b.c revision 1.2.2.2 1 1.2.2.2 tls /* $NetBSD: msm6242b.c,v 1.2.2.2 2012/11/20 03:02:07 tls Exp $ */
2 1.2.2.2 tls
3 1.2.2.2 tls /*-
4 1.2.2.2 tls * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.2.2.2 tls * All rights reserved.
6 1.2.2.2 tls *
7 1.2.2.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 tls * by Radoslaw Kujawa.
9 1.2.2.2 tls *
10 1.2.2.2 tls * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 tls * modification, are permitted provided that the following conditions
12 1.2.2.2 tls * are met:
13 1.2.2.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 tls * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 tls * documentation and/or other materials provided with the distribution.
18 1.2.2.2 tls *
19 1.2.2.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 tls */
31 1.2.2.2 tls
32 1.2.2.2 tls #include <sys/cdefs.h>
33 1.2.2.2 tls __KERNEL_RCSID(0, "$NetBSD: msm6242b.c,v 1.2.2.2 2012/11/20 03:02:07 tls Exp $");
34 1.2.2.2 tls
35 1.2.2.2 tls /*
36 1.2.2.2 tls * Driver for OKI MSM6242B Real Time Clock. Somewhat based on an ancient, amiga
37 1.2.2.2 tls * specifc a2kbbc driver (which was turned into frontend to this driver).
38 1.2.2.2 tls */
39 1.2.2.2 tls
40 1.2.2.2 tls #include <sys/param.h>
41 1.2.2.2 tls #include <sys/device.h>
42 1.2.2.2 tls #include <sys/bus.h>
43 1.2.2.2 tls
44 1.2.2.2 tls #include <dev/clock_subr.h>
45 1.2.2.2 tls
46 1.2.2.2 tls #include <dev/ic/msm6242bvar.h>
47 1.2.2.2 tls #include <dev/ic/msm6242breg.h>
48 1.2.2.2 tls
49 1.2.2.2 tls /* #define MSM6242B_DEBUG 1 */
50 1.2.2.2 tls
51 1.2.2.2 tls static int msm6242b_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
52 1.2.2.2 tls int msm6242b_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
53 1.2.2.2 tls
54 1.2.2.2 tls bool msm6242b_hold(struct msm6242b_softc *sc);
55 1.2.2.2 tls void msm6242b_free(struct msm6242b_softc *sc);
56 1.2.2.2 tls static uint8_t msm6242b_read(struct msm6242b_softc *, uint8_t);
57 1.2.2.2 tls static void msm6242b_write(struct msm6242b_softc *, uint8_t, uint8_t);
58 1.2.2.2 tls static void msm6242b_set(struct msm6242b_softc *, uint8_t, uint8_t);
59 1.2.2.2 tls static void msm6242b_unset(struct msm6242b_softc *, uint8_t, uint8_t);
60 1.2.2.2 tls
61 1.2.2.2 tls void
62 1.2.2.2 tls msm6242b_attach(struct msm6242b_softc *sc)
63 1.2.2.2 tls {
64 1.2.2.2 tls struct clock_ymdhms dt;
65 1.2.2.2 tls
66 1.2.2.2 tls todr_chip_handle_t handle;
67 1.2.2.2 tls aprint_normal(": OKI MSM6242B\n");
68 1.2.2.2 tls
69 1.2.2.2 tls handle = &sc->sc_handle;
70 1.2.2.2 tls handle->cookie = sc;
71 1.2.2.2 tls handle->todr_gettime = NULL;
72 1.2.2.2 tls handle->todr_settime = NULL;
73 1.2.2.2 tls handle->todr_gettime_ymdhms = msm6242b_gettime_ymdhms;
74 1.2.2.2 tls handle->todr_settime_ymdhms = msm6242b_settime_ymdhms;
75 1.2.2.2 tls handle->todr_setwen = NULL;
76 1.2.2.2 tls
77 1.2.2.2 tls if (msm6242b_gettime_ymdhms(handle, &dt) != 0) {
78 1.2.2.2 tls aprint_error_dev(sc->sc_dev, "RTC does not work correctly\n");
79 1.2.2.2 tls return;
80 1.2.2.2 tls }
81 1.2.2.2 tls
82 1.2.2.2 tls #ifdef MSM6242B_DEBUG
83 1.2.2.2 tls aprint_normal_dev(sc->sc_dev, "the time is %d %d %d %d %d %d\n",
84 1.2.2.2 tls dt.dt_year, dt.dt_mon, dt.dt_day, dt.dt_hour, dt.dt_min, dt.dt_sec);
85 1.2.2.2 tls #endif /* MSM6242B_DEBUG */
86 1.2.2.2 tls todr_attach(handle);
87 1.2.2.2 tls }
88 1.2.2.2 tls
89 1.2.2.2 tls static int
90 1.2.2.2 tls msm6242b_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
91 1.2.2.2 tls {
92 1.2.2.2 tls struct msm6242b_softc *sc;
93 1.2.2.2 tls
94 1.2.2.2 tls sc = handle->cookie;
95 1.2.2.2 tls /* XXX: splsched(); */
96 1.2.2.2 tls
97 1.2.2.2 tls if(!msm6242b_hold(sc))
98 1.2.2.2 tls return (ENXIO);
99 1.2.2.2 tls
100 1.2.2.2 tls dt->dt_sec = msm6242b_read(sc, MSM6242B_10SECOND) * 10 +
101 1.2.2.2 tls msm6242b_read(sc, MSM6242B_1SECOND);
102 1.2.2.2 tls dt->dt_min = msm6242b_read(sc, MSM6242B_10MINUTE) * 10 +
103 1.2.2.2 tls msm6242b_read(sc, MSM6242B_1MINUTE);
104 1.2.2.2 tls dt->dt_hour = (msm6242b_read(sc, MSM6242B_10HOUR_PMAM) &
105 1.2.2.2 tls MSM6242B_10HOUR_MASK) * 10 + msm6242b_read(sc, MSM6242B_1HOUR);
106 1.2.2.2 tls dt->dt_day = msm6242b_read(sc, MSM6242B_10DAY) * 10 +
107 1.2.2.2 tls msm6242b_read(sc, MSM6242B_1DAY);
108 1.2.2.2 tls dt->dt_mon = msm6242b_read(sc, MSM6242B_10MONTH) * 10 +
109 1.2.2.2 tls msm6242b_read(sc, MSM6242B_1MONTH);
110 1.2.2.2 tls dt->dt_year = msm6242b_read(sc, MSM6242B_10YEAR) * 10 +
111 1.2.2.2 tls msm6242b_read(sc, MSM6242B_1YEAR);
112 1.2.2.2 tls dt->dt_wday = msm6242b_read(sc, MSM6242B_WEEK);
113 1.2.2.2 tls
114 1.2.2.2 tls #ifdef MSM6242B_DEBUG
115 1.2.2.2 tls aprint_normal_dev(sc->sc_dev, "the time is %d %d %d %d %d %d\n",
116 1.2.2.2 tls dt->dt_year, dt->dt_mon, dt->dt_day, dt->dt_hour, dt->dt_min, dt->dt_sec);
117 1.2.2.2 tls #endif /* MSM6242B_DEBUG */
118 1.2.2.2 tls
119 1.2.2.2 tls /* handle 12h mode */
120 1.2.2.2 tls if ((msm6242b_read(sc, MSM6242B_CONTROL_F) &
121 1.2.2.2 tls MSM6242B_CONTROL_F_24H) == 0) {
122 1.2.2.2 tls if ((msm6242b_read(sc, MSM6242B_10HOUR_PMAM) &
123 1.2.2.2 tls MSM6242B_PMAM_BIT) == 0 && dt->dt_hour == 12)
124 1.2.2.2 tls dt->dt_hour = 0;
125 1.2.2.2 tls else if ((msm6242b_read(sc, MSM6242B_10HOUR_PMAM) &
126 1.2.2.2 tls MSM6242B_PMAM_BIT) && dt->dt_hour != 12);
127 1.2.2.2 tls dt->dt_hour += 12;
128 1.2.2.2 tls }
129 1.2.2.2 tls
130 1.2.2.2 tls msm6242b_free(sc);
131 1.2.2.2 tls
132 1.2.2.2 tls dt->dt_year += MSM6242B_BASE_YEAR;
133 1.2.2.2 tls if (dt->dt_year < POSIX_BASE_YEAR)
134 1.2.2.2 tls dt->dt_year += 100;
135 1.2.2.2 tls
136 1.2.2.2 tls if ((dt->dt_hour > 23) ||
137 1.2.2.2 tls (dt->dt_day > 31) ||
138 1.2.2.2 tls (dt->dt_mon > 12) ||
139 1.2.2.2 tls (dt->dt_year > 2036))
140 1.2.2.2 tls return (EINVAL);
141 1.2.2.2 tls
142 1.2.2.2 tls return 0;
143 1.2.2.2 tls }
144 1.2.2.2 tls
145 1.2.2.2 tls bool
146 1.2.2.2 tls msm6242b_hold(struct msm6242b_softc *sc)
147 1.2.2.2 tls {
148 1.2.2.2 tls int try;
149 1.2.2.2 tls
150 1.2.2.2 tls #define TRY_MAX 10
151 1.2.2.2 tls for (try = 0; try < TRY_MAX; try++) {
152 1.2.2.2 tls msm6242b_set(sc, MSM6242B_CONTROL_D, MSM6242B_CONTROL_D_HOLD);
153 1.2.2.2 tls if (msm6242b_read(sc, MSM6242B_CONTROL_D)
154 1.2.2.2 tls & MSM6242B_CONTROL_D_BUSY) {
155 1.2.2.2 tls #ifdef MSM6242B_DEBUG
156 1.2.2.2 tls aprint_normal_dev(sc->sc_dev, "gotta idle\n");
157 1.2.2.2 tls #endif /* MSM6242B_DEBUG */
158 1.2.2.2 tls msm6242b_unset(sc, MSM6242B_CONTROL_D,
159 1.2.2.2 tls MSM6242B_CONTROL_D_HOLD);
160 1.2.2.2 tls delay(70);
161 1.2.2.2 tls } else {
162 1.2.2.2 tls #ifdef MSM6242B_DEBUG
163 1.2.2.2 tls aprint_normal_dev(sc->sc_dev, "not busy\n");
164 1.2.2.2 tls #endif /* MSM6242B_DEBUG */
165 1.2.2.2 tls break;
166 1.2.2.2 tls }
167 1.2.2.2 tls }
168 1.2.2.2 tls
169 1.2.2.2 tls if (try == TRY_MAX) {
170 1.2.2.2 tls aprint_error_dev(sc->sc_dev, "can't hold the chip\n");
171 1.2.2.2 tls return false;
172 1.2.2.2 tls }
173 1.2.2.2 tls return true;
174 1.2.2.2 tls }
175 1.2.2.2 tls
176 1.2.2.2 tls void
177 1.2.2.2 tls msm6242b_free(struct msm6242b_softc *sc)
178 1.2.2.2 tls {
179 1.2.2.2 tls msm6242b_unset(sc, MSM6242B_CONTROL_D, MSM6242B_CONTROL_D_HOLD);
180 1.2.2.2 tls }
181 1.2.2.2 tls
182 1.2.2.2 tls static uint8_t
183 1.2.2.2 tls msm6242b_read(struct msm6242b_softc *sc, uint8_t reg)
184 1.2.2.2 tls {
185 1.2.2.2 tls uint8_t r;
186 1.2.2.2 tls r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, reg) & MSM6242B_MASK;
187 1.2.2.2 tls return r;
188 1.2.2.2 tls }
189 1.2.2.2 tls
190 1.2.2.2 tls static void
191 1.2.2.2 tls msm6242b_write(struct msm6242b_softc *sc, uint8_t reg, uint8_t val)
192 1.2.2.2 tls {
193 1.2.2.2 tls bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val);
194 1.2.2.2 tls }
195 1.2.2.2 tls
196 1.2.2.2 tls static void
197 1.2.2.2 tls msm6242b_set(struct msm6242b_softc *sc, uint8_t reg, uint8_t bits)
198 1.2.2.2 tls {
199 1.2.2.2 tls uint8_t v;
200 1.2.2.2 tls v = msm6242b_read(sc, reg) | bits;
201 1.2.2.2 tls msm6242b_write(sc, reg, v);
202 1.2.2.2 tls }
203 1.2.2.2 tls
204 1.2.2.2 tls static void
205 1.2.2.2 tls msm6242b_unset(struct msm6242b_softc *sc, uint8_t reg, uint8_t bits)
206 1.2.2.2 tls {
207 1.2.2.2 tls uint8_t v;
208 1.2.2.2 tls v = msm6242b_read(sc, reg) & ~bits;
209 1.2.2.2 tls msm6242b_write(sc, reg, v);
210 1.2.2.2 tls }
211 1.2.2.2 tls
212 1.2.2.2 tls int
213 1.2.2.2 tls msm6242b_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
214 1.2.2.2 tls {
215 1.2.2.2 tls struct msm6242b_softc *sc;
216 1.2.2.2 tls int ampm;
217 1.2.2.2 tls /* XXX: splsched(); */
218 1.2.2.2 tls
219 1.2.2.2 tls sc = handle->cookie;
220 1.2.2.2 tls
221 1.2.2.2 tls if(!msm6242b_hold(sc))
222 1.2.2.2 tls return (ENXIO);
223 1.2.2.2 tls
224 1.2.2.2 tls ampm = 0;
225 1.2.2.2 tls if ((msm6242b_read(sc, MSM6242B_CONTROL_F) &
226 1.2.2.2 tls MSM6242B_CONTROL_F_24H) == 0) {
227 1.2.2.2 tls if (dt->dt_hour >= 12) {
228 1.2.2.2 tls ampm = MSM6242B_CONTROL_F_24H;
229 1.2.2.2 tls if (dt->dt_hour != 12)
230 1.2.2.2 tls dt->dt_hour -= 12;
231 1.2.2.2 tls } else if (dt->dt_hour == 0) {
232 1.2.2.2 tls dt->dt_hour = 12;
233 1.2.2.2 tls }
234 1.2.2.2 tls }
235 1.2.2.2 tls
236 1.2.2.2 tls msm6242b_write(sc, MSM6242B_10HOUR_PMAM, (dt->dt_hour / 10) | ampm);
237 1.2.2.2 tls msm6242b_write(sc, MSM6242B_1HOUR, dt->dt_hour % 10);
238 1.2.2.2 tls msm6242b_write(sc, MSM6242B_10SECOND, dt->dt_sec / 10);
239 1.2.2.2 tls msm6242b_write(sc, MSM6242B_1SECOND, dt->dt_sec % 10);
240 1.2.2.2 tls msm6242b_write(sc, MSM6242B_10MINUTE, dt->dt_min / 10);
241 1.2.2.2 tls msm6242b_write(sc, MSM6242B_1MINUTE, dt->dt_min % 10);
242 1.2.2.2 tls msm6242b_write(sc, MSM6242B_10DAY, dt->dt_day / 10);
243 1.2.2.2 tls msm6242b_write(sc, MSM6242B_1DAY, dt->dt_day % 10);
244 1.2.2.2 tls msm6242b_write(sc, MSM6242B_10MONTH, dt->dt_mon / 10);
245 1.2.2.2 tls msm6242b_write(sc, MSM6242B_1MONTH, dt->dt_mon % 10);
246 1.2.2.2 tls msm6242b_write(sc, MSM6242B_10YEAR, (dt->dt_mon / 10) % 10);
247 1.2.2.2 tls msm6242b_write(sc, MSM6242B_1YEAR, dt->dt_mon % 10);
248 1.2.2.2 tls msm6242b_write(sc, MSM6242B_WEEK, dt->dt_wday);
249 1.2.2.2 tls
250 1.2.2.2 tls msm6242b_free(sc);
251 1.2.2.2 tls
252 1.2.2.2 tls return 0;
253 1.2.2.2 tls }
254 1.2.2.2 tls
255