3 * Floppy Disk Access Code
\r
8 #include <fs_devfs.h>
\r
9 #include <tpl_drv_disk.h>
\r
11 #include <iocache.h>
\r
15 // === CONSTANTS ===
\r
16 // --- Current Version
\r
17 #define FDD_VERSION ((0<<8)|(75))
\r
20 #define FDD_SEEK_TIMEOUT 10 // Timeout for a seek operation
\r
21 #define MOTOR_ON_DELAY 500 // Miliseconds
\r
22 #define MOTOR_OFF_DELAY 2000 // Miliseconds
\r
26 * \brief Representation of a floppy drive
\r
28 typedef struct sFloppyDrive
\r
31 volatile int motorState; //2 - On, 1 - Spinup, 0 - Off
\r
36 tIOCache *CacheHandle;
\r
41 * \brief Cached Sector
\r
46 Uint16 sector; // Allows 32Mb of addressable space (Plenty for FDD)
\r
50 // === CONSTANTS ===
\r
51 static const char *cFDD_TYPES[] = {"None", "360kB 5.25\"", "1.2MB 5.25\"", "720kB 3.5\"", "1.44MB 3.5\"", "2.88MB 3.5\"" };
\r
52 static const int cFDD_SIZES[] = { 0, 360*1024, 1200*1024, 720*1024, 1440*1024, 2880*1024 };
\r
53 static const short cPORTBASE[] = { 0x3F0, 0x370 };
\r
58 PORT_DIGOUTPUT = 0x2,
\r
59 PORT_MAINSTATUS = 0x4,
\r
60 PORT_DATARATE = 0x4,
\r
62 PORT_DIGINPUT = 0x7,
\r
63 PORT_CONFIGCTRL = 0x7
\r
66 enum FloppyCommands {
\r
67 FIX_DRIVE_DATA = 0x03,
\r
68 HECK_DRIVE_STATUS = 0x04,
\r
69 CALIBRATE_DRIVE = 0x07,
\r
70 CHECK_INTERRUPT_STATUS = 0x08,
\r
72 READ_SECTOR_ID = 0x4A,
\r
73 FORMAT_TRACK = 0x4D,
\r
76 WRITE_SECTOR = 0xC5,
\r
77 WRITE_DELETE_SECTOR = 0xC9,
\r
78 READ_DELETE_SECTOR = 0xCC,
\r
81 // === PROTOTYPES ===
\r
83 int FDD_Install(char **Arguments);
\r
84 void FDD_UnloadModule();
\r
86 char *FDD_ReadDir(tVFS_Node *Node, int pos);
\r
87 tVFS_Node *FDD_FindDir(tVFS_Node *dirNode, char *Name);
\r
88 int FDD_IOCtl(tVFS_Node *Node, int ID, void *Data);
\r
89 Uint64 FDD_ReadFS(tVFS_Node *node, Uint64 off, Uint64 len, void *buffer);
\r
90 // --- Functions for IOCache/DrvUtil
\r
91 Uint FDD_ReadSectors(Uint64 SectorAddr, Uint Count, void *Buffer, Uint Disk);
\r
92 // --- Raw Disk Access
\r
93 int FDD_ReadSector(Uint32 disk, Uint64 lba, void *Buffer);
\r
94 int FDD_WriteSector(Uint32 Disk, Uint64 LBA, void *Buffer);
\r
96 void FDD_IRQHandler(int Num);
\r
97 inline void FDD_WaitIRQ();
\r
98 void FDD_SensInt(int base, Uint8 *sr0, Uint8 *cyl);
\r
99 void FDD_int_SendByte(int base, char byte);
\r
100 int FDD_int_GetByte(int base);
\r
101 void FDD_Reset(int id);
\r
102 void FDD_Recalibrate(int disk);
\r
103 int FDD_int_SeekTrack(int disk, int head, int track);
\r
104 void FDD_int_TimerCallback(int arg);
\r
105 void FDD_int_StopMotor(int disk);
\r
106 void FDD_int_StartMotor(int disk);
\r
107 int FDD_int_GetDims(int type, int lba, int *c, int *h, int *s, int *spt);
\r
110 MODULE_DEFINE(0, FDD_VERSION, FDD, FDD_Install, NULL, "ISADMA", NULL);
\r
111 t_floppyDevice gFDD_Devices[2];
\r
113 volatile int gbFDD_IrqFired = 0;
\r
114 tDevFS_Driver gFDD_DriverInfo = {
\r
119 .ACLs = &gVFS_ACL_EveryoneRX,
\r
120 .Flags = VFS_FFLAG_DIRECTORY,
\r
121 .ReadDir = FDD_ReadDir,
\r
122 .FindDir = FDD_FindDir,
\r
129 * \fn int FDD_Install(char **Arguments)
\r
130 * \brief Installs floppy driver
\r
132 int FDD_Install(char **Arguments)
\r
136 // Determine Floppy Types (From CMOS)
\r
139 gFDD_Devices[0].type = data >> 4;
\r
140 gFDD_Devices[1].type = data & 0xF;
\r
141 gFDD_Devices[0].track[0] = -1;
\r
142 gFDD_Devices[1].track[1] = -1;
\r
144 Log("[FDD ] Detected Disk 0: %s and Disk 1: %s", cFDD_TYPES[data>>4], cFDD_TYPES[data&0xF]);
\r
147 return MODULE_ERR_NOTNEEDED;
\r
150 // Clear FDD IRQ Flag
\r
151 FDD_SensInt(0x3F0, NULL, NULL);
\r
152 // Install IRQ6 Handler
\r
153 IRQ_AddHandler(6, FDD_IRQHandler);
\r
154 // Reset Primary FDD Controller
\r
157 // Initialise Root Node
\r
158 gFDD_DriverInfo.RootNode.CTime = gFDD_DriverInfo.RootNode.MTime
\r
159 = gFDD_DriverInfo.RootNode.ATime = now();
\r
161 // Initialise Child Nodes
\r
162 gFDD_Devices[0].Node.Inode = 0;
\r
163 gFDD_Devices[0].Node.Flags = 0;
\r
164 gFDD_Devices[0].Node.NumACLs = 0;
\r
165 gFDD_Devices[0].Node.Read = FDD_ReadFS;
\r
166 gFDD_Devices[0].Node.Write = NULL;//FDD_WriteFS;
\r
167 memcpy(&gFDD_Devices[1].Node, &gFDD_Devices[0].Node, sizeof(tVFS_Node));
\r
169 gFDD_Devices[1].Node.Inode = 1;
\r
172 gFDD_Devices[0].Node.Size = cFDD_SIZES[data >> 4];
\r
173 gFDD_Devices[1].Node.Size = cFDD_SIZES[data & 0xF];
\r
175 // Create Sector Cache
\r
176 if( cFDD_SIZES[data >> 4] )
\r
178 gFDD_Devices[0].CacheHandle = IOCache_Create(
\r
179 FDD_WriteSector, 0, 512,
\r
180 gFDD_Devices[0].Node.Size / (512*4)
\r
181 ); // Cache is 1/4 the size of the disk
\r
183 if( cFDD_SIZES[data & 15] )
\r
185 gFDD_Devices[1].CacheHandle = IOCache_Create(
\r
186 FDD_WriteSector, 0, 512,
\r
187 gFDD_Devices[1].Node.Size / (512*4)
\r
188 ); // Cache is 1/4 the size of the disk
\r
191 // Register with devfs
\r
192 DevFS_AddDevice(&gFDD_DriverInfo);
\r
194 return MODULE_ERR_OK;
\r
198 * \brief Prepare the module for removal
\r
200 void FDD_UnloadModule()
\r
203 //DevFS_DelDevice( &gFDD_DriverInfo );
\r
206 Time_RemoveTimer(gFDD_Devices[i].timer);
\r
207 FDD_int_StopMotor(i);
\r
214 * \fn char *FDD_ReadDir(tVFS_Node *Node, int pos)
\r
215 * \brief Read Directory
\r
217 char *FDD_ReadDir(tVFS_Node *Node, int Pos)
\r
219 char name[2] = "0\0";
\r
221 if(Pos >= 2 || Pos < 0) return NULL;
\r
223 if(gFDD_Devices[Pos].type == 0) return VFS_SKIP;
\r
227 return strdup(name);
\r
231 * \fn tVFS_Node *FDD_FindDir(tVFS_Node *Node, char *filename);
\r
232 * \brief Find File Routine (for vfs_node)
\r
234 tVFS_Node *FDD_FindDir(tVFS_Node *Node, char *Filename)
\r
238 ENTER("sFilename", Filename);
\r
240 // Sanity check string
\r
241 if(Filename == NULL) {
\r
246 // Check string length (should be 1)
\r
247 if(Filename[0] == '\0' || Filename[1] != '\0') {
\r
252 // Get First character
\r
253 i = Filename[0] - '0';
\r
255 // Check for 1st disk and if it is present return
\r
256 if(i == 0 && gFDD_Devices[0].type != 0) {
\r
257 LEAVE('p', &gFDD_Devices[0].Node);
\r
258 return &gFDD_Devices[0].Node;
\r
261 // Check for 2nd disk and if it is present return
\r
262 if(i == 1 && gFDD_Devices[1].type != 0) {
\r
263 LEAVE('p', &gFDD_Devices[1].Node);
\r
264 return &gFDD_Devices[1].Node;
\r
267 // Else return null
\r
272 static const char *casIOCTLS[] = {DRV_IOCTLNAMES,DRV_DISK_IOCTLNAMES,NULL};
\r
274 * \fn int FDD_IOCtl(tVFS_Node *Node, int id, void *data)
\r
275 * \brief Stub ioctl function
\r
277 int FDD_IOCtl(tVFS_Node *Node, int ID, void *Data)
\r
281 case DRV_IOCTL_TYPE: return DRV_TYPE_DISK;
\r
282 case DRV_IOCTL_IDENT: return ModUtil_SetIdent(Data, "FDD");
\r
283 case DRV_IOCTL_VERSION: return FDD_VERSION;
\r
284 case DRV_IOCTL_LOOKUP: return ModUtil_LookupString((char**)casIOCTLS, Data);
\r
286 case DISK_IOCTL_GETBLOCKSIZE: return 512;
\r
294 * \fn Uint64 FDD_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
\r
295 * \brief Read Data from a disk
\r
297 Uint64 FDD_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
\r
301 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
\r
308 if(Node->Inode != 0 && Node->Inode != 1) {
\r
313 ret = DrvUtil_ReadBlock(Offset, Length, Buffer, FDD_ReadSectors, 512, Node->Inode);
\r
319 * \brief Reads \a Count contiguous sectors from a disk
\r
320 * \param SectorAddr Address of the first sector
\r
321 * \param Count Number of sectors to read
\r
322 * \param Buffer Destination Buffer
\r
323 * \param Disk Disk Number
\r
324 * \return Number of sectors read
\r
325 * \note Used as a ::DrvUtil_ReadBlock helper
\r
327 Uint FDD_ReadSectors(Uint64 SectorAddr, Uint Count, void *Buffer, Uint Disk)
\r
332 if( FDD_ReadSector(Disk, SectorAddr, Buffer) != 1 )
\r
335 Buffer = (void*)( (tVAddr)Buffer + 512 );
\r
343 * \fn int FDD_ReadSector(Uint32 Disk, Uint64 SectorAddr, void *Buffer)
\r
344 * \brief Read a sector from disk
\r
345 * \todo Make real-hardware safe (account for read errors)
\r
347 int FDD_ReadSector(Uint32 Disk, Uint64 SectorAddr, void *Buffer)
\r
349 int cyl, head, sec;
\r
352 int lba = SectorAddr;
\r
354 ENTER("iDisk XSectorAddr pBuffer", Disk, SectorAddr, Buffer);
\r
356 if( IOCache_Read( gFDD_Devices[Disk].CacheHandle, SectorAddr, Buffer ) == 1 ) {
\r
361 base = cPORTBASE[Disk >> 1];
\r
363 LOG("Calculating Disk Dimensions");
\r
364 // Get CHS position
\r
365 if(FDD_int_GetDims(gFDD_Devices[Disk].type, lba, &cyl, &head, &sec, &spt) != 1)
\r
370 LOG("Cyl=%i, Head=%i, Sector=%i", cyl, head, sec);
\r
372 LOCK(&glFDD); // Lock to stop the motor stopping on us
\r
373 Time_RemoveTimer(gFDD_Devices[Disk].timer); // Remove Old Timer
\r
374 // Start motor if needed
\r
375 if(gFDD_Devices[Disk].motorState != 2) FDD_int_StartMotor(Disk);
\r
378 LOG("Wait for the motor to spin up");
\r
381 while(gFDD_Devices[Disk].motorState == 1) Threads_Yield();
\r
383 LOG("Acquire Spinlock");
\r
387 outb(base + CALIBRATE_DRIVE, 0);
\r
389 while(FDD_int_SeekTrack(Disk, head, (Uint8)cyl) == 0 && i++ < FDD_SEEK_TIMEOUT )
\r
391 if( i > FDD_SEEK_TIMEOUT ) {
\r
395 //FDD_SensInt(base, NULL, NULL); // Wait for IRQ
\r
397 // Read Data from DMA
\r
398 LOG("Setting DMA for read");
\r
399 DMA_SetChannel(2, 512, 1); // Read 512 Bytes from channel 2
\r
401 LOG("Sending read command");
\r
403 //Threads_Wait(100); // Wait for Head to settle
\r
405 FDD_int_SendByte(base, READ_SECTOR); // Was 0xE6
\r
406 FDD_int_SendByte(base, (head << 2) | (Disk&1));
\r
407 FDD_int_SendByte(base, (Uint8)cyl);
\r
408 FDD_int_SendByte(base, (Uint8)head);
\r
409 FDD_int_SendByte(base, (Uint8)sec);
\r
410 FDD_int_SendByte(base, 0x02); // Bytes Per Sector (Real BPS=128*2^{val})
\r
411 FDD_int_SendByte(base, spt); // SPT
\r
412 FDD_int_SendByte(base, 0x1B); // Gap Length (27 is default)
\r
413 FDD_int_SendByte(base, 0xFF); // Data Length
\r
416 LOG("Waiting for Data to be read");
\r
419 // Read Data from DMA
\r
420 LOG(" FDD_ReadSector: Reading Data");
\r
421 DMA_ReadData(2, 512, Buffer);
\r
423 // Clear Input Buffer
\r
424 LOG("Clearing Input Buffer");
\r
425 FDD_int_GetByte(base); FDD_int_GetByte(base); FDD_int_GetByte(base);
\r
426 FDD_int_GetByte(base); FDD_int_GetByte(base); FDD_int_GetByte(base); FDD_int_GetByte(base);
\r
428 // Release Spinlock
\r
429 LOG("Realeasing Spinlock and setting motor to stop");
\r
432 // Don't turn the motor off now, wait for a while
\r
433 gFDD_Devices[Disk].timer = Time_CreateTimer(MOTOR_OFF_DELAY, FDD_int_StopMotor, (void*)Disk);
435 IOCache_Add( gFDD_Devices[Disk].CacheHandle, SectorAddr, Buffer );
\r
442 * \fn int FDD_WriteSector(Uint32 Disk, Uint64 LBA, void *Buffer)
\r
443 * \brief Write a sector to the floppy disk
\r
444 * \note Not Implemented
\r
446 int FDD_WriteSector(Uint32 Disk, Uint64 LBA, void *Buffer)
\r
448 Warning("[FDD ] Read Only at the moment");
\r
453 * \fn int FDD_int_SeekTrack(int disk, int track)
\r
454 * \brief Seek disk to selected track
\r
456 int FDD_int_SeekTrack(int disk, int head, int track)
\r
461 base = cPORTBASE[disk>>1];
\r
463 // Check if seeking is needed
\r
464 if(gFDD_Devices[disk].track[head] == track)
\r
468 FDD_int_SendByte(base, SEEK_TRACK);
\r
469 FDD_int_SendByte(base, (head<<2)|(disk&1));
\r
470 FDD_int_SendByte(base, track); // Send Seek command
\r
472 FDD_SensInt(base, &sr0, &cyl); // Wait for IRQ
\r
473 if((sr0 & 0xF0) != 0x20) {
474 LOG("sr0 = 0x%x", sr0);
475 return 0; //Check Status
477 if(cyl != track) return 0;
\r
479 // Set Track in structure
\r
480 gFDD_Devices[disk].track[head] = track;
\r
485 * \fn int FDD_int_GetDims(int type, int lba, int *c, int *h, int *s, int *spt)
\r
486 * \brief Get Dimensions of a disk
\r
488 int FDD_int_GetDims(int type, int lba, int *c, int *h, int *s, int *spt)
\r
497 *s = (lba % 9) + 1;
\r
499 *h = (lba / 9) & 1;
\r
505 *s = (lba % 15) + 1;
\r
507 *h = (lba / 15) & 1;
\r
513 *s = (lba % 9) + 1;
\r
515 *h = (lba / 9) & 1;
\r
521 *s = (lba % 18) + 1;
\r
523 *h = (lba / 18) & 1;
\r
524 //Log("1440k - lba=%i(0x%x), *s=%i,*c=%i,*h=%i", lba, lba, *s, *c, *h);
\r
530 *s = (lba % 36) + 1;
\r
532 *h = (lba / 32) & 1;
\r
542 * \fn void FDD_IRQHandler(int Num)
\r
543 * \brief Handles IRQ6
\r
545 void FDD_IRQHandler(int Num)
\r
547 gbFDD_IrqFired = 1;
\r
551 * \fn FDD_WaitIRQ()
\r
552 * \brief Wait for an IRQ6
\r
554 inline void FDD_WaitIRQ()
\r
557 while(!gbFDD_IrqFired) Threads_Yield();
\r
558 gbFDD_IrqFired = 0;
\r
561 void FDD_SensInt(int base, Uint8 *sr0, Uint8 *cyl)
\r
563 FDD_int_SendByte(base, CHECK_INTERRUPT_STATUS);
\r
564 if(sr0) *sr0 = FDD_int_GetByte(base);
\r
565 else FDD_int_GetByte(base);
\r
566 if(cyl) *cyl = FDD_int_GetByte(base);
\r
567 else FDD_int_GetByte(base);
\r
571 * void FDD_int_SendByte(int base, char byte)
\r
572 * \brief Sends a command to the controller
\r
574 void FDD_int_SendByte(int base, char byte)
\r
576 volatile int state;
\r
578 for( ; timeout--; )
\r
580 state = inb(base + PORT_MAINSTATUS);
\r
581 if ((state & 0xC0) == 0x80)
\r
583 outb(base + PORT_DATA, byte);
\r
590 Warning("FDD_int_SendByte - Timeout sending byte 0x%x to base 0x%x\n", byte, base);
\r
595 * int FDD_int_GetByte(int base, char byte)
\r
596 * \brief Receive data from fdd controller
\r
598 int FDD_int_GetByte(int base)
\r
600 volatile int state;
\r
602 for( timeout = 128; timeout--; )
\r
604 state = inb((base + PORT_MAINSTATUS));
\r
605 if ((state & 0xd0) == 0xd0)
\r
606 return inb(base + PORT_DATA);
\r
613 * \brief Recalibrate the specified disk
\r
615 void FDD_Recalibrate(int disk)
\r
617 ENTER("idisk", disk);
\r
619 LOG("Starting Motor");
\r
620 FDD_int_StartMotor(disk);
\r
622 while(gFDD_Devices[disk].motorState == 1) Threads_Yield();
\r
624 LOG("Sending Calibrate Command");
\r
625 FDD_int_SendByte(cPORTBASE[disk>>1], CALIBRATE_DRIVE);
\r
626 FDD_int_SendByte(cPORTBASE[disk>>1], disk&1);
\r
628 LOG("Waiting for IRQ");
\r
630 FDD_SensInt(cPORTBASE[disk>>1], NULL, NULL);
\r
632 LOG("Stopping Motor");
\r
633 FDD_int_StopMotor(disk);
\r
638 * \brief Reset the specified FDD controller
\r
640 void FDD_Reset(int id)
\r
642 int base = cPORTBASE[id];
\r
646 outb(base + PORT_DIGOUTPUT, 0); // Stop Motors & Disable FDC
\r
647 outb(base + PORT_DIGOUTPUT, 0x0C); // Re-enable FDC (DMA and Enable)
\r
649 LOG("Awaiting IRQ");
\r
652 FDD_SensInt(base, NULL, NULL);
\r
654 LOG("Setting Driver Info");
\r
655 outb(base + PORT_DATARATE, 0); // Set data rate to 500K/s
\r
656 FDD_int_SendByte(base, FIX_DRIVE_DATA); // Step and Head Load Times
\r
657 FDD_int_SendByte(base, 0xDF); // Step Rate Time, Head Unload Time (Nibble each)
\r
658 FDD_int_SendByte(base, 0x02); // Head Load Time >> 1
\r
659 while(FDD_int_SeekTrack(0, 0, 1) == 0); // set track
\r
660 while(FDD_int_SeekTrack(0, 1, 1) == 0); // set track
\r
662 LOG("Recalibrating Disk");
\r
663 FDD_Recalibrate((id<<1)|0);
\r
664 FDD_Recalibrate((id<<1)|1);
670 * \fn void FDD_int_TimerCallback()
\r
671 * \brief Called by timer
\r
673 void FDD_int_TimerCallback(int arg)
\r
675 ENTER("iarg", arg);
\r
676 if(gFDD_Devices[arg].motorState == 1)
\r
677 gFDD_Devices[arg].motorState = 2;
\r
678 Time_RemoveTimer(gFDD_Devices[arg].timer);
\r
679 gFDD_Devices[arg].timer = -1;
\r
684 * \fn void FDD_int_StartMotor(char disk)
\r
685 * \brief Starts FDD Motor
\r
687 void FDD_int_StartMotor(int disk)
\r
690 state = inb( cPORTBASE[ disk>>1 ] + PORT_DIGOUTPUT );
\r
691 state |= 1 << (4+disk);
\r
692 outb( cPORTBASE[ disk>>1 ] + PORT_DIGOUTPUT, state );
\r
693 gFDD_Devices[disk].motorState = 1;
\r
694 gFDD_Devices[disk].timer = Time_CreateTimer(MOTOR_ON_DELAY, FDD_int_TimerCallback, (void*)disk);
\r
698 * \fn void FDD_int_StopMotor(int disk)
\r
699 * \brief Stops FDD Motor
\r
701 void FDD_int_StopMotor(int disk)
\r
704 if( IS_LOCKED(&glFDD) ) return ;
\r
705 ENTER("iDisk", disk);
\r
707 state = inb( cPORTBASE[ disk>>1 ] + PORT_DIGOUTPUT );
\r
708 state &= ~( 1 << (4+disk) );
\r
709 outb( cPORTBASE[ disk>>1 ] + PORT_DIGOUTPUT, state );
\r
710 gFDD_Devices[disk].motorState = 0;
\r