xref: /netbsd-src/sys/dev/ofw/ofrtc.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: ofrtc.c,v 1.22 2009/05/12 14:39:22 cegger 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  * Copyright 1997
35  * Digital Equipment Corporation. All rights reserved.
36  *
37  * This software is furnished under license and may be used and
38  * copied only in accordance with the following terms and conditions.
39  * Subject to these conditions, you may download, copy, install,
40  * use, modify and distribute this software in source and/or binary
41  * form. No title or ownership is transferred hereby.
42  *
43  * 1) Any source code used, modified or distributed must reproduce
44  *    and retain this copyright notice and list of conditions as
45  *    they appear in the source file.
46  *
47  * 2) No right is granted to use any trade name, trademark, or logo of
48  *    Digital Equipment Corporation. Neither the "Digital Equipment
49  *    Corporation" name nor any trademark or logo of Digital Equipment
50  *    Corporation may be used to endorse or promote products derived
51  *    from this software without the prior written permission of
52  *    Digital Equipment Corporation.
53  *
54  * 3) This software is provided "AS-IS" and any express or implied
55  *    warranties, including but not limited to, any implied warranties
56  *    of merchantability, fitness for a particular purpose, or
57  *    non-infringement are disclaimed. In no event shall DIGITAL be
58  *    liable for any damages whatsoever, and in particular, DIGITAL
59  *    shall not be liable for special, indirect, consequential, or
60  *    incidental damages or damages for lost profits, loss of
61  *    revenue or loss of use, whether such damages arise in contract,
62  *    negligence, tort, under statute, in equity, at law or otherwise,
63  *    even if advised of the possibility of such damage.
64  */
65 
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: ofrtc.c,v 1.22 2009/05/12 14:39:22 cegger Exp $");
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/device.h>
72 #include <sys/conf.h>
73 #include <sys/event.h>
74 
75 #include <dev/clock_subr.h>
76 #include <dev/ofw/openfirm.h>
77 
78 #define OFRTC_SEC 0
79 #define OFRTC_MIN 1
80 #define OFRTC_HR  2
81 #define OFRTC_DOM 3
82 #define OFRTC_MON 4
83 #define OFRTC_YR  5
84 
85 struct ofrtc_softc {
86 	struct device sc_dev;
87 	int sc_phandle;
88 	int sc_ihandle;
89 	struct todr_chip_handle sc_todr;
90 };
91 
92 static int ofrtc_match(device_t, cfdata_t, void *);
93 static void ofrtc_attach(device_t, device_t, void *);
94 static int ofrtc_gettod(todr_chip_handle_t, struct clock_ymdhms *);
95 static int ofrtc_settod(todr_chip_handle_t, struct clock_ymdhms *);
96 
97 CFATTACH_DECL(ofrtc, sizeof(struct ofrtc_softc),
98     ofrtc_match, ofrtc_attach, NULL, NULL);
99 
100 static int
101 ofrtc_match(device_t parent, cfdata_t match, void *aux)
102 {
103 	struct ofbus_attach_args *oba = aux;
104 	char type[8];
105 	int l;
106 
107 	if (strcmp(oba->oba_busname, "ofw"))
108 		return (0);
109 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
110 	    sizeof type - 1)) < 0 ||
111 	    l >= sizeof type)
112 		return 0;
113 
114 	/* ensure null termination */
115 	type[l] = 0;
116 	return !strcmp(type, "rtc");
117 }
118 
119 static void
120 ofrtc_attach(device_t parent, device_t self, void *aux)
121 {
122 	struct ofrtc_softc *of = device_private(self);
123 	struct ofbus_attach_args *oba = aux;
124 	char name[32];
125 	char path[256];
126 	int l;
127 
128 	of->sc_phandle = oba->oba_phandle;
129 	of->sc_ihandle = 0;
130 	if ((l = OF_getprop(of->sc_phandle, "name", name,
131 	    sizeof name - 1)) < 0)
132 		panic("Device without name?");
133 	if (l >= sizeof name)
134 		l = sizeof name - 1;
135 	name[l] = 0;
136 
137 	if (!of->sc_ihandle) {
138 		if ((l = OF_package_to_path(of->sc_phandle, path,
139 			 sizeof path - 1)) < 0 ||
140 		    l >= sizeof path) {
141 			aprint_error(": cannot determine package path\n");
142  			return;
143 		}
144 		path[l] = 0;
145 
146 		if (!(of->sc_ihandle = OF_open(path))) {
147 			aprint_error(": cannot open path\n");
148  			return;
149  		}
150  	}
151 
152 	of->sc_todr.todr_gettime_ymdhms = ofrtc_gettod;
153 	of->sc_todr.todr_settime_ymdhms = ofrtc_settod;
154 	of->sc_todr.cookie = of;
155 	todr_attach(&of->sc_todr);
156 	printf(": %s\n", name);
157 }
158 
159 int
160 ofrtc_gettod(todr_chip_handle_t tch, struct clock_ymdhms *dt)
161 {
162 	struct ofrtc_softc *of = tch->cookie;
163 	int date[6];
164 
165 	/*
166 	 * We mostly ignore the suggested time and go for the RTC clock time
167 	 * stored in the CMOS RAM.  If the time can't be obtained from the
168 	 * CMOS, or if the time obtained from the CMOS is 5 or more years
169 	 * less than the suggested time, we used the suggested time.  (In
170 	 * the latter case, it's likely that the CMOS battery has died.)
171 	 */
172 
173 	if (OF_call_method("get-time", of->sc_ihandle, 0, 6,
174 	    date, date + 1, date + 2, date + 3, date + 4, date + 5)) {
175 		return EIO;
176 	}
177 
178 	dt->dt_sec = date[OFRTC_SEC];
179 	dt->dt_min = date[OFRTC_MIN];
180 	dt->dt_hour = date[OFRTC_HR];
181 	dt->dt_day = date[OFRTC_DOM];
182 	dt->dt_mon = date[OFRTC_MON];
183 	dt->dt_year = date[OFRTC_YR];
184 
185 	return 0;
186 }
187 
188 int
189 ofrtc_settod(todr_chip_handle_t tch, struct clock_ymdhms *dt)
190 {
191 	struct ofrtc_softc *of = tch->cookie;
192 	int sec, minute, hr, dom, mon, yr;
193 
194 	sec = dt->dt_sec;
195 	minute = dt->dt_min;
196 	hr = dt->dt_hour;
197 	dom = dt->dt_day;
198 	mon = dt->dt_mon;
199 	yr = dt->dt_year;
200 
201 	if (OF_call_method("set-time", of->sc_ihandle, 6, 0,
202 	     sec, minute, hr, dom, mon, yr))
203 		return EIO;
204 
205 	return 0;
206 }
207