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