ec6ddd5473ba4f8651e364b197378a78136d216e
[coreboot.git] / src / cpu / amd / agesa / apic_timer.c
1 /*
2  *****************************************************************************
3  *
4  * This file is part of the coreboot project.
5  *
6  * Copyright (C) 2011 Advanced Micro Devices, Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  * ***************************************************************************
21  *
22  */
23
24 #include <stdint.h>
25 #include <delay.h>
26 #include <cpu/x86/msr.h>
27 #include <cpu/x86/lapic.h>
28
29 /* NOTE: We use the APIC TIMER register is to hold flags for AP init during
30  * pre-memory init (__PRE_RAM__). Don't use init_timer() and  udelay is
31  * redirected to udelay_tsc().
32  */
33
34
35 void init_timer(void)
36 {
37         /* Set the apic timer to no interrupts and periodic mode */
38         lapic_write(LAPIC_LVTT, (1 << 17)|(1<< 16)|(0 << 12)|(0 << 0));
39
40         /* Set the divider to 1, no divider */
41         lapic_write(LAPIC_TDCR, LAPIC_TDR_DIV_1);
42
43         /* Set the initial counter to 0xffffffff */
44         lapic_write(LAPIC_TMICT, 0xffffffff);
45
46 }
47
48
49 void udelay(u32 usecs)
50 {
51         u32 start, value, ticks;
52         /* Calculate the number of ticks to run, our FSB runs a 200Mhz */
53         ticks = usecs * 200;
54         start = lapic_read(LAPIC_TMCCT);
55         do {
56                 value = lapic_read(LAPIC_TMCCT);
57         } while((start - value) < ticks);
58
59 }