xref: /netbsd-src/sys/arch/mac68k/mac68k/pram.c (revision 62a8debe1dc62962e18a1c918def78666141273b)
1 /*	$NetBSD: pram.c,v 1.22 2006/06/21 00:02:26 rjs Exp $	*/
2 
3 /*-
4  * Copyright (C) 1993	Allen K. Briggs, Chris P. Caputo,
5  *			Michael L. Finch, Bradley A. Grantham, and
6  *			Lawrence A. Kesteloot
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the Alice Group.
20  * 4. The names of the Alice Group or any of its members may not be used
21  *    to endorse or promote products derived from this software without
22  *    specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE ALICE GROUP ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE ALICE GROUP BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: pram.c,v 1.22 2006/06/21 00:02:26 rjs Exp $");
38 
39 #include "opt_adb.h"
40 
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #ifdef DEBUG
44 #include <sys/systm.h>
45 #endif
46 #include <machine/viareg.h>
47 
48 #include <mac68k/mac68k/pram.h>
49 #include <mac68k/mac68k/macrom.h>
50 #ifndef MRG_ADB
51 #include <mac68k/dev/adbvar.h>
52 #endif
53 
54 #if DEBUG
55 static const char *convtime(unsigned long t)
56 {
57   static long daypmon[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
58   static const char *monstr[] = {"January","February","March","April","May",
59     "June","July","August","September","October","November","December" };
60   static char s[200];
61   long year,month,day,hour,minute,seconds,i,dayperyear;
62 
63   year=1904;
64   month=0;  /* Jan */
65   day=1;
66   hour=0;
67   minute=0;
68   seconds=0;
69 
70   if(t == 0xffffffff)
71      return("<time value is -1>");
72 
73   while (t > 0)
74   {
75     if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
76     {
77       dayperyear=366;
78       daypmon[1]=29;
79     }
80     else
81     {
82       dayperyear=365;
83       daypmon[1]=28;
84     }
85     i=dayperyear*60*60*24;
86     if (t >= i)
87     {
88       t-=i;
89       year++;
90       continue;
91     }
92     i=daypmon[month]*60*60*24;
93     if (t >= i)
94     {
95       t-=i;
96       month++;
97       continue;
98     }
99     i=60*60*24;
100     if (t >= i)
101     {
102       t-=i;
103       day++;
104       continue;
105     }
106     i=60*60;
107     if (t >= i)
108     {
109       t-=i;
110       hour++;
111       continue;
112     }
113     i=60;
114     if (t >= i)
115     {
116       t-=i;
117       minute++;
118       continue;
119     }
120     seconds=t;
121     t=0;
122   }
123 
124   sprintf(s,"%s %ld, %ld   %ld:%ld:%ld",monstr[month],day,year,hour,minute,seconds);
125 
126   return s;
127 }
128 #endif
129 
130 unsigned long
131 pram_readtime(void)
132 {
133    unsigned long	timedata;
134 
135 #ifdef MRG_ADB
136    if (0 == jClkNoMem)
137 	timedata = 0;	/* cause comparision of MacOS boottime */
138 			/* and PRAM time to fail */
139    else
140 #endif
141 	timedata = getPramTime();
142 #if DEBUG
143    printf("time read from PRAM: 0x%lx\n", timedata);
144    printf("Date and time: %s\n",convtime(timedata));
145 #endif
146 
147    return(timedata);
148 }
149 
150 void
151 pram_settime(unsigned long curtime)
152 {
153 #ifdef MRG_ADB
154    if (0 == jClkNoMem)
155 	return;
156    else
157 #endif
158 	return setPramTime(curtime);
159 }
160 
161 #ifndef MRG_ADB
162 /*
163  * These functions are defined here only if we are not using
164  * the MRG method of accessing the ADB/PRAM/RTC.
165  */
166 
167 extern int adbHardware;	/* from adb_direct.c */
168 
169 /*
170  * getPramTime
171  * This function can be called regrardless of the machine
172  * type. It calls the correct hardware-specific code.
173  * (It's sort of redundant with the above, but it was
174  * added later.)
175  */
176 unsigned long
177 getPramTime(void)
178 {
179 	unsigned long curtime;
180 
181 	switch (adbHardware) {
182 	case ADB_HW_IOP:
183 	case ADB_HW_II:		/* access PRAM via VIA interface */
184 		curtime = (long)getPramTimeII();
185 		return curtime;
186 
187 	case ADB_HW_IISI:	/* access PRAM via pseudo-adb functions */
188 	case ADB_HW_CUDA:
189 		if (0 != adb_read_date_time(&curtime))
190 			return 0;
191 		else
192 			return curtime;
193 
194 	case ADB_HW_PB:		/* don't know how to access this yet */
195 		return 0;
196 
197 	case ADB_HW_UNKNOWN:
198 	default:
199 		return 0;
200 	}
201 }
202 
203 /*
204  * setPramTime
205  * This function can be called regrardless of the machine
206  * type. It calls the correct hardware-specific code.
207  * (It's sort of redundant with the above, but it was
208  * added later.)
209  */
210 void
211 setPramTime(unsigned long curtime)
212 {
213 	switch (adbHardware) {
214 	case ADB_HW_IOP:
215 	case ADB_HW_II:		/* access PRAM via ADB interface */
216 		setPramTimeII(curtime);
217 		return;
218 
219 	case ADB_HW_IISI:	/* access PRAM via pseudo-adb functions */
220 	case ADB_HW_CUDA:
221 		adb_set_date_time(curtime);
222 		return;
223 
224 	case ADB_HW_PB:		/* don't know how to access this yet */
225 		return;
226 
227 	case ADB_HW_UNKNOWN:
228 		return;
229 	}
230 }
231 
232 #endif  /* !MRG_ADB */
233