4 * Copyright (C) 2003, 2004 Eric Kohl
5 * Copyright (C) 2009 Hervé Poussineau
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 DBG_DEFAULT_CHANNEL(HWDETECT
);
33 #define CLOCK_TICK_RATE (1193182)
34 #define LATCH (CLOCK_TICK_RATE / HZ)
36 static unsigned int delay_count
= 1;
38 /* Used for BIOS disks pre-enumeration performed when detecting the boot devices in InitializeBootDevices() */
39 extern UCHAR PcBiosDiskCount
;
41 /* This function is slightly different in its PC and XBOX versions */
42 GET_HARDDISK_CONFIG_DATA GetHarddiskConfigurationData
= NULL
;
45 GetHarddiskIdentifier(UCHAR DriveNumber
);
47 /* FUNCTIONS *****************************************************************/
51 __StallExecutionProcessor(ULONG Loops
)
53 register volatile unsigned int i
;
54 for (i
= 0; i
< Loops
; i
++);
57 VOID
StallExecutionProcessor(ULONG Microseconds
)
59 ULONGLONG LoopCount
= ((ULONGLONG
)delay_count
* (ULONGLONG
)Microseconds
) / 1000ULL;
60 __StallExecutionProcessor((ULONG
)LoopCount
);
69 WRITE_PORT_UCHAR((PUCHAR
)0x43, 0x00);
70 Count
= READ_PORT_UCHAR((PUCHAR
)0x40);
71 Count
|= READ_PORT_UCHAR((PUCHAR
)0x40) << 8;
78 WaitFor8254Wraparound(VOID
)
84 CurCount
= Read8254Timer();
89 CurCount
= Read8254Timer();
90 Delta
= CurCount
- PrevCount
;
93 * This limit for delta seems arbitrary, but it isn't, it's
94 * slightly above the level of error a buggy Mercury/Neptune
95 * chipset timer can cause.
102 HalpCalibrateStallExecution(VOID
)
108 /* Initialise timer interrupt with MILLISECOND ms interval */
109 WRITE_PORT_UCHAR((PUCHAR
)0x43, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
110 WRITE_PORT_UCHAR((PUCHAR
)0x40, LATCH
& 0xff); /* LSB */
111 WRITE_PORT_UCHAR((PUCHAR
)0x40, LATCH
>> 8); /* MSB */
113 /* Stage 1: Coarse calibration */
119 /* Next delay count to try */
122 WaitFor8254Wraparound();
125 __StallExecutionProcessor(delay_count
);
127 CurCount
= Read8254Timer();
129 while (CurCount
> LATCH
/ 2);
131 /* Get bottom value for delay */
134 /* Stage 2: Fine calibration */
136 /* Which bit are we going to test */
137 calib_bit
= delay_count
;
139 for (i
= 0; i
< PRECISION
; i
++)
141 /* Next bit to calibrate */
144 /* If we have done all bits, stop */
145 if (!calib_bit
) break;
147 /* Set the bit in delay_count */
148 delay_count
|= calib_bit
;
150 WaitFor8254Wraparound();
153 __StallExecutionProcessor(delay_count
);
155 CurCount
= Read8254Timer();
156 /* If a tick has passed, turn the calibrated bit back off */
157 if (CurCount
<= LATCH
/ 2)
158 delay_count
&= ~calib_bit
;
161 /* We're finished: Do the finishing touches */
163 /* Calculate delay_count for 1ms */
164 delay_count
/= (MILLISEC
/ 2);
174 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
175 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
177 return ((Data
& 0xF0) ? 1 : 0) + ((Data
& 0x0F) ? 1 : 0);
182 GetFloppyType(UCHAR DriveNumber
)
186 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
187 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
189 if (DriveNumber
== 0)
191 else if (DriveNumber
== 1)
201 PUSHORT SegPtr
= (PUSHORT
)0x7A;
202 PUSHORT OfsPtr
= (PUSHORT
)0x78;
204 return (PVOID
)((ULONG_PTR
)(((ULONG
)(*SegPtr
)) << 4) + (ULONG
)(*OfsPtr
));
209 DetectBiosFloppyPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
211 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
212 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
213 PCM_FLOPPY_DEVICE_DATA FloppyData
;
215 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
219 ULONG MaxDensity
[6] = {0, 360, 1200, 720, 1440, 2880};
222 for (FloppyNumber
= 0; FloppyNumber
< 2; FloppyNumber
++)
224 FloppyType
= GetFloppyType(FloppyNumber
);
226 if ((FloppyType
> 5) || (FloppyType
== 0))
229 if (!DiskResetController(FloppyNumber
))
232 Ptr
= GetInt1eTable();
234 /* Set 'Identifier' value */
235 sprintf(Identifier
, "FLOPPY%d", FloppyNumber
+ 1);
237 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
238 sizeof(CM_FLOPPY_DEVICE_DATA
);
239 PartialResourceList
= FrLdrHeapAlloc(Size
, TAG_HW_RESOURCE_LIST
);
240 if (PartialResourceList
== NULL
)
242 ERR("Failed to allocate resource descriptor\n");
246 memset(PartialResourceList
, 0, Size
);
247 PartialResourceList
->Version
= 1;
248 PartialResourceList
->Revision
= 1;
249 PartialResourceList
->Count
= 1;
251 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
252 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
253 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
254 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_FLOPPY_DEVICE_DATA
);
256 FloppyData
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
257 FloppyData
->Version
= 2;
258 FloppyData
->Revision
= 0;
259 FloppyData
->MaxDensity
= MaxDensity
[FloppyType
];
260 FloppyData
->MountDensity
= 0;
261 RtlCopyMemory(&FloppyData
->StepRateHeadUnloadTime
, Ptr
, 11);
262 FloppyData
->MaximumTrackValue
= (FloppyType
== 1) ? 39 : 79;
263 FloppyData
->DataTransferRate
= 0;
265 FldrCreateComponentKey(ControllerKey
,
267 FloppyDiskPeripheral
,
279 PCONFIGURATION_COMPONENT_DATA
280 DetectBiosFloppyController(PCONFIGURATION_COMPONENT_DATA BusKey
)
282 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
283 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
284 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
288 FloppyCount
= GetFloppyCount();
289 TRACE("Floppy count: %u\n", FloppyCount
);
291 /* Always create a BIOS disk controller, no matter if we have floppy drives or not */
292 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
293 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
294 PartialResourceList
= FrLdrHeapAlloc(Size
, TAG_HW_RESOURCE_LIST
);
295 if (PartialResourceList
== NULL
)
297 ERR("Failed to allocate resource descriptor\n");
300 memset(PartialResourceList
, 0, Size
);
302 /* Initialize resource descriptor */
303 PartialResourceList
->Version
= 1;
304 PartialResourceList
->Revision
= 1;
305 PartialResourceList
->Count
= 3;
308 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
309 PartialDescriptor
->Type
= CmResourceTypePort
;
310 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
311 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
312 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x03F0;
313 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
314 PartialDescriptor
->u
.Port
.Length
= 8;
317 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
318 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
319 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
320 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
321 PartialDescriptor
->u
.Interrupt
.Level
= 6;
322 PartialDescriptor
->u
.Interrupt
.Vector
= 6;
323 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
325 /* Set DMA channel */
326 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
327 PartialDescriptor
->Type
= CmResourceTypeDma
;
328 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
329 PartialDescriptor
->Flags
= 0;
330 PartialDescriptor
->u
.Dma
.Channel
= 2;
331 PartialDescriptor
->u
.Dma
.Port
= 0;
333 /* Create floppy disk controller */
334 FldrCreateComponentKey(BusKey
,
344 TRACE("Created key: DiskController\\0\n");
347 DetectBiosFloppyPeripheral(ControllerKey
);
349 return ControllerKey
;
353 DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA SystemKey
,
354 PCONFIGURATION_COMPONENT_DATA BusKey
)
356 PCONFIGURATION_COMPONENT_DATA ControllerKey
, DiskKey
;
357 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
358 PCM_INT13_DRIVE_PARAMETER Int13Drives
;
360 UCHAR DiskCount
, DriveNumber
;
364 /* The pre-enumeration of the BIOS disks was already done in InitializeBootDevices() */
365 DiskCount
= PcBiosDiskCount
;
367 /* Use the floppy disk controller as our controller */
368 ControllerKey
= DetectBiosFloppyController(BusKey
);
371 ERR("Failed to detect BIOS disk controller\n");
375 /* Allocate resource descriptor */
376 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
377 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
378 PartialResourceList
= FrLdrHeapAlloc(Size
, TAG_HW_RESOURCE_LIST
);
379 if (PartialResourceList
== NULL
)
381 ERR("Failed to allocate resource descriptor\n");
385 /* Initialize resource descriptor */
386 memset(PartialResourceList
, 0, Size
);
387 PartialResourceList
->Version
= 1;
388 PartialResourceList
->Revision
= 1;
389 PartialResourceList
->Count
= 1;
390 PartialResourceList
->PartialDescriptors
[0].Type
= CmResourceTypeDeviceSpecific
;
391 PartialResourceList
->PartialDescriptors
[0].ShareDisposition
= 0;
392 PartialResourceList
->PartialDescriptors
[0].Flags
= 0;
393 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
394 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
396 /* Get harddisk Int13 geometry data */
397 Int13Drives
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
398 for (i
= 0; i
< DiskCount
; i
++)
400 DriveNumber
= 0x80 + i
;
402 if (MachDiskGetDriveGeometry(DriveNumber
, &Geometry
))
404 Int13Drives
[i
].DriveSelect
= DriveNumber
;
405 Int13Drives
[i
].MaxCylinders
= Geometry
.Cylinders
- 1;
406 Int13Drives
[i
].SectorsPerTrack
= (USHORT
)Geometry
.Sectors
;
407 Int13Drives
[i
].MaxHeads
= (USHORT
)Geometry
.Heads
- 1;
408 Int13Drives
[i
].NumberDrives
= DiskCount
;
410 TRACE("Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n",
412 Geometry
.Cylinders
- 1,
415 Geometry
.BytesPerSector
);
419 /* Update the 'System' key's configuration data with BIOS INT13h information */
420 FldrSetConfigurationData(SystemKey
, PartialResourceList
, Size
);
422 /* Create and fill subkey for each harddisk */
423 for (i
= 0; i
< DiskCount
; i
++)
427 DriveNumber
= 0x80 + i
;
429 /* Get disk values */
430 PartialResourceList
= GetHarddiskConfigurationData(DriveNumber
, &Size
);
431 Identifier
= GetHarddiskIdentifier(DriveNumber
);
433 /* Create disk key */
434 FldrCreateComponentKey(ControllerKey
,
448 FrLdrCheckCpuCompatibility(VOID
)
450 INT CpuInformation
[4] = {-1};
453 /* Check if the processor first supports ID 1 */
454 __cpuid(CpuInformation
, 0);
456 NumberOfIds
= CpuInformation
[0];
458 if (NumberOfIds
== 0)
460 FrLdrBugCheckWithMessage(MISSING_HARDWARE_REQUIREMENTS
,
463 "ReactOS requires the CPUID instruction to return "
464 "more than one supported ID.\n\n");
467 /* NumberOfIds will be greater than 1 if the processor is new enough */
468 if (NumberOfIds
== 1)
472 /* Get information */
473 __cpuid(CpuInformation
, 1);
475 ProcessorFamily
= (CpuInformation
[0] >> 8) & 0xF;
477 /* If it's Family 4 or lower, bugcheck */
478 if (ProcessorFamily
< 5)
480 FrLdrBugCheckWithMessage(MISSING_HARDWARE_REQUIREMENTS
,
483 "Processor is too old (family %u < 5)\n"
484 "ReactOS requires a Pentium-level processor or newer.",