1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#pragma once
14
15#include <stdint.h>
16
17#include <platsupport/timer.h>
18#include <platsupport/plat/acpi/acpi.h>
19#include <platsupport/pmem.h>
20
21#define DEFAULT_HPET_MSI_VECTOR 0
22
23typedef struct PACKED {
24    /* vaddr that HPET_BASE (parsed from acpi tables) is mapped in to.*/
25    void *vaddr;
26    /* irq number of hpet interrupts */
27    uint32_t irq;
28    /* Use IOAPIC instead of FSB delivery */
29    int ioapic_delivery;
30} hpet_config_t;
31
32/* hpet data structures / memory maps */
33typedef struct hpet_timer {
34    uint64_t config;
35    uint64_t comparator;
36    uint64_t fsb_irr;
37    char padding[8];
38} hpet_timer_t;
39
40/* the hpet has one set of global config registers */
41typedef struct hpet {
42    /* Pointer to base address of memory mapped HPET region */
43    void *base_addr;
44    uint64_t period_ns;
45} hpet_t;
46
47static UNUSED timer_properties_t hpet_properties =
48{
49        .upcounter = true,
50        .timeouts = true,
51        .absolute_timeouts = true,
52        .bit_width = 64,
53        .irqs = 1
54};
55
56int hpet_init(hpet_t *hpet, hpet_config_t config);
57int hpet_start(const hpet_t *hpet);
58int hpet_stop(const hpet_t *hpet);
59int hpet_set_timeout(const hpet_t *hept, uint64_t absolute_ns);
60uint64_t hpet_get_time(const hpet_t *hpet);
61
62/* Queries the HPET device mapped at the provided vaddr and returns
63 * whether timer0 (the timer used by hpet_get_timer) supports fsb
64 * deliver */
65bool hpet_supports_fsb_delivery(void *vaddr);
66
67/* Queries the HPET device mapped at the provided vaddr and returns
68 * the mask of interrupts supported by IOAPIC delivery */
69uint32_t hpet_ioapic_irq_delivery_mask(void *vaddr);
70
71/* Queries the HPET device mapped at the provided vaddr and returns
72 * the level bit */
73uint32_t hpet_level(void *vaddr);
74
75/*
76 * Find the HPET details from the ACPI tables.
77 *
78 * @param acpi initialised acpi to find hpet table in,
79 * @param[out] region to populate with details,
80 * @return     0 on success.
81 */
82int hpet_parse_acpi(acpi_t *acpi, pmem_region_t *region);
83