i8254.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2004 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <arch/types.h>
00036 #include <time/clock.h>
00037 #include <time/delay.h>
00038 #include <arch/interrupt.h>
00039 #include <arch/drivers/i8259.h>
00040 #include <arch/drivers/i8254.h>
00041 #include <cpu.h>
00042 #include <config.h>
00043 #include <arch/pm.h>
00044 #include <arch/asm.h>
00045 #include <arch/cpuid.h>
00046 #include <arch.h>
00047 #include <time/delay.h>
00048 #include <interrupt.h>
00049 
00050 /*
00051  * i8254 chip driver.
00052  * Low level time functions.
00053  */
00054 
00055 #define CLK_PORT1       0x40
00056 #define CLK_PORT4       0x43
00057 
00058 
00059 #define CLK_CONST       1193180
00060 #define MAGIC_NUMBER    1194
00061 
00062 static void i8254_interrupt(int n, istate_t *istate);
00063 
00064 void i8254_init(void)
00065 {
00066         i8254_normal_operation();
00067 }
00068 
00069 void i8254_normal_operation(void)
00070 {
00071         outb(CLK_PORT4, 0x36);
00072         pic_disable_irqs(1<<IRQ_CLK);
00073         outb(CLK_PORT1, (CLK_CONST/HZ) & 0xf);
00074         outb(CLK_PORT1, (CLK_CONST/HZ) >> 8);
00075         pic_enable_irqs(1<<IRQ_CLK);
00076         exc_register(VECTOR_CLK, "i8254_clock", (iroutine) i8254_interrupt);
00077 }
00078 
00079 #define LOOPS 150000
00080 #define SHIFT 11
00081 void i8254_calibrate_delay_loop(void)
00082 {
00083         __u64 clk1, clk2;
00084         __u32 t1, t2, o1, o2;
00085         __u8 not_ok;
00086 
00087 
00088         /*
00089          * One-shot timer. Count-down from 0xffff at 1193180Hz
00090          * MAGIC_NUMBER is the magic value for 1ms.
00091          */
00092         outb(CLK_PORT4, 0x30);
00093         outb(CLK_PORT1, 0xff);
00094         outb(CLK_PORT1, 0xff);
00095 
00096         do {
00097                 /* will read both status and count */
00098                 outb(CLK_PORT4, 0xc2);
00099                 not_ok = (inb(CLK_PORT1)>>6)&1;
00100                 t1 = inb(CLK_PORT1);
00101                 t1 |= inb(CLK_PORT1) << 8;
00102         } while (not_ok);
00103 
00104         asm_delay_loop(LOOPS);
00105 
00106         outb(CLK_PORT4, 0xd2);
00107         t2 = inb(CLK_PORT1);
00108         t2 |= inb(CLK_PORT1) << 8;
00109 
00110         /*
00111          * We want to determine the overhead of the calibrating mechanism.
00112          */
00113         outb(CLK_PORT4, 0xd2);
00114         o1 = inb(CLK_PORT1);
00115         o1 |= inb(CLK_PORT1) << 8;
00116 
00117         asm_fake_loop(LOOPS);
00118 
00119         outb(CLK_PORT4, 0xd2);
00120         o2 = inb(CLK_PORT1);
00121         o2 |= inb(CLK_PORT1) << 8;
00122 
00123         CPU->delay_loop_const = ((MAGIC_NUMBER*LOOPS)/1000) / ((t1-t2)-(o1-o2)) + (((MAGIC_NUMBER*LOOPS)/1000) % ((t1-t2)-(o1-o2)) ? 1 : 0);
00124 
00125         clk1 = rdtsc();
00126         delay(1<<SHIFT);
00127         clk2 = rdtsc();
00128         
00129         CPU->frequency_mhz = (clk2-clk1)>>SHIFT;
00130 
00131         return;
00132 }
00133 
00134 void i8254_interrupt(int n, istate_t *istate)
00135 {
00136         trap_virtual_eoi();
00137         clock();
00138 }
00139 

Generated on Sun Jun 18 16:38:50 2006 for HelenOS Kernel (ia32) by  doxygen 1.4.6