xref: /netbsd-src/sys/arch/mac68k/mac68k/pram.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: pram.c,v 1.13 1997/04/14 16:56:33 scottr 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 "opt_mrg_adb.h"
37 
38 /* #include "stand.h"  */
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #ifdef DEBUG
42 #include <sys/systm.h>
43 #endif
44 #include <machine/viareg.h>
45 
46 #include <arch/mac68k/mac68k/pram.h>
47 #include <arch/mac68k/mac68k/macrom.h>
48 #ifndef MRG_ADB
49 #include <arch/mac68k/dev/adbvar.h>
50 #endif
51 
52 #if DEBUG
53 static char *convtime(unsigned long t)
54 {
55   static long daypmon[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
56   static char *monstr[] = {"January","February","March","April","May","June",
57     "July","August","September","October","November","December" };
58   static char s[200];
59   long year,month,day,hour,minute,seconds,i,dayperyear;
60 
61   year=1904;
62   month=0;  /* Jan */
63   day=1;
64   hour=0;
65   minute=0;
66   seconds=0;
67 
68   if(t == 0xffffffff)
69      return("<time value is -1>");
70 
71   while (t > 0)
72   {
73     if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
74     {
75       dayperyear=366;
76       daypmon[1]=29;
77     }
78     else
79     {
80       dayperyear=365;
81       daypmon[1]=28;
82     }
83     i=dayperyear*60*60*24;
84     if (t >= i)
85     {
86       t-=i;
87       year++;
88       continue;
89     }
90     i=daypmon[month]*60*60*24;
91     if (t >= i)
92     {
93       t-=i;
94       month++;
95       continue;
96     }
97     i=60*60*24;
98     if (t >= i)
99     {
100       t-=i;
101       day++;
102       continue;
103     }
104     i=60*60;
105     if (t >= i)
106     {
107       t-=i;
108       hour++;
109       continue;
110     }
111     i=60;
112     if (t >= i)
113     {
114       t-=i;
115       minute++;
116       continue;
117     }
118     seconds=t;
119     t=0;
120   }
121 
122   sprintf(s,"%s %ld, %ld   %ld:%ld:%ld",monstr[month],day,year,hour,minute,seconds);
123 
124   return s;
125 }
126 #endif
127 
128 unsigned long
129 pram_readtime(void)
130 {
131    unsigned long	timedata;
132 
133    if (0 == jClkNoMem)
134 	timedata = 0;	/* cause comparision of MacOS boottime */
135 			/* and PRAM time to fail */
136    else
137 	timedata = getPramTime();
138 #if DEBUG
139    printf("time read from PRAM: 0x%lx\n", timedata);
140    printf("Date and time: %s\n",convtime(timedata));
141 #endif
142 
143    return(timedata);
144 }
145 
146 void
147 pram_settime(unsigned long time)
148 {
149    if (0 == jClkNoMem)
150 	return;
151    else
152 	return setPramTime(time);
153 }
154 
155 #ifndef MRG_ADB
156 /*
157  * These functions are defined here only if we are not using
158  * the MRG method of accessing the ADB/PRAM/RTC.
159  */
160 
161 extern int adbHardware;	/* from newadb.c */
162 
163 /*
164  * getPramTime
165  * This function can be called regrardless of the machine
166  * type. It calls the correct hardware-specific code.
167  * (It's sort of redundant with the above, but it was
168  * added later.)
169  */
170 unsigned long
171 getPramTime(void)
172 {
173 	unsigned long time;
174 
175 	switch (adbHardware) {
176 	case ADB_HW_II:		/* access PRAM via VIA interface */
177 		time = (long)getPramTimeII();
178 		return time;
179 
180 	case ADB_HW_IISI:	/* access PRAM via pseudo-adb functions */
181 		if (0 != adb_read_date_time(&time))
182 			return 0;
183 		else
184 			return time;
185 
186 	case ADB_HW_PB:		/* don't know how to access this yet */
187 		return 0;
188 
189 	case ADB_HW_UNKNOWN:
190 	default:
191 		return 0;
192 	}
193 }
194 
195 /*
196  * setPramTime
197  * This function can be called regrardless of the machine
198  * type. It calls the correct hardware-specific code.
199  * (It's sort of redundant with the above, but it was
200  * added later.)
201  */
202 void
203 setPramTime(unsigned long time)
204 {
205 	switch (adbHardware) {
206 	case ADB_HW_II:		/* access PRAM via ADB interface */
207 		setPramTimeII(time);
208 		return;
209 
210 	case ADB_HW_IISI:	/* access PRAM via pseudo-adb functions */
211 		adb_set_date_time(time);
212 		return;
213 
214 	case ADB_HW_PB:		/* don't know how to access this yet */
215 		return;
216 
217 	case ADB_HW_UNKNOWN:
218 		return;
219 	}
220 }
221 
222 #endif  /* !MRG_ADB */
223