summaryrefslogtreecommitdiffstats
path: root/src/arch/x86/interface/pcbios/acpi_timer.c
blob: 82e85a0342315adbad6ae8163df8afd81672c378 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2018 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <byteswap.h>
#include <ipxe/io.h>
#include <ipxe/acpi.h>

/** @file
 *
 * ACPI power management timer
 *
 */

/** ACPI timer frequency (fixed 3.579545MHz) */
#define ACPI_TIMER_HZ 3579545

/** ACPI timer mask
 *
 * Timers may be implemented as either 24-bit or 32-bit counters.  We
 * simplify the code by pessimistically assuming that the timer has
 * only 24 bits.
 */
#define ACPI_TIMER_MASK 0x00ffffffUL

/** Power management timer register address */
static unsigned int pm_tmr;

struct timer acpi_timer __timer ( TIMER_PREFERRED );

/**
 * Get current system time in ticks
 *
 * @ret ticks		Current time, in ticks
 */
static unsigned long acpi_currticks ( void ) {
	static unsigned long offset;
	static uint32_t prev;
	uint32_t now;

	/* Read timer and account for wraparound */
	now = ( inl ( pm_tmr ) & ACPI_TIMER_MASK );
	if ( now < prev ) {
		offset += ( ( ACPI_TIMER_MASK + 1 ) /
			    ( ACPI_TIMER_HZ / TICKS_PER_SEC ) );
	}
	prev = now;

	/* Convert to timer ticks */
	return ( offset + ( now / ( ACPI_TIMER_HZ / TICKS_PER_SEC ) ) );
}

/**
 * Delay for a fixed number of microseconds
 *
 * @v usecs		Number of microseconds for which to delay
 */
static void acpi_udelay ( unsigned long usecs ) {
	uint32_t start;
	uint32_t elapsed;
	uint32_t threshold;

	/* Delay until a suitable number of ticks have elapsed.  We do
	 * not need to allow for multiple wraparound, since the
	 * wraparound period for a 24-bit timer at 3.579545MHz is
	 * around 4700000us.
	 */
	start = inl ( pm_tmr );
	threshold = ( ( usecs * ACPI_TIMER_HZ ) / 1000000 );
	do {
		elapsed = ( ( inl ( pm_tmr ) - start ) & ACPI_TIMER_MASK );
	} while ( elapsed < threshold );
}

/**
 * Probe ACPI power management timer
 *
 * @ret rc		Return status code
 */
static int acpi_timer_probe ( void ) {
	struct acpi_fadt fadtab;
	userptr_t fadt;
	unsigned int pm_tmr_blk;

	/* Locate FADT */
	fadt = acpi_find ( FADT_SIGNATURE, 0 );
	if ( ! fadt ) {
		DBGC ( &acpi_timer, "ACPI could not find FADT\n" );
		return -ENOENT;
	}

	/* Read FADT */
	copy_from_user ( &fadtab, fadt, 0, sizeof ( fadtab ) );
	pm_tmr_blk = le32_to_cpu ( fadtab.pm_tmr_blk );
	if ( ! pm_tmr_blk ) {
		DBGC ( &acpi_timer, "ACPI has no timer\n" );
		return -ENOENT;
	}

	/* Record power management timer register address */
	pm_tmr = ( pm_tmr_blk + ACPI_PM_TMR );

	return 0;
}

/** ACPI timer */
struct timer acpi_timer __timer ( TIMER_PREFERRED ) = {
	.name = "acpi",
	.probe = acpi_timer_probe,
	.currticks = acpi_currticks,
	.udelay = acpi_udelay,
};