1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/*
* -----------------------------------------------------------------------
*
* Copyright 2009 Intel Corporation; author: H. Peter Anvin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 53 Temple Place Ste 330,
* Boston MA 02111-1307, USA; either version 2 of the License, or
* (at your option) any later version; incorporated herein by reference.
*
* -----------------------------------------------------------------------
*
* serirq.c
*
* Serial port IRQ code
*
* We don't know what IRQ, if any, we have, so map all of them...
*/
#include <sys/io.h>
#include <string.h>
#include <fs.h>
#include "bios.h"
static char serial_buf[serial_buf_size];
static unsigned short SerialIRQPort; /* Serial port w IRQ service */
char *SerialHead = serial_buf; /* Head of serial port rx buffer */
char *SerialTail = serial_buf; /* Tail of serial port rx buffer */
static unsigned char IRQMask[2]; /* PIC IRQ mask status */
static unsigned int oldirq[16];
typedef void (*irqhandler_t)(void);
void sirq_cleanup(void);
static void irq_common(unsigned short old_irq)
{
char *dst;
irqhandler_t next;
char val;
dst = SerialHead;
next = (irqhandler_t)oldirq[old_irq];
/* LSR */
val = inb(SerialPort + 5);
/* Received data */
while (val & 1) {
/* RDR */
*dst++ = inb(SerialPort);
/* LSR */
val = inb(SerialPort + 5);
if ((val & FlowIgnore) == FlowIgnore) {
/* Wrap around if necessary */
dst = (char *)((unsigned long)dst & (serial_buf_size - 1));
/* Would this cause overflow? */
if (dst != SerialTail)
SerialHead = dst;
}
}
/* Chain to next handler */
next();
}
#define SERIAL_IRQ_HANDLER(n) \
static void serstub_irq##n(void) \
{ \
irq_common(n); \
}
SERIAL_IRQ_HANDLER(0);
SERIAL_IRQ_HANDLER(1);
SERIAL_IRQ_HANDLER(2);
SERIAL_IRQ_HANDLER(3);
SERIAL_IRQ_HANDLER(4);
SERIAL_IRQ_HANDLER(5);
SERIAL_IRQ_HANDLER(6);
SERIAL_IRQ_HANDLER(7);
SERIAL_IRQ_HANDLER(8);
SERIAL_IRQ_HANDLER(9);
SERIAL_IRQ_HANDLER(10);
SERIAL_IRQ_HANDLER(11);
SERIAL_IRQ_HANDLER(12);
SERIAL_IRQ_HANDLER(13);
SERIAL_IRQ_HANDLER(14);
SERIAL_IRQ_HANDLER(15);
static inline void save_irq_vectors(uint32_t *src, uint32_t *dst)
{
int i;
for (i = 0; i < 8; i++)
*dst++ = *src++;
}
static inline void install_irq_vectors(uint32_t *dst, int first)
{
if (first) {
*dst++ = (uint32_t)serstub_irq0;
*dst++ = (uint32_t)serstub_irq1;
*dst++ = (uint32_t)serstub_irq2;
*dst++ = (uint32_t)serstub_irq3;
*dst++ = (uint32_t)serstub_irq4;
*dst++ = (uint32_t)serstub_irq5;
*dst++ = (uint32_t)serstub_irq6;
*dst++ = (uint32_t)serstub_irq7;
} else {
*dst++ = (uint32_t)serstub_irq8;
*dst++ = (uint32_t)serstub_irq9;
*dst++ = (uint32_t)serstub_irq10;
*dst++ = (uint32_t)serstub_irq11;
*dst++ = (uint32_t)serstub_irq12;
*dst++ = (uint32_t)serstub_irq13;
*dst++ = (uint32_t)serstub_irq14;
*dst++ = (uint32_t)serstub_irq15;
}
}
__export void sirq_install(void)
{
char val, val2;
sirq_cleanup();
save_irq_vectors((uint32_t *)(4 * 0x8), oldirq);
save_irq_vectors((uint32_t *)(4 * 0x70), &oldirq[8]);
install_irq_vectors((uint32_t *)(4 * 0x8), 1);
install_irq_vectors((uint32_t *)(4 * 0x70), 0);
SerialIRQPort = SerialPort;
/* Clear DLAB (should already be...) */
outb(0x3, SerialIRQPort + 5);
io_delay();
/* Enable receive interrupt */
outb(0x1, SerialIRQPort + 1);
io_delay();
/*
* Enable all the interrupt lines at the PIC. Some BIOSes only
* enable the timer interrupts and other interrupts actively
* in use by the BIOS.
*/
/* Secondary PIC mask register */
val = inb(0xA1);
val2 = inb(0x21);
IRQMask[0] = val;
IRQMask[1] = val2;
io_delay();
/* Remove all interrupt masks */
outb(0x21, 0);
outb(0xA1, 0);
}
__export void sirq_cleanup_nowipe(void)
{
uint32_t *dst;
int i;
if (!SerialIRQPort)
return;
/* Clear DLAB */
outb(0x3, SerialIRQPort + 5);
io_delay();
/* Clear IER */
outb(0x0, SerialIRQPort + 1);
io_delay();
/* Restore PIC masks */
outb(IRQMask[0], 0x21);
outb(IRQMask[1], 0xA1);
/* Restore the original interrupt vectors */
dst = (uint32_t *)(4 * 0x8);
for (i = 0; i < 8; i++)
*dst++ = oldirq[i];
dst = (uint32_t *)(4 * 0x70);
for (i = 8; i < 16; i++)
*dst++ = oldirq[i];
/* No active interrupt system */
SerialIRQPort = 0;
}
void sirq_cleanup(void)
{
sirq_cleanup_nowipe();
memset(SerialHead, 0x0, serial_buf_size);
}
|