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.
27 DBG_DEFAULT_CHANNEL(HWDETECT
);
34 #define CLOCK_TICK_RATE (1193182)
35 #define LATCH (CLOCK_TICK_RATE / HZ)
37 static unsigned int delay_count
= 1;
38 extern UCHAR PcBiosDiskCount
;
40 /* This function is slightly different in its PC and XBOX versions */
41 GET_HARDDISK_CONFIG_DATA GetHarddiskConfigurationData
= NULL
;
44 GetHarddiskIdentifier(UCHAR DriveNumber
);
46 /* FUNCTIONS *****************************************************************/
50 __StallExecutionProcessor(ULONG Loops
)
52 register volatile unsigned int i
;
53 for (i
= 0; i
< Loops
; i
++);
56 VOID
StallExecutionProcessor(ULONG Microseconds
)
58 ULONGLONG LoopCount
= ((ULONGLONG
)delay_count
* (ULONGLONG
)Microseconds
) / 1000ULL;
59 __StallExecutionProcessor((ULONG
)LoopCount
);
68 WRITE_PORT_UCHAR((PUCHAR
)0x43, 0x00);
69 Count
= READ_PORT_UCHAR((PUCHAR
)0x40);
70 Count
|= READ_PORT_UCHAR((PUCHAR
)0x40) << 8;
77 WaitFor8254Wraparound(VOID
)
83 CurCount
= Read8254Timer();
88 CurCount
= Read8254Timer();
89 Delta
= CurCount
- PrevCount
;
92 * This limit for delta seems arbitrary, but it isn't, it's
93 * slightly above the level of error a buggy Mercury/Neptune
94 * chipset timer can cause.
101 HalpCalibrateStallExecution(VOID
)
107 /* Initialise timer interrupt with MILLISECOND ms interval */
108 WRITE_PORT_UCHAR((PUCHAR
)0x43, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
109 WRITE_PORT_UCHAR((PUCHAR
)0x40, LATCH
& 0xff); /* LSB */
110 WRITE_PORT_UCHAR((PUCHAR
)0x40, LATCH
>> 8); /* MSB */
112 /* Stage 1: Coarse calibration */
114 WaitFor8254Wraparound();
120 /* Next delay count to try */
123 WaitFor8254Wraparound();
126 __StallExecutionProcessor(delay_count
);
128 CurCount
= Read8254Timer();
130 while (CurCount
> LATCH
/ 2);
132 /* Get bottom value for delay */
135 /* Stage 2: Fine calibration */
137 /* Which bit are we going to test */
138 calib_bit
= delay_count
;
140 for (i
= 0; i
< PRECISION
; i
++)
142 /* Next bit to calibrate */
145 /* If we have done all bits, stop */
146 if (!calib_bit
) break;
148 /* Set the bit in delay_count */
149 delay_count
|= calib_bit
;
151 WaitFor8254Wraparound();
154 __StallExecutionProcessor(delay_count
);
156 CurCount
= Read8254Timer();
157 /* If a tick has passed, turn the calibrated bit back off */
158 if (CurCount
<= LATCH
/ 2)
159 delay_count
&= ~calib_bit
;
162 /* We're finished: Do the finishing touches */
164 /* Calculate delay_count for 1ms */
165 delay_count
/= (MILLISEC
/ 2);
175 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
176 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
178 return ((Data
& 0xF0) ? 1 : 0) + ((Data
& 0x0F) ? 1 : 0);
183 GetFloppyType(UCHAR DriveNumber
)
187 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
188 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
190 if (DriveNumber
== 0)
192 else if (DriveNumber
== 1)
202 PUSHORT SegPtr
= (PUSHORT
)0x7A;
203 PUSHORT OfsPtr
= (PUSHORT
)0x78;
205 return (PVOID
)((ULONG_PTR
)(((ULONG
)(*SegPtr
)) << 4) + (ULONG
)(*OfsPtr
));
210 DetectBiosFloppyPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
212 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
213 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
214 PCM_FLOPPY_DEVICE_DATA FloppyData
;
216 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
220 ULONG MaxDensity
[6] = {0, 360, 1200, 720, 1440, 2880};
223 for (FloppyNumber
= 0; FloppyNumber
< 2; FloppyNumber
++)
225 FloppyType
= GetFloppyType(FloppyNumber
);
227 if ((FloppyType
> 5) || (FloppyType
== 0))
230 if (!DiskResetController(FloppyNumber
))
233 Ptr
= GetInt1eTable();
235 /* Set 'Identifier' value */
236 sprintf(Identifier
, "FLOPPY%d", FloppyNumber
+ 1);
238 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
239 sizeof(CM_FLOPPY_DEVICE_DATA
);
240 PartialResourceList
= FrLdrHeapAlloc(Size
, TAG_HW_RESOURCE_LIST
);
241 if (PartialResourceList
== NULL
)
243 ERR("Failed to allocate resource descriptor\n");
247 memset(PartialResourceList
, 0, Size
);
248 PartialResourceList
->Version
= 1;
249 PartialResourceList
->Revision
= 1;
250 PartialResourceList
->Count
= 1;
252 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
253 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
254 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
255 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_FLOPPY_DEVICE_DATA
);
257 FloppyData
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
258 FloppyData
->Version
= 2;
259 FloppyData
->Revision
= 0;
260 FloppyData
->MaxDensity
= MaxDensity
[FloppyType
];
261 FloppyData
->MountDensity
= 0;
262 RtlCopyMemory(&FloppyData
->StepRateHeadUnloadTime
, Ptr
, 11);
263 FloppyData
->MaximumTrackValue
= (FloppyType
== 1) ? 39 : 79;
264 FloppyData
->DataTransferRate
= 0;
266 FldrCreateComponentKey(ControllerKey
,
268 FloppyDiskPeripheral
,
280 PCONFIGURATION_COMPONENT_DATA
281 DetectBiosFloppyController(PCONFIGURATION_COMPONENT_DATA BusKey
)
283 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
284 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
285 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
289 FloppyCount
= GetFloppyCount();
290 TRACE("Floppy count: %u\n", FloppyCount
);
292 /* Always create a BIOS disk controller, no matter if we have floppy drives or not */
293 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
294 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
295 PartialResourceList
= FrLdrHeapAlloc(Size
, TAG_HW_RESOURCE_LIST
);
296 if (PartialResourceList
== NULL
)
298 ERR("Failed to allocate resource descriptor\n");
301 memset(PartialResourceList
, 0, Size
);
303 /* Initialize resource descriptor */
304 PartialResourceList
->Version
= 1;
305 PartialResourceList
->Revision
= 1;
306 PartialResourceList
->Count
= 3;
309 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
310 PartialDescriptor
->Type
= CmResourceTypePort
;
311 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
312 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
313 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x03F0;
314 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
315 PartialDescriptor
->u
.Port
.Length
= 8;
318 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
319 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
320 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
321 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
322 PartialDescriptor
->u
.Interrupt
.Level
= 6;
323 PartialDescriptor
->u
.Interrupt
.Vector
= 6;
324 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
326 /* Set DMA channel */
327 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
328 PartialDescriptor
->Type
= CmResourceTypeDma
;
329 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
330 PartialDescriptor
->Flags
= 0;
331 PartialDescriptor
->u
.Dma
.Channel
= 2;
332 PartialDescriptor
->u
.Dma
.Port
= 0;
334 /* Create floppy disk controller */
335 FldrCreateComponentKey(BusKey
,
345 TRACE("Created key: DiskController\\0\n");
348 DetectBiosFloppyPeripheral(ControllerKey
);
350 return ControllerKey
;
354 DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA SystemKey
,
355 PCONFIGURATION_COMPONENT_DATA BusKey
)
357 PCONFIGURATION_COMPONENT_DATA ControllerKey
, DiskKey
;
358 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
359 PCM_INT13_DRIVE_PARAMETER Int13Drives
;
361 UCHAR DiskCount
, DriveNumber
;
365 /* The pre-enumeration of the BIOS disks was already done in InitializeBootDevices() */
366 DiskCount
= PcBiosDiskCount
;
368 /* Use the floppy disk controller as our controller */
369 ControllerKey
= DetectBiosFloppyController(BusKey
);
372 ERR("Failed to detect BIOS disk controller\n");
376 /* Allocate resource descriptor */
377 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
378 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
379 PartialResourceList
= FrLdrHeapAlloc(Size
, TAG_HW_RESOURCE_LIST
);
380 if (PartialResourceList
== NULL
)
382 ERR("Failed to allocate resource descriptor\n");
386 /* Initialize resource descriptor */
387 memset(PartialResourceList
, 0, Size
);
388 PartialResourceList
->Version
= 1;
389 PartialResourceList
->Revision
= 1;
390 PartialResourceList
->Count
= 1;
391 PartialResourceList
->PartialDescriptors
[0].Type
= CmResourceTypeDeviceSpecific
;
392 PartialResourceList
->PartialDescriptors
[0].ShareDisposition
= 0;
393 PartialResourceList
->PartialDescriptors
[0].Flags
= 0;
394 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
395 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
397 /* Get harddisk Int13 geometry data */
398 Int13Drives
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
399 for (i
= 0; i
< DiskCount
; i
++)
401 DriveNumber
= 0x80 + i
;
403 if (MachDiskGetDriveGeometry(DriveNumber
, &Geometry
))
405 Int13Drives
[i
].DriveSelect
= DriveNumber
;
406 Int13Drives
[i
].MaxCylinders
= Geometry
.Cylinders
- 1;
407 Int13Drives
[i
].SectorsPerTrack
= (USHORT
)Geometry
.Sectors
;
408 Int13Drives
[i
].MaxHeads
= (USHORT
)Geometry
.Heads
- 1;
409 Int13Drives
[i
].NumberDrives
= DiskCount
;
411 TRACE("Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n",
413 Geometry
.Cylinders
- 1,
416 Geometry
.BytesPerSector
);
420 /* Update the 'System' key's configuration data with BIOS INT13h information */
421 FldrSetConfigurationData(SystemKey
, PartialResourceList
, Size
);
423 /* Create and fill subkey for each harddisk */
424 for (i
= 0; i
< DiskCount
; i
++)
428 DriveNumber
= 0x80 + i
;
430 /* Get disk values */
431 PartialResourceList
= GetHarddiskConfigurationData(DriveNumber
, &Size
);
432 Identifier
= GetHarddiskIdentifier(DriveNumber
);
434 /* Create disk key */
435 FldrCreateComponentKey(ControllerKey
,
449 FrLdrCheckCpuCompatibility(VOID
)
451 INT CpuInformation
[4] = {-1};
454 /* Check if the processor first supports ID 1 */
455 __cpuid(CpuInformation
, 0);
457 NumberOfIds
= CpuInformation
[0];
459 if (NumberOfIds
== 0)
461 FrLdrBugCheckWithMessage(MISSING_HARDWARE_REQUIREMENTS
,
464 "ReactOS requires the CPUID instruction to return "
465 "more than one supported ID.\n\n");
468 /* NumberOfIds will be greater than 1 if the processor is new enough */
469 if (NumberOfIds
== 1)
473 /* Get information */
474 __cpuid(CpuInformation
, 1);
476 ProcessorFamily
= (CpuInformation
[0] >> 8) & 0xF;
478 /* If it's Family 4 or lower, bugcheck */
479 if (ProcessorFamily
< 5)
481 FrLdrBugCheckWithMessage(MISSING_HARDWARE_REQUIREMENTS
,
484 "Processor is too old (family %u < 5)\n"
485 "ReactOS requires a Pentium-level processor or newer.",