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
|
/* $OpenBSD: ofrtc.c,v 1.5 2002/03/14 01:26:58 millert Exp $ */
/* $NetBSD: ofrtc.c,v 1.3 1996/10/13 01:38:14 christos Exp $ */
/*
* Copyright (C) 1996 Wolfgang Solfrank.
* Copyright (C) 1996 TooLs GmbH.
* All rights reserved.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by TooLs GmbH.
* 4. The name of TooLs GmbH may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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/device.h>
#include <dev/ofw/openfirm.h>
struct ofrtc_softc {
struct device sc_dev;
int sc_phandle;
int sc_ihandle;
};
static int ofrtcprobe(struct device *, void *, void *);
static void ofrtcattach(struct device *, struct device *, void *);
struct cfattach ofrtc_ca = {
sizeof(struct ofrtc_softc), ofrtcprobe, ofrtcattach
};
struct cfdriver ofrtc_cd = {
NULL, "ofrtc", DV_DULL
};
static int
ofrtcprobe(parent, match, aux)
struct device *parent;
void *match, *aux;
{
struct ofprobe *ofp = aux;
char type[8];
int l;
if ((l = OF_getprop(ofp->phandle, "device_type", type, sizeof type - 1)) < 0)
return 0;
if (l >= sizeof type)
return 0;
return !strcmp(type, "rtc");
}
int OF_clock_read(int *sec, int *min, int *hour, int *day,
int *mon, int *yr);
typedef int (clock_read_t)(int *sec, int *min, int *hour, int *day,
int *mon, int *yr);
extern clock_read_t *clock_read;
static void
ofrtcattach(parent, self, aux)
struct device *parent, *self;
void *aux;
{
struct ofrtc_softc *of = (void *)self;
struct ofprobe *ofp = aux;
char name[32];
int l;
clock_read = &OF_clock_read;
of->sc_phandle = ofp->phandle;
of->sc_ihandle = 0;
if ((l = OF_getprop(of->sc_phandle, "name", name, sizeof name - 1)) < 0)
panic("Device without name?");
if (l >= sizeof name)
l = sizeof name - 1;
name[l] = 0;
printf(": %s\n", name);
}
int
ofrtcopen(dev, flags, fmt)
dev_t dev;
int flags;
int fmt;
{
struct ofrtc_softc *of;
int unit = minor(dev);
char path[256];
int l;
if (unit >= ofrtc_cd.cd_ndevs)
return ENXIO;
if (!(of = ofrtc_cd.cd_devs[unit]))
return ENXIO;
if (!of->sc_ihandle) {
if ((l = OF_package_to_path(of->sc_phandle, path, sizeof path - 1)) < 0)
return ENXIO;
if (l >= sizeof path)
return ENXIO;
path[l] = 0;
if (!(of->sc_ihandle = OF_open(path))) {
if (of->sc_ihandle) {
OF_close(of->sc_ihandle);
of->sc_ihandle = 0;
}
return ENXIO;
}
}
return 0;
}
int
ofrtcclose(dev, flags, fmt)
dev_t dev;
int flags;
int fmt;
{
return 0;
}
static void
twodigit(bp, i)
char *bp;
int i;
{
*bp++ = i / 10 + '0';
*bp = i % 10 + '0';
}
static int
twodigits(bp)
char *bp;
{
int i;
i = *bp++ - '0';
return i * 10 + *bp++ - '0';
}
int
ofrtcread(dev, uio, flag)
dev_t dev;
struct uio *uio;
int flag;
{
struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
int date[6];
char buf[14];
int xlen;
if (uio->uio_offset >= sizeof buf)
return 0;
if (OF_call_method("get-time", of->sc_ihandle, 0, 6,
date, date + 1, date + 2,
date + 3, date + 4, date + 5))
return EIO;
twodigit(buf, date[5] % 100);
twodigit(buf + 2, date[4]);
twodigit(buf + 4, date[3]);
twodigit(buf + 6, date[2]);
twodigit(buf + 8, date[1]);
buf[10] = '.';
twodigit(buf + 11, date[0]);
buf[13] = '\n';
xlen = sizeof(buf) - uio->uio_offset;
if (xlen > uio->uio_resid)
xlen = uio->uio_resid;
return uiomove((caddr_t)buf, xlen, uio);
}
int
ofrtcwrite(dev, uio, flag)
dev_t dev;
struct uio *uio;
int flag;
{
struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
char buf[14];
int cnt, year, error;
/*
* We require atomic updates!
*/
cnt = uio->uio_resid;
if (uio->uio_offset || (cnt != sizeof buf && cnt != sizeof buf - 1))
return EINVAL;
if (error = uiomove((caddr_t)buf, sizeof buf, uio))
return error;
if (cnt == sizeof buf && buf[sizeof buf - 1] != '\n')
return EINVAL;
year = twodigits(buf) + 1900;
if (year < 1970)
year += 100;
if (OF_call_method("set-time", of->sc_ihandle, 6, 0,
twodigits(buf + 11), twodigits(buf + 8), twodigits(buf + 6),
twodigits(buf + 4), twodigits(buf + 2), year))
return EIO;
return 0;
}
/* powerpc expects clock_read data in BCD, discuss this if it is improper */
#define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
#define YEAR0 1900
int
OF_clock_read(int *sec, int *min, int *hour, int *day,
int *mon, int *yr)
{
struct ofrtc_softc *of;
char path[256];
int l;
if (!(of = ofrtc_cd.cd_devs[0]))
return 1;
if ((l = OF_package_to_path(of->sc_phandle, path, sizeof path - 1)) < 0)
return 1;
if (l >= sizeof path)
return 1;
path[l] = 0;
if (!(of->sc_ihandle = OF_open(path))) {
if (of->sc_ihandle) {
OF_close(of->sc_ihandle);
of->sc_ihandle = 0;
}
return 1;
}
if (OF_call_method("get-time", of->sc_ihandle, 0, 6,
sec, min, hour, day, mon, yr))
return 1;
#if 0
printf("y:%d m:%d d:%d H:%d M:%d S:%d\n",
*yr, *mon, *day, *hour, *min, *sec);
#endif
*yr -= YEAR0; /* bsd expects 0 - 200, not something like 1998 */
*yr = TOBCD(*yr);
*mon = TOBCD(*mon);
*day = TOBCD(*day);
*hour = TOBCD(*hour);
*min = TOBCD(*min);
*sec = TOBCD(*sec);
return 0;
}
|