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
|
/* $OpenBSD: dsrtc.c,v 1.3 2009/04/20 20:31:06 miod Exp $ */
/*
* Copyright (c) 2001-2004 Opsycon AB (www.opsycon.se / www.opsycon.com)
*
* 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.
*
*/
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <dev/ic/ds1687reg.h>
#include <machine/autoconf.h>
#include <machine/bus.h>
#include <mips64/archtype.h>
#include <mips64/dev/clockvar.h>
#include <sgi/localbus/macebus.h>
#include <sgi/pci/iocreg.h>
#include <sgi/pci/iocvar.h>
bus_space_handle_t clock_h; /* XXX */
struct dsrtc_softc {
struct device sc_dev;
bus_space_tag_t sc_clkt;
bus_space_handle_t sc_clkh, sc_clkh2;
int (*read)(struct dsrtc_softc *, int);
void (*write)(struct dsrtc_softc *, int, int);
};
int dsrtc_match_ioc(struct device *, void *, void *);
void dsrtc_attach_ioc(struct device *, struct device *, void *);
int dsrtc_match_macebus(struct device *, void *, void *);
void dsrtc_attach_macebus(struct device *, struct device *, void *);
struct cfdriver dsrtc_cd = {
NULL, "dsrtc", DV_DULL
};
struct cfattach dsrtc_macebus_ca = {
sizeof(struct dsrtc_softc), dsrtc_match_macebus, dsrtc_attach_macebus
};
struct cfattach dsrtc_ioc_ca = {
sizeof(struct dsrtc_softc), dsrtc_match_ioc, dsrtc_attach_ioc
};
int ip32_dsrtc_read(struct dsrtc_softc *, int);
void ip32_dsrtc_write(struct dsrtc_softc *, int, int);
int ip30_dsrtc_read(struct dsrtc_softc *, int);
void ip30_dsrtc_write(struct dsrtc_softc *, int, int);
void ds1687_get(void *, time_t, struct tod_time *);
void ds1687_set(void *, struct tod_time *);
static inline int frombcd(int);
static inline int tobcd(int);
static inline int
frombcd(int x)
{
return (x >> 4) * 10 + (x & 0xf);
}
static inline int
tobcd(int x)
{
return (x / 10 * 16) + (x % 10);
}
int
dsrtc_match_ioc(struct device *parent, void *match, void *aux)
{
struct ioc_attach_args *iaa = aux;
bus_space_handle_t ih, ih2;
uint c, c2, c3;
int rc = 0;
/*
* The IOC3 RTC is either a Dallas (now Maxim) DS1386 or compatible
* (likely a more recent DS1687), or a Mostek (now SGS Thomson)
* MK48T35.
*/
if (bus_space_map(iaa->iaa_memt, IOC3_BYTEBUS_1, 1, 0, &ih) != 0)
return 0;
if (bus_space_map(iaa->iaa_memt, IOC3_BYTEBUS_2, 1, 0, &ih2) != 0)
goto unmap;
/*
* Check the low 4 bits of control register C. If any is set,
* or if the values written to them stick, then this is not
* a Dallas chip.
*
* Note that the value we read the next few times can't be
* compared to the first value read, as the upper four bits
* are cleared by reading them. And might get set again
* between two reads.
*/
bus_space_write_1(iaa->iaa_memt, ih, 0, DS1687_CTRL_C);
c = bus_space_read_1(iaa->iaa_memt, ih2, 0);
if ((c & 0x0f) != 0)
goto done;
bus_space_write_1(iaa->iaa_memt, ih, 0, DS1687_CTRL_C);
bus_space_write_1(iaa->iaa_memt, ih2, 0, c | 0x0f);
bus_space_write_1(iaa->iaa_memt, ih, 0, DS1687_CTRL_C);
c2 = bus_space_read_1(iaa->iaa_memt, ih2, 0);
if ((c2 & 0x0f) == 0)
rc = 1;
bus_space_write_1(iaa->iaa_memt, ih, 0, DS1687_CTRL_C);
bus_space_write_1(iaa->iaa_memt, ih2, 0, c2 | 0x0f);
bus_space_write_1(iaa->iaa_memt, ih, 0, DS1687_CTRL_C);
c3 = bus_space_read_1(iaa->iaa_memt, ih2, 0);
if ((c3 & 0x0f) != 0)
rc = 0;
/* write back first value read in case this is not a Dallas chip */
bus_space_write_1(iaa->iaa_memt, ih, 0, DS1687_CTRL_C);
bus_space_write_1(iaa->iaa_memt, ih2, 0, c);
done:
bus_space_unmap(iaa->iaa_memt, ih2, 1);
unmap:
bus_space_unmap(iaa->iaa_memt, ih, 1);
return rc;
}
void
dsrtc_attach_ioc(struct device *parent, struct device *self, void *aux)
{
struct dsrtc_softc *sc = (void *)self;
struct ioc_attach_args *iaa = aux;
sc->sc_clkt = iaa->iaa_memt;
if (bus_space_map(sc->sc_clkt, IOC3_BYTEBUS_1, 1, 0, &sc->sc_clkh) ||
bus_space_map(sc->sc_clkt, IOC3_BYTEBUS_2, 1, 0, &sc->sc_clkh2)) {
printf(": can't map registers\n");
return;
}
printf(": DS1687\n");
sc->read = ip30_dsrtc_read;
sc->write = ip30_dsrtc_write;
sys_tod.tod_cookie = self;
sys_tod.tod_get = ds1687_get;
sys_tod.tod_set = ds1687_set;
}
int
dsrtc_match_macebus(struct device *parent, void *match, void *aux)
{
return 1;
}
void
dsrtc_attach_macebus(struct device *parent, struct device *self, void *aux)
{
struct dsrtc_softc *sc = (void *)self;
struct confargs *ca = aux;
sc->sc_clkt = ca->ca_iot;
if (bus_space_map(sc->sc_clkt, MACE_ISA_RTC_OFFS, 128*256, 0,
&sc->sc_clkh)) {
printf(": can't map registers\n");
return;
}
printf(": DS1687\n");
sc->read = ip32_dsrtc_read;
sc->write = ip32_dsrtc_write;
sys_tod.tod_cookie = self;
sys_tod.tod_get = ds1687_get;
sys_tod.tod_set = ds1687_set;
/*
* XXX Expose the clock address space so that it can be used
* outside of clock(4). This is rather inelegant, however it
* will have to do for now...
*/
clock_h = sc->sc_clkh;
}
int
ip32_dsrtc_read(struct dsrtc_softc *sc, int reg)
{
return bus_space_read_1(sc->sc_clkt, sc->sc_clkh, reg);
}
void
ip32_dsrtc_write(struct dsrtc_softc *sc, int reg, int val)
{
bus_space_write_1(sc->sc_clkt, sc->sc_clkh, reg, val);
}
int
ip30_dsrtc_read(struct dsrtc_softc *sc, int reg)
{
bus_space_write_1(sc->sc_clkt, sc->sc_clkh, 0, reg);
return bus_space_read_1(sc->sc_clkt, sc->sc_clkh2, 0);
}
void
ip30_dsrtc_write(struct dsrtc_softc *sc, int reg, int val)
{
bus_space_write_1(sc->sc_clkt, sc->sc_clkh, 0, reg);
bus_space_write_1(sc->sc_clkt, sc->sc_clkh2, 0, val);
}
/*
* Dallas clock driver.
*/
void
ds1687_get(void *v, time_t base, struct tod_time *ct)
{
struct dsrtc_softc *sc = v;
int ctrl, century;
/* Select bank 1. */
ctrl = (*sc->read)(sc, DS1687_CTRL_A);
(*sc->write)(sc, DS1687_CTRL_A, ctrl | DS1687_BANK_1);
/* Wait for no update in progress. */
while ((*sc->read)(sc, DS1687_CTRL_A) & DS1687_UIP)
/* Do nothing. */;
/* Read the RTC. */
ct->sec = frombcd((*sc->read)(sc, DS1687_SEC));
ct->min = frombcd((*sc->read)(sc, DS1687_MIN));
ct->hour = frombcd((*sc->read)(sc, DS1687_HOUR));
ct->day = frombcd((*sc->read)(sc, DS1687_DAY));
ct->mon = frombcd((*sc->read)(sc, DS1687_MONTH));
ct->year = frombcd((*sc->read)(sc, DS1687_YEAR));
century = frombcd((*sc->read)(sc, DS1687_CENTURY));
ct->year += 100 * (century - 19);
}
void
ds1687_set(void *v, struct tod_time *ct)
{
struct dsrtc_softc *sc = v;
int year, century, ctrl;
century = ct->year / 100 + 19;
year = ct->year % 100;
/* Select bank 1. */
ctrl = (*sc->read)(sc, DS1687_CTRL_A);
(*sc->write)(sc, DS1687_CTRL_A, ctrl | DS1687_BANK_1);
/* Select data mode 0 (BCD) and 24 hour time. */
ctrl = (*sc->read)(sc, DS1687_CTRL_B);
(*sc->write)(sc, DS1687_CTRL_B,
(ctrl & ~DS1687_DM_1) | DS1687_24_HR);
/* Prevent updates. */
ctrl = (*sc->read)(sc, DS1687_CTRL_B);
(*sc->write)(sc, DS1687_CTRL_B, ctrl | DS1687_SET_CLOCK);
/* Update the RTC. */
(*sc->write)(sc, DS1687_SEC, tobcd(ct->sec));
(*sc->write)(sc, DS1687_MIN, tobcd(ct->min));
(*sc->write)(sc, DS1687_HOUR, tobcd(ct->hour));
(*sc->write)(sc, DS1687_DOW, tobcd(ct->dow));
(*sc->write)(sc, DS1687_DAY, tobcd(ct->day));
(*sc->write)(sc, DS1687_MONTH, tobcd(ct->mon));
(*sc->write)(sc, DS1687_YEAR, tobcd(year));
(*sc->write)(sc, DS1687_CENTURY, tobcd(century));
/* Enable updates. */
(*sc->write)(sc, DS1687_CTRL_B, ctrl);
}
|