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 | { |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 33 | outb(ATA_PORT_DRIVE_SEL, 0xe0 | (lba >> 24) & 0x0f); |
| 34 | outb(0x1f1, 0x0); // waste some time |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 35 | outb(ATA_PORT_SECTOR_COUNT, num_sectors); |
| 36 | outb(ATA_PORT_LBA_LOW, lba & 0xff); |
| 37 | outb(ATA_PORT_LBA_MID, (lba >> 8) & 0xff); |
| 38 | outb(ATA_PORT_LBA_HIGH, (lba >> 16) & 0xff); |
| 39 | } |
| 40 | |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 41 | void ata_pio_read_sectors(void *buffer, uint lba, uchar num_sectors) |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 42 | { |
swissChili | 45df330 | 2021-06-30 20:47:51 -0700 | [diff] [blame] | 43 | ushort *word_buffer = buffer; |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 44 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 45 | for (int sector = 0; sector < num_sectors; sector++) |
swissChili | 45df330 | 2021-06-30 20:47:51 -0700 | [diff] [blame] | 46 | { |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 47 | ata_pio_wait_bsy(); |
| 48 | ata_pio_send_init(lba, num_sectors); |
| 49 | outb(ATA_PORT_CMD, ATA_CMD_READ); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 50 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 51 | ata_pio_wait_bsy(); |
| 52 | |
| 53 | if (inb(ATA_PORT_STATUS) & ATA_ERR) |
| 54 | { |
| 55 | kprintf(ERROR "ATA_ERR on read\n"); |
| 56 | kpanic("Failed to read"); |
| 57 | } |
swissChili | 83db6fd | 2021-04-05 21:32:49 -0700 | [diff] [blame] | 58 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 59 | for (int i = 0; i < 256; i++) |
| 60 | { |
| 61 | word_buffer[i + sector * 256] = inw(ATA_PORT_DATA); |
| 62 | } |
| 63 | |
| 64 | ata_pio_wait_bsy(); |
| 65 | outw(ATA_PORT_CMD, ATA_CMD_FLUSH_CACHE); |
| 66 | } |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 67 | } |
| 68 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 69 | void ata_pio_write_sectors(uint lba, uchar num_sectors, ushort *buffer) |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 70 | { |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 71 | for (int sector = 0; sector < num_sectors; sector++) |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 72 | { |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 73 | ata_pio_wait_bsy(); |
| 74 | ata_pio_send_init(lba, num_sectors); |
| 75 | outb(ATA_PORT_CMD, ATA_CMD_WRITE); |
swissChili | 45df330 | 2021-06-30 20:47:51 -0700 | [diff] [blame] | 76 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 77 | ata_pio_wait_bsy(); |
| 78 | |
| 79 | if (inb(ATA_PORT_STATUS) & ATA_ERR) |
| 80 | { |
| 81 | kprintf(ERROR "ATA_ERR on write\n"); |
| 82 | kpanic("Failed to write"); |
| 83 | } |
| 84 | |
| 85 | for (int i = 0; i < 256; i++) |
| 86 | { |
| 87 | outw(ATA_PORT_DATA, buffer[i + sector * 256]); |
| 88 | } |
| 89 | |
| 90 | ata_pio_wait_bsy(); |
| 91 | outw(ATA_PORT_CMD, ATA_CMD_FLUSH_CACHE); |
| 92 | } |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 93 | } |
| 94 | |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 95 | static void print_buffer() |
| 96 | { |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 97 | for (int i = 0; i < 32; i++) |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 98 | { |
| 99 | kprintf("%d ", test_buffer[i]); |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 100 | if ((i + 1) % 16 == 0) |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 101 | kprintf("\n"); |
| 102 | } |
| 103 | } |
| 104 | |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 105 | void test_ata_pio() |
| 106 | { |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 107 | // for (int i = 0; i < 2; i++) |
| 108 | { |
| 109 | kprintf(DEBUG "Writing 0s and reading back\n"); |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 110 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 111 | // for (int i = 0; i < 256; i++) |
| 112 | // test_buffer[i] = 0; |
| 113 | // ata_pio_write_sectors(0, 1, test_buffer); |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 114 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 115 | ata_pio_read_sectors(test_buffer, 2, 1); |
| 116 | print_buffer(); |
| 117 | kprintf(DEBUG "again...\n"); |
| 118 | ata_pio_read_sectors(test_buffer, 2, 1); |
| 119 | print_buffer(); |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 120 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 121 | for (int i = 0; i < 256; i++) |
| 122 | test_buffer[i] = i; |
swissChili | 2e0febf | 2021-04-05 21:44:00 -0700 | [diff] [blame] | 123 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 124 | kprintf(DEBUG "Writing 0..256 and reading back\n"); |
| 125 | |
| 126 | ata_pio_write_sectors(0, 1, test_buffer); |
| 127 | |
| 128 | ata_pio_read_sectors(test_buffer, 2, 1); |
| 129 | print_buffer(); |
| 130 | kprintf(DEBUG "again...\n"); |
| 131 | ata_pio_read_sectors(test_buffer, 2, 1); |
| 132 | print_buffer(); |
| 133 | } |
swissChili | 1a3f07f | 2021-04-05 20:01:55 -0700 | [diff] [blame] | 134 | } |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 135 | |
| 136 | void ata_pio_handle_irq(struct registers *regs) |
| 137 | { |
| 138 | // TODO: check that this IRQ came from the hard drive |
| 139 | |
| 140 | // TODO: use a lock and inform the scheduler that the thread waiting for |
| 141 | // this can stop sleeping |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 142 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 143 | // Acknowledge the IRQ |
| 144 | uchar status = inw(ATA_PORT_STATUS); |
| 145 | |
swissChili | 276b8cf | 2021-07-16 13:24:42 -0700 | [diff] [blame] | 146 | // kprintf(DEBUG "ATA PIO IRQ received %d (0x%x)\n", status, status); |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void init_ata_pio() |
| 150 | { |
| 151 | add_interrupt_handler(46, ata_pio_handle_irq); |
| 152 | |
| 153 | /* |
| 154 | // TODO: Consider adding this back. |
| 155 | |
| 156 | // 0xA0 for master, 0xB0 for slave |
| 157 | outb(ATA_PORT_DRIVE_SEL, 0xA0); |
| 158 | |
| 159 | outb(ATA_PORT_LBA_LOW, 0); |
| 160 | outb(ATA_PORT_LBA_MID, 0); |
| 161 | outb(ATA_PORT_LBA_HIGH, 0); |
| 162 | |
| 163 | outb(ATA_PORT_CMD, ATA_CMD_IDENTIFY); |
| 164 | |
| 165 | if (inb(ATA_PORT_STATUS)) |
| 166 | { |
| 167 | kprintf(OKAY "ATA drive exists\n"); |
| 168 | |
| 169 | // Wait until either DRQ or ERR is set |
| 170 | uchar status; |
| 171 | while ((status = inb(ATA_PORT_STATUS)) & (ATA_DRQ | ATA_ERR)) |
| 172 | { |
| 173 | if (status & ATA_ERR) |
| 174 | { |
| 175 | |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | kprintf(ERROR "ATA drive does not exist\n"); |
| 182 | } |
| 183 | */ |
| 184 | } |