ReactOS  0.4.15-dev-2106-g6de3300
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 /* GLOBALS *******************************************************************/
16 
17 /*
18  * The PC-98 hardware maps data from the NVRAM directly into the text video
19  * memory address space. Every fourth byte is a "writable data".
20  *
21  * |0x2FE2|0x2FE3|0x2FE4|0x2FE5|0x2FE6|0x2FE7| .... |0x2FFD|0x2FFE|
22  * | D | | | | D | | .... | | D |
23  *
24  * Most of these bits of the NVRAM are already used. There are some reserved
25  * bits in the 0x3FE6 and 0x3FFE that we can use.
26  */
27 #define NVRAM_START 0x3FE2
28 #define NVRAM_SIZE 0x1C
29 #define NVRAM_UNUSED_REG 0x14
30 #define NVRAM_UNUSED_BIT 0x80
31 
33 
34 /* PRIVATE FUNCTIONS *********************************************************/
35 
36 /* Avoid double calls */
37 #undef BCD_INT
38 static UCHAR
40  _In_ UCHAR Bcd)
41 {
42  return ((Bcd & 0xF0) >> 4) * 10 + (Bcd & 0x0F);
43 }
44 
45 static UCHAR
46 NTAPI
48  _In_ UCHAR Register)
49 {
50  return READ_REGISTER_UCHAR((PUCHAR)(MappedNvram + Register));
51 }
52 
54 static VOID
55 NTAPI
56 HalpWriteNvram(
57  _In_ UCHAR Register,
59 {
63 }
64 
66 static UCHAR
67 NTAPI
68 HalpRtcReadByte(VOID)
69 {
70  UCHAR i;
71  UCHAR Byte = 0;
72 
73  /* Read byte from single wire bus */
74  for (i = 0; i < 8; i++)
75  {
76  Byte |= (__inbyte(PPI_IO_i_PORT_B) & 1) << i;
77 
80 
83  }
84 
85  return Byte;
86 }
87 
89 static VOID
90 NTAPI
91 HalpRtcWriteBit(
92  _In_ UCHAR Bit)
93 {
94  Bit = (Bit & 1) << 5;
95 
98 
101 }
102 
104 static VOID
105 NTAPI
106 HalpRtcWriteCommand(
108 {
109  UCHAR i;
110 
111  for (i = 0; i < 4; i++)
112  HalpRtcWriteBit(Command >> i);
113 
116 
119 }
120 
121 UCHAR
122 NTAPI
124  _In_ UCHAR Reg)
125 {
126  /* Not supported by hardware */
127  return 0;
128 }
129 
130 VOID
131 NTAPI
133  _In_ UCHAR Reg,
134  _In_ UCHAR Value)
135 {
136  /* Not supported by hardware */
137  NOTHING;
138 }
139 
140 ULONG
141 NTAPI
146  _In_ ULONG Length)
147 {
148  /* Not supported by hardware */
149  return 0;
150 }
151 
152 ULONG
153 NTAPI
158  _In_ ULONG Length)
159 {
160  /* Not supported by hardware */
161  return 0;
162 }
163 
164 CODE_SEG("INIT")
165 VOID
166 NTAPI
168 {
170 
171  /* TODO: Detect TVRAM address */
172  if (TRUE)
174  else
177 }
178 
179 /* PUBLIC FUNCTIONS **********************************************************/
180 
182 NTAPI
184  _In_ PCH Name,
187 {
188  UCHAR Val;
189 
190  /* Only variable supported on x86 */
191  if (_stricmp(Name, "LastKnownGood"))
192  return ENOENT;
193 
194  if (!MappedNvram)
195  return ENOENT;
196 
198 
200 
202 
203  /* Check the flag */
204  if (Val)
205  strncpy(Value, "FALSE", ValueLength);
206  else
207  strncpy(Value, "TRUE", ValueLength);
208 
209  return ESUCCESS;
210 }
211 
213 NTAPI
215  _In_ PCH Name,
216  _In_ PCH Value)
217 {
218  UCHAR Val;
219 
220  /* Only variable supported on x86 */
221  if (_stricmp(Name, "LastKnownGood"))
222  return ENOMEM;
223 
224  if (!MappedNvram)
225  return ENOMEM;
226 
227  /* Check if this is true or false */
228  if (!_stricmp(Value, "TRUE"))
229  {
231 
233  }
234  else if (!_stricmp(Value, "FALSE"))
235  {
237 
239  }
240  else
241  {
242  /* Fail */
243  return ENOMEM;
244  }
245 
246  HalpWriteNvram(NVRAM_UNUSED_REG, Val);
247 
249 
250  return ESUCCESS;
251 }
252 
253 BOOLEAN
254 NTAPI
257 {
258  UCHAR Temp;
259 
261 
262  HalpRtcWriteCommand(RTC_CMD_TIME_READ);
263  HalpRtcWriteCommand(RTC_CMD_REGISTER_SHIFT);
265 
266  /* Set the time data */
267  Time->Second = BCD_INT(HalpRtcReadByte());
268  Time->Minute = BCD_INT(HalpRtcReadByte());
269  Time->Hour = BCD_INT(HalpRtcReadByte());
270  Time->Day = BCD_INT(HalpRtcReadByte());
271  Temp = HalpRtcReadByte();
272  Time->Weekday = Temp & 0x0F;
273  Time->Month = Temp >> 4;
274  Time->Year = BCD_INT(HalpRtcReadByte());
275  Time->Milliseconds = 0;
276 
277  Time->Year += (Time->Year >= 80) ? 1900 : 2000;
278 
279  HalpRtcWriteCommand(RTC_CMD_REGISTER_HOLD);
280 
282 
283  return TRUE;
284 }
285 
286 BOOLEAN
287 NTAPI
290 {
291  UCHAR i, j;
292  UCHAR SysTime[6];
293 
295 
296  HalpRtcWriteCommand(RTC_CMD_REGISTER_SHIFT);
297 
298  SysTime[0] = INT_BCD(Time->Second);
299  SysTime[1] = INT_BCD(Time->Minute);
300  SysTime[2] = INT_BCD(Time->Hour);
301  SysTime[3] = INT_BCD(Time->Day);
302  SysTime[4] = (Time->Month << 4) | (Time->Weekday & 0x0F);
303  SysTime[5] = INT_BCD(Time->Year % 100);
304 
305  /* Write time fields to RTC */
306  for (i = 0; i < 6; i++)
307  {
308  for (j = 0; j < 8; j++)
309  HalpRtcWriteBit(SysTime[i] >> j);
310  }
311 
312  HalpRtcWriteCommand(RTC_CMD_TIME_SET_COUNTER_HOLD);
313  HalpRtcWriteCommand(RTC_CMD_REGISTER_HOLD);
314 
316 
317  return TRUE;
318 }
_In_ WDFIORESREQLIST _In_ ULONG SlotNumber
Definition: wdfresource.h:65
#define RTC_IO_o_DATA
Definition: rtc.h:10
VOID FORCEINLINE HalpWriteCmos(IN UCHAR Reg, IN UCHAR Value)
Definition: cmos.c:35
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define _In_reads_bytes_(s)
Definition: no_sal2.h:170
#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:39
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
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define GDC1_NVRAM_PROTECT
Definition: video.h:251
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define _Out_writes_bytes_(s)
Definition: no_sal2.h:178
#define RTC_CLOCK
Definition: rtc.h:13
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:27
CHAR * PCH
Definition: ntbasedef.h:391
#define RTC_CMD_TIME_SET_COUNTER_HOLD
Definition: rtc.h:18
unsigned char BOOLEAN
#define GDC1_NVRAM_UNPROTECT
Definition: video.h:252
#define _Out_
Definition: no_sal2.h:160
UCHAR FORCEINLINE HalpReadCmos(IN UCHAR Reg)
Definition: cmos.c:24
Definition: bufpool.h:45
static UCHAR NTAPI HalpReadNvram(_In_ UCHAR Register)
Definition: cmos.c:47
_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:22
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
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:406
static ULONG_PTR MappedNvram
Definition: cmos.c:32
BOOLEAN NTAPI HalSetRealTimeClock(IN PTIME_FIELDS Time)
Definition: cmos.c:267
VOID NTAPI HalpAcquireCmosSpinLock(VOID)
Definition: spinlock.c:227
CODE_SEG("INIT")
Definition: fsrtlpc.c:19
#define RTC_CMD_REGISTER_HOLD
Definition: rtc.h:16
#define NVRAM_UNUSED_REG
Definition: cmos.c:29
#define BYTES_TO_PAGES(Size)
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS PhysicalAddress
Definition: iotypes.h:1098
unsigned char UCHAR
Definition: xmlstorage.h:181
#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
NTKERNELAPI UCHAR NTAPI READ_REGISTER_UCHAR(IN PUCHAR Register)
#define _In_
Definition: no_sal2.h:158
#define RTC_CMD_REGISTER_SHIFT
Definition: rtc.h:17
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
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:28
VOID NTAPI HalpInitializeCmos(VOID)
Definition: cmos.c:159
#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:30
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
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _In_ ULONG ValueLength
Definition: wdfregistry.h:271