blob: cd45998f62c825835127ab3c21bf66243f616888 [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
swissChili2e0febf2021-04-05 21:44:00 -07006static ushort test_buffer[256];
swissChili1a3f07f2021-04-05 20:01:55 -07007
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
swissChili83db6fd2021-04-05 21:32:49 -070020uint ata_pio_get_error()
21{
swissChili2e0febf2021-04-05 21:44:00 -070022 return inb(ATA_ERR);
swissChili83db6fd2021-04-05 21:32:49 -070023}
24
swissChili1a3f07f2021-04-05 20:01:55 -070025static void ata_pio_send_init(uint lba, uchar num_sectors)
26{
27 outb(ATA_PORT_DRIVE_SEL, 0xe0 | (lba >> 24));
28 outb(ATA_PORT_SECTOR_COUNT, num_sectors);
29 outb(ATA_PORT_LBA_LOW, lba & 0xff);
30 outb(ATA_PORT_LBA_MID, (lba >> 8) & 0xff);
31 outb(ATA_PORT_LBA_HIGH, (lba >> 16) & 0xff);
32}
33
swissChili83db6fd2021-04-05 21:32:49 -070034void ata_pio_read_sectors(void *buffer, uint lba, uchar num_sectors)
swissChili1a3f07f2021-04-05 20:01:55 -070035{
36 ata_pio_wait_bsy();
37
38 ata_pio_send_init(lba, num_sectors);
39 outb(ATA_PORT_CMD, ATA_CMD_READ);
40
swissChili83db6fd2021-04-05 21:32:49 -070041 ata_pio_wait_bsy();
swissChili1a3f07f2021-04-05 20:01:55 -070042
swissChili83db6fd2021-04-05 21:32:49 -070043 asm volatile("rep insw" ::
44 "c"(num_sectors * 256),
45 "d"(ATA_PORT_DATA),
46 "D"(buffer));
47
48 ata_pio_wait_bsy();
swissChili1a3f07f2021-04-05 20:01:55 -070049}
50
swissChili83db6fd2021-04-05 21:32:49 -070051void ata_pio_write_sectors(uint lba, uchar num_sectors, void *buffer)
swissChili1a3f07f2021-04-05 20:01:55 -070052{
53 ata_pio_wait_bsy();
54
55 ata_pio_send_init(lba, num_sectors);
56 outb(ATA_PORT_CMD, ATA_CMD_WRITE);
57
swissChili83db6fd2021-04-05 21:32:49 -070058 ata_pio_wait_bsy();
swissChili1a3f07f2021-04-05 20:01:55 -070059
swissChili83db6fd2021-04-05 21:32:49 -070060 asm volatile("rep outsw" ::
61 "c"(num_sectors * 256),
62 "d"(ATA_PORT_DATA),
63 "S"(buffer));
swissChili1a3f07f2021-04-05 20:01:55 -070064}
65
swissChili2e0febf2021-04-05 21:44:00 -070066static void print_buffer()
67{
68 for (int i = 0; i < 256; i++)
69 {
70 kprintf("%d ", test_buffer[i]);
71 if (i % 16 == 0)
72 kprintf("\n");
73 }
74}
75
swissChili1a3f07f2021-04-05 20:01:55 -070076void test_ata_pio()
77{
78 kprintf("Going to ata_pio_read_sectors\n");
swissChili1a3f07f2021-04-05 20:01:55 -070079
swissChili2e0febf2021-04-05 21:44:00 -070080 ata_pio_read_sectors(test_buffer, 0, 1);
81 print_buffer();
82
83 for (int i = 0; i < 256; i++)
84 test_buffer[i] = i;
85
86 ata_pio_write_sectors(0, 1, test_buffer);
87
88 ata_pio_read_sectors(test_buffer, 0, 1);
89 print_buffer();
swissChili1a3f07f2021-04-05 20:01:55 -070090}