elcr.c revision 140451
1128928Sjhb/*-
2128928Sjhb * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org>
3128928Sjhb * All rights reserved.
4128928Sjhb *
5128928Sjhb * Redistribution and use in source and binary forms, with or without
6128928Sjhb * modification, are permitted provided that the following conditions
7128928Sjhb * are met:
8128928Sjhb * 1. Redistributions of source code must retain the above copyright
9128928Sjhb *    notice, this list of conditions and the following disclaimer.
10128928Sjhb * 2. Redistributions in binary form must reproduce the above copyright
11128928Sjhb *    notice, this list of conditions and the following disclaimer in the
12128928Sjhb *    documentation and/or other materials provided with the distribution.
13128928Sjhb * 3. Neither the name of the author nor the names of any co-contributors
14128928Sjhb *    may be used to endorse or promote products derived from this software
15128928Sjhb *    without specific prior written permission.
16128928Sjhb *
17128928Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18128928Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19128928Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20128928Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21128928Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22128928Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23128928Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24128928Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25128928Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26128928Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27128928Sjhb * SUCH DAMAGE.
28128928Sjhb */
29128928Sjhb
30128928Sjhb#include <sys/cdefs.h>
31128928Sjhb__FBSDID("$FreeBSD: head/sys/i386/isa/elcr.c 140451 2005-01-18 20:24:47Z jhb $");
32128928Sjhb
33128928Sjhb/*
34128928Sjhb * The ELCR is a register that controls the trigger mode and polarity of
35128928Sjhb * EISA and ISA interrupts.  In FreeBSD 3.x and 4.x, the ELCR was only
36128928Sjhb * consulted for determining the appropriate trigger mode of EISA
37128928Sjhb * interrupts when using an APIC.  However, it seems that almost all
38128928Sjhb * systems that include PCI also include an ELCR that manages the ISA
39128928Sjhb * IRQs 0 through 15.  Thus, we check for the presence of an ELCR on
40128928Sjhb * every machine by checking to see if the values found at bootup are
41128928Sjhb * sane.  Note that the polarity of ISA and EISA IRQs are linked to the
42128928Sjhb * trigger mode.  All edge triggered IRQs use active-hi polarity, and
43128928Sjhb * all level triggered interrupts use active-lo polarity.
44128928Sjhb *
45128928Sjhb * The format of the ELCR is simple: it is a 16-bit bitmap where bit 0
46128928Sjhb * controls IRQ 0, bit 1 controls IRQ 1, etc.  If the bit is zero, the
47128928Sjhb * associated IRQ is edge triggered.  If the bit is one, the IRQ is
48128928Sjhb * level triggered.
49128928Sjhb */
50128928Sjhb
51128928Sjhb#include <sys/param.h>
52128928Sjhb#include <sys/bus.h>
53128928Sjhb#include <sys/systm.h>
54128928Sjhb#include <machine/intr_machdep.h>
55128928Sjhb
56128928Sjhb#define	ELCR_PORT	0x4d0
57128928Sjhb#define	ELCR_MASK(irq)	(1 << (irq))
58128928Sjhb
59128928Sjhbstatic int elcr_status;
60140451Sjhbint elcr_found;
61128928Sjhb
62128928Sjhb/*
63128928Sjhb * Check to see if we have what looks like a valid ELCR.  We do this by
64128928Sjhb * verifying that IRQs 0, 1, 2, and 13 are all edge triggered.
65128928Sjhb */
66128928Sjhbint
67128928Sjhbelcr_probe(void)
68128928Sjhb{
69128928Sjhb	int i;
70128928Sjhb
71128928Sjhb	elcr_status = inb(ELCR_PORT) | inb(ELCR_PORT + 1) << 8;
72128928Sjhb	if ((elcr_status & (ELCR_MASK(0) | ELCR_MASK(1) | ELCR_MASK(2) |
73128928Sjhb	    ELCR_MASK(8) | ELCR_MASK(13))) != 0)
74128928Sjhb		return (ENXIO);
75128928Sjhb	if (bootverbose) {
76128928Sjhb		printf("ELCR Found.  ISA IRQs programmed as:\n");
77128928Sjhb		for (i = 0; i < 16; i++)
78128928Sjhb			printf(" %2d", i);
79128928Sjhb		printf("\n");
80128928Sjhb		for (i = 0; i < 16; i++)
81128928Sjhb			if (elcr_status & ELCR_MASK(i))
82128928Sjhb				printf("  L");
83128928Sjhb			else
84128928Sjhb				printf("  E");
85128928Sjhb		printf("\n");
86128928Sjhb	}
87128928Sjhb	if (resource_disabled("elcr", 0))
88128928Sjhb		return (ENXIO);
89128928Sjhb	elcr_found = 1;
90128928Sjhb	return (0);
91128928Sjhb}
92128928Sjhb
93128928Sjhb/*
94128928Sjhb * Returns 1 for level trigger, 0 for edge.
95128928Sjhb */
96128928Sjhbenum intr_trigger
97128928Sjhbelcr_read_trigger(u_int irq)
98128928Sjhb{
99128928Sjhb
100128928Sjhb	KASSERT(elcr_found, ("%s: no ELCR was found!", __func__));
101128928Sjhb	KASSERT(irq <= 15, ("%s: invalid IRQ %u", __func__, irq));
102128928Sjhb	if (elcr_status & ELCR_MASK(irq))
103128928Sjhb		return (INTR_TRIGGER_LEVEL);
104128928Sjhb	else
105128928Sjhb		return (INTR_TRIGGER_EDGE);
106128928Sjhb}
107128928Sjhb
108128928Sjhb/*
109128928Sjhb * Set the trigger mode for a specified IRQ.  Mode of 0 means edge triggered,
110128928Sjhb * and a mode of 1 means level triggered.
111128928Sjhb */
112128928Sjhbvoid
113128928Sjhbelcr_write_trigger(u_int irq, enum intr_trigger trigger)
114128928Sjhb{
115128928Sjhb	int new_status;
116128928Sjhb
117128928Sjhb	KASSERT(elcr_found, ("%s: no ELCR was found!", __func__));
118128928Sjhb	KASSERT(irq <= 15, ("%s: invalid IRQ %u", __func__, irq));
119128928Sjhb	if (trigger == INTR_TRIGGER_LEVEL)
120128928Sjhb		new_status = elcr_status | ELCR_MASK(irq);
121128928Sjhb	else
122128928Sjhb		new_status = elcr_status & ~ELCR_MASK(irq);
123128928Sjhb	if (new_status == elcr_status)
124128928Sjhb		return;
125128928Sjhb	elcr_status = new_status;
126128928Sjhb	if (irq >= 8)
127128928Sjhb		outb(ELCR_PORT + 1, elcr_status >> 8);
128128928Sjhb	else
129128928Sjhb		outb(ELCR_PORT, elcr_status & 0xff);
130128928Sjhb}
131128928Sjhb
132128928Sjhbvoid
133128928Sjhbelcr_resume(void)
134128928Sjhb{
135128928Sjhb
136128928Sjhb	KASSERT(elcr_found, ("%s: no ELCR was found!", __func__));
137128928Sjhb	outb(ELCR_PORT, elcr_status & 0xff);
138128928Sjhb	outb(ELCR_PORT + 1, elcr_status >> 8);
139128928Sjhb}
140