blob: a8ec300fc80001aa19e254c4ff6d03cb14aeed4b [file] [log] [blame]
swissChili1a3f07f2021-04-05 20:01:55 -07001#include <dri/ata_pio/ata_pio.h>
2
3#include <io.h>
4#include <log.h>
5
6static uchar test_buffer[256 * 4];
7
8void ata_pio_wait_bsy()
9{
10 while (inb(ATA_PORT_CMD) & ATA_BSY)
11 {}
12}
13
14void ata_pio_wait_drq()
15{
16 while (!(inb(ATA_PORT_CMD) & ATA_RDY))
17 {}
18}
19
20static void ata_pio_send_init(uint lba, uchar num_sectors)
21{
22 outb(ATA_PORT_DRIVE_SEL, 0xe0 | (lba >> 24));
23 outb(ATA_PORT_SECTOR_COUNT, num_sectors);
24 outb(ATA_PORT_LBA_LOW, lba & 0xff);
25 outb(ATA_PORT_LBA_MID, (lba >> 8) & 0xff);
26 outb(ATA_PORT_LBA_HIGH, (lba >> 16) & 0xff);
27}
28
29void ata_pio_read_sectors(uchar *buffer, uint lba, uchar num_sectors)
30{
31 ata_pio_wait_bsy();
32
33 ata_pio_send_init(lba, num_sectors);
34 outb(ATA_PORT_CMD, ATA_CMD_READ);
35
36 for (int i = 0; i < num_sectors; i++)
37 {
38 ata_pio_wait_bsy();
39 ata_pio_wait_drq();
40
41 for (int j = 0; j < 256; j++)
42 buffer[i * 256 + j] = inb(ATA_PORT_DATA);
43 }
44}
45
46void ata_pio_write_sectors(uint lba, uchar num_sectors, uchar *buffer)
47{
48 ata_pio_wait_bsy();
49
50 ata_pio_send_init(lba, num_sectors);
51 outb(ATA_PORT_CMD, ATA_CMD_WRITE);
52
53 for (int i = 0; i < num_sectors; i++)
54 {
55 ata_pio_wait_bsy();
56 ata_pio_wait_drq();
57
58 for (int j = 0; j < 256; j++)
59 outb(ATA_PORT_DATA, buffer[i * 256 + j]);
60 }
61}
62
63void test_ata_pio()
64{
65 kprintf("Going to ata_pio_read_sectors\n");
66 ata_pio_read_sectors(test_buffer, 0, 4);
67
68 for (int i = 0; i < 256 * 4; i += 64)
69 kprintf("Byte %d = %d\n", test_buffer[i]);
70}