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