source: mainline/kernel/arch/ia32/src/drivers/i8254.c

Last change on this file was 2a103b5, checked in by Jakub Jermar <jakub@…>, 6 years ago

Introduce PIC operations indirection mechanism

Some architectures switch from one interrupt controller implementation
to another during runtime. By providing a cleaner indirection mechanism,
it is possible e.g. for the ia32 IRQ 7 handler to distinguish i8259
spurious interrupts from actual IRQ 7 device interrupts, even when the
i8259 interrupt controller is no longer active.

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[c5429fe]29/** @addtogroup kernel_ia32
[b45c443]30 * @{
31 */
[3c5006a0]32/**
33 * @file
[84afc7b]34 * @brief i8254 chip driver.
[3c5006a0]35 *
36 * Low level time functions.
[b45c443]37 */
38
[83dab11]39#include <stdint.h>
[f761f1eb]40#include <time/clock.h>
41#include <time/delay.h>
[134877d]42#include <arch/cycle.h>
[f761f1eb]43#include <arch/interrupt.h>
[87a5796]44#include <genarch/drivers/i8259/i8259.h>
[80d31883]45#include <arch/drivers/i8254.h>
[f761f1eb]46#include <cpu.h>
47#include <config.h>
48#include <arch/pm.h>
49#include <arch/asm.h>
[9c0a9b3]50#include <arch/cpuid.h>
[f761f1eb]51#include <arch.h>
[cea12e9]52#include <ddi/irq.h>
[f761f1eb]53
[dc0b964]54#define CLK_PORT1 ((ioport8_t *) 0x40U)
55#define CLK_PORT4 ((ioport8_t *) 0x43U)
[f761f1eb]56
[da1bafb]57#define CLK_CONST 1193180
58#define MAGIC_NUMBER 1194
59
60#define LOOPS 150000
61#define SHIFT 11
[f761f1eb]62
[cea12e9]63static irq_t i8254_irq;
64
[c9b550b]65static irq_ownership_t i8254_claim(irq_t *irq)
[cea12e9]66{
67 return IRQ_ACCEPT;
68}
69
[6cd9aa6]70static void i8254_irq_handler(irq_t *irq)
[cea12e9]71{
[f619ec11]72 /*
73 * This IRQ is responsible for kernel preemption.
74 * Nevertheless, we are now holding a spinlock which prevents
75 * preemption. For this particular IRQ, we don't need the
76 * lock. We just release it, call clock() and then reacquire it again.
77 */
[da1bafb]78 irq_spinlock_unlock(&irq->lock, false);
[cea12e9]79 clock();
[da1bafb]80 irq_spinlock_lock(&irq->lock, false);
[cea12e9]81}
[fcfac420]82
[f761f1eb]83void i8254_init(void)
84{
[cea12e9]85 irq_initialize(&i8254_irq);
[7bcfbbc]86 i8254_irq.preack = true;
[cea12e9]87 i8254_irq.inr = IRQ_CLK;
88 i8254_irq.claim = i8254_claim;
89 i8254_irq.handler = i8254_irq_handler;
90 irq_register(&i8254_irq);
[a35b458]91
[f761f1eb]92 i8254_normal_operation();
93}
94
95void i8254_normal_operation(void)
96{
[ee06f2a]97 pio_write_8(CLK_PORT4, 0x36);
[2a103b5]98 i8259_disable_irqs(1 << IRQ_CLK);
[ee06f2a]99 pio_write_8(CLK_PORT1, (CLK_CONST / HZ) & 0xf);
100 pio_write_8(CLK_PORT1, (CLK_CONST / HZ) >> 8);
[2a103b5]101 i8259_enable_irqs(1 << IRQ_CLK);
[f761f1eb]102}
103
104void i8254_calibrate_delay_loop(void)
105{
106 /*
107 * One-shot timer. Count-down from 0xffff at 1193180Hz
108 * MAGIC_NUMBER is the magic value for 1ms.
109 */
[ee06f2a]110 pio_write_8(CLK_PORT4, 0x30);
111 pio_write_8(CLK_PORT1, 0xff);
112 pio_write_8(CLK_PORT1, 0xff);
[a35b458]113
[da1bafb]114 uint8_t not_ok;
115 uint32_t t1;
116 uint32_t t2;
[a35b458]117
[f761f1eb]118 do {
[76cec1e]119 /* will read both status and count */
[ee06f2a]120 pio_write_8(CLK_PORT4, 0xc2);
121 not_ok = (uint8_t) ((pio_read_8(CLK_PORT1) >> 6) & 1);
122 t1 = pio_read_8(CLK_PORT1);
123 t1 |= pio_read_8(CLK_PORT1) << 8;
[f761f1eb]124 } while (not_ok);
[a35b458]125
[f761f1eb]126 asm_delay_loop(LOOPS);
[a35b458]127
[ee06f2a]128 pio_write_8(CLK_PORT4, 0xd2);
129 t2 = pio_read_8(CLK_PORT1);
130 t2 |= pio_read_8(CLK_PORT1) << 8;
[a35b458]131
[f761f1eb]132 /*
133 * We want to determine the overhead of the calibrating mechanism.
134 */
[ee06f2a]135 pio_write_8(CLK_PORT4, 0xd2);
[da1bafb]136 uint32_t o1 = pio_read_8(CLK_PORT1);
[ee06f2a]137 o1 |= pio_read_8(CLK_PORT1) << 8;
[a35b458]138
[f761f1eb]139 asm_fake_loop(LOOPS);
[a35b458]140
[ee06f2a]141 pio_write_8(CLK_PORT4, 0xd2);
[da1bafb]142 uint32_t o2 = pio_read_8(CLK_PORT1);
[ee06f2a]143 o2 |= pio_read_8(CLK_PORT1) << 8;
[a35b458]144
[a78cdcd]145 uint32_t delta = (t1 - t2) - (o1 - o2);
146 if (!delta)
147 delta = 1;
148
[f619ec11]149 CPU->delay_loop_const =
[a78cdcd]150 ((MAGIC_NUMBER * LOOPS) / 1000) / delta +
151 (((MAGIC_NUMBER * LOOPS) / 1000) % delta ? 1 : 0);
[a35b458]152
[da1bafb]153 uint64_t clk1 = get_cycle();
[134877d]154 delay(1 << SHIFT);
[da1bafb]155 uint64_t clk2 = get_cycle();
[a35b458]156
[134877d]157 CPU->frequency_mhz = (clk2 - clk1) >> SHIFT;
[a35b458]158
[f761f1eb]159 return;
160}
161
[3c5006a0]162/** @}
[b45c443]163 */
Note: See TracBrowser for help on using the repository browser.