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> |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 5 | #include <pic.h> |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 6 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 7 | /* TODO: Rewrite all of this to work with dri_ide in the case of multiple |
| 8 | * devices */ |
| 9 | |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 10 | static ushort test_buffer[256]; |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 11 | |
| 12 | void ata_pio_wait_bsy() |
| 13 | { |
| 14 | while (inb(ATA_PORT_CMD) & ATA_BSY) |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 15 | { |
| 16 | } |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | void ata_pio_wait_drq() |
| 20 | { |
| 21 | while (!(inb(ATA_PORT_CMD) & ATA_RDY)) |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 22 | { |
| 23 | } |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 24 | } |
| 25 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 26 | uint ata_pio_get_error() |
| 27 | { |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 28 | return inb(ATA_ERR); |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 29 | } |
| 30 | |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 31 | static void ata_pio_send_init(uint lba, uchar num_sectors) |
| 32 | { |
| 33 | outb(ATA_PORT_DRIVE_SEL, 0xe0 | (lba >> 24)); |
| 34 | outb(ATA_PORT_SECTOR_COUNT, num_sectors); |
| 35 | outb(ATA_PORT_LBA_LOW, lba & 0xff); |
| 36 | outb(ATA_PORT_LBA_MID, (lba >> 8) & 0xff); |
| 37 | outb(ATA_PORT_LBA_HIGH, (lba >> 16) & 0xff); |
| 38 | } |
| 39 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 40 | void ata_pio_read_sectors(void *buffer, uint lba, uchar num_sectors) |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 41 | { |
swissChili | 45df330 | 2021-06-30 20:47:51 -0700 | [diff] [blame] | 42 | ushort *word_buffer = buffer; |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 43 | |
swissChili | 45df330 | 2021-06-30 20:47:51 -0700 | [diff] [blame] | 44 | ata_pio_wait_bsy(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 45 | ata_pio_send_init(lba, num_sectors); |
| 46 | outb(ATA_PORT_CMD, ATA_CMD_READ); |
| 47 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 48 | ata_pio_wait_bsy(); |
swissChili | 45df330 | 2021-06-30 20:47:51 -0700 | [diff] [blame] | 49 | |
| 50 | if (inb(ATA_PORT_CMD) & ATA_ERR) |
| 51 | { |
| 52 | kprintf(ERROR "ATA_ERR on read\n"); |
| 53 | kpanic("Failed to read"); |
| 54 | } |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 55 | |
swissChili | 45df330 | 2021-06-30 20:47:51 -0700 | [diff] [blame] | 56 | for (int i = 0; i < num_sectors * 256; i++) |
| 57 | { |
| 58 | word_buffer[i] = inw(ATA_PORT_DATA); |
| 59 | } |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 60 | |
| 61 | ata_pio_wait_bsy(); |
swissChili | 45df330 | 2021-06-30 20:47:51 -0700 | [diff] [blame] | 62 | outw(ATA_PORT_CMD, ATA_CMD_FLUSH_CACHE); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 63 | } |
| 64 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 65 | void ata_pio_write_sectors(uint lba, uchar num_sectors, ushort *buffer) |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 66 | { |
| 67 | ata_pio_wait_bsy(); |
| 68 | |
| 69 | ata_pio_send_init(lba, num_sectors); |
| 70 | outb(ATA_PORT_CMD, ATA_CMD_WRITE); |
| 71 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 72 | ata_pio_wait_bsy(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 73 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 74 | for (int i = 0; i < (num_sectors * 256); i++) |
| 75 | { |
| 76 | outw(ATA_PORT_DATA, buffer[i]); |
| 77 | } |
swissChili | 45df330 | 2021-06-30 20:47:51 -0700 | [diff] [blame] | 78 | |
| 79 | ata_pio_wait_bsy(); |
| 80 | outw(ATA_PORT_CMD, ATA_CMD_FLUSH_CACHE); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 81 | } |
| 82 | |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 83 | static void print_buffer() |
| 84 | { |
| 85 | for (int i = 0; i < 256; i++) |
| 86 | { |
| 87 | kprintf("%d ", test_buffer[i]); |
| 88 | if (i % 16 == 0) |
| 89 | kprintf("\n"); |
| 90 | } |
| 91 | } |
| 92 | |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 93 | void test_ata_pio() |
| 94 | { |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 95 | kprintf(INFO "Going to ata_pio_read_sectors\n"); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 96 | |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 97 | ata_pio_read_sectors(test_buffer, 0, 1); |
| 98 | print_buffer(); |
| 99 | |
| 100 | for (int i = 0; i < 256; i++) |
| 101 | test_buffer[i] = i; |
| 102 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 103 | // ata_pio_write_sectors(0, 1, test_buffer); |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 104 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 105 | // ata_pio_read_sectors(test_buffer, 0, 1); |
| 106 | // print_buffer(); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 107 | } |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 108 | |
| 109 | void ata_pio_handle_irq(struct registers *regs) |
| 110 | { |
| 111 | // TODO: check that this IRQ came from the hard drive |
| 112 | |
| 113 | // TODO: use a lock and inform the scheduler that the thread waiting for |
| 114 | // this can stop sleeping |
| 115 | |
| 116 | // Acknowledge the IRQ |
| 117 | uchar status = inw(ATA_PORT_STATUS); |
| 118 | |
| 119 | kprintf(DEBUG "ATA PIO IRQ received %d (0x%x)\n", status, status); |
| 120 | } |
| 121 | |
| 122 | void init_ata_pio() |
| 123 | { |
| 124 | add_interrupt_handler(46, ata_pio_handle_irq); |
| 125 | |
| 126 | /* |
| 127 | // TODO: Consider adding this back. |
| 128 | |
| 129 | // 0xA0 for master, 0xB0 for slave |
| 130 | outb(ATA_PORT_DRIVE_SEL, 0xA0); |
| 131 | |
| 132 | outb(ATA_PORT_LBA_LOW, 0); |
| 133 | outb(ATA_PORT_LBA_MID, 0); |
| 134 | outb(ATA_PORT_LBA_HIGH, 0); |
| 135 | |
| 136 | outb(ATA_PORT_CMD, ATA_CMD_IDENTIFY); |
| 137 | |
| 138 | if (inb(ATA_PORT_STATUS)) |
| 139 | { |
| 140 | kprintf(OKAY "ATA drive exists\n"); |
| 141 | |
| 142 | // Wait until either DRQ or ERR is set |
| 143 | uchar status; |
| 144 | while ((status = inb(ATA_PORT_STATUS)) & (ATA_DRQ | ATA_ERR)) |
| 145 | { |
| 146 | if (status & ATA_ERR) |
| 147 | { |
| 148 | |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | kprintf(ERROR "ATA drive does not exist\n"); |
| 155 | } |
| 156 | */ |
| 157 | } |