Merge branch 'master' of [email protected]:acess2
[tpg/acess2.git] / Modules / Storage / ATA / main.c
1 /*
2  * Acess2 IDE Harddisk Driver
3  * - main.c
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <modules.h>
8 #include <vfs.h>
9 #include <fs_devfs.h>
10 #include <drv_pci.h>
11 #include <tpl_drv_common.h>
12 #include <tpl_drv_disk.h>
13 #include "common.h"
14
15 // --- Flags ---
16 #define START_BEFORE_CMD        0
17
18 // === STRUCTURES ===
19 typedef struct
20 {
21         Uint32  PBufAddr;
22         Uint16  Bytes;
23         Uint16  Flags;
24 } __attribute__ ((packed))      tPRDT_Ent;
25 typedef struct
26 {
27         Uint16  Flags;          // 1
28         Uint16  Usused1[9];     // 10
29         char    SerialNum[20];  // 20
30         Uint16  Usused2[3];     // 23
31         char    FirmwareVer[8]; // 27
32         char    ModelNumber[40];        // 47
33         Uint16  SectPerInt;     // 48 - AND with 0xFF to get true value;
34         Uint16  Unused3;        // 49
35         Uint16  Capabilities[2];        // 51
36         Uint16  Unused4[2];     // 53
37         Uint16  ValidExtData;   // 54
38         Uint16  Unused5[5];      // 59
39         Uint16  SizeOfRWMultiple;       // 60
40         Uint32  Sectors28;      // 62
41         Uint16  Unused6[100-62];
42         Uint64  Sectors48;
43         Uint16  Unused7[256-104];
44 } __attribute__ ((packed))      tIdentify;
45
46 // === IMPORTS ===
47 extern void     ATA_ParseMBR(int Disk);
48
49 // === PROTOTYPES ===
50  int    ATA_Install();
51  int    ATA_SetupIO();
52 void    ATA_SetupPartitions();
53 void    ATA_SetupVFS();
54  int    ATA_ScanDisk(int Disk);
55 void    ATA_ParseGPT(int Disk);
56 void    ATA_int_MakePartition(tATA_Partition *Part, int Disk, int Num, Uint64 Start, Uint64 Length);
57 Uint16  ATA_GetBasePort(int Disk);
58 // Filesystem Interface
59 char    *ATA_ReadDir(tVFS_Node *Node, int Pos);
60 tVFS_Node       *ATA_FindDir(tVFS_Node *Node, char *Name);
61 Uint64  ATA_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
62 Uint64  ATA_WriteFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
63  int    ATA_IOCtl(tVFS_Node *Node, int Id, void *Data);
64 // Read/Write Interface/Quantiser
65 Uint    ATA_ReadRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk);
66 Uint    ATA_WriteRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk);
67 // Read/Write DMA
68  int    ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer);
69  int    ATA_WriteDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer);
70 // IRQs
71 void    ATA_IRQHandlerPri(int unused);
72 void    ATA_IRQHandlerSec(int unused);
73 // Controller IO
74 Uint8   ATA_int_BusMasterReadByte(int Ofs);
75 void    ATA_int_BusMasterWriteByte(int Ofs, Uint8 Value);
76 void    ATA_int_BusMasterWriteDWord(int Ofs, Uint32 Value);
77
78 // === GLOBALS ===
79 MODULE_DEFINE(0, 0x0032, i386ATA, ATA_Install, NULL, "PCI", NULL);
80 tDevFS_Driver   gATA_DriverInfo = {
81         NULL, "ata",
82         {
83                 .NumACLs = 1,
84                 .Size = -1,
85                 .Flags = VFS_FFLAG_DIRECTORY,
86                 .ACLs = &gVFS_ACL_EveryoneRX,
87                 .ReadDir = ATA_ReadDir,
88                 .FindDir = ATA_FindDir
89         }
90 };
91 tATA_Disk       gATA_Disks[MAX_ATA_DISKS];
92  int    giATA_NumNodes;
93 tVFS_Node       **gATA_Nodes;
94 Uint16  gATA_BusMasterBase = 0;
95 Uint8   *gATA_BusMasterBasePtr = 0;
96  int    gATA_IRQPri = 14;
97  int    gATA_IRQSec = 15;
98  int    giaATA_ControllerLock[2] = {0}; //!< Spinlocks for each controller
99 Uint8   gATA_Buffers[2][4096] __attribute__ ((section(".padata")));
100  int    gaATA_IRQs[2] = {0};
101 tPRDT_Ent       gATA_PRDTs[2] = {
102         {0, 512, IDE_PRDT_LAST},
103         {0, 512, IDE_PRDT_LAST}
104 };
105
106 // === CODE ===
107 /**
108  * \fn int ATA_Install()
109  */
110 int ATA_Install()
111 {
112         int     ret;
113
114         ret = ATA_SetupIO();
115         if(ret) return ret;
116
117         ATA_SetupPartitions();
118
119         ATA_SetupVFS();
120
121         if( DevFS_AddDevice( &gATA_DriverInfo ) == 0 )
122                 return MODULE_ERR_MISC;
123
124         return MODULE_ERR_OK;
125 }
126
127 /**
128  * \fn int ATA_SetupIO()
129  * \brief Sets up the ATA controller's DMA mode
130  */
131 int ATA_SetupIO()
132 {
133          int    ent;
134         tPAddr  addr;
135
136         ENTER("");
137
138         // Get IDE Controller's PCI Entry
139         ent = PCI_GetDeviceByClass(0x0101, 0xFFFF, -1);
140         LOG("ent = %i", ent);
141         gATA_BusMasterBase = PCI_GetBAR4( ent );
142         if( gATA_BusMasterBase == 0 ) {
143                 Log_Warning("ATA", "It seems that there is no Bus Master Controller on this machine. Get one");
144                 // TODO: Use PIO mode instead
145                 LEAVE('i', MODULE_ERR_NOTNEEDED);
146                 return MODULE_ERR_NOTNEEDED;
147         }
148         
149         // Map memory
150         if( !(gATA_BusMasterBase & 1) )
151         {
152                 if( gATA_BusMasterBase < 0x100000 )
153                         gATA_BusMasterBasePtr = (void*)(KERNEL_BASE|gATA_BusMasterBase);
154                 else
155                         gATA_BusMasterBasePtr = (void*)( MM_MapHWPages( gATA_BusMasterBase, 1 ) + (gATA_BusMasterBase&0xFFF) );
156                 LOG("gATA_BusMasterBasePtr = %p", gATA_BusMasterBasePtr);
157         }
158         else {
159                 // Bit 0 is left set as a flag to other functions
160                 LOG("gATA_BusMasterBase = 0x%x", gATA_BusMasterBase & ~1);
161         }
162
163         // Register IRQs and get Buffers
164         IRQ_AddHandler( gATA_IRQPri, ATA_IRQHandlerPri );
165         IRQ_AddHandler( gATA_IRQSec, ATA_IRQHandlerSec );
166
167         gATA_PRDTs[0].PBufAddr = MM_GetPhysAddr( (Uint)&gATA_Buffers[0] );
168         gATA_PRDTs[1].PBufAddr = MM_GetPhysAddr( (Uint)&gATA_Buffers[1] );
169
170         LOG("gATA_PRDTs = {PBufAddr: 0x%x, PBufAddr: 0x%x}", gATA_PRDTs[0].PBufAddr, gATA_PRDTs[1].PBufAddr);
171
172         addr = MM_GetPhysAddr( (Uint)&gATA_PRDTs[0] );
173         LOG("addr = 0x%x", addr);
174         ATA_int_BusMasterWriteDWord(4, addr);
175         addr = MM_GetPhysAddr( (Uint)&gATA_PRDTs[1] );
176         LOG("addr = 0x%x", addr);
177         ATA_int_BusMasterWriteDWord(12, addr);
178
179         // Enable controllers
180         outb(IDE_PRI_BASE+1, 1);
181         outb(IDE_SEC_BASE+1, 1);
182
183         // return
184         LEAVE('i', MODULE_ERR_OK);
185         return MODULE_ERR_OK;
186 }
187
188 /**
189  * \fn void ATA_SetupPartitions()
190  */
191 void ATA_SetupPartitions()
192 {
193          int    i;
194         for( i = 0; i < MAX_ATA_DISKS; i ++ )
195         {
196                 if( !ATA_ScanDisk(i) ) {
197                         gATA_Disks[i].Name[0] = '\0';   // Mark as unused
198                         continue;
199                 }
200         }
201 }
202
203 /**
204  * \fn void ATA_SetupVFS()
205  * \brief Sets up the ATA drivers VFS information and registers with DevFS
206  */
207 void ATA_SetupVFS()
208 {
209          int    i, j, k;
210
211         // Count number of nodes needed
212         giATA_NumNodes = 0;
213         for( i = 0; i < MAX_ATA_DISKS; i++ )
214         {
215                 if(gATA_Disks[i].Name[0] == '\0')       continue;       // Ignore
216                 giATA_NumNodes ++;
217                 giATA_NumNodes += gATA_Disks[i].NumPartitions;
218         }
219
220         // Allocate Node space
221         gATA_Nodes = malloc( giATA_NumNodes * sizeof(void*) );
222
223         // Set nodes
224         k = 0;
225         for( i = 0; i < MAX_ATA_DISKS; i++ )
226         {
227                 if(gATA_Disks[i].Name[0] == '\0')       continue;       // Ignore
228                 gATA_Nodes[ k++ ] = &gATA_Disks[i].Node;
229                 for( j = 0; j < gATA_Disks[i].NumPartitions; j ++ )
230                         gATA_Nodes[ k++ ] = &gATA_Disks[i].Partitions[j].Node;
231         }
232
233         gATA_DriverInfo.RootNode.Size = giATA_NumNodes;
234 }
235
236 /**
237  * \fn int ATA_ScanDisk(int Disk)
238  */
239 int ATA_ScanDisk(int Disk)
240 {
241         Uint16  buf[256];
242         tIdentify       *identify = (void*)buf;
243         tMBR    *mbr = (void*)buf;
244         Uint16  base;
245         Uint8   val;
246          int    i;
247         tVFS_Node       *node;
248
249         ENTER("iDisk", Disk);
250
251         base = ATA_GetBasePort( Disk );
252
253         LOG("base = 0x%x", base);
254
255         // Send Disk Selector
256         if(Disk == 1 || Disk == 3)
257                 outb(base+6, 0xB0);
258         else
259                 outb(base+6, 0xA0);
260
261         // Send IDENTIFY
262         outb(base+7, 0xEC);
263         val = inb(base+7);      // Read status
264         if(val == 0) {
265                 LEAVE('i', 0);
266                 return 0;       // Disk does not exist
267         }
268
269         // Poll until BSY clears and DRQ sets or ERR is set
270         while( ((val & 0x80) || !(val & 0x08)) && !(val & 1))   val = inb(base+7);
271
272         if(val & 1) {
273                 LEAVE('i', 0);
274                 return 0;       // Error occured, so return false
275         }
276
277         // Read Data
278         for(i=0;i<256;i++)      buf[i] = inw(base);
279
280         // Populate Disk Structure
281         if(identify->Sectors48 != 0)
282                 gATA_Disks[ Disk ].Sectors = identify->Sectors48;
283         else
284                 gATA_Disks[ Disk ].Sectors = identify->Sectors28;
285
286
287         LOG("gATA_Disks[ Disk ].Sectors = 0x%x", gATA_Disks[ Disk ].Sectors);
288
289         if( gATA_Disks[ Disk ].Sectors / (2048*1024) )
290                 Log("Disk %i: 0x%llx Sectors (%i GiB)", Disk,
291                         gATA_Disks[ Disk ].Sectors, gATA_Disks[ Disk ].Sectors / (2048*1024));
292         else if( gATA_Disks[ Disk ].Sectors / 2048 )
293                 Log("Disk %i: 0x%llx Sectors (%i MiB)", Disk,
294                         gATA_Disks[ Disk ].Sectors, gATA_Disks[ Disk ].Sectors / 2048);
295         else
296                 Log("Disk %i: 0x%llx Sectors (%i KiB)", Disk,
297                         gATA_Disks[ Disk ].Sectors, gATA_Disks[ Disk ].Sectors / 2);
298
299         // Create Name
300         gATA_Disks[ Disk ].Name[0] = 'A'+Disk;
301         gATA_Disks[ Disk ].Name[1] = '\0';
302
303         // Get pointer to vfs node and populate it
304         node = &gATA_Disks[ Disk ].Node;
305         node->Size = gATA_Disks[Disk].Sectors * SECTOR_SIZE;
306         node->NumACLs = 0;      // Means Superuser only can access it
307         node->Inode = (Disk << 8) | 0xFF;
308         node->ImplPtr = gATA_Disks[ Disk ].Name;
309
310         node->ATime = node->MTime
311                 = node->CTime = now();
312
313         node->Read = ATA_ReadFS;
314         node->Write = ATA_WriteFS;
315         node->IOCtl = ATA_IOCtl;
316
317
318         // --- Scan Partitions ---
319         LOG("Reading MBR");
320         // Read Boot Sector
321         ATA_ReadDMA( Disk, 0, 1, mbr );
322
323         // Check for a GPT table
324         if(mbr->Parts[0].SystemID == 0xEE)
325                 ATA_ParseGPT(Disk);
326         else    // No? Just parse the MBR
327                 ATA_ParseMBR(Disk);
328
329         LEAVE('i', 0);
330         return 1;
331 }
332
333 /**
334  * \fn void ATA_int_MakePartition(tATA_Partition *Part, int Disk, int Num, Uint64 Start, Uint64 Length)
335  * \brief Fills a parition's information structure
336  */
337 void ATA_int_MakePartition(tATA_Partition *Part, int Disk, int Num, Uint64 Start, Uint64 Length)
338 {
339         ENTER("pPart iDisk iNum XStart XLength", Part, Disk, Num, Start, Length);
340         Part->Start = Start;
341         Part->Length = Length;
342         Part->Name[0] = 'A'+Disk;
343         if(Num >= 10) {
344                 Part->Name[1] = '1'+Num/10;
345                 Part->Name[2] = '1'+Num%10;
346                 Part->Name[3] = '\0';
347         } else {
348                 Part->Name[1] = '1'+Num;
349                 Part->Name[2] = '\0';
350         }
351         Part->Node.NumACLs = 0; // Only root can read/write raw block devices
352         Part->Node.Inode = (Disk << 8) | Num;
353         Part->Node.ImplPtr = Part->Name;
354
355         Part->Node.Read = ATA_ReadFS;
356         Part->Node.Write = ATA_WriteFS;
357         Part->Node.IOCtl = ATA_IOCtl;
358         LOG("Made '%s' (&Node=%p)", Part->Name, &Part->Node);
359         LEAVE('-');
360 }
361
362 /**
363  * \fn void ATA_ParseGPT(int Disk)
364  * \brief Parses the GUID Partition Table
365  */
366 void ATA_ParseGPT(int Disk)
367 {
368         ///\todo Support GPT Disks
369         Warning("GPT Disks are currently unsupported");
370 }
371
372 /**
373  * \fn Uint16 ATA_GetPortBase(int Disk)
374  * \brief Returns the base port for a given disk
375  */
376 Uint16 ATA_GetBasePort(int Disk)
377 {
378         switch(Disk)
379         {
380         case 0: case 1:         return IDE_PRI_BASE;
381         case 2: case 3:         return IDE_SEC_BASE;
382         }
383         return 0;
384 }
385
386 /**
387  * \fn char *ATA_ReadDir(tVFS_Node *Node, int Pos)
388  */
389 char *ATA_ReadDir(tVFS_Node *Node, int Pos)
390 {
391         if(Pos >= giATA_NumNodes || Pos < 0)    return NULL;
392         return strdup( gATA_Nodes[Pos]->ImplPtr );
393 }
394
395 /**
396  * \fn tVFS_Node *ATA_FindDir(tVFS_Node *Node, char *Name)
397  */
398 tVFS_Node *ATA_FindDir(tVFS_Node *Node, char *Name)
399 {
400          int    part;
401         // Check first character
402         if(Name[0] < 'A' || Name[0] > 'A'+MAX_ATA_DISKS)
403                 return NULL;
404         // Raw Disk
405         if(Name[1] == '\0') {
406                 if( gATA_Disks[Name[0]-'A'].Sectors == 0 )
407                         return NULL;
408                 return &gATA_Disks[Name[0]-'A'].Node;
409         }
410
411         // Partitions
412         if(Name[1] < '0' || '9' < Name[1])      return NULL;
413         if(Name[2] == '\0') {   // <= 9
414                 part = Name[1] - '0';
415                 part --;
416                 return &gATA_Disks[Name[0]-'A'].Partitions[part].Node;
417         }
418         // > 9
419         if('0' > Name[2] || '9' < Name[2])      return NULL;
420         if(Name[3] != '\0')     return NULL;
421
422         part = (Name[1] - '0') * 10;
423         part += Name[2] - '0';
424         part --;
425         return &gATA_Disks[Name[0]-'A'].Partitions[part].Node;
426
427 }
428
429 /**
430  * \fn Uint64 ATA_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
431  */
432 Uint64 ATA_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
433 {
434          int    disk = Node->Inode >> 8;
435          int    part = Node->Inode & 0xFF;
436
437         // Raw Disk Access
438         if(part == 0xFF)
439         {
440                 if( Offset >= gATA_Disks[disk].Sectors * SECTOR_SIZE )
441                         return 0;
442                 if( Offset + Length > gATA_Disks[disk].Sectors*SECTOR_SIZE )
443                         Length = gATA_Disks[disk].Sectors*SECTOR_SIZE - Offset;
444         }
445         // Partition
446         else
447         {
448                 if( Offset >= gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE )
449                         return 0;
450                 if( Offset + Length > gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE )
451                         Length = gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE - Offset;
452                 Offset += gATA_Disks[disk].Partitions[part].Start * SECTOR_SIZE;
453         }
454
455         //Log("ATA_ReadFS: (Node=%p, Offset=0x%llx, Length=0x%llx, Buffer=%p)", Node, Offset, Length, Buffer);
456         return DrvUtil_ReadBlock(Offset, Length, Buffer, ATA_ReadRaw, SECTOR_SIZE, disk);
457 }
458
459 /**
460  * \fn Uint64 ATA_WriteFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
461  */
462 Uint64 ATA_WriteFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
463 {
464          int    disk = Node->Inode >> 8;
465          int    part = Node->Inode & 0xFF;
466
467         // Raw Disk Access
468         if(part == 0xFF)
469         {
470                 if( Offset >= gATA_Disks[disk].Sectors * SECTOR_SIZE )
471                         return 0;
472                 if( Offset + Length > gATA_Disks[disk].Sectors*SECTOR_SIZE )
473                         Length = gATA_Disks[disk].Sectors*SECTOR_SIZE - Offset;
474         }
475         // Partition
476         else
477         {
478                 if( Offset >= gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE )
479                         return 0;
480                 if( Offset + Length > gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE )
481                         Length = gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE - Offset;
482                 Offset += gATA_Disks[disk].Partitions[part].Start * SECTOR_SIZE;
483         }
484
485         Log("ATA_WriteFS: (Node=%p, Offset=0x%llx, Length=0x%llx, Buffer=%p)", Node, Offset, Length, Buffer);
486         Debug_HexDump("ATA_WriteFS", Buffer, Length);
487         return DrvUtil_WriteBlock(Offset, Length, Buffer, ATA_ReadRaw, ATA_WriteRaw, SECTOR_SIZE, disk);
488 }
489
490 /**
491  * \fn int ATA_IOCtl(tVFS_Node *Node, int Id, void *Data)
492  * \brief IO Control Funtion
493  */
494 int ATA_IOCtl(tVFS_Node *Node, int Id, void *Data)
495 {
496         switch(Id)
497         {
498         case DRV_IOCTL_TYPE:    return DRV_TYPE_DISK;
499         }
500         return 0;
501 }
502
503 // --- Disk Access ---
504 /**
505  * \fn Uint ATA_ReadRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk)
506  */
507 Uint ATA_ReadRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk)
508 {
509          int    ret;
510         Uint    offset;
511         Uint    done = 0;
512
513         // Pass straight on to ATA_ReadDMAPage if we can
514         if(Count <= MAX_DMA_SECTORS)
515         {
516                 ret = ATA_ReadDMA(Disk, Address, Count, Buffer);
517                 if(ret == 0)    return 0;
518                 return Count;
519         }
520
521         // Else we will have to break up the transfer
522         offset = 0;
523         while(Count > MAX_DMA_SECTORS)
524         {
525                 ret = ATA_ReadDMA(Disk, Address+offset, MAX_DMA_SECTORS, Buffer+offset);
526                 // Check for errors
527                 if(ret != 1)    return done;
528                 // Change Position
529                 done += MAX_DMA_SECTORS;
530                 Count -= MAX_DMA_SECTORS;
531                 offset += MAX_DMA_SECTORS*SECTOR_SIZE;
532         }
533
534         ret = ATA_ReadDMA(Disk, Address+offset, Count, Buffer+offset);
535         if(ret != 1)    return 0;
536         return done+Count;
537 }
538
539 /**
540  * \fn Uint ATA_WriteRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk)
541  */
542 Uint ATA_WriteRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk)
543 {
544          int    ret;
545         Uint    offset;
546         Uint    done = 0;
547
548         // Pass straight on to ATA_WriteDMA if we can
549         if(Count <= MAX_DMA_SECTORS)
550         {
551                 ret = ATA_WriteDMA(Disk, Address, Count, Buffer);
552                 if(ret == 0)    return 0;
553                 return Count;
554         }
555
556         // Else we will have to break up the transfer
557         offset = 0;
558         while(Count > MAX_DMA_SECTORS)
559         {
560                 ret = ATA_WriteDMA(Disk, Address+offset, MAX_DMA_SECTORS, Buffer+offset);
561                 // Check for errors
562                 if(ret != 1)    return done;
563                 // Change Position
564                 done += MAX_DMA_SECTORS;
565                 Count -= MAX_DMA_SECTORS;
566                 offset += MAX_DMA_SECTORS*SECTOR_SIZE;
567         }
568
569         ret = ATA_WriteDMA(Disk, Address+offset, Count, Buffer+offset);
570         if(ret != 1)    return 0;
571         return done+Count;
572 }
573
574 /**
575  * \fn int ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
576  */
577 int ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
578 {
579          int    cont = (Disk>>1)&1;     // Controller ID
580          int    disk = Disk & 1;
581         Uint16  base;
582
583         ENTER("iDisk XAddress iCount pBuffer", Disk, Address, Count, Buffer);
584
585         // Check if the count is small enough
586         if(Count > MAX_DMA_SECTORS) {
587                 Warning("Passed too many sectors for a bulk DMA read (%i > %i)",
588                         Count, MAX_DMA_SECTORS);
589                 LEAVE('i');
590                 return 0;
591         }
592
593         // Get exclusive access to the disk controller
594         LOCK( &giaATA_ControllerLock[ cont ] );
595
596         // Set Size
597         gATA_PRDTs[ cont ].Bytes = Count * SECTOR_SIZE;
598
599         // Get Port Base
600         base = ATA_GetBasePort(Disk);
601
602         // Reset IRQ Flag
603         gaATA_IRQs[cont] = 0;
604
605         // Set up transfer
606         outb(base+0x01, 0x00);
607         if( Address > 0x0FFFFFFF )      // Use LBA48
608         {
609                 outb(base+0x6, 0x40 | (disk << 4));
610                 outb(base+0x2, 0 >> 8); // Upper Sector Count
611                 outb(base+0x3, Address >> 24);  // Low 2 Addr
612                 outb(base+0x3, Address >> 28);  // Mid 2 Addr
613                 outb(base+0x3, Address >> 32);  // High 2 Addr
614         }
615         else
616         {
617                 outb(base+0x06, 0xE0 | (disk << 4) | ((Address >> 24) & 0x0F)); //Disk,Magic,High addr
618         }
619
620         outb(base+0x02, (Uint8) Count);         // Sector Count
621         outb(base+0x03, (Uint8) Address);               // Low Addr
622         outb(base+0x04, (Uint8) (Address >> 8));        // Middle Addr
623         outb(base+0x05, (Uint8) (Address >> 16));       // High Addr
624
625         LOG("Starting Transfer");
626         #if START_BEFORE_CMD
627         // Start transfer
628         ATA_int_BusMasterWriteByte( cont << 3, 9 );     // Read and start
629         if( Address > 0x0FFFFFFF )
630                 outb(base+0x07, HDD_DMA_R48);   // Read Command (LBA48)
631         else
632                 outb(base+0x07, HDD_DMA_R28);   // Read Command (LBA28)
633         #else
634         if( Address > 0x0FFFFFFF )
635                 outb(base+0x07, HDD_DMA_R48);   // Read Command (LBA48)
636         else
637                 outb(base+0x07, HDD_DMA_R28);   // Read Command (LBA28)
638         // Start transfer
639         ATA_int_BusMasterWriteByte( cont << 3, 9 );     // Read and start
640         #endif
641
642         // Wait for transfer to complete
643         //ATA_int_BusMasterWriteByte( (cont << 3) + 2, 0x4 );
644         while( gaATA_IRQs[cont] == 0 ) {
645                 //Uint8 val = ATA_int_BusMasterReadByte( (cont << 3) + 2, 0x4 );
646                 //LOG("val = 0x%02x", val);
647                 Threads_Yield();
648         }
649
650         // Complete Transfer
651         ATA_int_BusMasterWriteByte( cont << 3, 0 );     // Write and stop
652
653         LOG("Transfer Completed & Acknowledged");
654
655         // Copy to destination buffer
656         memcpy( Buffer, gATA_Buffers[cont], Count*SECTOR_SIZE );
657
658         // Release controller lock
659         RELEASE( &giaATA_ControllerLock[ cont ] );
660
661         LEAVE('i', 1);
662         return 1;
663 }
664
665 /**
666  * \fn int ATA_WriteDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
667  */
668 int ATA_WriteDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
669 {
670          int    cont = (Disk>>1)&1;     // Controller ID
671          int    disk = Disk & 1;
672         Uint16  base;
673
674         // Check if the count is small enough
675         if(Count > MAX_DMA_SECTORS)     return 0;
676
677         // Get exclusive access to the disk controller
678         LOCK( &giaATA_ControllerLock[ cont ] );
679
680         // Set Size
681         gATA_PRDTs[ cont ].Bytes = Count * SECTOR_SIZE;
682
683         // Get Port Base
684         base = ATA_GetBasePort(Disk);
685
686         // Set up transfer
687         outb(base+0x01, 0x00);
688         if( Address > 0x0FFFFFFF )      // Use LBA48
689         {
690                 outb(base+0x6, 0x40 | (disk << 4));
691                 outb(base+0x2, 0 >> 8); // Upper Sector Count
692                 outb(base+0x3, Address >> 24);  // Low 2 Addr
693                 outb(base+0x3, Address >> 28);  // Mid 2 Addr
694                 outb(base+0x3, Address >> 32);  // High 2 Addr
695         }
696         else
697         {
698                 outb(base+0x06, 0xE0 | (disk << 4) | ((Address >> 24) & 0x0F)); //Disk,Magic,High addr
699         }
700
701         outb(base+0x02, (Uint8) Count);         // Sector Count
702         outb(base+0x03, (Uint8) Address);               // Low Addr
703         outb(base+0x04, (Uint8) (Address >> 8));        // Middle Addr
704         outb(base+0x05, (Uint8) (Address >> 16));       // High Addr
705         if( Address > 0x0FFFFFFF )
706                 outb(base+0x07, HDD_DMA_W48);   // Write Command (LBA48)
707         else
708                 outb(base+0x07, HDD_DMA_W28);   // Write Command (LBA28)
709
710         // Reset IRQ Flag
711         gaATA_IRQs[cont] = 0;
712
713         // Copy to output buffer
714         memcpy( gATA_Buffers[cont], Buffer, Count*SECTOR_SIZE );
715
716         // Start transfer
717         ATA_int_BusMasterWriteByte( cont << 3, 1 );     // Write and start
718
719         // Wait for transfer to complete
720         while( gaATA_IRQs[cont] == 0 )  Threads_Yield();
721
722         // Complete Transfer
723         ATA_int_BusMasterWriteByte( cont << 3, 0 );     // Write and stop
724
725         // Release controller lock
726         RELEASE( &giaATA_ControllerLock[ cont ] );
727
728         return 1;
729 }
730
731 /**
732  * \fn void ATA_IRQHandlerPri(int unused)
733  */
734 void ATA_IRQHandlerPri(int unused)
735 {
736         Uint8   val;
737
738         // IRQ bit set for Primary Controller
739         val = ATA_int_BusMasterReadByte( 0x2 );
740         LOG("IRQ val = 0x%x", val);
741         if(val & 4) {
742                 LOG("IRQ hit (val = 0x%x)", val);
743                 ATA_int_BusMasterWriteByte( 0x2, 4 );
744                 gaATA_IRQs[0] = 1;
745                 return ;
746         }
747 }
748
749 /**
750  * \fn void ATA_IRQHandlerSec(int unused)
751  */
752 void ATA_IRQHandlerSec(int unused)
753 {
754         Uint8   val;
755         // IRQ bit set for Secondary Controller
756         val = ATA_int_BusMasterReadByte( 0xA );
757         LOG("IRQ val = 0x%x", val);
758         if(val & 4) {
759                 LOG("IRQ hit (val = 0x%x)", val);
760                 ATA_int_BusMasterWriteByte( 0xA, 4 );
761                 gaATA_IRQs[1] = 1;
762                 return ;
763         }
764 }
765
766 /**
767  * \fn Uint8 ATA_int_BusMasterReadByte(int Ofs)
768  */
769 Uint8 ATA_int_BusMasterReadByte(int Ofs)
770 {
771         if( gATA_BusMasterBase & 1 )
772                 return inb( (gATA_BusMasterBase & ~1) + Ofs );
773         else
774                 return *(Uint8*)(gATA_BusMasterBasePtr + Ofs);
775 }
776
777 /**
778  * \fn void ATA_int_BusMasterWriteByte(int Ofs, Uint8 Value)
779  * \brief Writes a byte to a Bus Master Register
780  */
781 void ATA_int_BusMasterWriteByte(int Ofs, Uint8 Value)
782 {
783         if( gATA_BusMasterBase & 1 )
784                 outb( (gATA_BusMasterBase & ~1) + Ofs, Value );
785         else
786                 *(Uint8*)(gATA_BusMasterBasePtr + Ofs) = Value;
787 }
788
789 /**
790  * \fn void ATA_int_BusMasterWriteDWord(int Ofs, Uint32 Value)
791  * \brief Writes a dword to a Bus Master Register
792  */
793 void ATA_int_BusMasterWriteDWord(int Ofs, Uint32 Value)
794 {
795
796         if( gATA_BusMasterBase & 1 )
797                 outd( (gATA_BusMasterBase & ~1) + Ofs, Value );
798         else
799                 *(Uint32*)(gATA_BusMasterBasePtr + Ofs) = Value;
800 }

UCC git Repository :: git.ucc.asn.au