| 1 | /* @title: Programmable Interval Timer */ |
| 2 | #pragma once |
| 3 | #include <acpi/ioapic.h> |
| 4 | #include <irq/irq.h> |
| 5 | #include <stdbool.h> |
| 6 | #include <stddef.h> |
| 7 | #include <stdint.h> |
| 8 | #include <time/time.h> |
| 9 | #include <types/types.h> |
| 10 | |
| 11 | /* IO ports */ |
| 12 | #define PIT_PORT_CHANNEL0 0x40 |
| 13 | #define PIT_PORT_CHANNEL1 0x41 |
| 14 | #define PIT_PORT_CHANNEL2 0x42 |
| 15 | #define PIT_PORT_COMMAND 0x43 |
| 16 | |
| 17 | #define PIT_PORT_CHANNEL(ch) ((uint16_t) (PIT_PORT_CHANNEL0 + (ch))) |
| 18 | |
| 19 | /* PIT Base Frequency (1.193182 MHz) */ |
| 20 | #define PIT_BASE_FREQUENCY_HZ 1193182ULL |
| 21 | #define PIT_FREQUENCY 1193182ULL |
| 22 | |
| 23 | /* |
| 24 | * PIT Command Register (PIT_PORT_COMMAND) Bit Definitions |
| 25 | * |
| 26 | * Bit 7..6: Channel Selection |
| 27 | * Bit 5..4: Access / Operating Mode |
| 28 | * Bit 3..1: Operating Mode (0-5) |
| 29 | * Bit 0: BCD / Binary (0 = 16 bit binary, 1 = BCD) |
| 30 | */ |
| 31 | |
| 32 | #define PIT_CMD_CHANNEL(ch) ((uint8_t) (((ch) & 0x3) << 6)) |
| 33 | #define PIT_CMD_CHANNEL0 0x00 |
| 34 | #define PIT_CMD_CHANNEL1 0x40 |
| 35 | #define PIT_CMD_CHANNEL2 0x80 |
| 36 | #define PIT_CMD_READBACK 0xC0 |
| 37 | |
| 38 | #define PIT_CMD_ACCESS_LATCH 0x00 /* Latch count value command */ |
| 39 | #define PIT_CMD_ACCESS_LO 0x10 /* Low byte only */ |
| 40 | #define PIT_CMD_ACCESS_HI 0x20 /* High byte only */ |
| 41 | #define PIT_CMD_ACCESS_LOHI 0x30 /* Low byte then high byte (16 bit) */ |
| 42 | |
| 43 | /* Operating Modes */ |
| 44 | #define PIT_CMD_MODE_ONESHOT 0x00 |
| 45 | #define PIT_CMD_MODE_HW_ONESHOT 0x02 |
| 46 | #define PIT_CMD_MODE_RATE_GEN 0x04 |
| 47 | #define PIT_CMD_MODE_SQUARE_WAVE 0x06 |
| 48 | #define PIT_CMD_MODE_SW_STROBE 0x08 |
| 49 | #define PIT_CMD_MODE_HW_STROBE 0x0A |
| 50 | |
| 51 | /* Binary / BCD */ |
| 52 | #define PIT_CMD_BINARY 0x00 |
| 53 | #define PIT_CMD_BCD 0x01 |
| 54 | |
| 55 | #define PIT_DEFAULT_MODE 0x34 |
| 56 | #define PIT_DEFAULT_COUNT 0x4AF2 |
| 57 | #define PIT_CALIBRATION_MODE 0x30 |
| 58 | #define PIT_CALIBRATION_COUNT UINT16_MAX |
| 59 | |
| 60 | #define PIT_DIVISOR_MIN 1 |
| 61 | #define PIT_DIVISOR_MAX_COUNT UINT16_MAX |
| 62 | #define PIT_DIVISOR_ROLLOVER_VAL 0 /* 0 is 65536 in binary mode */ |
| 63 | |
| 64 | void pit_init(void); |
| 65 | |
| 66 | void pit_set_divisor(uint16_t divisor); |
| 67 | void pit_set_frequency(freq_hz_t freq_hz); |
| 68 | void pit_set_interval_ns(time_ns_t interval_ns); |
| 69 | void pit_set_periodic_mode(uint16_t divisor); |
| 70 | void pit_set_oneshot_mode(uint16_t divisor); |
| 71 | void pit_wire_periodic_nmi(time_ns_t interval_ns); |
| 72 | freq_hz_t pit_measure_tsc_freq(void); |
| 73 | |