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
|
/* $NetBSD: msm6242b.c,v 1.4 2018/02/06 13:26:32 rin Exp $ */
/*-
* Copyright (c) 2012 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Radoslaw Kujawa.
*
* 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>
__KERNEL_RCSID(0, "$NetBSD: msm6242b.c,v 1.4 2018/02/06 13:26:32 rin Exp $");
/*
* Driver for OKI MSM6242B Real Time Clock. Somewhat based on an ancient, amiga
* specifc a2kbbc driver (which was turned into frontend to this driver).
*/
#include <sys/param.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <dev/clock_subr.h>
#include <dev/ic/msm6242bvar.h>
#include <dev/ic/msm6242breg.h>
/* #define MSM6242B_DEBUG 1 */
static int msm6242b_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
int msm6242b_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
bool msm6242b_hold(struct msm6242b_softc *sc);
void msm6242b_free(struct msm6242b_softc *sc);
static uint8_t msm6242b_read(struct msm6242b_softc *, uint8_t);
static void msm6242b_write(struct msm6242b_softc *, uint8_t, uint8_t);
static void msm6242b_set(struct msm6242b_softc *, uint8_t, uint8_t);
static void msm6242b_unset(struct msm6242b_softc *, uint8_t, uint8_t);
void
msm6242b_attach(struct msm6242b_softc *sc)
{
struct clock_ymdhms dt;
todr_chip_handle_t handle;
aprint_normal(": OKI MSM6242B\n");
handle = &sc->sc_handle;
handle->cookie = sc;
handle->todr_gettime = NULL;
handle->todr_settime = NULL;
handle->todr_gettime_ymdhms = msm6242b_gettime_ymdhms;
handle->todr_settime_ymdhms = msm6242b_settime_ymdhms;
handle->todr_setwen = NULL;
if (msm6242b_gettime_ymdhms(handle, &dt) != 0) {
aprint_error_dev(sc->sc_dev, "RTC does not work correctly\n");
return;
}
#ifdef MSM6242B_DEBUG
aprint_normal_dev(sc->sc_dev, "the time is %d %d %d %d %d %d\n",
dt.dt_year, dt.dt_mon, dt.dt_day, dt.dt_hour, dt.dt_min, dt.dt_sec);
#endif /* MSM6242B_DEBUG */
todr_attach(handle);
}
static int
msm6242b_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
{
struct msm6242b_softc *sc;
sc = handle->cookie;
/* XXX: splsched(); */
if(!msm6242b_hold(sc))
return (ENXIO);
dt->dt_sec = msm6242b_read(sc, MSM6242B_10SECOND) * 10 +
msm6242b_read(sc, MSM6242B_1SECOND);
dt->dt_min = msm6242b_read(sc, MSM6242B_10MINUTE) * 10 +
msm6242b_read(sc, MSM6242B_1MINUTE);
dt->dt_hour = (msm6242b_read(sc, MSM6242B_10HOUR_PMAM) &
MSM6242B_10HOUR_MASK) * 10 + msm6242b_read(sc, MSM6242B_1HOUR);
dt->dt_day = msm6242b_read(sc, MSM6242B_10DAY) * 10 +
msm6242b_read(sc, MSM6242B_1DAY);
dt->dt_mon = msm6242b_read(sc, MSM6242B_10MONTH) * 10 +
msm6242b_read(sc, MSM6242B_1MONTH);
dt->dt_year = msm6242b_read(sc, MSM6242B_10YEAR) * 10 +
msm6242b_read(sc, MSM6242B_1YEAR);
dt->dt_wday = msm6242b_read(sc, MSM6242B_WEEK);
#ifdef MSM6242B_DEBUG
aprint_normal_dev(sc->sc_dev, "the time is %d %d %d %d %d %d\n",
dt->dt_year, dt->dt_mon, dt->dt_day, dt->dt_hour, dt->dt_min, dt->dt_sec);
#endif /* MSM6242B_DEBUG */
/* handle 12h mode */
if ((msm6242b_read(sc, MSM6242B_CONTROL_F) &
MSM6242B_CONTROL_F_24H) == 0) {
if ((msm6242b_read(sc, MSM6242B_10HOUR_PMAM) &
MSM6242B_PMAM_BIT) == 0 && dt->dt_hour == 12)
dt->dt_hour = 0;
else if ((msm6242b_read(sc, MSM6242B_10HOUR_PMAM) &
MSM6242B_PMAM_BIT) && dt->dt_hour != 12)
dt->dt_hour += 12;
}
msm6242b_free(sc);
dt->dt_year += MSM6242B_BASE_YEAR;
if (dt->dt_year < POSIX_BASE_YEAR)
dt->dt_year += 100;
if ((dt->dt_hour > 23) ||
(dt->dt_day > 31) ||
(dt->dt_mon > 12) ||
(dt->dt_year > 2036))
return (EINVAL);
return 0;
}
bool
msm6242b_hold(struct msm6242b_softc *sc)
{
int try;
#define TRY_MAX 10
for (try = 0; try < TRY_MAX; try++) {
msm6242b_set(sc, MSM6242B_CONTROL_D, MSM6242B_CONTROL_D_HOLD);
if (msm6242b_read(sc, MSM6242B_CONTROL_D)
& MSM6242B_CONTROL_D_BUSY) {
#ifdef MSM6242B_DEBUG
aprint_normal_dev(sc->sc_dev, "gotta idle\n");
#endif /* MSM6242B_DEBUG */
msm6242b_unset(sc, MSM6242B_CONTROL_D,
MSM6242B_CONTROL_D_HOLD);
delay(70);
} else {
#ifdef MSM6242B_DEBUG
aprint_normal_dev(sc->sc_dev, "not busy\n");
#endif /* MSM6242B_DEBUG */
break;
}
}
if (try == TRY_MAX) {
aprint_error_dev(sc->sc_dev, "can't hold the chip\n");
return false;
}
return true;
}
void
msm6242b_free(struct msm6242b_softc *sc)
{
msm6242b_unset(sc, MSM6242B_CONTROL_D, MSM6242B_CONTROL_D_HOLD);
}
static uint8_t
msm6242b_read(struct msm6242b_softc *sc, uint8_t reg)
{
uint8_t r;
r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, reg) & MSM6242B_MASK;
return r;
}
static void
msm6242b_write(struct msm6242b_softc *sc, uint8_t reg, uint8_t val)
{
bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val);
}
static void
msm6242b_set(struct msm6242b_softc *sc, uint8_t reg, uint8_t bits)
{
uint8_t v;
v = msm6242b_read(sc, reg) | bits;
msm6242b_write(sc, reg, v);
}
static void
msm6242b_unset(struct msm6242b_softc *sc, uint8_t reg, uint8_t bits)
{
uint8_t v;
v = msm6242b_read(sc, reg) & ~bits;
msm6242b_write(sc, reg, v);
}
int
msm6242b_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
{
struct msm6242b_softc *sc;
int ampm;
/* XXX: splsched(); */
sc = handle->cookie;
if(!msm6242b_hold(sc))
return (ENXIO);
ampm = 0;
if ((msm6242b_read(sc, MSM6242B_CONTROL_F) &
MSM6242B_CONTROL_F_24H) == 0) {
if (dt->dt_hour >= 12) {
ampm = MSM6242B_CONTROL_F_24H;
if (dt->dt_hour != 12)
dt->dt_hour -= 12;
} else if (dt->dt_hour == 0) {
dt->dt_hour = 12;
}
}
msm6242b_write(sc, MSM6242B_10HOUR_PMAM, (dt->dt_hour / 10) | ampm);
msm6242b_write(sc, MSM6242B_1HOUR, dt->dt_hour % 10);
msm6242b_write(sc, MSM6242B_10SECOND, dt->dt_sec / 10);
msm6242b_write(sc, MSM6242B_1SECOND, dt->dt_sec % 10);
msm6242b_write(sc, MSM6242B_10MINUTE, dt->dt_min / 10);
msm6242b_write(sc, MSM6242B_1MINUTE, dt->dt_min % 10);
msm6242b_write(sc, MSM6242B_10DAY, dt->dt_day / 10);
msm6242b_write(sc, MSM6242B_1DAY, dt->dt_day % 10);
msm6242b_write(sc, MSM6242B_10MONTH, dt->dt_mon / 10);
msm6242b_write(sc, MSM6242B_1MONTH, dt->dt_mon % 10);
msm6242b_write(sc, MSM6242B_10YEAR, (dt->dt_year / 10) % 10);
msm6242b_write(sc, MSM6242B_1YEAR, dt->dt_year % 10);
msm6242b_write(sc, MSM6242B_WEEK, dt->dt_wday);
msm6242b_free(sc);
return 0;
}
|