3 * Daniel Engström, Omicron Ceti AB, daniel@omicron.se.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <asm/i8254.h>
27 #include <asm/ibmpc.h>
30 static volatile unsigned long system_ticks;
31 static int timer_init_done =0;
33 static void timer_isr(void *unused)
38 unsigned long get_system_ticks(void)
43 #define TIMER0_VALUE 0x04aa /* 1kHz 1.9318MHz / 1000 */
44 #define TIMER2_VALUE 0x0a8e /* 440Hz */
50 irq_install_handler(0, timer_isr, NULL);
52 /* initialize timer 0 and 2
54 * Timer 0 is used to increment system_tick 1000 times/sec
55 * Timer 1 was used for DRAM refresh in early PC's
56 * Timer 2 is used to drive the speaker
57 * (to stasrt a beep: write 3 to port 0x61,
58 * to stop it again: write 0)
61 outb(PIT_CMD_CTR0|PIT_CMD_BOTH|PIT_CMD_MODE2, PIT_BASE + PIT_COMMAND);
62 outb(TIMER0_VALUE&0xff, PIT_BASE + PIT_T0);
63 outb(TIMER0_VALUE>>8, PIT_BASE + PIT_T0);
65 outb(PIT_CMD_CTR2|PIT_CMD_BOTH|PIT_CMD_MODE3, PIT_BASE + PIT_COMMAND);
66 outb(TIMER2_VALUE&0xff, PIT_BASE + PIT_T2);
67 outb(TIMER2_VALUE>>8, PIT_BASE + PIT_T2);
75 #ifdef CFG_TIMER_GENERIC
77 /* the unit for these is CFG_HZ */
79 /* FixMe: implement these */
80 void reset_timer (void)
85 ulong get_timer (ulong base)
87 return (system_ticks - base);
90 void set_timer (ulong t)
95 static u16 read_pit(void)
98 outb(PIT_CMD_LATCH, PIT_BASE + PIT_COMMAND);
99 low = inb(PIT_BASE + PIT_T0);
100 return ((inb(PIT_BASE + PIT_T0) << 8) | low);
103 /* this is not very exact */
104 void udelay (unsigned long usec)
109 if (!timer_init_done) {
112 counter = read_pit();
125 int new_count = read_pit();
127 if (((new_count < usec) && !wraps) || wraps < 0) {
131 if (new_count > counter) {
140 /* this is a version with debug output */
141 void _udelay (unsigned long usec)
146 int usec1, usec2, usec3;
147 int wraps1, wraps2, wraps3, wraps4;
148 int ctr1, ctr2, ctr3, nct1, nct2;
151 if (!timer_init_done) {
154 counter = read_pit();
173 ctr2 = wraps3 = nct1 = 4711;
174 ctr3 = wraps4 = nct2 = 4711;
177 int new_count = read_pit();
179 if ((new_count < usec && !wraps) || wraps < 0) {
183 if (new_count > counter) {
199 printf("udelay(%d)\n", usec1);
200 printf("counter %d\n", ctr1);
201 printf("1: wraps %d, usec %d\n", wraps1, usec2);
202 printf("2: wraps %d, usec %d\n", wraps2, usec3);
203 printf("new_count[0] %d counter %d wraps %d\n", nct1, ctr2, wraps3);
204 printf("new_count[%d] %d counter %d wraps %d\n", i, nct2, ctr3, wraps4);
206 printf("%d %d %d %d %d\n",
207 read_pit(), read_pit(), read_pit(),
208 read_pit(), read_pit());