isa.c revision 3670
1139827Simp/*- 2194619Srwatson * Copyright (c) 1991 The Regents of the University of California. 3133422Srwatson * All rights reserved. 4133422Srwatson * 5133422Srwatson * This code is derived from software contributed to Berkeley by 6133422Srwatson * William Jolitz. 7133422Srwatson * 8133422Srwatson * Redistribution and use in source and binary forms, with or without 9133422Srwatson * modification, are permitted provided that the following conditions 10133422Srwatson * are met: 11133422Srwatson * 1. Redistributions of source code must retain the above copyright 12133422Srwatson * notice, this list of conditions and the following disclaimer. 13133422Srwatson * 2. Redistributions in binary form must reproduce the above copyright 14133422Srwatson * notice, this list of conditions and the following disclaimer in the 15133422Srwatson * documentation and/or other materials provided with the distribution. 16133422Srwatson * 3. All advertising materials mentioning features or use of this software 17133422Srwatson * must display the following acknowledgement: 18133422Srwatson * This product includes software developed by the University of 19133422Srwatson * California, Berkeley and its contributors. 20133422Srwatson * 4. Neither the name of the University nor the names of its contributors 21133422Srwatson * may be used to endorse or promote products derived from this software 22133422Srwatson * without specific prior written permission. 23133422Srwatson * 24133422Srwatson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25165891Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26165974Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2756555Sbrian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28133422Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29133422Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30133422Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31133422Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32133422Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33133422Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34133422Srwatson * SUCH DAMAGE. 35133422Srwatson * 36133422Srwatson * from: @(#)isa.c 7.2 (Berkeley) 5/13/91 37133422Srwatson * $Id: isa.c,v 1.27 1994/10/01 02:56:14 davidg Exp $ 38133422Srwatson */ 39133422Srwatson 40133422Srwatson/* 41133422Srwatson * code to manage AT bus 42133422Srwatson * 43133422Srwatson * 92/08/18 Frank P. MacLachlan (fpm@crash.cts.com): 44133422Srwatson * Fixed uninitialized variable problem and added code to deal 45133422Srwatson * with DMA page boundaries in isa_dmarangecheck(). Fixed word 46133422Srwatson * mode DMA count compution and reorganized DMA setup code in 47133422Srwatson * isa_dmastart() 48133422Srwatson */ 4956555Sbrian 5015885Sjulian#include <sys/param.h> 5115885Sjulian#include <sys/systm.h> /* isn't it a joy */ 5215885Sjulian#include <sys/kernel.h> /* to have three of these */ 5315885Sjulian#include <sys/conf.h> 5495759Stanimura#include <sys/file.h> 5515885Sjulian#include <sys/buf.h> 5695759Stanimura#include <sys/uio.h> 5715885Sjulian#include <sys/syslog.h> 5815885Sjulian#include <sys/malloc.h> 5995759Stanimura#include <sys/rlist.h> 6095759Stanimura#include <machine/segments.h> 6115885Sjulian#include <vm/vm.h> 6215885Sjulian#include <machine/spl.h> 6315885Sjulian#include <i386/isa/isa_device.h> 6418207Sbde#include <i386/isa/isa.h> 6518207Sbde#include <i386/isa/icu.h> 6618207Sbde#include <i386/isa/ic/i8237.h> 6718207Sbde#include <i386/isa/ic/i8042.h> 68132043Srwatson#include "vector.h" 6915885Sjulian 7015885Sjulian/* 71163606Srwatson** Register definitions for DMA controller 1 (channels 0..3): 72163606Srwatson*/ 7329185Sbde#define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */ 7429185Sbde#define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */ 7529184Sbde#define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */ 76132043Srwatson#define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */ 7729185Sbde 7829184Sbde/* 7929185Sbde** Register definitions for DMA controller 2 (channels 4..7): 8015885Sjulian*/ 8115885Sjulian#define DMA2_CHN(c) (IO_DMA2 + 2*(2*(c))) /* addr reg for channel c */ 8215885Sjulian#define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */ 8315885Sjulian#define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */ 84111888Sjlemon#define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */ 85111888Sjlemon 8615885Sjulian/* 8715885Sjulian * Bits to specify the type and amount of conflict checking. 88111888Sjlemon */ 89165974Srwatson#define CC_ATTACH (1 << 0) 90111888Sjlemon#define CC_DRQ (1 << 1) 91111888Sjlemon#define CC_IOADDR (1 << 2) 92111888Sjlemon#define CC_IRQ (1 << 3) 9315885Sjulian#define CC_MEMADDR (1 << 4) 94111888Sjlemon 95111888Sjlemon/* 96111888Sjlemon * XXX these defines should be in a central place. 97111888Sjlemon */ 9815885Sjulian#define read_eflags() ({u_long ef; \ 99111888Sjlemon __asm("pushfl; popl %0" : "=a" (ef)); \ 100111888Sjlemon ef; }) 101111888Sjlemon#define write_eflags(ef) __asm("pushl %0; popfl" : : "a" ((u_long)(ef))) 102165974Srwatson 103165974Srwatsonu_long *intr_countp[ICU_LEN]; 104111888Sjlemoninthand2_t *intr_handler[ICU_LEN]; 105111888Sjlemonu_int intr_mask[ICU_LEN]; 10615885Sjulianint intr_unit[ICU_LEN]; 10715885Sjulian 10830822Sjulianstatic inthand_t *fastintr[ICU_LEN] = { 109111888Sjlemon &IDTVEC(fastintr0), &IDTVEC(fastintr1), 11030822Sjulian &IDTVEC(fastintr2), &IDTVEC(fastintr3), 111111888Sjlemon &IDTVEC(fastintr4), &IDTVEC(fastintr5), 112111888Sjlemon &IDTVEC(fastintr6), &IDTVEC(fastintr7), 11315885Sjulian &IDTVEC(fastintr8), &IDTVEC(fastintr9), 114165974Srwatson &IDTVEC(fastintr10), &IDTVEC(fastintr11), 115111888Sjlemon &IDTVEC(fastintr12), &IDTVEC(fastintr13), 116111888Sjlemon &IDTVEC(fastintr14), &IDTVEC(fastintr15) 117165974Srwatson}; 118165974Srwatson 11915885Sjulianstatic inthand_t *slowintr[ICU_LEN] = { 12015885Sjulian &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3), 12157178Speter &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7), 122165974Srwatson &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11), 12315885Sjulian &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) 124165974Srwatson}; 125165974Srwatson 126165974Srwatsonstatic void config_isadev __P((struct isa_device *isdp, u_int *mp)); 127165974Srwatsonstatic void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp, 128165974Srwatson int item, char const *whatnot, char const *reason, 129165974Srwatson char const *format)); 130165974Srwatsonstatic int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp, 13115885Sjulian u_int checkbits)); 132165974Srwatsonstatic int haveseen_isadev __P((struct isa_device *dvp, u_int checkbits)); 133165974Srwatsonstatic inthand2_t isa_strayintr; 134165974Srwatsonstatic void register_imask __P((struct isa_device *dvp, u_int mask)); 135165974Srwatson 136165974Srwatson/* 137165974Srwatson * print a conflict message 138165974Srwatson */ 139165974Srwatsonstatic void 140165974Srwatsonconflict(dvp, tmpdvp, item, whatnot, reason, format) 14115885Sjulian struct isa_device *dvp; 142165974Srwatson struct isa_device *tmpdvp; 143165974Srwatson int item; 144165974Srwatson char const *whatnot; 145165974Srwatson char const *reason; 146165974Srwatson char const *format; 14715885Sjulian{ 148165974Srwatson printf("%s%d not %sed due to %s conflict with %s%d at ", 149165974Srwatson dvp->id_driver->name, dvp->id_unit, whatnot, reason, 150165974Srwatson tmpdvp->id_driver->name, tmpdvp->id_unit); 151165974Srwatson printf(format, item); 15215885Sjulian printf("\n"); 153165974Srwatson} 154165974Srwatson 155165974Srwatson/* 156165974Srwatson * Check to see if things are alread in use, like IRQ's, I/O addresses 157165974Srwatson * and Memory addresses. 158165974Srwatson */ 15915885Sjulianstatic int 160165974Srwatsonhaveseen(dvp, tmpdvp, checkbits) 161165974Srwatson struct isa_device *dvp; 162165974Srwatson struct isa_device *tmpdvp; 163165974Srwatson u_int checkbits; 164194619Srwatson{ 165194913Srwatson /* 166165974Srwatson * Only check against devices that have already been found 167165974Srwatson */ 168165974Srwatson if (tmpdvp->id_alive) { 169165974Srwatson char const *whatnot; 170165974Srwatson 171165974Srwatson whatnot = checkbits & CC_ATTACH ? "attach" : "prob"; 172165974Srwatson /* 173165974Srwatson * Check for I/O address conflict. We can only check the 174165974Srwatson * starting address of the device against the range of the 175165974Srwatson * device that has already been probed since we do not 176165974Srwatson * know how many I/O addresses this device uses. 177194619Srwatson */ 178165974Srwatson if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) { 179165974Srwatson if ((dvp->id_iobase >= tmpdvp->id_iobase) && 180165974Srwatson (dvp->id_iobase <= 181165974Srwatson (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) { 182165974Srwatson conflict(dvp, tmpdvp, dvp->id_iobase, whatnot, 183165974Srwatson "I/O address", "0x%x"); 184165974Srwatson return 1; 185165974Srwatson } 186165974Srwatson } 187165974Srwatson /* 188165974Srwatson * Check for Memory address conflict. We can check for 18915885Sjulian * range overlap, but it will not catch all cases since the 190165974Srwatson * driver may adjust the msize paramater during probe, for 191165974Srwatson * now we just check that the starting address does not 192194619Srwatson * fall within any allocated region. 193165974Srwatson * XXX could add a second check after the probe for overlap, 194165974Srwatson * since at that time we would know the full range. 195165974Srwatson * XXX KERNBASE is a hack, we should have vaddr in the table! 19615885Sjulian */ 197165974Srwatson if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) { 198165974Srwatson if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) && 199165974Srwatson (KERNBASE + dvp->id_maddr <= 200165974Srwatson (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) { 20115885Sjulian conflict(dvp, tmpdvp, (int)dvp->id_maddr, 202165974Srwatson whatnot, "maddr", "0x%x"); 203165974Srwatson return 1; 204165974Srwatson } 205165974Srwatson } 206165974Srwatson /* 207165974Srwatson * Check for IRQ conflicts. 208165974Srwatson */ 209165974Srwatson if (checkbits & CC_IRQ && tmpdvp->id_irq) { 210165974Srwatson if (tmpdvp->id_irq == dvp->id_irq) { 211165974Srwatson conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1, 212194619Srwatson whatnot, "irq", "%d"); 213165974Srwatson return 1; 214165974Srwatson } 215165974Srwatson } 216165974Srwatson /* 217165974Srwatson * Check for DRQ conflicts. 218165974Srwatson */ 219165974Srwatson if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) { 220165974Srwatson if (tmpdvp->id_drq == dvp->id_drq) { 221165974Srwatson conflict(dvp, tmpdvp, dvp->id_drq, whatnot, 222165974Srwatson "drq", "%d"); 223165974Srwatson return 1; 224165974Srwatson } 225165974Srwatson } 226165974Srwatson } 227194913Srwatson return 0; 228165974Srwatson} 229165974Srwatson 230165974Srwatson/* 231165974Srwatson * Search through all the isa_devtab_* tables looking for anything that 232165974Srwatson * conflicts with the current device. 233165974Srwatson */ 234165974Srwatsonstatic int 235165974Srwatsonhaveseen_isadev(dvp, checkbits) 236165974Srwatson struct isa_device *dvp; 237165974Srwatson u_int checkbits; 238165974Srwatson{ 239165974Srwatson struct isa_device *tmpdvp; 240165974Srwatson int status = 0; 241165974Srwatson 242165974Srwatson for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) { 243165974Srwatson status |= haveseen(dvp, tmpdvp, checkbits); 244165974Srwatson if (status) 245165974Srwatson return status; 246194913Srwatson } 247165974Srwatson for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) { 248165974Srwatson status |= haveseen(dvp, tmpdvp, checkbits); 249165974Srwatson if (status) 250165974Srwatson return status; 251165974Srwatson } 252165974Srwatson for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) { 253165974Srwatson status |= haveseen(dvp, tmpdvp, checkbits); 254165974Srwatson if (status) 255165974Srwatson return status; 256165974Srwatson } 257165974Srwatson for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) { 258165974Srwatson status |= haveseen(dvp, tmpdvp, checkbits); 259165974Srwatson if (status) 260165974Srwatson return status; 261165974Srwatson } 262165974Srwatson return(status); 263165974Srwatson} 264165974Srwatson 265165974Srwatson/* 266165974Srwatson * Configure all ISA devices 267165974Srwatson */ 268165974Srwatsonvoid 269165974Srwatsonisa_configure() { 270165974Srwatson struct isa_device *dvp; 271165974Srwatson 272165974Srwatson splhigh(); 273165974Srwatson enable_intr(); 274165974Srwatson INTREN(IRQ_SLAVE); 275165974Srwatson printf("Probing for devices on the ISA bus:\n"); 276165974Srwatson /* First probe all the sensitive probes */ 277165974Srwatson for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) 278165974Srwatson if (dvp->id_driver->sensitive_hw) 279165974Srwatson config_isadev(dvp, &tty_imask); 280165974Srwatson for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) 281165974Srwatson if (dvp->id_driver->sensitive_hw) 28215885Sjulian config_isadev(dvp, &bio_imask); 283194619Srwatson for (dvp = isa_devtab_net; dvp->id_driver; dvp++) 284194619Srwatson if (dvp->id_driver->sensitive_hw) 285194619Srwatson config_isadev(dvp, &net_imask); 28615885Sjulian for (dvp = isa_devtab_null; dvp->id_driver; dvp++) 287165974Srwatson if (dvp->id_driver->sensitive_hw) 288165974Srwatson config_isadev(dvp, (u_int *)NULL); 289165974Srwatson 290165974Srwatson /* Then all the bad ones */ 291165974Srwatson for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) 292165974Srwatson if (!dvp->id_driver->sensitive_hw) 293165974Srwatson config_isadev(dvp, &tty_imask); 294165974Srwatson for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) 295194619Srwatson if (!dvp->id_driver->sensitive_hw) 296165974Srwatson config_isadev(dvp, &bio_imask); 297165974Srwatson for (dvp = isa_devtab_net; dvp->id_driver; dvp++) 298165974Srwatson if (!dvp->id_driver->sensitive_hw) 29915885Sjulian config_isadev(dvp, &net_imask); 300165974Srwatson for (dvp = isa_devtab_null; dvp->id_driver; dvp++) 301165974Srwatson if (!dvp->id_driver->sensitive_hw) 302165974Srwatson config_isadev(dvp, (u_int *)NULL); 303165974Srwatson 304165974Srwatson bio_imask |= SWI_CLOCK_MASK; 305165974Srwatson net_imask |= SWI_NET_MASK; 306165974Srwatson tty_imask |= SWI_TTY_MASK; 307165974Srwatson 308165974Srwatson/* 309165974Srwatson * XXX we should really add the tty device to net_imask when the line is 310165974Srwatson * switched to SLIPDISC, and then remove it when it is switched away from 311194619Srwatson * SLIPDISC. No need to block out ALL ttys during a splimp when only one 312194619Srwatson * of them is running slip. 313165974Srwatson * 314165974Srwatson * XXX actually, blocking all ttys during a splimp doesn't matter so much 315165974Srwatson * with sio because the serial interrupt layer doesn't use tty_imask. Only 316165974Srwatson * non-serial ttys suffer. It's more stupid that ALL 'net's are blocked 317165974Srwatson * during spltty. 318165974Srwatson */ 319165974Srwatson#include "sl.h" 320165974Srwatson#if NSL > 0 321165974Srwatson net_imask |= tty_imask; 322165974Srwatson tty_imask = net_imask; 323165974Srwatson#endif 324165974Srwatson /* bio_imask |= tty_imask ; can some tty devices use buffers? */ 325165974Srwatson#ifdef DIAGNOSTIC 326165974Srwatson printf("bio_imask %x tty_imask %x net_imask %x\n", 32715885Sjulian bio_imask, tty_imask, net_imask); 328165974Srwatson#endif 32930822Sjulian /* 330165974Srwatson * Finish initializing intr_mask[]. Note that the partly 331165974Srwatson * constructed masks aren't actually used since we're at splhigh. 332165974Srwatson * For fully dynamic initialization, register_intr() and 333165974Srwatson * unregister_intr() will have to adjust the masks for _all_ 33430822Sjulian * interrupts and for tty_imask, etc. 335165974Srwatson */ 336165974Srwatson for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) 337165974Srwatson register_imask(dvp, tty_imask); 338165974Srwatson for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) 339165974Srwatson register_imask(dvp, bio_imask); 340165974Srwatson for (dvp = isa_devtab_net; dvp->id_driver; dvp++) 341165974Srwatson register_imask(dvp, net_imask); 342165974Srwatson for (dvp = isa_devtab_null; dvp->id_driver; dvp++) 34315885Sjulian register_imask(dvp, SWI_CLOCK_MASK); 344165974Srwatson spl0(); 345165974Srwatson} 346165974Srwatson 347165974Srwatson/* 34830822Sjulian * Configure an ISA device. 349165974Srwatson */ 350165974Srwatson 351194619Srwatson 352194619Srwatsonstatic void config_isadev_c(); 35330822Sjulian 35430822Sjulianstatic void 355165974Srwatsonconfig_isadev(isdp, mp) 356165974Srwatson struct isa_device *isdp; 357165974Srwatson u_int *mp; 35830822Sjulian{ 359165974Srwatson config_isadev_c(isdp, mp, 0); 360194619Srwatson} 361194619Srwatson 36215885Sjulianvoid 363165974Srwatsonreconfig_isadev(isdp, mp) 364165974Srwatson struct isa_device *isdp; 365165974Srwatson u_int *mp; 366165974Srwatson{ 367165974Srwatson config_isadev_c(isdp, mp, 1); 368165974Srwatson} 369165974Srwatson 370165974Srwatsonstatic void 371165974Srwatsonconfig_isadev_c(isdp, mp, reconfig) 372165974Srwatson struct isa_device *isdp; 373165974Srwatson u_int *mp; 374165974Srwatson int reconfig; 375165974Srwatson{ 376165974Srwatson u_int checkbits; 377165974Srwatson int id_alive; 378194619Srwatson int last_alive; 379194619Srwatson struct isa_driver *dp = isdp->id_driver; 380165974Srwatson 38115885Sjulian checkbits = 0; 38230822Sjulian#ifndef ALLOW_CONFLICT_IRQ 38330822Sjulian checkbits |= CC_IRQ; 384165974Srwatson#endif 38530822Sjulian#ifndef ALLOW_CONFLICT_DRQ 386165974Srwatson checkbits |= CC_DRQ; 387165974Srwatson#endif 38815885Sjulian#ifndef ALLOW_CONFLICT_IOADDR 38930822Sjulian checkbits |= CC_IOADDR; 390165974Srwatson#endif 39130822Sjulian#ifndef ALLOW_CONFLICT_MEMADDR 392165974Srwatson checkbits |= CC_MEMADDR; 393165974Srwatson#endif 394165974Srwatson if (!reconfig && haveseen_isadev(isdp, checkbits)) 395165974Srwatson return; 396194619Srwatson if (!reconfig && isdp->id_maddr) { 397165974Srwatson isdp->id_maddr -= 0xa0000; /* XXX should be a define */ 398165974Srwatson isdp->id_maddr += atdevbase; 399165974Srwatson } 400165974Srwatson if (reconfig) { 40115885Sjulian last_alive = isdp->id_alive; 402165974Srwatson } 403165974Srwatson else { 40430822Sjulian last_alive = 0; 405165974Srwatson } 406165974Srwatson id_alive = (*dp->probe)(isdp); 407194619Srwatson if (id_alive) { 40815885Sjulian /* 409165974Srwatson * Only print the I/O address range if id_alive != -1 410193332Srwatson * Right now this is a temporary fix just for the new 411194619Srwatson * NPX code so that if it finds a 486 that can use trap 412101937Srwatson * 16 it will not report I/O addresses. 413101937Srwatson * Rod Grimes 04/26/94 41430822Sjulian */ 415165974Srwatson if (!isdp->id_reconfig) { 41630822Sjulian printf("%s%d", dp->name, isdp->id_unit); 417165974Srwatson if (id_alive != -1) { 418165974Srwatson printf(" at 0x%x", isdp->id_iobase); 419165974Srwatson if ((isdp->id_iobase + id_alive - 1) != 420165974Srwatson isdp->id_iobase) { 421165974Srwatson printf("-0x%x", 422165974Srwatson isdp->id_iobase + id_alive - 1); 423165974Srwatson } 424165974Srwatson } 425194619Srwatson if (isdp->id_irq) 42615885Sjulian printf(" irq %d", ffs(isdp->id_irq) - 1); 42715885Sjulian if (isdp->id_drq != -1) 428165974Srwatson printf(" drq %d", isdp->id_drq); 429165974Srwatson if (isdp->id_maddr) 430165974Srwatson printf(" maddr 0x%lx", kvtop(isdp->id_maddr)); 431165974Srwatson if (isdp->id_msize) 432165974Srwatson printf(" msize %d", isdp->id_msize); 433194619Srwatson if (isdp->id_flags) 434194619Srwatson printf(" flags 0x%x", isdp->id_flags); 435165974Srwatson if (isdp->id_iobase) { 436194619Srwatson if (isdp->id_iobase < 0x100) { 437194619Srwatson printf(" on motherboard\n"); 438165974Srwatson } else { 439165974Srwatson if (isdp->id_iobase >= 0x1000) { 44015885Sjulian printf (" on eisa\n"); 441 } else { 442 printf (" on isa\n"); 443 } 444 } 445 } 446 /* 447 * Check for conflicts again. The driver may have 448 * changed *dvp. We should weaken the early check 449 * since the driver may have been able to change 450 * *dvp to avoid conflicts if given a chance. We 451 * already skip the early check for IRQs and force 452 * a check for IRQs in the next group of checks. 453 */ 454#ifndef ALLOW_CONFLICT_IRQ 455 checkbits |= CC_IRQ; 456#endif 457 if (haveseen_isadev(isdp, checkbits)) 458 return; 459 isdp->id_alive = id_alive; 460 } 461 (*dp->attach)(isdp); 462 if (isdp->id_irq) { 463 if (mp) 464 INTRMASK(*mp, isdp->id_irq); 465 register_intr(ffs(isdp->id_irq) - 1, isdp->id_id, 466 isdp->id_ri_flags, isdp->id_intr, 467 mp ? *mp : 0, isdp->id_unit); 468 INTREN(isdp->id_irq); 469 } 470 } else { 471 if (isdp->id_reconfig) { 472 (*dp->attach)(isdp); /* reconfiguration attach */ 473 } 474 if (!last_alive) { 475 if (!isdp->id_reconfig) { 476 printf("%s%d not found", dp->name, isdp->id_unit); 477 if (isdp->id_iobase) { 478 printf(" at 0x%x", isdp->id_iobase); 479 } 480 printf("\n"); 481 } 482 } 483 else { 484 /* This code has not been tested.... */ 485 if (isdp->id_irq) { 486 INTRDIS(isdp->id_irq); 487 unregister_intr(ffs(isdp->id_irq) - 1, 488 isdp->id_intr); 489 if (mp) 490 INTRUNMASK(*mp, isdp->id_irq); 491 } 492 } 493 } 494} 495 496/* 497 * Fill in default interrupt table (in case of spuruious interrupt 498 * during configuration of kernel, setup interrupt control unit 499 */ 500void 501isa_defaultirq() 502{ 503 int i; 504 505 /* icu vectors */ 506 for (i = 0; i < ICU_LEN; i++) 507 unregister_intr(i, (inthand2_t *)NULL); 508 509 /* initialize 8259's */ 510 outb(IO_ICU1, 0x11); /* reset; program device, four bytes */ 511 outb(IO_ICU1+1, NRSVIDT); /* starting at this vector index */ 512 outb(IO_ICU1+1, 1<<2); /* slave on line 2 */ 513#ifdef AUTO_EOI_1 514 outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */ 515#else 516 outb(IO_ICU1+1, 1); /* 8086 mode */ 517#endif 518 outb(IO_ICU1+1, 0xff); /* leave interrupts masked */ 519 outb(IO_ICU1, 0x0a); /* default to IRR on read */ 520 outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */ 521 522 outb(IO_ICU2, 0x11); /* reset; program device, four bytes */ 523 outb(IO_ICU2+1, NRSVIDT+8); /* staring at this vector index */ 524 outb(IO_ICU2+1,2); /* my slave id is 2 */ 525#ifdef AUTO_EOI_2 526 outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */ 527#else 528 outb(IO_ICU2+1,1); /* 8086 mode */ 529#endif 530 outb(IO_ICU2+1, 0xff); /* leave interrupts masked */ 531 outb(IO_ICU2, 0x0a); /* default to IRR on read */ 532} 533 534/* region of physical memory known to be contiguous */ 535vm_offset_t isaphysmem; 536static caddr_t dma_bounce[8]; /* XXX */ 537static char bounced[8]; /* XXX */ 538#define MAXDMASZ 512 /* XXX */ 539 540/* high byte of address is stored in this port for i-th dma channel */ 541static short dmapageport[8] = 542 { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a }; 543 544/* 545 * isa_dmacascade(): program 8237 DMA controller channel to accept 546 * external dma control by a board. 547 */ 548void isa_dmacascade(unsigned chan) 549{ 550 if (chan > 7) 551 panic("isa_dmacascade: impossible request"); 552 553 /* set dma channel mode, and set dma channel mode */ 554 if ((chan & 4) == 0) { 555 outb(DMA1_MODE, DMA37MD_CASCADE | chan); 556 outb(DMA1_SMSK, chan); 557 } else { 558 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3)); 559 outb(DMA2_SMSK, chan & 3); 560 } 561} 562 563static int 564isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan); 565 566/* 567 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment 568 * problems by using a bounce buffer. 569 */ 570void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan) 571{ vm_offset_t phys; 572 int waport; 573 caddr_t newaddr; 574 575 if ( chan > 7 576 || (chan < 4 && nbytes > (1<<16)) 577 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1))) 578 panic("isa_dmastart: impossible request"); 579 580 if (isa_dmarangecheck(addr, nbytes, chan)) { 581 if (dma_bounce[chan] == 0) 582 dma_bounce[chan] = 583 /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/ 584 (caddr_t) isaphysmem + NBPG*chan; 585 bounced[chan] = 1; 586 newaddr = dma_bounce[chan]; 587 *(int *) newaddr = 0; /* XXX */ 588 589 /* copy bounce buffer on write */ 590 if (!(flags & B_READ)) 591 bcopy(addr, newaddr, nbytes); 592 addr = newaddr; 593 } 594 595 /* translate to physical */ 596 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr); 597 598 if ((chan & 4) == 0) { 599 /* 600 * Program one of DMA channels 0..3. These are 601 * byte mode channels. 602 */ 603 /* set dma channel mode, and reset address ff */ 604 if (flags & B_READ) 605 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan); 606 else 607 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan); 608 outb(DMA1_FFC, 0); 609 610 /* send start address */ 611 waport = DMA1_CHN(chan); 612 outb(waport, phys); 613 outb(waport, phys>>8); 614 outb(dmapageport[chan], phys>>16); 615 616 /* send count */ 617 outb(waport + 1, --nbytes); 618 outb(waport + 1, nbytes>>8); 619 620 /* unmask channel */ 621 outb(DMA1_SMSK, chan); 622 } else { 623 /* 624 * Program one of DMA channels 4..7. These are 625 * word mode channels. 626 */ 627 /* set dma channel mode, and reset address ff */ 628 if (flags & B_READ) 629 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3)); 630 else 631 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3)); 632 outb(DMA2_FFC, 0); 633 634 /* send start address */ 635 waport = DMA2_CHN(chan - 4); 636 outb(waport, phys>>1); 637 outb(waport, phys>>9); 638 outb(dmapageport[chan], phys>>16); 639 640 /* send count */ 641 nbytes >>= 1; 642 outb(waport + 2, --nbytes); 643 outb(waport + 2, nbytes>>8); 644 645 /* unmask channel */ 646 outb(DMA2_SMSK, chan & 3); 647 } 648} 649 650void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan) 651{ 652 653 /* copy bounce buffer on read */ 654 /*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/ 655 if (bounced[chan]) { 656 bcopy(dma_bounce[chan], addr, nbytes); 657 bounced[chan] = 0; 658 } 659} 660 661/* 662 * Check for problems with the address range of a DMA transfer 663 * (non-contiguous physical pages, outside of bus address space, 664 * crossing DMA page boundaries). 665 * Return true if special handling needed. 666 */ 667 668static int 669isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) { 670 vm_offset_t phys, priorpage = 0, endva; 671 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1); 672 673 endva = (vm_offset_t)round_page(va + length); 674 for (; va < (caddr_t) endva ; va += NBPG) { 675 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va)); 676#define ISARAM_END RAM_END 677 if (phys == 0) 678 panic("isa_dmacheck: no physical page present"); 679 if (phys >= ISARAM_END) 680 return (1); 681 if (priorpage) { 682 if (priorpage + NBPG != phys) 683 return (1); 684 /* check if crossing a DMA page boundary */ 685 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk) 686 return (1); 687 } 688 priorpage = phys; 689 } 690 return (0); 691} 692 693/* head of queue waiting for physmem to become available */ 694struct buf isa_physmemq; 695 696/* blocked waiting for resource to become free for exclusive use */ 697static isaphysmemflag; 698/* if waited for and call requested when free (B_CALL) */ 699static void (*isaphysmemunblock)(); /* needs to be a list */ 700 701/* 702 * Allocate contiguous physical memory for transfer, returning 703 * a *virtual* address to region. May block waiting for resource. 704 * (assumed to be called at splbio()) 705 */ 706caddr_t 707isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) { 708 709 isaphysmemunblock = func; 710 while (isaphysmemflag & B_BUSY) { 711 isaphysmemflag |= B_WANTED; 712 tsleep((caddr_t)&isaphysmemflag, PRIBIO, "isaphys", 0); 713 } 714 isaphysmemflag |= B_BUSY; 715 716 return((caddr_t)isaphysmem); 717} 718 719/* 720 * Free contiguous physical memory used for transfer. 721 * (assumed to be called at splbio()) 722 */ 723void 724isa_freephysmem(caddr_t va, unsigned length) { 725 726 isaphysmemflag &= ~B_BUSY; 727 if (isaphysmemflag & B_WANTED) { 728 isaphysmemflag &= B_WANTED; 729 wakeup((caddr_t)&isaphysmemflag); 730 if (isaphysmemunblock) 731 (*isaphysmemunblock)(); 732 } 733} 734 735#define NMI_PARITY (1 << 7) 736#define NMI_IOCHAN (1 << 6) 737#define ENMI_WATCHDOG (1 << 7) 738#define ENMI_BUSTIMER (1 << 6) 739#define ENMI_IOSTATUS (1 << 5) 740 741/* 742 * Handle a NMI, possibly a machine check. 743 * return true to panic system, false to ignore. 744 */ 745int 746isa_nmi(cd) 747 int cd; 748{ 749 int isa_port = inb(0x61); 750 int eisa_port = inb(0x461); 751 if(isa_port & NMI_PARITY) { 752 panic("RAM parity error, likely hardware failure."); 753 } else if(isa_port & NMI_IOCHAN) { 754 panic("I/O channel check, likely hardware failure."); 755 } else if(eisa_port & ENMI_WATCHDOG) { 756 panic("EISA watchdog timer expired, likely hardware failure."); 757 } else if(eisa_port & ENMI_BUSTIMER) { 758 panic("EISA bus timeout, likely hardware failure."); 759 } else if(eisa_port & ENMI_IOSTATUS) { 760 panic("EISA I/O port status error."); 761 } else { 762 printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port); 763 return(0); 764 } 765} 766 767/* 768 * Caught a stray interrupt, notify 769 */ 770static void 771isa_strayintr(d) 772 int d; 773{ 774 775 /* DON'T BOTHER FOR NOW! */ 776 /* for some reason, we get bursts of intr #7, even if not enabled! */ 777 /* 778 * Well the reason you got bursts of intr #7 is because someone 779 * raised an interrupt line and dropped it before the 8259 could 780 * prioritize it. This is documented in the intel data book. This 781 * means you have BAD hardware! I have changed this so that only 782 * the first 5 get logged, then it quits logging them, and puts 783 * out a special message. rgrimes 3/25/1993 784 */ 785 /* 786 * XXX TODO print a different message for #7 if it is for a 787 * glitch. Glitches can be distinguished from real #7's by 788 * testing that the in-service bit is _not_ set. The test 789 * must be done before sending an EOI so it can't be done if 790 * we are using AUTO_EOI_1. 791 */ 792 if (intrcnt[NR_DEVICES + d] <= 5) 793 log(LOG_ERR, "stray irq %d\n", d); 794 if (intrcnt[NR_DEVICES + d] == 5) 795 log(LOG_CRIT, 796 "too many stray irq %d's; not logging any more\n", d); 797} 798 799/* 800 * find an ISA device in a given isa_devtab_* table, given 801 * the table to search, the expected id_driver entry, and the unit number. 802 * 803 * this function is defined in isa_device.h, and this location is debatable; 804 * i put it there because it's useless w/o, and directly operates on 805 * the other stuff in that file. 806 * 807 */ 808 809struct isa_device *find_isadev(table, driverp, unit) 810 struct isa_device *table; 811 struct isa_driver *driverp; 812 int unit; 813{ 814 if (driverp == NULL) /* sanity check */ 815 return NULL; 816 817 while ((table->id_driver != driverp) || (table->id_unit != unit)) { 818 if (table->id_driver == 0) 819 return NULL; 820 821 table++; 822 } 823 824 return table; 825} 826 827/* 828 * Return nonzero if a (masked) irq is pending for a given device. 829 */ 830int 831isa_irq_pending(dvp) 832 struct isa_device *dvp; 833{ 834 unsigned id_irq; 835 836 id_irq = dvp->id_irq; 837 if (id_irq & 0xff) 838 return (inb(IO_ICU1) & id_irq); 839 return (inb(IO_ICU2) & (id_irq >> 8)); 840} 841 842int 843register_intr(intr, device_id, flags, handler, mask, unit) 844 int intr; 845 int device_id; 846 u_int flags; 847 inthand2_t *handler; 848 u_int mask; 849 int unit; 850{ 851 char *cp; 852 u_long ef; 853 int id; 854 855 if ((u_int)intr >= ICU_LEN || intr == 2 856 || (u_int)device_id >= NR_DEVICES) 857 return (EINVAL); 858 if (intr_handler[intr] != isa_strayintr) 859 return (EBUSY); 860 ef = read_eflags(); 861 disable_intr(); 862 intr_countp[intr] = &intrcnt[device_id]; 863 intr_handler[intr] = handler; 864 intr_mask[intr] = mask | (1 << intr); 865 intr_unit[intr] = unit; 866 setidt(ICU_OFFSET + intr, 867 flags & RI_FAST ? fastintr[intr] : slowintr[intr], 868 SDT_SYS386IGT, SEL_KPL); 869 write_eflags(ef); 870 for (cp = intrnames, id = 0; id <= device_id; id++) 871 while (*cp++ != '\0') 872 ; 873 if (cp > eintrnames) 874 return (0); 875 if (intr < 10) { 876 cp[-3] = intr + '0'; 877 cp[-2] = ' '; 878 } else { 879 cp[-3] = '1'; 880 cp[-2] = intr - 10 + '0'; 881 } 882 return (0); 883} 884 885static void 886register_imask(dvp, mask) 887 struct isa_device *dvp; 888 u_int mask; 889{ 890 if (dvp->id_alive && dvp->id_irq) { 891 int intr; 892 893 intr = ffs(dvp->id_irq) - 1; 894 intr_mask[intr] = mask | (1 <<intr); 895 } 896} 897 898int 899unregister_intr(intr, handler) 900 int intr; 901 inthand2_t *handler; 902{ 903 u_long ef; 904 905 if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr]) 906 return (EINVAL); 907 ef = read_eflags(); 908 disable_intr(); 909 intr_countp[intr] = &intrcnt[NR_DEVICES + intr]; 910 intr_handler[intr] = isa_strayintr; 911 intr_mask[intr] = HWI_MASK | SWI_MASK; 912 intr_unit[intr] = intr; 913 setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL); 914 write_eflags(ef); 915 return (0); 916} 917