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