xref: /netbsd-src/sys/arch/mac68k/mac68k/pram.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: pram.c,v 1.23 2014/03/26 17:46:04 christos 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.23 2014/03/26 17:46:04 christos 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   snprintf(s, sizeof(s), "%s %ld, %ld   %ld:%ld:%ld",
125       monstr[month], day, year, hour, minute, seconds);
126 
127   return s;
128 }
129 #endif
130 
131 unsigned long
132 pram_readtime(void)
133 {
134    unsigned long	timedata;
135 
136 #ifdef MRG_ADB
137    if (0 == jClkNoMem)
138 	timedata = 0;	/* cause comparision of MacOS boottime */
139 			/* and PRAM time to fail */
140    else
141 #endif
142 	timedata = getPramTime();
143 #if DEBUG
144    printf("time read from PRAM: 0x%lx\n", timedata);
145    printf("Date and time: %s\n",convtime(timedata));
146 #endif
147 
148    return(timedata);
149 }
150 
151 void
152 pram_settime(unsigned long curtime)
153 {
154 #ifdef MRG_ADB
155    if (0 == jClkNoMem)
156 	return;
157    else
158 #endif
159 	return setPramTime(curtime);
160 }
161 
162 #ifndef MRG_ADB
163 /*
164  * These functions are defined here only if we are not using
165  * the MRG method of accessing the ADB/PRAM/RTC.
166  */
167 
168 extern int adbHardware;	/* from adb_direct.c */
169 
170 /*
171  * getPramTime
172  * This function can be called regrardless of the machine
173  * type. It calls the correct hardware-specific code.
174  * (It's sort of redundant with the above, but it was
175  * added later.)
176  */
177 unsigned long
178 getPramTime(void)
179 {
180 	unsigned long curtime;
181 
182 	switch (adbHardware) {
183 	case ADB_HW_IOP:
184 	case ADB_HW_II:		/* access PRAM via VIA interface */
185 		curtime = (long)getPramTimeII();
186 		return curtime;
187 
188 	case ADB_HW_IISI:	/* access PRAM via pseudo-adb functions */
189 	case ADB_HW_CUDA:
190 		if (0 != adb_read_date_time(&curtime))
191 			return 0;
192 		else
193 			return curtime;
194 
195 	case ADB_HW_PB:		/* don't know how to access this yet */
196 		return 0;
197 
198 	case ADB_HW_UNKNOWN:
199 	default:
200 		return 0;
201 	}
202 }
203 
204 /*
205  * setPramTime
206  * This function can be called regrardless of the machine
207  * type. It calls the correct hardware-specific code.
208  * (It's sort of redundant with the above, but it was
209  * added later.)
210  */
211 void
212 setPramTime(unsigned long curtime)
213 {
214 	switch (adbHardware) {
215 	case ADB_HW_IOP:
216 	case ADB_HW_II:		/* access PRAM via ADB interface */
217 		setPramTimeII(curtime);
218 		return;
219 
220 	case ADB_HW_IISI:	/* access PRAM via pseudo-adb functions */
221 	case ADB_HW_CUDA:
222 		adb_set_date_time(curtime);
223 		return;
224 
225 	case ADB_HW_PB:		/* don't know how to access this yet */
226 		return;
227 
228 	case ADB_HW_UNKNOWN:
229 		return;
230 	}
231 }
232 
233 #endif  /* !MRG_ADB */
234