2 * ReactOS Floppy Driver
3 * Copyright (C) 2004, Vizzini (vizzini@plasmic.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * PROJECT: ReactOS Floppy Driver
21 * PURPOSE: FDC Hardware control routines
22 * PROGRAMMER: Vizzini (vizzini@plasmic.com)
24 * 15-Feb-2004 vizzini - Created
26 * - Many of these functions are based directly on information from the
27 * Intel datasheet for their enhanced floppy controller. Send_Byte and
28 * Get_Byte are direct C implementations of their flowcharts, and the
29 * read/write routine and others are loose adaptations of their charts.
30 * - These routines are generally designed to be small, atomic operations. They
31 * do not wait for interrupts, deal with DMA, or do any other Windows-
32 * specific things, unless they have to.
33 * - If you compare this to Microsoft samples or to the old ReactOS driver,
34 * or even to the linux driver, you will notice a big difference: we use
35 * a system thread to drain the queue. This is because it's illegal to block
36 * in a dispatch routine, unless you're a top-level driver (which we absolutely
37 * are not). One big reason is that we may be called at raised IRQL, at which
38 * it's illegal to block. The floppy controller is a *dumb* piece of hardware,
39 * too - it is slow and difficult to deal with. The solution is to do all
40 * of the blocking and servicing of the controller in a dedicated worker
42 * - Some information taken from Intel 82077AA data sheet (order #290166-007)
44 * TODO: ATM the constants defined in hardware.h *might* be shifted to line up
45 * with the bit position in the register, or they *might not*. This should
46 * all be converted to standardize on absolute values or shifts.
47 * I prefer bit fields, but they break endianness.
53 * Hardware Support Routines
58 ReadyForWrite(PCONTROLLER_INFO ControllerInfo
)
60 * FUNCTION: Determine of the controller is ready to accept a byte on the FIFO
62 * ControllerInfo: Info structure for the FDC we're testing
64 * TRUE if the controller can accept a byte right now
67 * - it is necessary to check both that the FIFO is set to "outbound"
68 * and that the "ready for i/o" bit is set.
71 UCHAR Status
= READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ MAIN_STATUS_REGISTER
);
73 if(Status
& MSR_IO_DIRECTION
) /* 0 for out */
76 if(!(Status
& MSR_DATA_REG_READY_FOR_IO
))
84 ReadyForRead(PCONTROLLER_INFO ControllerInfo
)
86 * FUNCTION: Determine of the controller is ready to read a byte on the FIFO
88 * ControllerInfo: Info structure for the FDC we're testing
90 * TRUE if the controller can read a byte right now
93 * - it is necessary to check both that the FIFO is set to "inbound"
94 * and that the "ready for i/o" bit is set.
97 UCHAR Status
= READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ MAIN_STATUS_REGISTER
);
99 if(!(Status
& MSR_IO_DIRECTION
)) /* Read = 1 */
102 if(!(Status
& MSR_DATA_REG_READY_FOR_IO
))
109 static NTSTATUS NTAPI
110 Send_Byte(PCONTROLLER_INFO ControllerInfo
, UCHAR Byte
)
112 * FUNCTION: Send a byte from the host to the controller's FIFO
114 * ControllerInfo: Info structure for the controller we're writing to
115 * Offset: Offset over the controller's base address that we're writing to
116 * Byte: Byte to write to the bus
118 * STATUS_SUCCESS if the byte was written successfully
119 * STATUS_UNSUCCESSFUL if not
121 * - Function designed after flowchart in intel datasheet
122 * - 250us max delay. Note that this is exactly 5 times longer
123 * than Microsoft recommends stalling the processor
124 * - PAGED_CODE, because we spin for more than the Microsoft-recommended
126 * - This function is necessary because sometimes the FIFO reacts slowly
127 * and isn't yet ready to read or write the next byte
134 for(i
= 0; i
< 5; i
++)
136 if(ReadyForWrite(ControllerInfo
))
139 KeStallExecutionProcessor(50);
144 WRITE_PORT_UCHAR(ControllerInfo
->BaseAddress
+ FIFO
, Byte
);
145 return STATUS_SUCCESS
;
149 INFO_(FLOPPY
, "Send_Byte: timed out trying to write\n");
150 HwDumpRegisters(ControllerInfo
);
151 return STATUS_UNSUCCESSFUL
;
156 static NTSTATUS NTAPI
157 Get_Byte(PCONTROLLER_INFO ControllerInfo
, PUCHAR Byte
)
159 * FUNCTION: Read a byte from the controller to the host
161 * ControllerInfo: Info structure for the controller we're reading from
162 * Offset: Offset over the controller's base address that we're reading from
163 * Byte: Byte to read from the bus
165 * STATUS_SUCCESS if the byte was read successfully
166 * STATUS_UNSUCCESSFUL if not
168 * - Function designed after flowchart in intel datasheet
169 * - 250us max delay. Note that this is exactly 5 times longer
170 * than Microsoft recommends stalling the processor
171 * - Remember that we can be interrupted here, so this might
172 * take much more wall clock time than 250us
173 * - PAGED_CODE because we spin for longer than Microsoft recommends
180 for(i
= 0; i
< 5; i
++)
182 if(ReadyForRead(ControllerInfo
))
185 KeStallExecutionProcessor(50);
190 *Byte
= READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ FIFO
);
191 return STATUS_SUCCESS
;
195 INFO_(FLOPPY
, "Get_Byte: timed out trying to write\n");
196 HwDumpRegisters(ControllerInfo
);
197 return STATUS_UNSUCCESSFUL
;
203 HwSetDataRate(PCONTROLLER_INFO ControllerInfo
, UCHAR DataRate
)
205 * FUNCTION: Set the data rte on a controller
207 * ControllerInfo: Controller whose rate is being set
208 * DataRate: Data rate code to set the controller to
213 TRACE_(FLOPPY
, "HwSetDataRate called; writing rate code 0x%x to offset 0x%x\n", DataRate
, DATA_RATE_SELECT_REGISTER
);
215 WRITE_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DATA_RATE_SELECT_REGISTER
, DataRate
);
217 return STATUS_SUCCESS
;
222 HwTurnOffMotor(PCONTROLLER_INFO ControllerInfo
)
224 * FUNCTION: Turn off all motors
226 * DriveInfo: drive to turn off
228 * STATUS_SUCCESS if the motor is successfully turned off
230 * - Don't call this routine directly unless you've thought about it
231 * and read the source to StartMotor() and StopMotor().
232 * - Called at DISPATCH_LEVEL
235 TRACE_(FLOPPY
, "HwTurnOffMotor: writing byte 0x%x to offset 0x%x\n", DOR_FDC_ENABLE
|DOR_DMA_IO_INTERFACE_ENABLE
, DIGITAL_OUTPUT_REGISTER
);
237 WRITE_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DIGITAL_OUTPUT_REGISTER
, DOR_FDC_ENABLE
|DOR_DMA_IO_INTERFACE_ENABLE
);
239 return STATUS_SUCCESS
;
244 HwTurnOnMotor(PDRIVE_INFO DriveInfo
)
246 * FUNCTION: Turn on the motor on the selected drive
248 * DriveInfo: drive to turn on
250 * STATUS_SUCCESS if the motor is successfully turned on
251 * STATUS_UNSUCCESSFUL otherwise
253 * - Doesn't interrupt
254 * - Currently cannot fail
257 PCONTROLLER_INFO ControllerInfo
= DriveInfo
->ControllerInfo
;
258 UCHAR Unit
= DriveInfo
->UnitNumber
;
266 Buffer
|= DOR_FDC_ENABLE
;
267 Buffer
|= DOR_DMA_IO_INTERFACE_ENABLE
;
270 Buffer
|= DOR_FLOPPY_MOTOR_ON_A
;
272 Buffer
|= DOR_FLOPPY_MOTOR_ON_B
;
274 Buffer
|= DOR_FLOPPY_MOTOR_ON_C
;
276 Buffer
|= DOR_FLOPPY_MOTOR_ON_D
;
278 TRACE_(FLOPPY
, "HwTurnOnMotor: writing byte 0x%x to offset 0x%x\n", Buffer
, DIGITAL_OUTPUT_REGISTER
);
279 WRITE_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DIGITAL_OUTPUT_REGISTER
, Buffer
);
281 return STATUS_SUCCESS
;
286 HwSenseDriveStatus(PDRIVE_INFO DriveInfo
)
288 * FUNCTION: Start a sense status command
290 * DriveInfo: Drive to inquire about
292 * STATUS_SUCCESS if the command is successfully queued to the controller
293 * STATUS_UNSUCCESSFUL if not
295 * - Generates an interrupt
296 * - hard-wired to head 0
304 TRACE_(FLOPPY
, "HwSenseDriveStatus called\n");
306 Buffer
[0] = COMMAND_SENSE_DRIVE_STATUS
;
307 Buffer
[1] = DriveInfo
->UnitNumber
; /* hard-wired to head 0 for now */
309 for(i
= 0; i
< 2; i
++)
310 if(Send_Byte(DriveInfo
->ControllerInfo
, Buffer
[i
]) != STATUS_SUCCESS
)
312 WARN_(FLOPPY
, "HwSenseDriveStatus: failed to write FIFO\n");
313 return STATUS_UNSUCCESSFUL
;
316 return STATUS_SUCCESS
;
321 HwReadWriteData(PCONTROLLER_INFO ControllerInfo
,
327 UCHAR BytesPerSector
,
332 * FUNCTION: Read or write data to the drive
334 * ControllerInfo: controller to target the read/write request to
335 * Read: TRUE if the device should be read; FALSE if written
336 * Unit: Drive number to target
337 * Cylinder: cylinder to start the read on
338 * Head: head to start the read on
339 * Sector: sector to start the read on (1-based!)
340 * BytesPerSector: sector size constant (hardware.h)
341 * EndOfTrack: Marks the last sector number to read/write on the track
342 * Gap3Length: Gap length for the operation
343 * DataLength: Bytes to read, *unless* BytesPerSector is specified
345 * STATUS_SUCCESS if the operation was successfully queued to the controller
346 * STATUS_UNSUCCESSFUL otherwise
348 * - Generates an interrupt
356 /* Shouldn't be using DataLength in this driver */
357 ASSERT(DataLength
== 0xff);
359 /* Build the command to send */
361 Buffer
[0] = COMMAND_READ_DATA
;
363 Buffer
[0] = COMMAND_WRITE_DATA
;
365 Buffer
[0] |= READ_DATA_MFM
| READ_DATA_MT
;
367 Buffer
[1] = (Head
<< COMMAND_HEAD_NUMBER_SHIFT
) | Unit
;
368 Buffer
[2] = Cylinder
;
371 Buffer
[5] = BytesPerSector
;
372 Buffer
[6] = EndOfTrack
;
373 Buffer
[7] = Gap3Length
;
374 Buffer
[8] = DataLength
;
376 /* Send the command */
377 for(i
= 0; i
< 9; i
++)
379 INFO_(FLOPPY
, "HwReadWriteData: Sending a command byte to the FIFO: 0x%x\n", Buffer
[i
]);
381 if(Send_Byte(ControllerInfo
, Buffer
[i
]) != STATUS_SUCCESS
)
383 WARN_(FLOPPY
, "HwReadWriteData: Unable to write to the FIFO\n");
384 return STATUS_UNSUCCESSFUL
;
388 return STATUS_SUCCESS
;
393 HwRecalibrateResult(PCONTROLLER_INFO ControllerInfo
)
395 * FUNCTION: Get the result of a recalibrate command
397 * ControllerInfo: controller to query
399 * STATUS_SUCCESS if the recalibratewas a success
400 * STATUS_UNSUCCESSFUL otherwise
402 * - This function tests the error conditions itself, and boils the
403 * whole thing down to a single SUCCESS or FAILURE result
404 * - Called post-interrupt; does not interrupt
406 * - perhaps handle more status
414 if(Send_Byte(ControllerInfo
, COMMAND_SENSE_INTERRUPT_STATUS
) != STATUS_SUCCESS
)
416 WARN_(FLOPPY
, "HwRecalibrateResult: Unable to write the controller\n");
417 return STATUS_UNSUCCESSFUL
;
420 for(i
= 0; i
< 2; i
++)
421 if(Get_Byte(ControllerInfo
, &Buffer
[i
]) != STATUS_SUCCESS
)
423 WARN_(FLOPPY
, "HwRecalibrateResult: unable to read FIFO\n");
424 return STATUS_UNSUCCESSFUL
;
427 /* Validate that it did what we told it to */
428 INFO_(FLOPPY
, "HwRecalibrateResult results: ST0: 0x%x PCN: 0x%x\n", Buffer
[0], Buffer
[1]);
438 WARN_(FLOPPY
, "HwRecalibrateResult: PCN not 0\n");
439 return STATUS_UNSUCCESSFUL
;
442 /* test seek complete */
443 if((Buffer
[0] & SR0_SEEK_COMPLETE
) != SR0_SEEK_COMPLETE
)
445 WARN_(FLOPPY
, "HwRecalibrateResult: Failed to complete the seek\n");
446 return STATUS_UNSUCCESSFUL
;
449 /* Is the equipment check flag set? Could be no disk in drive... */
450 if((Buffer
[0] & SR0_EQUIPMENT_CHECK
) == SR0_EQUIPMENT_CHECK
)
452 WARN_(FLOPPY
, "HwRecalibrateResult: Seeked to track 0 successfully, but EC is set; returning failure\n");
453 return STATUS_UNSUCCESSFUL
;
456 return STATUS_SUCCESS
;
461 HwReadWriteResult(PCONTROLLER_INFO ControllerInfo
)
463 * FUNCTION: Get the result of a read or write from the controller
465 * ControllerInfo: controller to query
467 * STATUS_SUCCESS if the read/write was a success
468 * STATUS_UNSUCCESSFUL otherwise
470 * - This function tests the error conditions itself, and boils the
471 * whole thing down to a single SUCCESS or FAILURE result
472 * - Called post-interrupt; does not interrupt
474 * - perhaps handle more status
482 for(i
= 0; i
< 7; i
++)
483 if(Get_Byte(ControllerInfo
, &Buffer
[i
]) != STATUS_SUCCESS
)
485 WARN_(FLOPPY
, "HwReadWriteResult: unable to read fifo\n");
486 return STATUS_UNSUCCESSFUL
;
489 /* Validate that it did what we told it to */
490 INFO_(FLOPPY
, "HwReadWriteResult results: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", Buffer
[0], Buffer
[1], Buffer
[2], Buffer
[3],
491 Buffer
[4], Buffer
[5], Buffer
[6]);
493 /* Last command successful? */
494 if((Buffer
[0] & SR0_LAST_COMMAND_STATUS
) != SR0_LCS_SUCCESS
)
495 return STATUS_UNSUCCESSFUL
;
497 return STATUS_SUCCESS
;
502 HwRecalibrate(PDRIVE_INFO DriveInfo
)
504 * FUNCTION: Start a recalibration of a drive
506 * DriveInfo: Drive to recalibrate
508 * STATUS_SUCCESS if the command was successfully queued to the controller
509 * STATUS_UNSUCCESSFUL otherwise
511 * - Generates an interrupt
514 PCONTROLLER_INFO ControllerInfo
= DriveInfo
->ControllerInfo
;
515 UCHAR Unit
= DriveInfo
->UnitNumber
;
519 TRACE_(FLOPPY
, "HwRecalibrate called\n");
523 Buffer
[0] = COMMAND_RECALIBRATE
;
526 for(i
= 0; i
< 2; i
++)
527 if(Send_Byte(ControllerInfo
, Buffer
[i
]) != STATUS_SUCCESS
)
529 WARN_(FLOPPY
, "HwRecalibrate: unable to write FIFO\n");
530 return STATUS_UNSUCCESSFUL
;
533 return STATUS_SUCCESS
;
538 HwSenseInterruptStatus(PCONTROLLER_INFO ControllerInfo
)
540 * FUNCTION: Send a sense interrupt status command to a controller
542 * ControllerInfo: controller to queue the command to
544 * STATUS_SUCCESS if the command is queued successfully
545 * STATUS_UNSUCCESSFUL if not
553 if(Send_Byte(ControllerInfo
, COMMAND_SENSE_INTERRUPT_STATUS
) != STATUS_SUCCESS
)
555 WARN_(FLOPPY
, "HwSenseInterruptStatus: failed to write controller\n");
556 return STATUS_UNSUCCESSFUL
;
559 for(i
= 0; i
< 2; i
++)
561 if(Get_Byte(ControllerInfo
, &Buffer
[i
]) != STATUS_SUCCESS
)
563 WARN_(FLOPPY
, "HwSenseInterruptStatus: failed to read controller\n");
564 return STATUS_UNSUCCESSFUL
;
568 INFO_(FLOPPY
, "HwSenseInterruptStatus returned 0x%x 0x%x\n", Buffer
[0], Buffer
[1]);
570 return STATUS_SUCCESS
;
575 HwReadId(PDRIVE_INFO DriveInfo
, UCHAR Head
)
577 * FUNCTION: Issue a read id command to the drive
579 * DriveInfo: Drive to read id from
580 * Head: Head to read the ID from
582 * STATUS_SUCCESS if the command is queued
583 * STATUS_UNSUCCESSFUL otherwise
585 * - Generates an interrupt
591 TRACE_(FLOPPY
, "HwReadId called\n");
595 Buffer
[0] = COMMAND_READ_ID
| READ_ID_MFM
;
596 Buffer
[1] = (Head
<< COMMAND_HEAD_NUMBER_SHIFT
) | DriveInfo
->UnitNumber
;
598 for(i
= 0; i
< 2; i
++)
599 if(Send_Byte(DriveInfo
->ControllerInfo
, Buffer
[i
]) != STATUS_SUCCESS
)
601 WARN_(FLOPPY
, "HwReadId: unable to send bytes to fifo\n");
602 return STATUS_UNSUCCESSFUL
;
605 return STATUS_SUCCESS
;
610 HwFormatTrack(PCONTROLLER_INFO ControllerInfo
,
613 UCHAR BytesPerSector
,
614 UCHAR SectorsPerTrack
,
618 * FUNCTION: Format a track
620 * ControllerInfo: controller to target with the request
621 * Unit: drive to format on
622 * Head: head to format on
623 * BytesPerSector: constant from hardware.h to select density
624 * SectorsPerTrack: sectors per track
625 * Gap3Length: gap length to use during format
626 * FillerPattern: pattern to write into the data portion of sectors
628 * STATUS_SUCCESS if the command is successfully queued
629 * STATUS_UNSUCCESSFUL otherwise
635 TRACE_(FLOPPY
, "HwFormatTrack called\n");
639 Buffer
[0] = COMMAND_FORMAT_TRACK
;
640 Buffer
[1] = (Head
<< COMMAND_HEAD_NUMBER_SHIFT
) | Unit
;
641 Buffer
[2] = BytesPerSector
;
642 Buffer
[3] = SectorsPerTrack
;
643 Buffer
[4] = Gap3Length
;
644 Buffer
[5] = FillerPattern
;
646 for(i
= 0; i
< 6; i
++)
647 if(Send_Byte(ControllerInfo
, Buffer
[i
]) != STATUS_SUCCESS
)
649 WARN_(FLOPPY
, "HwFormatTrack: unable to send bytes to floppy\n");
650 return STATUS_UNSUCCESSFUL
;
653 return STATUS_SUCCESS
;
658 HwSeek(PDRIVE_INFO DriveInfo
, UCHAR Cylinder
)
660 * FUNCTION: Seek the heads to a particular cylinder
662 * DriveInfo: Drive to seek
663 * Cylinder: cylinder to move to
665 * STATUS_SUCCESS if the command is successfully sent
666 * STATUS_UNSUCCESSFUL otherwise
668 * - Generates an interrupt
675 TRACE_(FLOPPY
, "HwSeek called for cyl 0x%x\n", Cylinder
);
679 Buffer
[0] = COMMAND_SEEK
;
680 Buffer
[1] = DriveInfo
->UnitNumber
;
681 Buffer
[2] = Cylinder
;
683 for(i
= 0; i
< 3; i
++)
684 if(Send_Byte(DriveInfo
->ControllerInfo
, Buffer
[i
]) != STATUS_SUCCESS
)
686 WARN_(FLOPPY
, "HwSeek: failed to write fifo\n");
687 return STATUS_UNSUCCESSFUL
;
690 /* Wait for the head to settle */
691 Delay
.QuadPart
= 10 * 1000;
692 Delay
.QuadPart
*= -1;
693 Delay
.QuadPart
*= DriveInfo
->FloppyDeviceData
.HeadSettleTime
;
695 KeDelayExecutionThread(KernelMode
, FALSE
, &Delay
);
697 return STATUS_SUCCESS
;
702 HwConfigure(PCONTROLLER_INFO ControllerInfo
,
709 * FUNCTION: Sends configuration to the drive
711 * ControllerInfo: controller to target with the request
712 * EIS: Enable implied seek
713 * EFIFO: Enable advanced fifo
714 * POLL: Enable polling
715 * FIFOTHR: fifo threshold
716 * PRETRK: precomp (see intel datasheet)
718 * STATUS_SUCCESS if the command is successfully sent
719 * STATUS_UNSUCCESSFUL otherwise
727 TRACE_(FLOPPY
, "HwConfigure called\n");
731 Buffer
[0] = COMMAND_CONFIGURE
;
733 Buffer
[2] = (EIS
* CONFIGURE_EIS
) + (EFIFO
* CONFIGURE_EFIFO
) + (POLL
* CONFIGURE_POLL
) + (FIFOTHR
);
736 for(i
= 0; i
< 4; i
++)
737 if(Send_Byte(ControllerInfo
, Buffer
[i
]) != STATUS_SUCCESS
)
739 WARN_(FLOPPY
, "HwConfigure: failed to write the fifo\n");
740 return STATUS_UNSUCCESSFUL
;
743 return STATUS_SUCCESS
;
748 HwGetVersion(PCONTROLLER_INFO ControllerInfo
)
750 * FUNCTION: Gets the version of the controller
752 * ControllerInfo: controller to target with the request
753 * ConfigValue: Configuration value to send to the drive (see header)
755 * Version number returned by the command, or
758 * - This command doesn't interrupt, so we go right to reading after
759 * we issue the command
766 if(Send_Byte(ControllerInfo
, COMMAND_VERSION
) != STATUS_SUCCESS
)
768 WARN_(FLOPPY
, "HwGetVersion: unable to write fifo\n");
769 return STATUS_UNSUCCESSFUL
;
772 if(Get_Byte(ControllerInfo
, &Buffer
) != STATUS_SUCCESS
)
774 WARN_(FLOPPY
, "HwGetVersion: unable to write fifo\n");
775 return STATUS_UNSUCCESSFUL
;
778 INFO_(FLOPPY
, "HwGetVersion returning version 0x%x\n", Buffer
);
784 HwDiskChanged(PDRIVE_INFO DriveInfo
, PBOOLEAN DiskChanged
)
786 * FUNCTION: Detect whether the hardware has sensed a disk change
788 * DriveInfo: pointer to the drive that we are to check
789 * DiskChanged: boolean that is set with whether or not the controller thinks there has been a disk change
791 * STATUS_SUCCESS if the drive is successfully queried
793 * - Does not interrupt.
794 * - Guessing a bit at the Model30 stuff
798 PCONTROLLER_INFO ControllerInfo
= (PCONTROLLER_INFO
) DriveInfo
->ControllerInfo
;
800 Buffer
= READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DIGITAL_INPUT_REGISTER
);
802 TRACE_(FLOPPY
, "HwDiskChanged: read 0x%x from DIR\n", Buffer
);
804 if(ControllerInfo
->Model30
)
806 if(!(Buffer
& DIR_DISKETTE_CHANGE
))
808 INFO_(FLOPPY
, "HdDiskChanged - Model30 - returning TRUE\n");
813 INFO_(FLOPPY
, "HdDiskChanged - Model30 - returning FALSE\n");
814 *DiskChanged
= FALSE
;
819 if(Buffer
& DIR_DISKETTE_CHANGE
)
821 INFO_(FLOPPY
, "HdDiskChanged - PS2 - returning TRUE\n");
826 INFO_(FLOPPY
, "HdDiskChanged - PS2 - returning FALSE\n");
827 *DiskChanged
= FALSE
;
831 return STATUS_SUCCESS
;
835 HwSenseDriveStatusResult(PCONTROLLER_INFO ControllerInfo
, PUCHAR Status
)
837 * FUNCTION: Get the result of a sense drive status command
839 * ControllerInfo: controller to query
840 * Status: Status from the drive sense command
842 * STATUS_SUCCESS if we can successfully read the status
843 * STATUS_UNSUCCESSFUL otherwise
845 * - Called post-interrupt; does not interrupt
850 if(Get_Byte(ControllerInfo
, Status
) != STATUS_SUCCESS
)
852 WARN_(FLOPPY
, "HwSenseDriveStatus: unable to read fifo\n");
853 return STATUS_UNSUCCESSFUL
;
856 TRACE_(FLOPPY
, "HwSenseDriveStatusResult: ST3: 0x%x\n", *Status
);
858 return STATUS_SUCCESS
;
863 HwReadIdResult(PCONTROLLER_INFO ControllerInfo
,
867 * FUNCTION: Get the result of a read id command
869 * ControllerInfo: controller to query
870 * CurCylinder: Returns the cylinder that we're at
871 * CurHead: Returns the head that we're at
873 * STATUS_SUCCESS if the read id was a success
874 * STATUS_UNSUCCESSFUL otherwise
876 * - This function tests the error conditions itself, and boils the
877 * whole thing down to a single SUCCESS or FAILURE result
878 * - Called post-interrupt; does not interrupt
880 * - perhaps handle more status
883 UCHAR Buffer
[7] = {0,0,0,0,0,0,0};
888 for(i
= 0; i
< 7; i
++)
889 if(Get_Byte(ControllerInfo
, &Buffer
[i
]) != STATUS_SUCCESS
)
891 WARN_(FLOPPY
, "ReadIdResult(): can't read from the controller\n");
892 return STATUS_UNSUCCESSFUL
;
895 /* Validate that it did what we told it to */
896 INFO_(FLOPPY
, "ReadId results: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", Buffer
[0], Buffer
[1], Buffer
[2], Buffer
[3],
897 Buffer
[4], Buffer
[5], Buffer
[6]);
899 /* Last command successful? */
900 if((Buffer
[0] & SR0_LAST_COMMAND_STATUS
) != SR0_LCS_SUCCESS
)
902 WARN_(FLOPPY
, "ReadId didn't return last command success\n");
903 return STATUS_UNSUCCESSFUL
;
907 if(Buffer
[1] & SR1_CANNOT_FIND_ID_ADDRESS
)
909 WARN_(FLOPPY
, "ReadId didn't find an address mark\n");
910 return STATUS_UNSUCCESSFUL
;
914 *CurCylinder
= Buffer
[3];
917 *CurHead
= Buffer
[4];
919 return STATUS_SUCCESS
;
924 HwSpecify(PCONTROLLER_INFO ControllerInfo
,
926 UCHAR HeadUnloadTime
,
930 * FUNCTION: Set up timing and DMA mode for the controller
932 * ControllerInfo: Controller to set up
933 * HeadLoadTime: Head load time (see data sheet for details)
934 * HeadUnloadTime: Head unload time
935 * StepRateTime: Step rate time
936 * NonDma: TRUE to disable DMA mode
938 * STATUS_SUCCESS if the contrller is successfully programmed
939 * STATUS_UNSUCCESSFUL if not
941 * - Does not interrupt
943 * TODO: Figure out timings
949 Buffer
[0] = COMMAND_SPECIFY
;
951 Buffer[1] = (StepRateTime << 4) + HeadUnloadTime;
952 Buffer[2] = (HeadLoadTime << 1) + (NonDma ? 1 : 0);
957 //INFO_(FLOPPY, "HwSpecify: sending 0x%x 0x%x 0x%x to FIFO\n", Buffer[0], Buffer[1], Buffer[2]);
958 WARN_(FLOPPY
, "HWSPECIFY: FIXME - sending 0x3 0xd1 0x2 to FIFO\n");
960 for(i
= 0; i
< 3; i
++)
961 if(Send_Byte(ControllerInfo
, Buffer
[i
]) != STATUS_SUCCESS
)
963 WARN_(FLOPPY
, "HwSpecify: unable to write to controller\n");
964 return STATUS_UNSUCCESSFUL
;
967 return STATUS_SUCCESS
;
972 HwReset(PCONTROLLER_INFO ControllerInfo
)
974 * FUNCTION: Reset the controller
976 * ControllerInfo: controller to reset
978 * STATUS_SUCCESS in all cases
980 * - Generates an interrupt that must be serviced four times (one per drive)
983 TRACE_(FLOPPY
, "HwReset called\n");
985 /* Write the reset bit in the DRSR */
986 WRITE_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DATA_RATE_SELECT_REGISTER
, DRSR_SW_RESET
);
988 /* Check for the reset bit in the DOR and set it if necessary (see Intel doc) */
989 if(!(READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DIGITAL_OUTPUT_REGISTER
) & DOR_RESET
))
991 HwDumpRegisters(ControllerInfo
);
992 INFO_(FLOPPY
, "HwReset: Setting Enable bit\n");
993 WRITE_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DIGITAL_OUTPUT_REGISTER
, DOR_DMA_IO_INTERFACE_ENABLE
|DOR_RESET
);
994 HwDumpRegisters(ControllerInfo
);
996 if(!(READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DIGITAL_OUTPUT_REGISTER
) & DOR_RESET
))
998 WARN_(FLOPPY
, "HwReset: failed to set the DOR enable bit!\n");
999 HwDumpRegisters(ControllerInfo
);
1000 return STATUS_UNSUCCESSFUL
;
1004 return STATUS_SUCCESS
;
1009 HwPowerOff(PCONTROLLER_INFO ControllerInfo
)
1011 * FUNCTION: Power down a controller
1013 * ControllerInfo: Controller to power down
1017 * - Wake up with a hardware reset
1020 TRACE_(FLOPPY
, "HwPowerOff called on controller 0x%p\n", ControllerInfo
);
1022 WRITE_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DATA_RATE_SELECT_REGISTER
, DRSR_POWER_DOWN
);
1024 return STATUS_SUCCESS
;
1028 HwDumpRegisters(PCONTROLLER_INFO ControllerInfo
)
1030 * FUNCTION: Dump all readable registers from the floppy controller
1032 * ControllerInfo: Controller to dump registers from
1035 UNREFERENCED_PARAMETER(ControllerInfo
);
1037 INFO_(FLOPPY
, "STATUS:\n");
1038 INFO_(FLOPPY
, "STATUS_REGISTER_A = 0x%x\n", READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ STATUS_REGISTER_A
));
1039 INFO_(FLOPPY
, "STATUS_REGISTER_B = 0x%x\n", READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ STATUS_REGISTER_B
));
1040 INFO_(FLOPPY
, "DIGITAL_OUTPUT_REGISTER = 0x%x\n", READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DIGITAL_OUTPUT_REGISTER
));
1041 INFO_(FLOPPY
, "MAIN_STATUS_REGISTER =0x%x\n", READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ MAIN_STATUS_REGISTER
));
1042 INFO_(FLOPPY
, "DIGITAL_INPUT_REGISTER = 0x%x\n", READ_PORT_UCHAR(ControllerInfo
->BaseAddress
+ DIGITAL_INPUT_REGISTER
));