Cleanup cdrom emulation.
[seabios.git] / src / ata.h
1 // Low level ATA disk definitions
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #ifndef __ATA_H
9 #define __ATA_H
10
11 #include "types.h" // u16
12 #include "atabits.h" // ATA_CB_DH_DEV1
13
14 struct ata_pio_command {
15     void *far_buffer;
16     u8 biosid;
17
18     u8 feature;
19     u8 sector_count;
20     u8 lba_low;
21     u8 lba_mid;
22     u8 lba_high;
23     u8 device;
24     u8 command;
25
26     u8 sector_count2;
27     u8 lba_low2;
28     u8 lba_mid2;
29     u8 lba_high2;
30 };
31
32 // Function definitions
33 void ata_reset(u16 device);
34 int ata_transfer(struct ata_pio_command *cmd);
35 int ata_cmd_packet(u16 device, u8 *cmdbuf, u8 cmdlen
36                    , u32 length, void *far_buffer);
37 int cdrom_read(u16 device, u32 lba, u32 count, void *far_buffer);
38 int cdrom_read_512(u16 device, u32 lba, u32 count, void *far_buffer);
39 void ata_detect();
40
41 static inline int
42 ata_cmd_data(u16 biosid, u16 command, u32 lba, u16 count, void *far_buffer)
43 {
44     u8 slave   = biosid % 2;
45
46     struct ata_pio_command cmd;
47     cmd.far_buffer = far_buffer;
48     cmd.biosid = biosid;
49
50     if (count >= (1<<8) || lba + count >= (1<<28)) {
51         cmd.sector_count2 = count >> 8;
52         cmd.lba_low2 = lba >> 24;
53         cmd.lba_mid2 = 0;
54         cmd.lba_high2 = 0;
55
56         command |= 0x04;
57         lba &= 0xffffff;
58     }
59
60     cmd.feature = 0;
61     cmd.sector_count = count;
62     cmd.lba_low = lba;
63     cmd.lba_mid = lba >> 8;
64     cmd.lba_high = lba >> 16;
65     cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
66                   | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
67     cmd.command = command;
68     return ata_transfer(&cmd);
69 }
70
71 #endif /* __ATA_H */