ReactOS  0.4.15-dev-1177-g6cb3b62
cmos.c
Go to the documentation of this file.
1 /*
2  * PROJECT: NEC PC-98 series HAL
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: RTC and NVRAM access routines
5  * COPYRIGHT: Copyright 2020 Dmitry Borisov (di.sean@protonmail.com)
6  */
7 
8 /* INCLUDES ******************************************************************/
9 
10 #include <hal.h>
11 
12 #define NDEBUG
13 #include <debug.h>
14 
15 #if defined(ALLOC_PRAGMA) && !defined(_MINIHAL_)
16 #pragma alloc_text(INIT, HalpInitializeCmos)
17 #endif
18 
19 /* GLOBALS *******************************************************************/
20 
21 /*
22  * The PC-98 hardware maps data from the NVRAM directly into the text video
23  * memory address space. Every fourth byte is a "writable data".
24  *
25  * |0x2FE2|0x2FE3|0x2FE4|0x2FE5|0x2FE6|0x2FE7| .... |0x2FFD|0x2FFE|
26  * | D | | | | D | | .... | | D |
27  *
28  * Most of these bits of the NVRAM are already used. There are some reserved
29  * bits in the 0x3FE6 and 0x3FFE that we can use.
30  */
31 #define NVRAM_START 0x3FE2
32 #define NVRAM_SIZE 0x1C
33 #define NVRAM_UNUSED_REG 0x14
34 #define NVRAM_UNUSED_BIT 0x80
35 
37 
38 /* PRIVATE FUNCTIONS *********************************************************/
39 
40 /* Avoid double calls */
41 #undef BCD_INT
42 static UCHAR
44  _In_ UCHAR Bcd)
45 {
46  return ((Bcd & 0xF0) >> 4) * 10 + (Bcd & 0x0F);
47 }
48 
49 static UCHAR
50 NTAPI
52  _In_ UCHAR Register)
53 {
54  return READ_REGISTER_UCHAR((PUCHAR)(MappedNvram + Register));
55 }
56 
58 static VOID
59 NTAPI
60 HalpWriteNvram(
61  _In_ UCHAR Register,
63 {
67 }
68 
70 static UCHAR
71 NTAPI
72 HalpRtcReadByte(VOID)
73 {
74  UCHAR i;
75  UCHAR Byte = 0;
76 
77  /* Read byte from single wire bus */
78  for (i = 0; i < 8; i++)
79  {
80  Byte |= (__inbyte(PPI_IO_i_PORT_B) & 1) << i;
81 
84 
87  }
88 
89  return Byte;
90 }
91 
93 static VOID
94 NTAPI
95 HalpRtcWriteBit(
96  _In_ UCHAR Bit)
97 {
98  Bit = (Bit & 1) << 5;
99 
102 
105 }
106 
108 static VOID
109 NTAPI
110 HalpRtcWriteCommand(
112 {
113  UCHAR i;
114 
115  for (i = 0; i < 4; i++)
116  HalpRtcWriteBit(Command >> i);
117 
120 
123 }
124 
125 UCHAR
126 NTAPI
128  _In_ UCHAR Reg)
129 {
130  /* Not supported by hardware */
131  return 0;
132 }
133 
134 VOID
135 NTAPI
137  _In_ UCHAR Reg,
138  _In_ UCHAR Value)
139 {
140  /* Not supported by hardware */
141  NOTHING;
142 }
143 
144 ULONG
145 NTAPI
150  _In_ ULONG Length)
151 {
152  /* Not supported by hardware */
153  return 0;
154 }
155 
156 ULONG
157 NTAPI
162  _In_ ULONG Length)
163 {
164  /* Not supported by hardware */
165  return 0;
166 }
167 
168 INIT_FUNCTION
169 VOID
170 NTAPI
172 {
174 
175  /* TODO: Detect TVRAM address */
176  if (TRUE)
178  else
181 }
182 
183 /* PUBLIC FUNCTIONS **********************************************************/
184 
186 NTAPI
188  _In_ PCH Name,
191 {
192  UCHAR Val;
193 
194  /* Only variable supported on x86 */
195  if (_stricmp(Name, "LastKnownGood"))
196  return ENOENT;
197 
198  if (!MappedNvram)
199  return ENOENT;
200 
202 
204 
206 
207  /* Check the flag */
208  if (Val)
209  strncpy(Value, "FALSE", ValueLength);
210  else
211  strncpy(Value, "TRUE", ValueLength);
212 
213  return ESUCCESS;
214 }
215 
217 NTAPI
219  _In_ PCH Name,
220  _In_ PCH Value)
221 {
222  UCHAR Val;
223 
224  /* Only variable supported on x86 */
225  if (_stricmp(Name, "LastKnownGood"))
226  return ENOMEM;
227 
228  if (!MappedNvram)
229  return ENOMEM;
230 
231  /* Check if this is true or false */
232  if (!_stricmp(Value, "TRUE"))
233  {
235 
237  }
238  else if (!_stricmp(Value, "FALSE"))
239  {
241 
243  }
244  else
245  {
246  /* Fail */
247  return ENOMEM;
248  }
249 
250  HalpWriteNvram(NVRAM_UNUSED_REG, Val);
251 
253 
254  return ESUCCESS;
255 }
256 
257 BOOLEAN
258 NTAPI
261 {
262  UCHAR Temp;
263 
265 
266  HalpRtcWriteCommand(RTC_CMD_TIME_READ);
267  HalpRtcWriteCommand(RTC_CMD_REGISTER_SHIFT);
269 
270  /* Set the time data */
271  Time->Second = BCD_INT(HalpRtcReadByte());
272  Time->Minute = BCD_INT(HalpRtcReadByte());
273  Time->Hour = BCD_INT(HalpRtcReadByte());
274  Time->Day = BCD_INT(HalpRtcReadByte());
275  Temp = HalpRtcReadByte();
276  Time->Weekday = Temp & 0x0F;
277  Time->Month = Temp >> 4;
278  Time->Year = BCD_INT(HalpRtcReadByte());
279  Time->Milliseconds = 0;
280 
281  Time->Year += (Time->Year >= 80) ? 1900 : 2000;
282 
283  HalpRtcWriteCommand(RTC_CMD_REGISTER_HOLD);
284 
286 
287  return TRUE;
288 }
289 
290 BOOLEAN
291 NTAPI
294 {
295  UCHAR i, j;
296  UCHAR SysTime[6];
297 
299 
300  HalpRtcWriteCommand(RTC_CMD_REGISTER_SHIFT);
301 
302  SysTime[0] = INT_BCD(Time->Second);
303  SysTime[1] = INT_BCD(Time->Minute);
304  SysTime[2] = INT_BCD(Time->Hour);
305  SysTime[3] = INT_BCD(Time->Day);
306  SysTime[4] = (Time->Month << 4) | (Time->Weekday & 0x0F);
307  SysTime[5] = INT_BCD(Time->Year % 100);
308 
309  /* Write time fields to RTC */
310  for (i = 0; i < 6; i++)
311  {
312  for (j = 0; j < 8; j++)
313  HalpRtcWriteBit(SysTime[i] >> j);
314  }
315 
316  HalpRtcWriteCommand(RTC_CMD_TIME_SET_COUNTER_HOLD);
317  HalpRtcWriteCommand(RTC_CMD_REGISTER_HOLD);
318 
320 
321  return TRUE;
322 }
#define RTC_IO_o_DATA
Definition: rtc.h:10
VOID FORCEINLINE HalpWriteCmos(IN UCHAR Reg, IN UCHAR Value)
Definition: cmos.c:35
#define RTC_CMD_SERIAL_TRANSFER_MODE
Definition: rtc.h:23
Definition: arc.h:32
#define TRUE
Definition: types.h:120
#define GDC1_IO_o_MODE_FLIPFLOP1
Definition: video.h:238
#define VRAM_HI_RESO_TEXT
Definition: video.h:32
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
unsigned char * PUCHAR
Definition: retypes.h:3
static UCHAR BCD_INT(_In_ UCHAR Bcd)
Definition: cmos.c:43
ULONG ARC_STATUS
Definition: arc.h:4
PPC_QUAL void __outbyte(unsigned long const Port, const unsigned char Data)
Definition: intrin_ppc.h:605
#define RTC_STROBE
Definition: rtc.h:14
Definition: shell.h:41
ULONG NTAPI HalpGetCmosData(IN ULONG BusNumber, IN ULONG SlotNumber, IN PVOID Buffer, IN ULONG Length)
Definition: cmos.c:47
#define _stricmp
Definition: cat.c:22
Definition: arc.h:48
IN UCHAR Value
Definition: halp.h:394
#define _Out_writes_z_(size)
Definition: no_sal2.h:384
#define GDC1_NVRAM_PROTECT
Definition: video.h:251
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define RTC_CLOCK
Definition: rtc.h:13
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
VOID NTAPI HalpReleaseCmosSpinLock(VOID)
Definition: spinlock.c:244
PVOID NTAPI HalpMapPhysicalMemory64(IN PHYSICAL_ADDRESS PhysicalAddress, IN ULONG NumberPage)
Definition: misc.c:28
#define NVRAM_START
Definition: cmos.c:31
INIT_FUNCTION VOID NTAPI HalpInitializeCmos(VOID)
Definition: cmos.c:163
CHAR * PCH
Definition: ntbasedef.h:392
#define RTC_CMD_TIME_SET_COUNTER_HOLD
Definition: rtc.h:18
unsigned char BOOLEAN
#define GDC1_NVRAM_UNPROTECT
Definition: video.h:252
#define _Out_writes_bytes_(size)
Definition: no_sal2.h:370
#define _Out_
Definition: no_sal2.h:323
UCHAR FORCEINLINE HalpReadCmos(IN UCHAR Reg)
Definition: cmos.c:24
Definition: bufpool.h:45
static UCHAR NTAPI HalpReadNvram(_In_ UCHAR Register)
Definition: cmos.c:51
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE _In_ ULONG BusNumber
Definition: halfuncs.h:156
_Requires_lock_held_(HalpSystemHardwareLock)
Definition: cmos.c:26
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
static ULONG_PTR MappedNvram
Definition: cmos.c:36
BOOLEAN NTAPI HalSetRealTimeClock(IN PTIME_FIELDS Time)
Definition: cmos.c:267
VOID NTAPI HalpAcquireCmosSpinLock(VOID)
Definition: spinlock.c:227
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define RTC_CMD_REGISTER_HOLD
Definition: rtc.h:16
#define NVRAM_UNUSED_REG
Definition: cmos.c:33
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE _In_ ULONG _In_ ULONG SlotNumber
Definition: halfuncs.h:156
#define BYTES_TO_PAGES(Size)
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS PhysicalAddress
Definition: iotypes.h:1081
unsigned char UCHAR
Definition: xmlstorage.h:181
_In_ GUID _In_ PVOID _In_ ULONG ValueLength
Definition: hubbusif.h:311
#define VRAM_NORMAL_TEXT
Definition: video.h:17
#define NOTHING
Definition: env_spec_w32.h:461
#define RTC_CMD_TIME_READ
Definition: rtc.h:19
#define _In_reads_bytes_(size)
Definition: no_sal2.h:229
NTKERNELAPI UCHAR NTAPI READ_REGISTER_UCHAR(IN PUCHAR Register)
#define _In_
Definition: no_sal2.h:204
#define RTC_CMD_REGISTER_SHIFT
Definition: rtc.h:17
unsigned short USHORT
Definition: pedump.c:61
ARC_STATUS NTAPI HalSetEnvironmentVariable(IN PCH Name, IN PCH Value)
Definition: cmos.c:193
ARC_STATUS NTAPI HalGetEnvironmentVariable(IN PCH Name, IN USHORT ValueLength, IN PCH Value)
Definition: cmos.c:156
Definition: arc.h:46
BOOLEAN NTAPI HalQueryRealTimeClock(OUT PTIME_FIELDS Time)
Definition: cmos.c:234
KSPIN_LOCK HalpSystemHardwareLock
Definition: cmos.c:18
unsigned char Byte
Definition: zlib.h:37
#define PPI_IO_i_PORT_B
Definition: sysport.h:19
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
#define NVRAM_SIZE
Definition: cmos.c:32
#define INT_BCD(int)
Definition: halp.h:31
NTKERNELAPI VOID NTAPI WRITE_REGISTER_UCHAR(IN PUCHAR Register, IN UCHAR Value)
VOID NTAPI KeStallExecutionProcessor(IN ULONG MicroSeconds)
Definition: ntoskrnl.c:95
static PLARGE_INTEGER Time
Definition: time.c:105
#define NVRAM_UNUSED_BIT
Definition: cmos.c:34
ULONG NTAPI HalpSetCmosData(IN ULONG BusNumber, IN ULONG SlotNumber, IN PVOID Buffer, IN ULONG Length)
Definition: cmos.c:99
LONGLONG QuadPart
Definition: typedefs.h:114
PPC_QUAL unsigned char __inbyte(const unsigned long Port)
Definition: intrin_ppc.h:539