ofrtc.c revision 1.1 1 /* $NetBSD: ofrtc.c,v 1.1 1996/09/30 16:35:09 ws Exp $ */
2
3 /*
4 * Copyright (C) 1996 Wolfgang Solfrank.
5 * Copyright (C) 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/device.h>
36
37 #include <dev/ofw/openfirm.h>
38
39 struct ofrtc_softc {
40 struct device sc_dev;
41 int sc_phandle;
42 int sc_ihandle;
43 };
44
45 static int ofrtcprobe __P((struct device *, void *, void *));
46 static void ofrtcattach __P((struct device *, struct device *, void *));
47
48 struct cfattach ofrtc_ca = {
49 sizeof(struct ofrtc_softc), ofrtcprobe, ofrtcattach
50 };
51
52 struct cfdriver ofrtc_cd = {
53 NULL, "ofrtc", DV_DULL
54 };
55
56 static int
57 ofrtcprobe(parent, match, aux)
58 struct device *parent;
59 void *match, *aux;
60 {
61 struct ofprobe *ofp = aux;
62 char type[8];
63 int l;
64
65 if ((l = OF_getprop(ofp->phandle, "device_type", type, sizeof type - 1)) < 0)
66 return 0;
67 if (l >= sizeof type)
68 return 0;
69
70 return !strcmp(type, "rtc");
71 }
72
73 static void
74 ofrtcattach(parent, self, aux)
75 struct device *parent, *self;
76 void *aux;
77 {
78 struct ofrtc_softc *of = (void *)self;
79 struct ofprobe *ofp = aux;
80 char name[32];
81 int l;
82
83 of->sc_phandle = ofp->phandle;
84 of->sc_ihandle = 0;
85 if ((l = OF_getprop(of->sc_phandle, "name", name, sizeof name - 1)) < 0)
86 panic("Device without name?");
87 if (l >= sizeof name)
88 l = sizeof name - 1;
89 name[l] = 0;
90 printf(": %s\n", name);
91 }
92
93 int
94 ofrtcopen(dev, flags, fmt)
95 dev_t dev;
96 int flags;
97 int fmt;
98 {
99 struct ofrtc_softc *of;
100 int unit = minor(dev);
101 char path[256];
102 int l;
103
104 if (unit >= ofrtc_cd.cd_ndevs)
105 return ENXIO;
106 if (!(of = ofrtc_cd.cd_devs[unit]))
107 return ENXIO;
108 if (!of->sc_ihandle) {
109 if ((l = OF_package_to_path(of->sc_phandle, path, sizeof path - 1)) < 0)
110 return ENXIO;
111 if (l >= sizeof path)
112 return ENXIO;
113 path[l] = 0;
114
115 if (!(of->sc_ihandle = OF_open(path))) {
116 if (of->sc_ihandle) {
117 OF_close(of->sc_ihandle);
118 of->sc_ihandle = 0;
119 }
120 return ENXIO;
121 }
122
123 }
124
125 return 0;
126 }
127
128 int
129 ofrtcclose(dev, flags, fmt)
130 dev_t dev;
131 int flags;
132 int fmt;
133 {
134 return 0;
135 }
136
137 static void
138 twodigit(bp, i)
139 char *bp;
140 int i;
141 {
142 *bp++ = i / 10 + '0';
143 *bp = i % 10 + '0';
144 }
145
146 static int
147 twodigits(bp)
148 char *bp;
149 {
150 int i;
151
152 i = *bp++ - '0';
153 return i * 10 + *bp++ - '0';
154 }
155
156 int
157 ofrtcread(dev, uio, flag)
158 dev_t dev;
159 struct uio *uio;
160 int flag;
161 {
162 struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
163 int date[6];
164 char buf[14];
165 int xlen;
166
167 if (uio->uio_offset >= sizeof buf)
168 return 0;
169
170 if (OF_call_method("get-time", of->sc_ihandle, 0, 6,
171 date, date + 1, date + 2,
172 date + 3, date + 4, date + 5))
173 return EIO;
174
175 twodigit(buf, date[5] % 100);
176 twodigit(buf + 2, date[4]);
177 twodigit(buf + 4, date[3]);
178 twodigit(buf + 6, date[2]);
179 twodigit(buf + 8, date[1]);
180 buf[10] = '.';
181 twodigit(buf + 11, date[0]);
182 buf[13] = '\n';
183
184 xlen = sizeof(buf) - uio->uio_offset;
185 if (xlen > uio->uio_resid)
186 xlen = uio->uio_resid;
187
188 return uiomove((caddr_t)buf, xlen, uio);
189 }
190
191 int
192 ofrtcwrite(dev, uio, flag)
193 dev_t dev;
194 struct uio *uio;
195 int flag;
196 {
197 struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
198 char buf[14];
199 int cnt, year, error;
200
201 /*
202 * We require atomic updates!
203 */
204 cnt = uio->uio_resid;
205 if (uio->uio_offset || (cnt != sizeof buf && cnt != sizeof buf - 1))
206 return EINVAL;
207
208 if (error = uiomove((caddr_t)buf, sizeof buf, uio))
209 return error;
210
211 if (cnt == sizeof buf && buf[sizeof buf - 1] != '\n')
212 return EINVAL;
213
214 year = twodigits(buf) + 1900;
215 if (year < 1970)
216 year += 100;
217 if (OF_call_method("set-time", of->sc_ihandle, 6, 0,
218 twodigits(buf + 11), twodigits(buf + 8), twodigits(buf + 6),
219 twodigits(buf + 4), twodigits(buf + 2), year))
220 return EIO;
221 return 0;
222 }
223