ReactOS 0.4.16-dev-2207-geb15453
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
36static
40{
42}
43
45static
46VOID
47HalpWriteNvram(
50{
54}
55
57static
59HalpRtcReadByte(VOID)
60{
61 UCHAR i;
62 UCHAR Byte = 0;
63
64 /* Read byte from single wire bus */
65 for (i = 0; i < 8; i++)
66 {
67 Byte |= (__inbyte(PPI_IO_i_PORT_B) & 1) << i;
68
71
74 }
75
76 return Byte;
77}
78
80static
81VOID
82HalpRtcWriteBit(
83 _In_ UCHAR Bit)
84{
85 Bit = (Bit & 1) << 5;
86
89
92}
93
95static
96VOID
97HalpRtcWriteCommand(
99{
100 UCHAR i;
101
102 for (i = 0; i < 4; i++)
103 HalpRtcWriteBit(Command >> i);
104
107
110}
111
112UCHAR
113NTAPI
115 _In_ UCHAR Reg)
116{
117 /* Not supported by hardware */
118 return 0;
119}
120
121VOID
122NTAPI
124 _In_ UCHAR Reg,
126{
127 /* Not supported by hardware */
128 NOTHING;
129}
130
131ULONG
132NTAPI
138{
139 /* Not supported by hardware */
140 return 0;
141}
142
143ULONG
144NTAPI
150{
151 /* Not supported by hardware */
152 return 0;
153}
154
155CODE_SEG("INIT")
156VOID
157NTAPI
159{
161
162 /* TODO: Detect TVRAM address */
163 if (TRUE)
165 else
168}
169
170/* PUBLIC FUNCTIONS **********************************************************/
171
173NTAPI
175 _In_ PCH Name,
178{
179 UCHAR Val;
180
181 /* Only variable supported on x86 */
182 if (_stricmp(Name, "LastKnownGood"))
183 return ENOENT;
184
185 if (!MappedNvram)
186 return ENOENT;
187
189
191
193
194 /* Check the flag */
195 if (Val)
196 strncpy(Value, "FALSE", ValueLength);
197 else
198 strncpy(Value, "TRUE", ValueLength);
199
200 return ESUCCESS;
201}
202
204NTAPI
206 _In_ PCH Name,
207 _In_ PCH Value)
208{
209 UCHAR Val;
210
211 /* Only variable supported on x86 */
212 if (_stricmp(Name, "LastKnownGood"))
213 return ENOMEM;
214
215 if (!MappedNvram)
216 return ENOMEM;
217
218 /* Check if this is true or false */
219 if (!_stricmp(Value, "TRUE"))
220 {
222
224 }
225 else if (!_stricmp(Value, "FALSE"))
226 {
228
229 Val = HalpReadNvram(NVRAM_UNUSED_REG) & ~NVRAM_UNUSED_BIT;
230 }
231 else
232 {
233 /* Fail */
234 return ENOMEM;
235 }
236
237 HalpWriteNvram(NVRAM_UNUSED_REG, Val);
238
240
241 return ESUCCESS;
242}
243
245NTAPI
248{
249 UCHAR Temp;
250
252
253 HalpRtcWriteCommand(RTC_CMD_TIME_READ);
254 HalpRtcWriteCommand(RTC_CMD_REGISTER_SHIFT);
256
257 /* Set the time data */
258 Time->Second = BCD_INT(HalpRtcReadByte());
259 Time->Minute = BCD_INT(HalpRtcReadByte());
260 Time->Hour = BCD_INT(HalpRtcReadByte());
261 Time->Day = BCD_INT(HalpRtcReadByte());
262 Temp = HalpRtcReadByte();
263 Time->Weekday = Temp & 0x0F;
264 Time->Month = Temp >> 4;
265 Time->Year = BCD_INT(HalpRtcReadByte());
266 Time->Milliseconds = 0;
267
268 Time->Year += (Time->Year >= 80) ? 1900 : 2000;
269
270 HalpRtcWriteCommand(RTC_CMD_REGISTER_HOLD);
271
273
274 return TRUE;
275}
276
278NTAPI
281{
282 UCHAR i, j;
283 UCHAR SysTime[6];
284
286
287 HalpRtcWriteCommand(RTC_CMD_REGISTER_SHIFT);
288
289 SysTime[0] = INT_BCD(Time->Second);
290 SysTime[1] = INT_BCD(Time->Minute);
291 SysTime[2] = INT_BCD(Time->Hour);
292 SysTime[3] = INT_BCD(Time->Day);
293 SysTime[4] = (Time->Month << 4) | (Time->Weekday & 0x0F);
294 SysTime[5] = INT_BCD(Time->Year % 100);
295
296 /* Write time fields to RTC */
297 for (i = 0; i < 6; i++)
298 {
299 for (j = 0; j < 8; j++)
300 HalpRtcWriteBit(SysTime[i] >> j);
301 }
302
303 HalpRtcWriteCommand(RTC_CMD_TIME_SET_COUNTER_HOLD);
304 HalpRtcWriteCommand(RTC_CMD_REGISTER_HOLD);
305
307
308 return TRUE;
309}
#define CODE_SEG(...)
unsigned char BOOLEAN
#define _stricmp
Definition: cat.c:22
Definition: bufpool.h:45
#define _Requires_lock_held_(lock)
LPWSTR Name
Definition: desk.c:124
#define TRUE
Definition: types.h:120
unsigned char Byte
Definition: zlib.h:37
#define ENOENT
Definition: errno.h:25
#define ENOMEM
Definition: errno.h:35
#define ULONG_PTR
Definition: config.h:101
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
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
BOOLEAN NTAPI HalQueryRealTimeClock(OUT PTIME_FIELDS Time)
Definition: cmos.c:260
ARC_STATUS NTAPI HalGetEnvironmentVariable(_In_ PCH Name, _In_ USHORT ValueLength, _Out_writes_z_(ValueLength) PCH Value)
Definition: cmos.c:176
ULONG NTAPI HalpSetCmosData(_In_ ULONG BusNumber, _In_ ULONG SlotNumber, _In_reads_bytes_(Length) PVOID Buffer, _In_ ULONG Length)
Definition: cmos.c:104
VOID NTAPI HalpInitializeCmos(VOID)
Definition: cmos.c:160
BOOLEAN NTAPI HalSetRealTimeClock(IN PTIME_FIELDS Time)
Definition: cmos.c:295
ARC_STATUS NTAPI HalSetEnvironmentVariable(IN PCH Name, IN PCH Value)
Definition: cmos.c:216
ULONG NTAPI HalpGetCmosData(_In_ ULONG BusNumber, _In_ ULONG SlotNumber, _Out_writes_bytes_(Length) PVOID Buffer, _In_ ULONG Length)
Definition: cmos.c:49
PVOID NTAPI HalpMapPhysicalMemory64(IN PHYSICAL_ADDRESS PhysicalAddress, IN PFN_COUNT PageCount)
Definition: memory.c:140
KSPIN_LOCK HalpSystemHardwareLock
Definition: spinlock.c:25
VOID NTAPI HalpReleaseCmosSpinLock(VOID)
Definition: spinlock.c:243
VOID NTAPI HalpAcquireCmosSpinLock(VOID)
Definition: spinlock.c:226
#define NVRAM_UNUSED_REG
Definition: cmos.c:29
static UCHAR HalpReadNvram(_In_ UCHAR Register)
Definition: cmos.c:38
#define NVRAM_UNUSED_BIT
Definition: cmos.c:30
#define NVRAM_START
Definition: cmos.c:27
VOID NTAPI HalpWriteCmos(_In_ UCHAR Reg, _In_ UCHAR Value)
Definition: cmos.c:123
UCHAR NTAPI HalpReadCmos(_In_ UCHAR Reg)
Definition: cmos.c:114
static ULONG_PTR MappedNvram
Definition: cmos.c:32
#define NVRAM_SIZE
Definition: cmos.c:28
FORCEINLINE UCHAR INT_BCD(_In_ CSHORT Int)
Definition: halp.h:80
#define NOTHING
Definition: input_list.c:10
PPC_QUAL unsigned char __inbyte(const unsigned long Port)
Definition: intrin_ppc.h:539
PPC_QUAL void __outbyte(unsigned long const Port, const unsigned char Data)
Definition: intrin_ppc.h:605
#define KeStallExecutionProcessor(MicroSeconds)
Definition: precomp.h:27
static PLARGE_INTEGER Time
Definition: time.c:105
#define _In_reads_bytes_(s)
Definition: no_sal2.h:170
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define _Out_writes_bytes_(s)
Definition: no_sal2.h:178
CHAR * PCH
Definition: ntbasedef.h:403
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
static UCHAR BCD_INT(_In_ UCHAR Bcd)
Definition: hwinfo.c:15
unsigned short USHORT
Definition: pedump.c:61
#define RTC_STROBE
Definition: rtc.h:14
#define RTC_CMD_TIME_READ
Definition: rtc.h:19
#define RTC_IO_o_DATA
Definition: rtc.h:10
#define RTC_CMD_SERIAL_TRANSFER_MODE
Definition: rtc.h:23
#define RTC_CMD_REGISTER_HOLD
Definition: rtc.h:16
#define RTC_CMD_TIME_SET_COUNTER_HOLD
Definition: rtc.h:18
#define RTC_CLOCK
Definition: rtc.h:13
#define RTC_CMD_REGISTER_SHIFT
Definition: rtc.h:17
@ ESUCCESS
Definition: arc.h:32
ULONG ARC_STATUS
Definition: arc.h:4
#define GDC1_IO_o_MODE_FLIPFLOP1
Definition: video.h:238
#define GDC1_NVRAM_PROTECT
Definition: video.h:251
#define GDC1_NVRAM_UNPROTECT
Definition: video.h:252
#define VRAM_NORMAL_TEXT
Definition: video.h:17
#define VRAM_HI_RESO_TEXT
Definition: video.h:32
strncpy
Definition: string.h:335
Definition: shell.h:41
#define PPI_IO_i_PORT_B
Definition: sysport.h:19
#define NTAPI
Definition: typedefs.h:36
uint32_t ULONG_PTR
Definition: typedefs.h:65
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
LONGLONG QuadPart
Definition: typedefs.h:114
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _In_ ULONG ValueLength
Definition: wdfregistry.h:275
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
_In_ WDFIORESREQLIST _In_ ULONG SlotNumber
Definition: wdfresource.h:68
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE _In_ ULONG BusNumber
Definition: halfuncs.h:160
NTKERNELAPI VOID NTAPI WRITE_REGISTER_UCHAR(IN PUCHAR Register, IN UCHAR Value)
NTKERNELAPI UCHAR NTAPI READ_REGISTER_UCHAR(IN PUCHAR Register)
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS PhysicalAddress
Definition: iotypes.h:1098
#define BYTES_TO_PAGES(Size)
unsigned char UCHAR
Definition: xmlstorage.h:181