blob: cf55b517fe0aee789484517529353447621a88b6 [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
swissChilief829f32021-06-13 20:00:54 -07006/* TODO: Rewrite all of this to work with dri_ide in the case of multiple
7 * devices */
8
swissChili2e0febf2021-04-05 21:44:00 -07009static ushort test_buffer[256];
swissChili1a3f07f2021-04-05 20:01:55 -070010
11void ata_pio_wait_bsy()
12{
13 while (inb(ATA_PORT_CMD) & ATA_BSY)
swissChilief829f32021-06-13 20:00:54 -070014 {
15 }
swissChili1a3f07f2021-04-05 20:01:55 -070016}
17
18void ata_pio_wait_drq()
19{
20 while (!(inb(ATA_PORT_CMD) & ATA_RDY))
swissChilief829f32021-06-13 20:00:54 -070021 {
22 }
swissChili1a3f07f2021-04-05 20:01:55 -070023}
24
swissChili83db6fd2021-04-05 21:32:49 -070025uint ata_pio_get_error()
26{
swissChili2e0febf2021-04-05 21:44:00 -070027 return inb(ATA_ERR);
swissChili83db6fd2021-04-05 21:32:49 -070028}
29
swissChili1a3f07f2021-04-05 20:01:55 -070030static void ata_pio_send_init(uint lba, uchar num_sectors)
31{
32 outb(ATA_PORT_DRIVE_SEL, 0xe0 | (lba >> 24));
33 outb(ATA_PORT_SECTOR_COUNT, num_sectors);
34 outb(ATA_PORT_LBA_LOW, lba & 0xff);
35 outb(ATA_PORT_LBA_MID, (lba >> 8) & 0xff);
36 outb(ATA_PORT_LBA_HIGH, (lba >> 16) & 0xff);
37}
38
swissChili83db6fd2021-04-05 21:32:49 -070039void ata_pio_read_sectors(void *buffer, uint lba, uchar num_sectors)
swissChili1a3f07f2021-04-05 20:01:55 -070040{
41 ata_pio_wait_bsy();
42
43 ata_pio_send_init(lba, num_sectors);
44 outb(ATA_PORT_CMD, ATA_CMD_READ);
45
swissChili83db6fd2021-04-05 21:32:49 -070046 ata_pio_wait_bsy();
swissChili1a3f07f2021-04-05 20:01:55 -070047
swissChilief829f32021-06-13 20:00:54 -070048 asm volatile("rep insw" ::"c"(num_sectors * 256), "d"(ATA_PORT_DATA),
swissChili83db6fd2021-04-05 21:32:49 -070049 "D"(buffer));
50
51 ata_pio_wait_bsy();
swissChili1a3f07f2021-04-05 20:01:55 -070052}
53
swissChili83db6fd2021-04-05 21:32:49 -070054void ata_pio_write_sectors(uint lba, uchar num_sectors, void *buffer)
swissChili1a3f07f2021-04-05 20:01:55 -070055{
56 ata_pio_wait_bsy();
57
58 ata_pio_send_init(lba, num_sectors);
59 outb(ATA_PORT_CMD, ATA_CMD_WRITE);
60
swissChili83db6fd2021-04-05 21:32:49 -070061 ata_pio_wait_bsy();
swissChili1a3f07f2021-04-05 20:01:55 -070062
swissChilief829f32021-06-13 20:00:54 -070063 asm volatile("rep outsw" :: "c"(num_sectors * 256), "d"(ATA_PORT_DATA),
swissChili83db6fd2021-04-05 21:32:49 -070064 "S"(buffer));
swissChili1a3f07f2021-04-05 20:01:55 -070065}
66
swissChili2e0febf2021-04-05 21:44:00 -070067static void print_buffer()
68{
69 for (int i = 0; i < 256; i++)
70 {
71 kprintf("%d ", test_buffer[i]);
72 if (i % 16 == 0)
73 kprintf("\n");
74 }
75}
76
swissChili1a3f07f2021-04-05 20:01:55 -070077void test_ata_pio()
78{
swissChili9bd74de2021-06-15 20:30:48 -070079 kprintf(INFO "Going to ata_pio_read_sectors\n");
swissChili1a3f07f2021-04-05 20:01:55 -070080
swissChili2e0febf2021-04-05 21:44:00 -070081 ata_pio_read_sectors(test_buffer, 0, 1);
82 print_buffer();
83
84 for (int i = 0; i < 256; i++)
85 test_buffer[i] = i;
86
swissChilief829f32021-06-13 20:00:54 -070087 // ata_pio_write_sectors(0, 1, test_buffer);
swissChili2e0febf2021-04-05 21:44:00 -070088
swissChilief829f32021-06-13 20:00:54 -070089 // ata_pio_read_sectors(test_buffer, 0, 1);
90 // print_buffer();
swissChili1a3f07f2021-04-05 20:01:55 -070091}