ofrtc.c revision 1.14 1 /* $NetBSD: ofrtc.c,v 1.14 2002/10/02 16:34:34 thorpej 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/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ofrtc.c,v 1.14 2002/10/02 16:34:34 thorpej Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/conf.h>
41
42 #include <dev/ofw/openfirm.h>
43
44 struct ofrtc_softc {
45 struct device sc_dev;
46 int sc_phandle;
47 int sc_ihandle;
48 };
49
50 static int ofrtc_match __P((struct device *, struct cfdata *, void *));
51 static void ofrtc_attach __P((struct device *, struct device *, void *));
52
53 CFATTACH_DECL(ofrtc, sizeof(struct ofrtc_softc),
54 ofrtc_match, ofrtc_attach, NULL, NULL);
55
56 extern struct cfdriver ofrtc_cd;
57
58 dev_type_open(ofrtc_open);
59 dev_type_read(ofrtc_read);
60 dev_type_write(ofrtc_write);
61
62 const struct cdevsw ofrtc_cdevsw = {
63 ofrtc_open, nullclose, ofrtc_read, ofrtc_write, noioctl,
64 nostop, notty, nopoll, nommap,
65 };
66
67 static int
68 ofrtc_match(struct device *parent, struct cfdata *match, void *aux)
69 {
70 struct ofbus_attach_args *oba = aux;
71 char type[8];
72 int l;
73
74 if (strcmp(oba->oba_busname, "ofw"))
75 return (0);
76 if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
77 sizeof type - 1)) < 0 ||
78 l >= sizeof type)
79 return 0;
80
81 return !strcmp(type, "rtc");
82 }
83
84 static void
85 ofrtc_attach(struct device *parent, struct device *self, void *aux)
86 {
87 struct ofrtc_softc *of = (void *)self;
88 struct ofbus_attach_args *oba = aux;
89 char name[32];
90 int l;
91
92 of->sc_phandle = oba->oba_phandle;
93 of->sc_ihandle = 0;
94 if ((l = OF_getprop(of->sc_phandle, "name", name,
95 sizeof name - 1)) < 0)
96 panic("Device without name?");
97 if (l >= sizeof name)
98 l = sizeof name - 1;
99 name[l] = 0;
100 printf(": %s\n", name);
101 }
102
103 int
104 ofrtc_open(dev_t dev, int flags, int fmt, struct proc *p)
105 {
106 struct ofrtc_softc *of;
107 int unit = minor(dev);
108 char path[256];
109 int l;
110
111 if (unit >= ofrtc_cd.cd_ndevs)
112 return ENXIO;
113 if (!(of = ofrtc_cd.cd_devs[unit]))
114 return ENXIO;
115 if (!of->sc_ihandle) {
116 if ((l = OF_package_to_path(of->sc_phandle, path,
117 sizeof path - 1)) < 0 ||
118 l >= sizeof path)
119 return ENXIO;
120 path[l] = 0;
121
122 if (!(of->sc_ihandle = OF_open(path))) {
123 if (of->sc_ihandle) {
124 OF_close(of->sc_ihandle);
125 of->sc_ihandle = 0;
126 }
127 return ENXIO;
128 }
129
130 }
131
132 return 0;
133 }
134
135 static void
136 twodigit(char *bp, int i)
137 {
138 *bp++ = i / 10 + '0';
139 *bp = i % 10 + '0';
140 }
141
142 static int
143 twodigits(char *bp)
144 {
145 int i;
146
147 i = *bp++ - '0';
148 return i * 10 + *bp++ - '0';
149 }
150
151 int
152 ofrtc_read(dev_t dev, struct uio *uio, int flag)
153 {
154 struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
155 int date[6];
156 char buf[14];
157 int xlen;
158
159 if (uio->uio_offset >= sizeof buf)
160 return 0;
161
162 if (OF_call_method("get-time", of->sc_ihandle, 0, 6,
163 date, date + 1, date + 2,
164 date + 3, date + 4, date + 5))
165 return EIO;
166
167 twodigit(buf, date[5] % 100);
168 twodigit(buf + 2, date[4]);
169 twodigit(buf + 4, date[3]);
170 twodigit(buf + 6, date[2]);
171 twodigit(buf + 8, date[1]);
172 buf[10] = '.';
173 twodigit(buf + 11, date[0]);
174 buf[13] = '\n';
175
176 xlen = sizeof(buf) - uio->uio_offset;
177 if (xlen > uio->uio_resid)
178 xlen = uio->uio_resid;
179
180 return uiomove((caddr_t)buf, xlen, uio);
181 }
182
183 int
184 ofrtc_write(dev_t dev, struct uio *uio, int flag)
185 {
186 struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
187 char buf[14];
188 int cnt, year, error;
189
190 /*
191 * We require atomic updates!
192 */
193 cnt = uio->uio_resid;
194 if (uio->uio_offset || (cnt != sizeof buf && cnt != sizeof buf - 1))
195 return EINVAL;
196
197 if ((error = uiomove((caddr_t)buf, sizeof buf, uio)) != 0)
198 return error;
199
200 if (cnt == sizeof buf && buf[sizeof buf - 1] != '\n')
201 return EINVAL;
202
203 year = twodigits(buf) + 1900;
204 if (year < 1970)
205 year += 100;
206 if (OF_call_method("set-time", of->sc_ihandle, 6, 0,
207 twodigits(buf + 11), twodigits(buf + 8), twodigits(buf + 6),
208 twodigits(buf + 4), twodigits(buf + 2), year))
209 return EIO;
210 return 0;
211 }
212