Home | History | Annotate | Line # | Download | only in isa
pcppi.c revision 1.1.14.1
      1 /* $NetBSD: pcppi.c,v 1.1.14.1 2000/11/20 11:41:20 bouyer 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/callout.h>
     33 #include <sys/kernel.h>
     34 #include <sys/proc.h>
     35 #include <sys/device.h>
     36 #include <sys/errno.h>
     37 
     38 #include <machine/bus.h>
     39 
     40 #include <dev/isa/isareg.h>
     41 #include <dev/isa/isavar.h>
     42 #include <dev/isa/pcppireg.h>
     43 #include <dev/isa/pcppivar.h>
     44 
     45 #include <dev/ic/i8253reg.h>
     46 
     47 #include "pckbd.h"
     48 #if NPCKBD > 0
     49 #include <dev/ic/pckbcvar.h>
     50 #include <dev/pckbc/pckbdvar.h>
     51 
     52 void	pcppi_pckbd_bell __P((void *, u_int, u_int, u_int, int));
     53 #endif
     54 
     55 struct pcppi_softc {
     56 	struct device sc_dv;
     57 
     58 	bus_space_tag_t sc_iot;
     59 	bus_space_handle_t sc_ppi_ioh, sc_pit1_ioh;
     60 
     61 	struct callout sc_bell_ch;
     62 
     63 	int sc_bellactive, sc_bellpitch;
     64 	int sc_slp;
     65 	int sc_timeout;
     66 };
     67 
     68 int	pcppi_match __P((struct device *, struct cfdata *, void *));
     69 void	pcppi_attach __P((struct device *, struct device *, void *));
     70 
     71 struct cfattach pcppi_ca = {
     72 	sizeof(struct pcppi_softc), pcppi_match, pcppi_attach,
     73 };
     74 
     75 static void pcppi_bell_stop __P((void*));
     76 
     77 #define PCPPIPRI (PZERO - 1)
     78 
     79 int
     80 pcppi_match(parent, match, aux)
     81 	struct device *parent;
     82 	struct cfdata *match;
     83 	void *aux;
     84 {
     85 	struct isa_attach_args *ia = aux;
     86 	bus_space_handle_t ppi_ioh, pit1_ioh;
     87 	int have_pit1, have_ppi, rv;
     88 	u_int8_t v, nv;
     89 
     90 	/* If values are hardwired to something that they can't be, punt. */
     91 	if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_PPI) ||
     92 	    ia->ia_maddr != MADDRUNK || ia->ia_msize != 0 ||
     93 	    ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
     94 		return (0);
     95 
     96 	rv = 0;
     97 	have_pit1 = have_ppi = 0;
     98 
     99 	if (bus_space_map(ia->ia_iot, IO_TIMER1, 4, 0, &pit1_ioh))
    100 		goto lose;
    101 	have_pit1 = 1;
    102 	if (bus_space_map(ia->ia_iot, IO_PPI, 1, 0, &ppi_ioh))
    103 		goto lose;
    104 	have_ppi = 1;
    105 
    106 	/*
    107 	 * Check for existence of PPI.  Realistically, this is either going to
    108 	 * be here or nothing is going to be here.
    109 	 *
    110 	 * We don't want to have any chance of changing speaker output (which
    111 	 * this test might, if it crashes in the middle, or something;
    112 	 * normally it's be to quick to produce anthing audible), but
    113 	 * many "combo chip" mock-PPI's don't seem to support the top bit
    114 	 * of Port B as a settable bit.  The bottom bit has to be settable,
    115 	 * since the speaker driver hardware still uses it.
    116 	 */
    117 	v = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    118 	bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v ^ 0x01);	/* XXX */
    119 	nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    120 	if (((nv ^ v) & 0x01) == 0x01)
    121 		rv = 1;
    122 	bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v);		/* XXX */
    123 	nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    124 	if (((nv ^ v) & 0x01) != 0x00) {
    125 		rv = 0;
    126 		goto lose;
    127 	}
    128 
    129 	/*
    130 	 * We assume that the programmable interval timer is there.
    131 	 */
    132 
    133 lose:
    134 	if (have_pit1)
    135 		bus_space_unmap(ia->ia_iot, pit1_ioh, 4);
    136 	if (have_ppi)
    137 		bus_space_unmap(ia->ia_iot, ppi_ioh, 1);
    138 	if (rv) {
    139 		ia->ia_iobase = IO_PPI;
    140 		ia->ia_iosize = 0x1;
    141 		ia->ia_msize = 0x0;
    142 	}
    143 	return (rv);
    144 }
    145 
    146 void
    147 pcppi_attach(parent, self, aux)
    148 	struct device *parent, *self;
    149 	void *aux;
    150 {
    151 	struct pcppi_softc *sc = (struct pcppi_softc *)self;
    152 	struct isa_attach_args *ia = aux;
    153 	bus_space_tag_t iot;
    154 	struct pcppi_attach_args pa;
    155 
    156 	callout_init(&sc->sc_bell_ch);
    157 
    158 	sc->sc_iot = iot = ia->ia_iot;
    159 
    160 	if (bus_space_map(iot, IO_TIMER1, 4, 0, &sc->sc_pit1_ioh) ||
    161 	    bus_space_map(iot, IO_PPI, 1, 0, &sc->sc_ppi_ioh))
    162 		panic("pcppi_attach: couldn't map");
    163 
    164 	printf("\n");
    165 
    166 	sc->sc_bellactive = sc->sc_bellpitch = sc->sc_slp = 0;
    167 
    168 #if NPCKBD > 0
    169 	/* Provide a beeper for the PC Keyboard, if there isn't one already. */
    170 	pckbd_hookup_bell(pcppi_pckbd_bell, sc);
    171 #endif
    172 
    173 	pa.pa_cookie = sc;
    174 	while (config_found(self, &pa, 0));
    175 }
    176 
    177 void
    178 pcppi_bell(self, pitch, period, slp)
    179 	pcppi_tag_t self;
    180 	int pitch, period;
    181 	int slp;
    182 {
    183 	struct pcppi_softc *sc = self;
    184 	int s1, s2;
    185 
    186 	s1 = spltty(); /* ??? */
    187 	if (sc->sc_bellactive) {
    188 		if (sc->sc_timeout) {
    189 			sc->sc_timeout = 0;
    190 			callout_stop(&sc->sc_bell_ch);
    191 		}
    192 		if (sc->sc_slp)
    193 			wakeup(pcppi_bell_stop);
    194 	}
    195 	if (pitch == 0 || period == 0) {
    196 		pcppi_bell_stop(sc);
    197 		sc->sc_bellpitch = 0;
    198 		splx(s1);
    199 		return;
    200 	}
    201 	if (!sc->sc_bellactive || sc->sc_bellpitch != pitch) {
    202 		s2 = splhigh();
    203 		bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_MODE,
    204 		    TIMER_SEL2 | TIMER_16BIT | TIMER_SQWAVE);
    205 		bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_CNTR2,
    206 		    TIMER_DIV(pitch) % 256);
    207 		bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_CNTR2,
    208 		    TIMER_DIV(pitch) / 256);
    209 		splx(s2);
    210 		/* enable speaker */
    211 		bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    212 			bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    213 			| PIT_SPKR);
    214 	}
    215 	sc->sc_bellpitch = pitch;
    216 
    217 	sc->sc_bellactive = 1;
    218 	if (slp & PCPPI_BELL_POLL) {
    219 		delay((period * 1000000) / hz);
    220 		pcppi_bell_stop(sc);
    221 	} else {
    222 		sc->sc_timeout = 1;
    223 		callout_reset(&sc->sc_bell_ch, period, pcppi_bell_stop, sc);
    224 		if (slp & PCPPI_BELL_SLEEP) {
    225 			sc->sc_slp = 1;
    226 			tsleep(pcppi_bell_stop, PCPPIPRI | PCATCH, "bell", 0);
    227 			sc->sc_slp = 0;
    228 		}
    229 	}
    230 	splx(s1);
    231 }
    232 
    233 static void
    234 pcppi_bell_stop(arg)
    235 	void *arg;
    236 {
    237 	struct pcppi_softc *sc = arg;
    238 	int s;
    239 
    240 	s = spltty(); /* ??? */
    241 	sc->sc_timeout = 0;
    242 
    243 	/* disable bell */
    244 	bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    245 			  bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    246 			  & ~PIT_SPKR);
    247 	sc->sc_bellactive = 0;
    248 	if (sc->sc_slp)
    249 		wakeup(pcppi_bell_stop);
    250 	splx(s);
    251 }
    252 
    253 #if NPCKBD > 0
    254 void
    255 pcppi_pckbd_bell(arg, pitch, period, volume, poll)
    256 	void *arg;
    257 	u_int pitch, period, volume;
    258 	int poll;
    259 {
    260 
    261 	/*
    262 	 * Comes in as ms, goes out at ticks; volume ignored.
    263 	 */
    264 	pcppi_bell(arg, pitch, (period * hz) / 1000,
    265 	    poll ? PCPPI_BELL_POLL : 0);
    266 }
    267 #endif /* NPCKBD > 0 */
    268