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 | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 6 | static ushort test_buffer[256]; |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 7 | |
| 8 | void ata_pio_wait_bsy() |
| 9 | { |
| 10 | while (inb(ATA_PORT_CMD) & ATA_BSY) |
| 11 | {} |
| 12 | } |
| 13 | |
| 14 | void ata_pio_wait_drq() |
| 15 | { |
| 16 | while (!(inb(ATA_PORT_CMD) & ATA_RDY)) |
| 17 | {} |
| 18 | } |
| 19 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 20 | uint ata_pio_get_error() |
| 21 | { |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 22 | return inb(ATA_ERR); |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 23 | } |
| 24 | |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 25 | static 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 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 34 | void ata_pio_read_sectors(void *buffer, uint lba, uchar num_sectors) |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 35 | { |
| 36 | ata_pio_wait_bsy(); |
| 37 | |
| 38 | ata_pio_send_init(lba, num_sectors); |
| 39 | outb(ATA_PORT_CMD, ATA_CMD_READ); |
| 40 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 41 | ata_pio_wait_bsy(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 42 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 43 | asm volatile("rep insw" :: |
| 44 | "c"(num_sectors * 256), |
| 45 | "d"(ATA_PORT_DATA), |
| 46 | "D"(buffer)); |
| 47 | |
| 48 | ata_pio_wait_bsy(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 49 | } |
| 50 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 51 | void ata_pio_write_sectors(uint lba, uchar num_sectors, void *buffer) |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 52 | { |
| 53 | ata_pio_wait_bsy(); |
| 54 | |
| 55 | ata_pio_send_init(lba, num_sectors); |
| 56 | outb(ATA_PORT_CMD, ATA_CMD_WRITE); |
| 57 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 58 | ata_pio_wait_bsy(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 59 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 60 | asm volatile("rep outsw" :: |
| 61 | "c"(num_sectors * 256), |
| 62 | "d"(ATA_PORT_DATA), |
| 63 | "S"(buffer)); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 64 | } |
| 65 | |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 66 | static 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 | |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 76 | void test_ata_pio() |
| 77 | { |
| 78 | kprintf("Going to ata_pio_read_sectors\n"); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 79 | |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 80 | 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(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 90 | } |