swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 1 | #include <dri/ata_pio/ata_pio.h> |
| 2 | |
| 3 | #include <io.h> |
| 4 | #include <log.h> |
| 5 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 6 | /* TODO: Rewrite all of this to work with dri_ide in the case of multiple |
| 7 | * devices */ |
| 8 | |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 9 | static ushort test_buffer[256]; |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 10 | |
| 11 | void ata_pio_wait_bsy() |
| 12 | { |
| 13 | while (inb(ATA_PORT_CMD) & ATA_BSY) |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 14 | { |
| 15 | } |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | void ata_pio_wait_drq() |
| 19 | { |
| 20 | while (!(inb(ATA_PORT_CMD) & ATA_RDY)) |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 21 | { |
| 22 | } |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 23 | } |
| 24 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 25 | uint ata_pio_get_error() |
| 26 | { |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 27 | return inb(ATA_ERR); |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 28 | } |
| 29 | |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 30 | static 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 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 39 | void ata_pio_read_sectors(void *buffer, uint lba, uchar num_sectors) |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 40 | { |
| 41 | ata_pio_wait_bsy(); |
| 42 | |
| 43 | ata_pio_send_init(lba, num_sectors); |
| 44 | outb(ATA_PORT_CMD, ATA_CMD_READ); |
| 45 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 46 | ata_pio_wait_bsy(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 47 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 48 | asm volatile("rep insw" ::"c"(num_sectors * 256), "d"(ATA_PORT_DATA), |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 49 | "D"(buffer)); |
| 50 | |
| 51 | ata_pio_wait_bsy(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 52 | } |
| 53 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 54 | void ata_pio_write_sectors(uint lba, uchar num_sectors, void *buffer) |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 55 | { |
| 56 | ata_pio_wait_bsy(); |
| 57 | |
| 58 | ata_pio_send_init(lba, num_sectors); |
| 59 | outb(ATA_PORT_CMD, ATA_CMD_WRITE); |
| 60 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 61 | ata_pio_wait_bsy(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 62 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 63 | asm volatile("rep outsw" :: "c"(num_sectors * 256), "d"(ATA_PORT_DATA), |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 64 | "S"(buffer)); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 65 | } |
| 66 | |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 67 | static 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 | |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 77 | void test_ata_pio() |
| 78 | { |
| 79 | kprintf("Going to ata_pio_read_sectors\n"); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 80 | |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 81 | 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 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 87 | // ata_pio_write_sectors(0, 1, test_buffer); |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 88 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 89 | // ata_pio_read_sectors(test_buffer, 0, 1); |
| 90 | // print_buffer(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 91 | } |