xref: /netbsd-src/sys/dev/ofw/ofrtc.c (revision 9af887f060e17e46eb657793447c86658de58ed1)
1*9af887f0Smrg /*	$NetBSD: ofrtc.c,v 1.23 2011/07/26 08:59:38 mrg Exp $	*/
25804d3f6Sws 
35804d3f6Sws /*
45804d3f6Sws  * Copyright (C) 1996 Wolfgang Solfrank.
55804d3f6Sws  * Copyright (C) 1996 TooLs GmbH.
65804d3f6Sws  * All rights reserved.
75804d3f6Sws  *
85804d3f6Sws  * Redistribution and use in source and binary forms, with or without
95804d3f6Sws  * modification, are permitted provided that the following conditions
105804d3f6Sws  * are met:
115804d3f6Sws  * 1. Redistributions of source code must retain the above copyright
125804d3f6Sws  *    notice, this list of conditions and the following disclaimer.
135804d3f6Sws  * 2. Redistributions in binary form must reproduce the above copyright
145804d3f6Sws  *    notice, this list of conditions and the following disclaimer in the
155804d3f6Sws  *    documentation and/or other materials provided with the distribution.
165804d3f6Sws  * 3. All advertising materials mentioning features or use of this software
175804d3f6Sws  *    must display the following acknowledgement:
185804d3f6Sws  *	This product includes software developed by TooLs GmbH.
195804d3f6Sws  * 4. The name of TooLs GmbH may not be used to endorse or promote products
205804d3f6Sws  *    derived from this software without specific prior written permission.
215804d3f6Sws  *
225804d3f6Sws  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
235804d3f6Sws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
245804d3f6Sws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
255804d3f6Sws  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265804d3f6Sws  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
275804d3f6Sws  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
285804d3f6Sws  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
295804d3f6Sws  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
305804d3f6Sws  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
315804d3f6Sws  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325804d3f6Sws  */
33cf407be2Sgdamore /*
34cf407be2Sgdamore  * Copyright 1997
35cf407be2Sgdamore  * Digital Equipment Corporation. All rights reserved.
36cf407be2Sgdamore  *
37cf407be2Sgdamore  * This software is furnished under license and may be used and
38cf407be2Sgdamore  * copied only in accordance with the following terms and conditions.
39cf407be2Sgdamore  * Subject to these conditions, you may download, copy, install,
40cf407be2Sgdamore  * use, modify and distribute this software in source and/or binary
41cf407be2Sgdamore  * form. No title or ownership is transferred hereby.
42cf407be2Sgdamore  *
43cf407be2Sgdamore  * 1) Any source code used, modified or distributed must reproduce
44cf407be2Sgdamore  *    and retain this copyright notice and list of conditions as
45cf407be2Sgdamore  *    they appear in the source file.
46cf407be2Sgdamore  *
47cf407be2Sgdamore  * 2) No right is granted to use any trade name, trademark, or logo of
48cf407be2Sgdamore  *    Digital Equipment Corporation. Neither the "Digital Equipment
49cf407be2Sgdamore  *    Corporation" name nor any trademark or logo of Digital Equipment
50cf407be2Sgdamore  *    Corporation may be used to endorse or promote products derived
51cf407be2Sgdamore  *    from this software without the prior written permission of
52cf407be2Sgdamore  *    Digital Equipment Corporation.
53cf407be2Sgdamore  *
54cf407be2Sgdamore  * 3) This software is provided "AS-IS" and any express or implied
55cf407be2Sgdamore  *    warranties, including but not limited to, any implied warranties
56cf407be2Sgdamore  *    of merchantability, fitness for a particular purpose, or
57cf407be2Sgdamore  *    non-infringement are disclaimed. In no event shall DIGITAL be
58cf407be2Sgdamore  *    liable for any damages whatsoever, and in particular, DIGITAL
59cf407be2Sgdamore  *    shall not be liable for special, indirect, consequential, or
60cf407be2Sgdamore  *    incidental damages or damages for lost profits, loss of
61cf407be2Sgdamore  *    revenue or loss of use, whether such damages arise in contract,
62cf407be2Sgdamore  *    negligence, tort, under statute, in equity, at law or otherwise,
63cf407be2Sgdamore  *    even if advised of the possibility of such damage.
64cf407be2Sgdamore  */
655804d3f6Sws 
66ab5d9d2bSlukem #include <sys/cdefs.h>
67*9af887f0Smrg __KERNEL_RCSID(0, "$NetBSD: ofrtc.c,v 1.23 2011/07/26 08:59:38 mrg Exp $");
68ab5d9d2bSlukem 
695804d3f6Sws #include <sys/param.h>
700a2c48d0Scgd #include <sys/systm.h>
715804d3f6Sws #include <sys/device.h>
723ec769dfSmatt #include <sys/conf.h>
73e0cc03a0Sjdolecek #include <sys/event.h>
745804d3f6Sws 
75cf407be2Sgdamore #include <dev/clock_subr.h>
765804d3f6Sws #include <dev/ofw/openfirm.h>
775804d3f6Sws 
78cf407be2Sgdamore #define OFRTC_SEC 0
79cf407be2Sgdamore #define OFRTC_MIN 1
80cf407be2Sgdamore #define OFRTC_HR  2
81cf407be2Sgdamore #define OFRTC_DOM 3
82cf407be2Sgdamore #define OFRTC_MON 4
83cf407be2Sgdamore #define OFRTC_YR  5
84cf407be2Sgdamore 
855804d3f6Sws struct ofrtc_softc {
865804d3f6Sws 	int sc_phandle;
875804d3f6Sws 	int sc_ihandle;
88cf407be2Sgdamore 	struct todr_chip_handle sc_todr;
895804d3f6Sws };
905804d3f6Sws 
917cf29912Scegger static int ofrtc_match(device_t, cfdata_t, void *);
927cf29912Scegger static void ofrtc_attach(device_t, device_t, void *);
93cf407be2Sgdamore static int ofrtc_gettod(todr_chip_handle_t, struct clock_ymdhms *);
94cf407be2Sgdamore static int ofrtc_settod(todr_chip_handle_t, struct clock_ymdhms *);
955804d3f6Sws 
96*9af887f0Smrg CFATTACH_DECL_NEW(ofrtc, sizeof(struct ofrtc_softc),
97c9b3657cSthorpej     ofrtc_match, ofrtc_attach, NULL, NULL);
985804d3f6Sws 
995804d3f6Sws static int
ofrtc_match(device_t parent,cfdata_t match,void * aux)1007cf29912Scegger ofrtc_match(device_t parent, cfdata_t match, void *aux)
1015804d3f6Sws {
102310f6fefSmycroft 	struct ofbus_attach_args *oba = aux;
1035804d3f6Sws 	char type[8];
1045804d3f6Sws 	int l;
1055804d3f6Sws 
106310f6fefSmycroft 	if (strcmp(oba->oba_busname, "ofw"))
107310f6fefSmycroft 		return (0);
108310f6fefSmycroft 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
109310f6fefSmycroft 	    sizeof type - 1)) < 0 ||
110310f6fefSmycroft 	    l >= sizeof type)
1115804d3f6Sws 		return 0;
1125804d3f6Sws 
113cf407be2Sgdamore 	/* ensure null termination */
114cf407be2Sgdamore 	type[l] = 0;
1155804d3f6Sws 	return !strcmp(type, "rtc");
1165804d3f6Sws }
1175804d3f6Sws 
1185804d3f6Sws static void
ofrtc_attach(device_t parent,device_t self,void * aux)1197cf29912Scegger ofrtc_attach(device_t parent, device_t self, void *aux)
1205804d3f6Sws {
121838ee1e0Sthorpej 	struct ofrtc_softc *of = device_private(self);
122310f6fefSmycroft 	struct ofbus_attach_args *oba = aux;
1235804d3f6Sws 	char name[32];
124cf407be2Sgdamore 	char path[256];
1255804d3f6Sws 	int l;
1265804d3f6Sws 
127310f6fefSmycroft 	of->sc_phandle = oba->oba_phandle;
1285804d3f6Sws 	of->sc_ihandle = 0;
129310f6fefSmycroft 	if ((l = OF_getprop(of->sc_phandle, "name", name,
130310f6fefSmycroft 	    sizeof name - 1)) < 0)
1315804d3f6Sws 		panic("Device without name?");
1325804d3f6Sws 	if (l >= sizeof name)
1335804d3f6Sws 		l = sizeof name - 1;
1345804d3f6Sws 	name[l] = 0;
135cf407be2Sgdamore 
136cf407be2Sgdamore 	if (!of->sc_ihandle) {
137cf407be2Sgdamore 		if ((l = OF_package_to_path(of->sc_phandle, path,
138cf407be2Sgdamore 			 sizeof path - 1)) < 0 ||
139cf407be2Sgdamore 		    l >= sizeof path) {
140cf407be2Sgdamore 			aprint_error(": cannot determine package path\n");
141cf407be2Sgdamore  			return;
142cf407be2Sgdamore 		}
143cf407be2Sgdamore 		path[l] = 0;
144cf407be2Sgdamore 
145cf407be2Sgdamore 		if (!(of->sc_ihandle = OF_open(path))) {
146cf407be2Sgdamore 			aprint_error(": cannot open path\n");
147cf407be2Sgdamore  			return;
148cf407be2Sgdamore  		}
149cf407be2Sgdamore  	}
150cf407be2Sgdamore 
151cf407be2Sgdamore 	of->sc_todr.todr_gettime_ymdhms = ofrtc_gettod;
152cf407be2Sgdamore 	of->sc_todr.todr_settime_ymdhms = ofrtc_settod;
153cf407be2Sgdamore 	of->sc_todr.cookie = of;
154cf407be2Sgdamore 	todr_attach(&of->sc_todr);
15586373f8cSchristos 	printf(": %s\n", name);
1565804d3f6Sws }
1575804d3f6Sws 
1585804d3f6Sws int
ofrtc_gettod(todr_chip_handle_t tch,struct clock_ymdhms * dt)159cf407be2Sgdamore ofrtc_gettod(todr_chip_handle_t tch, struct clock_ymdhms *dt)
1605804d3f6Sws {
161cf407be2Sgdamore 	struct ofrtc_softc *of = tch->cookie;
1625804d3f6Sws 	int date[6];
1635804d3f6Sws 
1645804d3f6Sws 	/*
165cf407be2Sgdamore 	 * We mostly ignore the suggested time and go for the RTC clock time
166cf407be2Sgdamore 	 * stored in the CMOS RAM.  If the time can't be obtained from the
167cf407be2Sgdamore 	 * CMOS, or if the time obtained from the CMOS is 5 or more years
168cf407be2Sgdamore 	 * less than the suggested time, we used the suggested time.  (In
169cf407be2Sgdamore 	 * the latter case, it's likely that the CMOS battery has died.)
1705804d3f6Sws 	 */
1715804d3f6Sws 
172cf407be2Sgdamore 	if (OF_call_method("get-time", of->sc_ihandle, 0, 6,
173cf407be2Sgdamore 	    date, date + 1, date + 2, date + 3, date + 4, date + 5)) {
1745804d3f6Sws 		return EIO;
175cf407be2Sgdamore 	}
176cf407be2Sgdamore 
177cf407be2Sgdamore 	dt->dt_sec = date[OFRTC_SEC];
178cf407be2Sgdamore 	dt->dt_min = date[OFRTC_MIN];
179cf407be2Sgdamore 	dt->dt_hour = date[OFRTC_HR];
180cf407be2Sgdamore 	dt->dt_day = date[OFRTC_DOM];
181cf407be2Sgdamore 	dt->dt_mon = date[OFRTC_MON];
182cf407be2Sgdamore 	dt->dt_year = date[OFRTC_YR];
183cf407be2Sgdamore 
184cf407be2Sgdamore 	return 0;
185cf407be2Sgdamore }
186cf407be2Sgdamore 
187cf407be2Sgdamore int
ofrtc_settod(todr_chip_handle_t tch,struct clock_ymdhms * dt)188cf407be2Sgdamore ofrtc_settod(todr_chip_handle_t tch, struct clock_ymdhms *dt)
189cf407be2Sgdamore {
190cf407be2Sgdamore 	struct ofrtc_softc *of = tch->cookie;
191cf407be2Sgdamore 	int sec, minute, hr, dom, mon, yr;
192cf407be2Sgdamore 
193cf407be2Sgdamore 	sec = dt->dt_sec;
194cf407be2Sgdamore 	minute = dt->dt_min;
195cf407be2Sgdamore 	hr = dt->dt_hour;
196cf407be2Sgdamore 	dom = dt->dt_day;
197cf407be2Sgdamore 	mon = dt->dt_mon;
198cf407be2Sgdamore 	yr = dt->dt_year;
199cf407be2Sgdamore 
200cf407be2Sgdamore 	if (OF_call_method("set-time", of->sc_ihandle, 6, 0,
201cf407be2Sgdamore 	     sec, minute, hr, dom, mon, yr))
202cf407be2Sgdamore 		return EIO;
203cf407be2Sgdamore 
2045804d3f6Sws 	return 0;
2055804d3f6Sws }
206