2 * Acess2 IDE Harddisk Driver
10 #include <tpl_drv_common.h>
13 #define MAX_ATA_DISKS 4
14 #define SECTOR_SIZE 512
15 #define MAX_DMA_SECTORS (0x1000 / SECTOR_SIZE)
17 #define IDE_PRI_BASE 0x1F0
18 #define IDE_SEC_BASE 0x170
20 #define IDE_PRDT_LAST 0x8000
23 \brief Commands to be sent to HDD_CMD
44 Uint16 Usused1[9]; // 10
45 char SerialNum[20]; // 20
46 Uint16 Usused2[3]; // 23
47 char FirmwareVer[8]; // 27
48 char ModelNumber[40]; // 47
49 Uint16 SectPerInt; // 48 - and with 0xFF to get true value;
51 Uint16 Capabilities[2]; // 51
52 Uint16 Unused4[2]; // 53
53 Uint16 ValidExtData; // 54
54 Uint16 Unused5[5]; // 59
55 Uint16 SizeOfRWMultiple; // 60
56 Uint32 Sectors28; // 62
57 Uint16 Unused6[100-62];
59 Uint16 Unused7[256-104];
62 Uint8 BootCode[0x1BE];
65 Uint8 Unused1; // Also CHS Start
66 Uint16 StartHi; // Also CHS Start
68 Uint8 Unused2; // Also CHS Length
69 Uint16 LengthHi; // Also CHS Length
72 } __attribute__ ((packed)) Parts[4];
73 Uint16 BootFlag; // = 0xAA 55
74 } __attribute__ ((packed)) tMBR;
87 tATA_Partition *Partitions;
93 static void ATA_SetupPartitions();
95 int ATA_ScanDisk(int Disk);
96 void ATA_ParseGPT(int Disk);
97 void ATA_ParseMBR(int Disk);
98 void ATA_int_MakePartition(tATA_Partition *Part, int Disk, int Num, Uint64 Start, Uint64 Length);
99 Uint16 ATA_GetBasePort(int Disk);
100 char *ATA_ReadDir(tVFS_Node *Node, int Pos);
101 tVFS_Node *ATA_FindDir(tVFS_Node *Node, char *Name);
102 Uint64 ATA_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
103 int ATA_IOCtl(tVFS_Node *Node, int Id, void *Data);
104 int ATA_Read(Uint8 Disk, Uint64 Address, Uint64 Count, void *Buffer);
105 int ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer);
106 void ATA_IRQHandlerPri(void);
107 void ATA_IRQHandlerSec(void);
108 Uint8 ATA_int_BusMasterReadByte(int Ofs);
109 void ATA_int_BusMasterWriteByte(int Ofs, Uint8 Value);
110 void ATA_int_BusMasterWriteDWord(int Ofs, Uint32 Value);
113 MODULE_DEFINE(0, 0x0032, i386ATA, ATA_Install, NULL);
114 tDevFS_Driver gATA_DriverInfo = {
118 .Flags = VFS_FFLAG_DIRECTORY,
119 .ACLs = &gVFS_ACL_EveryoneRX,
120 .ReadDir = ATA_ReadDir,
121 .FindDir = ATA_FindDir
124 tATA_Disk gATA_Disks[MAX_ATA_DISKS];
126 tVFS_Node **gATA_Nodes;
127 Uint16 gATA_BusMasterBase = 0;
128 Uint8 *gATA_BusMasterBasePtr = 0;
129 int gATA_IRQPri = 14;
130 int gATA_IRQSec = 15;
131 int giaATA_ControllerLock[2] = {0}; //!< Spinlocks for each controller
132 Uint8 gATA_Buffers[2][4096] __attribute__ ((section(".padata")));
133 int gaATA_IRQs[2] = {0};
134 tPRDT_Ent gATA_PRDTs[2] = {
135 {0, 512, IDE_PRDT_LAST},
136 {0, 512, IDE_PRDT_LAST}
141 * \fn int ATA_Install()
148 if(ret != 1) return ret;
150 ATA_SetupPartitions();
154 if( DevFS_AddDevice( &gATA_DriverInfo ) == 0 )
161 * \fn int ATA_SetupIO()
162 * \brief Sets up the ATA controller's DMA mode
171 // Get IDE Controller's PCI Entry
172 ent = PCI_GetDeviceByClass(0x0101, 0xFFFF, -1);
173 LOG("ent = %i\n", ent);
174 gATA_BusMasterBase = PCI_GetBAR4( ent );
175 LOG("gATA_BusMasterBase = 0x%x\n", gATA_BusMasterBase);
176 if( gATA_BusMasterBase == 0 ) {
177 Warning("It seems that there is no Bus Master Controller on this machine, get one");
181 if( !(gATA_BusMasterBase & 1) )
183 if( gATA_BusMasterBase < 0x100000 )
184 gATA_BusMasterBasePtr = (void*)(0xC0000000|gATA_BusMasterBase);
186 gATA_BusMasterBasePtr = (void*)( MM_MapHWPage( gATA_BusMasterBase, 1 ) + (gATA_BusMasterBase&0xFFF) );
189 IRQ_AddHandler( gATA_IRQPri, ATA_IRQHandlerPri );
190 IRQ_AddHandler( gATA_IRQSec, ATA_IRQHandlerSec );
192 gATA_PRDTs[0].PBufAddr = MM_GetPhysAddr( (Uint)&gATA_Buffers[0] );
193 gATA_PRDTs[1].PBufAddr = MM_GetPhysAddr( (Uint)&gATA_Buffers[1] );
195 LOG("gATA_PRDTs = {0x%x, 0x%x}", gATA_PRDTs[0].PBufAddr, gATA_PRDTs[1].PBufAddr);
197 addr = MM_GetPhysAddr( (Uint)&gATA_PRDTs[0] );
198 ATA_int_BusMasterWriteDWord(4, addr);
199 addr = MM_GetPhysAddr( (Uint)&gATA_PRDTs[1] );
200 ATA_int_BusMasterWriteDWord(12, addr);
207 * \fn static void ATA_SetupPartitions()
209 static void ATA_SetupPartitions()
212 for( i = 0; i < MAX_ATA_DISKS; i ++ )
214 if( !ATA_ScanDisk(i) ) {
215 gATA_Disks[i].Name[0] = '\0'; // Mark as unused
222 * \fn void ATA_SetupVFS()
223 * \brief Sets up the ATA drivers VFS information and registers with DevFS
229 // Count number of nodes needed
231 for( i = 0; i < MAX_ATA_DISKS; i++ )
233 if(gATA_Disks[i].Name[0] == '\0') continue; // Ignore
235 giATA_NumNodes += gATA_Disks[i].NumPartitions;
238 // Allocate Node space
239 gATA_Nodes = malloc( giATA_NumNodes * sizeof(void*) );
243 for( i = 0; i < MAX_ATA_DISKS; i++ )
245 if(gATA_Disks[i].Name[0] == '\0') continue; // Ignore
246 gATA_Nodes[ k++ ] = &gATA_Disks[i].Node;
247 for( j = 0; j < gATA_Disks[i].NumPartitions; j ++ )
248 gATA_Nodes[ k++ ] = &gATA_Disks[i].Partitions[j].Node;
253 * \fn int ATA_ScanDisk(int Disk)
255 int ATA_ScanDisk(int Disk)
258 tIdentify *identify = (void*)buf;
259 tMBR *mbr = (void*)buf;
265 base = ATA_GetBasePort( Disk );
267 // Send Disk Selector
268 if(Disk == 1 || Disk == 3)
275 val = inb(base+7); // Read status
276 if(val == 0) return 0; // Disk does not exist
278 // Poll until BSY clears and DRQ sets or ERR is set
279 while( ((val & 0x80) || !(val & 0x08)) && !(val & 1)) val = inb(base+7);
281 if(val & 1) return 0; // Error occured, so return false
284 for(i=0;i<256;i++) buf[i] = inw(base);
286 // Populate Disk Structure
287 if(identify->Sectors48 != 0)
288 gATA_Disks[ Disk ].Sectors = identify->Sectors48;
290 gATA_Disks[ Disk ].Sectors = identify->Sectors28;
293 if( gATA_Disks[ Disk ].Sectors / (2048*1024) )
294 Log("Disk %i: 0x%llx Sectors (%i GiB)", Disk,
295 gATA_Disks[ Disk ].Sectors, gATA_Disks[ Disk ].Sectors / (2048*1024));
296 else if( gATA_Disks[ Disk ].Sectors / 2048 )
297 Log("Disk %i: 0x%llx Sectors (%i MiB)", Disk,
298 gATA_Disks[ Disk ].Sectors, gATA_Disks[ Disk ].Sectors / 2048);
300 Log("Disk %i: 0x%llx Sectors (%i KiB)", Disk,
301 gATA_Disks[ Disk ].Sectors, gATA_Disks[ Disk ].Sectors / 2);
304 gATA_Disks[ Disk ].Name[0] = 'A'+Disk;
305 gATA_Disks[ Disk ].Name[1] = '\0';
307 // Get pointer to vfs node and populate it
308 node = &gATA_Disks[ Disk ].Node;
309 node->Size = gATA_Disks[Disk].Sectors * SECTOR_SIZE;
310 node->NumACLs = 0; // Means Superuser only can access it
311 node->Inode = (Disk << 8) | 0xFF;
312 node->ImplPtr = gATA_Disks[ Disk ].Name;
314 node->ATime = node->MTime
315 = node->CTime = now();
317 node->Read = ATA_ReadFS;
318 //node->Write = ATA_WriteFS;
319 node->IOCtl = ATA_IOCtl;
322 // --- Scan Partitions ---
324 ATA_ReadDMA( Disk, 0, 1, mbr );
326 // Check for a GPT table
327 if(mbr->Parts[0].SystemID == 0xEE)
329 else // No? Just parse the MBR
336 * \fn void ATA_ParseGPT(int Disk)
337 * \brief Parses the GUID Partition Table
339 void ATA_ParseGPT(int Disk)
341 ///\todo Support GPT Disks
342 Warning("GPT Disks are currently unsupported");
346 * \fn void ATA_ParseMBR(int Disk)
348 void ATA_ParseMBR(int Disk)
355 ATA_ReadDMA( Disk, 0, 1, &mbr );
358 gATA_Disks[Disk].NumPartitions = 0;
360 for( i = 0; i < 4; i ++ )
362 if( mbr.Parts[i].SystemID == 0 ) continue;
364 mbr.Parts[i].Boot == 0x0 || mbr.Parts[i].Boot == 0x80 // LBA 28
365 || mbr.Parts[i].Boot == 0x1 || mbr.Parts[i].Boot == 0x81 // LBA 48
368 if( mbr.Parts[i].SystemID == 0xF || mbr.Parts[i].SystemID == 5 ) {
369 if(extendedLBA != 0) {
370 Warning("Disk %i has multiple extended partitions, ignoring rest", Disk);
373 extendedLBA = mbr.Parts[i].LBAStart;
377 gATA_Disks[Disk].NumPartitions ++;
380 // Invalid Partition, so don't count it
382 while(extendedLBA != 0)
384 if( ATA_ReadDMA( Disk, extendedLBA, 1, &mbr ) != 0 )
385 break; // Stop on Errors
389 if( mbr.Parts[0].SystemID == 0 ) continue;
390 if( mbr.Parts[0].Boot == 0x0 || mbr.Parts[0].Boot == 0x80 // LBA 28
391 || mbr.Parts[0].Boot == 0x1 || mbr.Parts[0].Boot == 0x81 // LBA 48
394 if(mbr.Parts[0].SystemID == 0xF || mbr.Parts[0].SystemID == 0x7)
395 extendedLBA = mbr.Parts[0].LBAStart;
397 gATA_Disks[Disk].NumPartitions ++;
400 if( mbr.Parts[1].SystemID == 0 ) continue;
401 if( mbr.Parts[1].Boot == 0x0 || mbr.Parts[1].Boot == 0x80 // LBA 28
402 || mbr.Parts[1].Boot == 0x1 || mbr.Parts[1].Boot == 0x81 // LBA 48
405 if(mbr.Parts[1].SystemID == 0xF || mbr.Parts[1].SystemID == 0x7) {
406 if(extendedLBA == 0) {
407 Warning("Disk %i has twp forward link in the extended partition",
411 extendedLBA = mbr.Parts[1].LBAStart;
414 if(extendedLBA != 0) {
415 Warning("Disk %i lacks a forward link in the extended partition",
419 gATA_Disks[Disk].NumPartitions ++;
424 // Create patition array
425 gATA_Disks[Disk].Partitions = malloc( gATA_Disks[Disk].NumPartitions * sizeof(tATA_Partition) );
427 // --- Fill Partition Info ---
429 for( i = 0; i < 4; i ++ )
431 if( mbr.Parts[i].SystemID == 0 ) continue;
432 if( mbr.Parts[i].Boot == 0x0 || mbr.Parts[i].Boot == 0x80 ) // LBA 28
434 if( mbr.Parts[1].SystemID == 0xF || mbr.Parts[1].SystemID == 5 ) {
435 if(extendedLBA != 0) {
436 Warning("Disk %i has multiple extended partitions, ignoring rest", Disk);
439 extendedLBA = mbr.Parts[1].LBAStart;
443 ATA_int_MakePartition( &gATA_Disks[Disk].Partitions[j], Disk, i,
444 mbr.Parts[i].LBAStart, mbr.Parts[i].LBALength
449 if( mbr.Parts[i].Boot == 0x1 || mbr.Parts[i].Boot == 0x81 ) // LBA 48
451 if( mbr.Parts[i].SystemID == 0xF || mbr.Parts[i].SystemID == 5 ) {
452 if(extendedLBA != 0) {
453 Warning("Disk %i has multiple extended partitions, ignoring rest", Disk);
456 extendedLBA = (mbr.Parts[i].StartHi << 16) | mbr.Parts[i].LBAStart;
459 ATA_int_MakePartition( &gATA_Disks[Disk].Partitions[j], Disk, i,
460 (mbr.Parts[i].StartHi << 16) | mbr.Parts[i].LBAStart,
461 (mbr.Parts[i].LengthHi << 16) | mbr.Parts[i].LBALength
465 // Invalid Partition, so don't count it
467 // Scan extended partition
468 while(extendedLBA != 0)
470 if( ATA_ReadDMA( Disk, extendedLBA, 1, &mbr ) != 0 )
471 break; // Stop on Errors
475 // Check first entry (should be partition)
476 if( mbr.Parts[0].SystemID != 0)
478 if( mbr.Parts[0].Boot == 0x0 || mbr.Parts[0].Boot == 0x80 ) // LBA 28
480 // Forward Link to next Extended partition entry
481 if(mbr.Parts[0].SystemID == 0xF || mbr.Parts[0].SystemID == 0x7)
482 extendedLBA = mbr.Parts[0].LBAStart;
484 ATA_int_MakePartition( &gATA_Disks[Disk].Partitions[j], Disk, k,
485 mbr.Parts[0].LBAStart, mbr.Parts[0].LBALength
490 else if( mbr.Parts[0].Boot == 0x1 || mbr.Parts[0].Boot == 0x81 ) // LBA 48
492 if(mbr.Parts[0].SystemID == 0xF || mbr.Parts[0].SystemID == 0x7)
493 extendedLBA = (mbr.Parts[0].StartHi << 16) | mbr.Parts[0].LBAStart;
495 ATA_int_MakePartition( &gATA_Disks[Disk].Partitions[j], Disk, k,
496 (mbr.Parts[0].StartHi << 16) | mbr.Parts[0].LBAStart,
497 (mbr.Parts[0].LengthHi << 16) | mbr.Parts[0].LBALength
504 // Check second entry (should be forward link)
505 if( mbr.Parts[1].SystemID != 0)
507 if(mbr.Parts[1].Boot == 0x0 || mbr.Parts[1].Boot == 0x80 ) // LBA 28
509 if(mbr.Parts[1].SystemID == 0xF || mbr.Parts[1].SystemID == 0x7) {
510 if(extendedLBA == 0) {
511 Warning("Disk %i has twp forward link in the extended partition",
515 extendedLBA = mbr.Parts[1].LBAStart;
519 if(extendedLBA != 0) {
520 Warning("Disk %i lacks a forward link in the extended partition",
524 ATA_int_MakePartition( &gATA_Disks[Disk].Partitions[j], Disk, k,
525 mbr.Parts[1].LBAStart, mbr.Parts[1].LBALength
531 else if( mbr.Parts[1].Boot == 0x1 || mbr.Parts[1].Boot == 0x81 ) // LBA 48
533 if(mbr.Parts[1].SystemID == 0xF || mbr.Parts[1].SystemID == 0x7) {
534 if(extendedLBA == 0) {
535 Warning("Disk %i has twp forward link in the extended partition",
539 extendedLBA = (mbr.Parts[1].StartHi << 16) | mbr.Parts[1].LBAStart;
543 if(extendedLBA != 0) {
544 Warning("Disk %i lacks a forward link in the extended partition",
548 ATA_int_MakePartition( &gATA_Disks[Disk].Partitions[j], Disk, k,
549 (mbr.Parts[1].StartHi << 16) | mbr.Parts[1].LBAStart,
550 (mbr.Parts[1].LengthHi << 16) | mbr.Parts[1].LBALength
560 * \fn void ATA_int_MakePartition(tATA_Partition *Part, int Disk, int Num, Uint64 Start, Uint64 Length)
561 * \brief Fills a parition's information structure
563 void ATA_int_MakePartition(tATA_Partition *Part, int Disk, int Num, Uint64 Start, Uint64 Length)
565 ENTER("pPart iDisk iNum XStart XLength", Part, Disk, Num, Start, Length);
567 Part->Length = Length;
568 Part->Name[0] = 'A'+Disk;
570 Part->Name[1] = '1'+Num/10;
571 Part->Name[2] = '1'+Num%10;
572 Part->Name[3] = '\0';
574 Part->Name[1] = '1'+Num;
575 Part->Name[2] = '\0';
577 Part->Node.NumACLs = 0; // Only root can read/write raw block devices
578 Part->Node.Inode = (Disk << 8) | Num;
579 Part->Node.ImplPtr = Part->Name;
581 Part->Node.Read = ATA_ReadFS;
582 Part->Node.IOCtl = ATA_IOCtl;
583 LOG("Made '%s' (&Node=%p)", Part->Name, &Part->Node);
588 * \fn Uint16 ATA_GetPortBase(int Disk)
589 * \brief Returns the base port for a given disk
591 Uint16 ATA_GetBasePort(int Disk)
595 case 0: case 1: return IDE_PRI_BASE;
596 case 2: case 3: return IDE_SEC_BASE;
602 * \fn char *ATA_ReadDir(tVFS_Node *Node, int Pos)
604 char *ATA_ReadDir(tVFS_Node *Node, int Pos)
606 if(Pos >= giATA_NumNodes || Pos < 0) return NULL;
607 return gATA_Nodes[Pos]->ImplPtr;
611 * \fn tVFS_Node *ATA_FindDir(tVFS_Node *Node, char *Name)
613 tVFS_Node *ATA_FindDir(tVFS_Node *Node, char *Name)
616 // Check first character
617 if(Name[0] < 'A' || Name[0] > 'A'+MAX_ATA_DISKS)
620 if(Name[1] == '\0') return &gATA_Disks[Name[0]-'A'].Node;
623 if(Name[1] < '0' || '9' < Name[1]) return NULL;
624 if(Name[2] == '\0') { // <= 9
625 part = Name[1] - '0';
627 return &gATA_Disks[Name[0]-'A'].Partitions[part].Node;
630 if('0' > Name[2] || '9' < Name[2]) return NULL;
631 if(Name[3] != '\0') return NULL;
633 part = (Name[1] - '0') * 10;
634 part += Name[2] - '0';
636 return &gATA_Disks[Name[0]-'A'].Partitions[part].Node;
641 * \fn Uint64 ATA_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
643 Uint64 ATA_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
647 Uint64 sector, count;
649 disk = Node->Inode >> 8;
650 part = Node->Inode & 0xFF;
653 if(Offset % SECTOR_SIZE == 0 && Length % SECTOR_SIZE == 0)
655 sector = Offset / SECTOR_SIZE;
656 count = Length / SECTOR_SIZE;
661 if( sector >= gATA_Disks[disk].Sectors ) return 0;
662 if( sector + count > gATA_Disks[disk].Sectors )
663 count = gATA_Disks[disk].Sectors - sector;
665 ret = ATA_Read(disk, sector, count, Buffer);
667 else // Or a partition
669 //Log(" ATA_ReadFS: %i:%i 0x%llx + 0x%llx\n", disk, part,
670 // gATA_Disks[disk].Partitions[part].Start,
671 // gATA_Disks[disk].Partitions[part].Length );
674 if( sector >= gATA_Disks[disk].Partitions[part].Length ) return 0;
675 if( sector + count > gATA_Disks[disk].Partitions[part].Length )
676 count = gATA_Disks[disk].Partitions[part].Length - sector;
679 gATA_Disks[disk].Partitions[part].Start + sector,
683 // Check return value
685 return count * SECTOR_SIZE;
687 Warning("ATA_ReadFS: RETURN 0 (Read failed with ret = %i)", ret);
691 Warning("ATA_ReadFS: RETURN 0 (Non-Aligned Read 0x%llx 0x%llx)", Offset, Length);
696 * \fn Uint64 ATA_WriteFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
698 Uint64 ATA_WriteFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
704 * \fn int ATA_IOCtl(tVFS_Node *Node, int Id, void *Data)
705 * \brief IO Control Funtion
707 int ATA_IOCtl(tVFS_Node *Node, int Id, void *Data)
711 case DRV_IOCTL_TYPE: return DRV_TYPE_DISK;
716 // --- Disk Access ---
718 * \fn int ATA_Read(Uint8 Disk, Uint64 Address, Uint64 Count, void *Buffer)
720 int ATA_Read(Uint8 Disk, Uint64 Address, Uint64 Count, void *Buffer)
725 // Pass straight on to ATA_ReadDMAPage if we can
726 if(Count <= MAX_DMA_SECTORS)
727 return ATA_ReadDMA(Disk, Address, Count, Buffer);
729 // Else we will have to break up the transfer
731 while(Count > MAX_DMA_SECTORS)
733 ret = ATA_ReadDMA(Disk, Address+offset, MAX_DMA_SECTORS, Buffer+offset);
735 if(ret != 1) return ret;
737 Count -= MAX_DMA_SECTORS;
738 offset += MAX_DMA_SECTORS*SECTOR_SIZE;
741 return ATA_ReadDMA(Disk, Address+offset, Count, Buffer+offset);
745 * \fn int ATA_ReadDMAPage(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
747 int ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
749 int cont = (Disk>>1)&1; // Controller ID
753 // Check if the count is small enough
754 if(Count > MAX_DMA_SECTORS) return -1;
756 // Get exclusive access to the disk controller
757 LOCK( &giaATA_ControllerLock[ cont ] );
760 gATA_PRDTs[ cont ].Bytes = Count * SECTOR_SIZE;
763 base = ATA_GetBasePort(Disk);
766 outb(base+0x01, 0x00);
767 if( Address > 0x0FFFFFFF ) // Use LBA48
769 outb(base+0x6, 0x40 | (disk << 4));
770 outb(base+0x2, 0 >> 8); // Upper Sector Count
771 outb(base+0x3, Address >> 24); // Low 2 Addr
772 outb(base+0x3, Address >> 28); // Mid 2 Addr
773 outb(base+0x3, Address >> 32); // High 2 Addr
777 outb(base+0x06, 0xE0 | (disk << 4) | ((Address >> 24) & 0x0F)); //Disk,Magic,High addr
780 outb(base+0x02, (Uint8) Count); // Sector Count
781 outb(base+0x03, (Uint8) Address); // Low Addr
782 outb(base+0x04, (Uint8) (Address >> 8)); // Middle Addr
783 outb(base+0x05, (Uint8) (Address >> 16)); // High Addr
784 if( Address > 0x0FFFFFFF )
785 outb(base+0x07, HDD_DMA_R48); // Read Command (LBA48)
787 outb(base+0x07, HDD_DMA_R28); // Read Command (LBA28)
790 gaATA_IRQs[cont] = 0;
793 ATA_int_BusMasterWriteByte( cont << 3, 9 ); // Read and start
795 // Wait for transfer to complete
796 while( gaATA_IRQs[cont] == 0 ) Threads_Yield();
799 ATA_int_BusMasterWriteByte( cont << 3, 0 ); // Write and stop
801 // Copy to destination buffer
802 memcpy( Buffer, gATA_Buffers[cont], Count*SECTOR_SIZE );
804 // Release controller lock
805 RELEASE( &giaATA_ControllerLock[ cont ] );
811 * \fn void ATA_IRQHandlerPri(void)
813 void ATA_IRQHandlerPri(void)
817 // IRQ bit set for Primary Controller
818 val = ATA_int_BusMasterReadByte( 0x2 );
820 //Log(" ATA_IRQHandlerPri: IRQ hit (val = 0x%x)", val);
821 ATA_int_BusMasterWriteByte( 0x2, 4 );
828 * \fn void ATA_IRQHandlerSec(void)
830 void ATA_IRQHandlerSec(void)
833 // IRQ bit set for Secondary Controller
834 val = ATA_int_BusMasterReadByte( 0xA );
836 //Log(" ATA_IRQHandlerSec: IRQ hit (val = 0x%x)", val);
837 ATA_int_BusMasterWriteByte( 0xA, 4 );
844 * \fn Uint8 ATA_int_BusMasterReadByte(int Ofs)
846 Uint8 ATA_int_BusMasterReadByte(int Ofs)
848 if( gATA_BusMasterBase & 1 )
849 return inb( (gATA_BusMasterBase & ~1) + Ofs );
851 return *(Uint8*)(gATA_BusMasterBasePtr + Ofs);
855 * \fn void ATA_int_BusMasterWriteByte(int Ofs, Uint8 Value)
856 * \brief Writes a byte to a Bus Master Register
858 void ATA_int_BusMasterWriteByte(int Ofs, Uint8 Value)
860 if( gATA_BusMasterBase & 1 )
861 outb( (gATA_BusMasterBase & ~1) + Ofs, Value );
863 *(Uint8*)(gATA_BusMasterBasePtr + Ofs) = Value;
867 * \fn void ATA_int_BusMasterWriteDWord(int Ofs, Uint32 Value)
868 * \brief Writes a dword to a Bus Master Register
870 void ATA_int_BusMasterWriteDWord(int Ofs, Uint32 Value)
873 if( gATA_BusMasterBase & 1 )
874 outd( (gATA_BusMasterBase & ~1) + Ofs, Value );
876 *(Uint32*)(gATA_BusMasterBasePtr + Ofs) = Value;