Home | History | Annotate | Line # | Download | only in isa
pcppi.c revision 1.2
      1 /* $NetBSD: pcppi.c,v 1.2 2000/03/06 21:40:08 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/kernel.h>
     33 #include <sys/proc.h>
     34 #include <sys/device.h>
     35 #include <sys/errno.h>
     36 
     37 #include <machine/bus.h>
     38 
     39 #include <dev/isa/isareg.h>
     40 #include <dev/isa/isavar.h>
     41 #include <dev/isa/pcppireg.h>
     42 #include <dev/isa/pcppivar.h>
     43 
     44 #include <dev/ic/i8253reg.h>
     45 
     46 #include "pckbd.h"
     47 #if NPCKBD > 0
     48 #include <dev/ic/pckbcvar.h>
     49 #include <dev/pckbc/pckbdvar.h>
     50 
     51 void	pcppi_pckbd_bell __P((void *, u_int, u_int, u_int));
     52 #endif
     53 
     54 struct pcppi_softc {
     55 	struct device sc_dv;
     56 
     57 	bus_space_tag_t sc_iot;
     58 	bus_space_handle_t sc_ppi_ioh, sc_pit1_ioh;
     59 
     60 	int sc_bellactive, sc_bellpitch;
     61 	int sc_slp;
     62 	int sc_timeout;
     63 };
     64 
     65 int	pcppi_match __P((struct device *, struct cfdata *, void *));
     66 void	pcppi_attach __P((struct device *, struct device *, void *));
     67 
     68 struct cfattach pcppi_ca = {
     69 	sizeof(struct pcppi_softc), pcppi_match, pcppi_attach,
     70 };
     71 
     72 static void pcppi_bell_stop __P((void*));
     73 
     74 #define PCPPIPRI (PZERO - 1)
     75 
     76 int
     77 pcppi_match(parent, match, aux)
     78 	struct device *parent;
     79 	struct cfdata *match;
     80 	void *aux;
     81 {
     82 	struct isa_attach_args *ia = aux;
     83 	bus_space_handle_t ppi_ioh, pit1_ioh;
     84 	int have_pit1, have_ppi, rv;
     85 	u_int8_t v, nv;
     86 
     87 	/* If values are hardwired to something that they can't be, punt. */
     88 	if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_PPI) ||
     89 	    ia->ia_maddr != MADDRUNK || ia->ia_msize != 0 ||
     90 	    ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
     91 		return (0);
     92 
     93 	rv = 0;
     94 	have_pit1 = have_ppi = 0;
     95 
     96 	if (bus_space_map(ia->ia_iot, IO_TIMER1, 4, 0, &pit1_ioh))
     97 		goto lose;
     98 	have_pit1 = 1;
     99 	if (bus_space_map(ia->ia_iot, IO_PPI, 1, 0, &ppi_ioh))
    100 		goto lose;
    101 	have_ppi = 1;
    102 
    103 	/*
    104 	 * Check for existence of PPI.  Realistically, this is either going to
    105 	 * be here or nothing is going to be here.
    106 	 *
    107 	 * We don't want to have any chance of changing speaker output (which
    108 	 * this test might, if it crashes in the middle, or something;
    109 	 * normally it's be to quick to produce anthing audible), but
    110 	 * many "combo chip" mock-PPI's don't seem to support the top bit
    111 	 * of Port B as a settable bit.  The bottom bit has to be settable,
    112 	 * since the speaker driver hardware still uses it.
    113 	 */
    114 	v = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    115 	bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v ^ 0x01);	/* XXX */
    116 	nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    117 	if (((nv ^ v) & 0x01) == 0x01)
    118 		rv = 1;
    119 	bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v);		/* XXX */
    120 	nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    121 	if (((nv ^ v) & 0x01) != 0x00) {
    122 		rv = 0;
    123 		goto lose;
    124 	}
    125 
    126 	/*
    127 	 * We assume that the programmable interval timer is there.
    128 	 */
    129 
    130 lose:
    131 	if (have_pit1)
    132 		bus_space_unmap(ia->ia_iot, pit1_ioh, 4);
    133 	if (have_ppi)
    134 		bus_space_unmap(ia->ia_iot, ppi_ioh, 1);
    135 	if (rv) {
    136 		ia->ia_iobase = IO_PPI;
    137 		ia->ia_iosize = 0x1;
    138 		ia->ia_msize = 0x0;
    139 	}
    140 	return (rv);
    141 }
    142 
    143 void
    144 pcppi_attach(parent, self, aux)
    145 	struct device *parent, *self;
    146 	void *aux;
    147 {
    148 	struct pcppi_softc *sc = (struct pcppi_softc *)self;
    149 	struct isa_attach_args *ia = aux;
    150 	bus_space_tag_t iot;
    151 	struct pcppi_attach_args pa;
    152 
    153 	sc->sc_iot = iot = ia->ia_iot;
    154 
    155 	if (bus_space_map(iot, IO_TIMER1, 4, 0, &sc->sc_pit1_ioh) ||
    156 	    bus_space_map(iot, IO_PPI, 1, 0, &sc->sc_ppi_ioh))
    157 		panic("pcppi_attach: couldn't map");
    158 
    159 	printf("\n");
    160 
    161 	sc->sc_bellactive = sc->sc_bellpitch = sc->sc_slp = 0;
    162 
    163 #if NPCKBD > 0
    164 	/* Provide a beeper for the PC Keyboard, if there isn't one already. */
    165 	pckbd_hookup_bell(pcppi_pckbd_bell, sc);
    166 #endif
    167 
    168 	pa.pa_cookie = sc;
    169 	while (config_found(self, &pa, 0));
    170 }
    171 
    172 void
    173 pcppi_bell(self, pitch, period, slp)
    174 	pcppi_tag_t self;
    175 	int pitch, period;
    176 	int slp;
    177 {
    178 	struct pcppi_softc *sc = self;
    179 	int s1, s2;
    180 
    181 	s1 = spltty(); /* ??? */
    182 	if (sc->sc_bellactive) {
    183 		if (sc->sc_timeout) {
    184 			sc->sc_timeout = 0;
    185 			untimeout(pcppi_bell_stop, sc);
    186 		}
    187 		if (sc->sc_slp)
    188 			wakeup(pcppi_bell_stop);
    189 	}
    190 	if (pitch == 0 || period == 0) {
    191 		pcppi_bell_stop(sc);
    192 		sc->sc_bellpitch = 0;
    193 		splx(s1);
    194 		return;
    195 	}
    196 	if (!sc->sc_bellactive || sc->sc_bellpitch != pitch) {
    197 		s2 = splhigh();
    198 		bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_MODE,
    199 		    TIMER_SEL2 | TIMER_16BIT | TIMER_SQWAVE);
    200 		bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_CNTR2,
    201 		    TIMER_DIV(pitch) % 256);
    202 		bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_CNTR2,
    203 		    TIMER_DIV(pitch) / 256);
    204 		splx(s2);
    205 		/* enable speaker */
    206 		bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    207 			bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    208 			| PIT_SPKR);
    209 	}
    210 	sc->sc_bellpitch = pitch;
    211 
    212 	sc->sc_bellactive = 1;
    213 
    214 	if (slp & PCPPI_BELL_POLL) {
    215 		delay((period * 1000000) / hz);
    216 		pcppi_bell_stop(sc);
    217 	} else {
    218 		sc->sc_timeout = 1;
    219 		timeout(pcppi_bell_stop, sc, period);
    220 		if (slp & PCPPI_BELL_SLEEP) {
    221 			sc->sc_slp = 1;
    222 			tsleep(pcppi_bell_stop, PCPPIPRI | PCATCH, "bell", 0);
    223 			sc->sc_slp = 0;
    224 		}
    225 	}
    226 	splx(s1);
    227 }
    228 
    229 static void
    230 pcppi_bell_stop(arg)
    231 	void *arg;
    232 {
    233 	struct pcppi_softc *sc = arg;
    234 	int s;
    235 
    236 	s = spltty(); /* ??? */
    237 	sc->sc_timeout = 0;
    238 
    239 	/* disable bell */
    240 	bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    241 			  bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    242 			  & ~PIT_SPKR);
    243 	sc->sc_bellactive = 0;
    244 	if (sc->sc_slp)
    245 		wakeup(pcppi_bell_stop);
    246 	splx(s);
    247 }
    248 
    249 #if NPCKBD > 0
    250 void
    251 pcppi_pckbd_bell(arg, pitch, period, volume)
    252 	void *arg;
    253 	u_int pitch, period, volume;
    254 {
    255 
    256 	/*
    257 	 * Comes in as ms, goes out at ticks; volume ignored.
    258 	 */
    259 	pcppi_bell(arg, pitch, (period * hz) / 1000, PCPPI_BELL_POLL);
    260 }
    261 #endif /* NPCKBD > 0 */
    262