1/*
2 * Copyright 2014, General Dynamics C4 Systems
3 *
4 * This software may be distributed and modified according to the terms of
5 * the GNU General Public License version 2. Note that NO WARRANTY is provided.
6 * See "LICENSE_GPLv2.txt" for details.
7 *
8 * @TAG(GD_GPL)
9 */
10
11#include <linker.h>
12#include <machine/io.h>
13#include <plat/machine/pit.h>
14
15/* PIT (i8253) registers */
16#define PIT_MODE 0x43
17#define PIT_CH0  0x40
18
19/* Count frequency in Hz */
20#define PIT_HZ 1193182
21
22BOOT_CODE void
23pit_init(void)
24{
25    uint16_t divisor = (PIT_HZ * PIT_WRAPAROUND_MS) / 1000;
26
27    out8(PIT_MODE, 0x34);          /* Set mode 2 and wait for divisor bytes */
28    out8(PIT_CH0, divisor & 0xff); /* Set low byte of divisor */
29    out8(PIT_CH0, divisor >> 8);   /* Set high byte of divisor */
30}
31
32BOOT_CODE void
33pit_wait_wraparound(void)
34{
35    uint16_t count;
36    uint16_t count_old;
37
38    out8(PIT_MODE, 0x00);
39    count = in8(PIT_CH0);
40    count |= (in8(PIT_CH0) << 8);
41    count_old = count;
42
43    while (count <= count_old) {
44        count_old = count;
45        out8(PIT_MODE, 0x00);
46        count = in8(PIT_CH0);
47        count |= (in8(PIT_CH0) << 8);
48    }
49}
50