swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 1 | #include <alloc.h> |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 2 | #include <dri/ata_pio/ata_pio.h> |
| 3 | #include <dri/ide/ide.h> |
swissChili | 1b83922 | 2021-06-03 13:54:40 -0700 | [diff] [blame] | 4 | #include <log.h> |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 5 | #include <task.h> |
swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 6 | |
| 7 | struct ide_thread_data |
| 8 | { |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 9 | struct pci_device dev; |
| 10 | uchar bus, slot, func; |
swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 11 | }; |
| 12 | |
| 13 | bool ide_supports(struct pci_device *dev) |
| 14 | { |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 15 | return dev->class == 1 && dev->subclass == 1; |
swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 16 | } |
| 17 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 18 | void ide_print_device(struct ide_device *dev) |
| 19 | { |
swissChili | d00ec61 | 2022-01-16 21:21:29 -0800 | [diff] [blame] | 20 | kprintf(INFO "IDE device dma=%b\n", dev->supports_dma); |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 21 | } |
| 22 | |
swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 23 | void ide_thread(struct ide_thread_data *data) |
| 24 | { |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 25 | struct ide_device dev; |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 26 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 27 | uchar p = data->dev.prog_if; |
| 28 | dev.channel_mode[0] = p & 1; |
| 29 | dev.channel_mode_modifiable[0] = p & (1 << 1); |
| 30 | dev.channel_mode[1] = p & (1 << 2); |
| 31 | dev.channel_mode_modifiable[1] = p & (1 << 3); |
| 32 | dev.supports_dma = p & (1 << 7); |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 33 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 34 | ide_print_device(&dev); |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 35 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 36 | // TODO: pass ATA PIO driver information about the IDE device (i.e. what |
| 37 | // ports to use) |
| 38 | init_ata_pio(); |
| 39 | |
| 40 | kprintf(OKAY "Set up ATA PIO\n"); |
| 41 | |
| 42 | free(data); |
swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void ide_init(struct pci_device dev, uchar bus, uchar slot, uchar func) |
| 46 | { |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 47 | struct ide_thread_data *data = malloc(sizeof(struct ide_thread_data)); |
| 48 | data->dev = dev; |
| 49 | data->bus = bus; |
| 50 | data->slot = slot; |
| 51 | data->func = func; |
swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 52 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 53 | ide_thread(data); |
swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void ide_register() |
| 57 | { |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 58 | struct pci_device_driver dri = { |
| 59 | .supports = ide_supports, |
| 60 | .init = ide_init, |
| 61 | .generic_name = "IDE Controller", |
| 62 | }; |
swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 63 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 64 | pci_register_device_driver(dri); |
swissChili | 5fe85a1 | 2021-05-31 08:10:27 -0700 | [diff] [blame] | 65 | } |