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#pragma once
13
14#include <autoconf.h>
15#include <platsupport/gen_config.h>
16#include <platsupport/io.h>
17#include <platsupport/timer.h>
18
19#ifdef CONFIG_IRQ_PIC
20#define PIT_INTERRUPT       0
21#else
22#define PIT_INTERRUPT       2
23#endif
24
25#define PIT_IO_PORT_MIN 0x40
26#define PIT_IO_PORT_MAX 0x43
27
28#define TICKS_PER_SECOND 1193182
29
30#define PIT_NS_TO_TICKS(ns) ((ns) * TICKS_PER_SECOND / NS_IN_S)
31#define PIT_TICKS_TO_NS(ticks) ((uint32_t)(ticks) * (NS_IN_S / TICKS_PER_SECOND))
32
33#define PIT_MIN_TICKS 2
34#define PIT_MAX_TICKS 0xFFFF
35
36#define PIT_MIN_NS PIT_TICKS_TO_NS(PIT_MIN_TICKS)
37#define PIT_MAX_NS PIT_TICKS_TO_NS(PIT_MAX_TICKS)
38
39typedef struct {
40    ps_io_port_ops_t ops;
41} pit_t;
42
43static inline timer_properties_t get_pit_properties(void)
44{
45   return (timer_properties_t) {
46        .upcounter = false,
47        .timeouts = true,
48        .relative_timeouts = true,
49        .periodic_timeouts = true,
50        .absolute_timeouts = false,
51        .bit_width = 16,
52        .irqs = 1
53    };
54}
55
56/*
57 * Get the pit interface. This may only be called once.
58 *
59 * @param io_port_ops io port operations. This is all the pit requires.
60 * @return initialised interface, NULL on error.
61 */
62int pit_init(pit_t *pit, ps_io_port_ops_t io_port_ops);
63int pit_cancel_timeout(pit_t *pit);
64uint64_t pit_get_time(pit_t *pit);
65int pit_set_timeout(pit_t *pit, uint64_t ns, bool periodic);
66