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