Home | History | Annotate | Line # | Download | only in isa
pcppi.c revision 1.15
      1 /* $NetBSD: pcppi.c,v 1.15 2005/03/21 14:05:18 xtraeme 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/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: pcppi.c,v 1.15 2005/03/21 14:05:18 xtraeme Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/callout.h>
     36 #include <sys/kernel.h>
     37 #include <sys/proc.h>
     38 #include <sys/device.h>
     39 #include <sys/errno.h>
     40 
     41 #include <machine/bus.h>
     42 
     43 #include <dev/isa/isareg.h>
     44 #include <dev/isa/isavar.h>
     45 #include <dev/isa/pcppireg.h>
     46 #include <dev/isa/pcppivar.h>
     47 
     48 #include <dev/ic/i8253reg.h>
     49 
     50 #include "pckbd.h"
     51 #if NPCKBD > 0
     52 #include <dev/pckbport/pckbdvar.h>
     53 
     54 void	pcppi_pckbd_bell(void *, u_int, u_int, u_int, int);
     55 #endif
     56 
     57 int	pcppi_match(struct device *, struct cfdata *, void *);
     58 void	pcppi_isa_attach(struct device *, struct device *, void *);
     59 
     60 CFATTACH_DECL(pcppi, sizeof(struct pcppi_softc),
     61     pcppi_match, pcppi_isa_attach, NULL, NULL);
     62 
     63 static void pcppi_bell_stop(void*);
     64 
     65 #define PCPPIPRI (PZERO - 1)
     66 
     67 int
     68 pcppi_match(struct device *parent, struct cfdata *match, void *aux)
     69 {
     70 	struct isa_attach_args *ia = aux;
     71 	bus_space_handle_t ppi_ioh, pit1_ioh;
     72 	int have_pit1, have_ppi, rv;
     73 	u_int8_t v, nv;
     74 
     75 	if (ISA_DIRECT_CONFIG(ia))
     76 		return (0);
     77 
     78 	/* If values are hardwired to something that they can't be, punt. */
     79 	if (ia->ia_nio < 1 ||
     80 	    (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
     81 	    ia->ia_io[0].ir_addr != IO_PPI))
     82 		return (0);
     83 
     84 	if (ia->ia_niomem > 0 &&
     85 	    (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
     86 		return (0);
     87 
     88 	if (ia->ia_nirq > 0 &&
     89 	    (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ))
     90 		return (0);
     91 
     92 	if (ia->ia_ndrq > 0 &&
     93 	    (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
     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_io[0].ir_addr = IO_PPI;
    140 		ia->ia_io[0].ir_size = 1;
    141 		ia->ia_nio = 1;
    142 
    143 		ia->ia_niomem = 0;
    144 		ia->ia_nirq = 0;
    145 		ia->ia_ndrq = 0;
    146 	}
    147 	return (rv);
    148 }
    149 
    150 void
    151 pcppi_isa_attach(struct device *parent, struct device *self, void *aux)
    152 {
    153         struct pcppi_softc *sc = (struct pcppi_softc *)self;
    154         struct isa_attach_args *ia = aux;
    155         bus_space_tag_t iot;
    156 
    157         sc->sc_iot = iot = ia->ia_iot;
    158 
    159         if (bus_space_map(iot, IO_TIMER1, 4, 0, &sc->sc_pit1_ioh) ||
    160 	    bus_space_map(iot, IO_PPI, 1, 0, &sc->sc_ppi_ioh))
    161                 panic("pcppi_attach: couldn't map");
    162 
    163         printf("\n");
    164 
    165         pcppi_attach(sc);
    166 }
    167 
    168 void
    169 pcppi_attach(struct pcppi_softc *sc)
    170 {
    171         struct pcppi_attach_args pa;
    172 
    173         callout_init(&sc->sc_bell_ch);
    174 
    175         sc->sc_bellactive = sc->sc_bellpitch = sc->sc_slp = 0;
    176 
    177 #if NPCKBD > 0
    178 	/* Provide a beeper for the PC Keyboard, if there isn't one already. */
    179 	pckbd_hookup_bell(pcppi_pckbd_bell, sc);
    180 #endif
    181 
    182 	pa.pa_cookie = sc;
    183 	while (config_found((struct device *)sc, &pa, 0));
    184 }
    185 
    186 void
    187 pcppi_bell(pcppi_tag_t self, int pitch, int period, int slp)
    188 {
    189 	struct pcppi_softc *sc = self;
    190 	int s1, s2;
    191 
    192 	s1 = spltty(); /* ??? */
    193 	if (sc->sc_bellactive) {
    194 		if (sc->sc_timeout) {
    195 			sc->sc_timeout = 0;
    196 			callout_stop(&sc->sc_bell_ch);
    197 		}
    198 		if (sc->sc_slp)
    199 			wakeup(pcppi_bell_stop);
    200 	}
    201 	if (pitch == 0 || period == 0) {
    202 		pcppi_bell_stop(sc);
    203 		sc->sc_bellpitch = 0;
    204 		splx(s1);
    205 		return;
    206 	}
    207 	if (!sc->sc_bellactive || sc->sc_bellpitch != pitch) {
    208 		s2 = splhigh();
    209 		bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_MODE,
    210 		    TIMER_SEL2 | TIMER_16BIT | TIMER_SQWAVE);
    211 		bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_CNTR2,
    212 		    TIMER_DIV(pitch) % 256);
    213 		bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_CNTR2,
    214 		    TIMER_DIV(pitch) / 256);
    215 		splx(s2);
    216 		/* enable speaker */
    217 		bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    218 			bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    219 			| PIT_SPKR);
    220 	}
    221 	sc->sc_bellpitch = pitch;
    222 
    223 	sc->sc_bellactive = 1;
    224 	if (slp & PCPPI_BELL_POLL) {
    225 		delay((period * 1000000) / hz);
    226 		pcppi_bell_stop(sc);
    227 	} else {
    228 		sc->sc_timeout = 1;
    229 		callout_reset(&sc->sc_bell_ch, period, pcppi_bell_stop, sc);
    230 		if (slp & PCPPI_BELL_SLEEP) {
    231 			sc->sc_slp = 1;
    232 			tsleep(pcppi_bell_stop, PCPPIPRI | PCATCH, "bell", 0);
    233 			sc->sc_slp = 0;
    234 		}
    235 	}
    236 	splx(s1);
    237 }
    238 
    239 static void
    240 pcppi_bell_stop(void *arg)
    241 {
    242 	struct pcppi_softc *sc = arg;
    243 	int s;
    244 
    245 	s = spltty(); /* ??? */
    246 	sc->sc_timeout = 0;
    247 
    248 	/* disable bell */
    249 	bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    250 			  bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    251 			  & ~PIT_SPKR);
    252 	sc->sc_bellactive = 0;
    253 	if (sc->sc_slp)
    254 		wakeup(pcppi_bell_stop);
    255 	splx(s);
    256 }
    257 
    258 #if NPCKBD > 0
    259 void
    260 pcppi_pckbd_bell(void *arg, u_int pitch, u_int period, u_int volume, int poll)
    261 {
    262 
    263 	/*
    264 	 * Comes in as ms, goes out at ticks; volume ignored.
    265 	 */
    266 	pcppi_bell(arg, pitch, (period * hz) / 1000,
    267 	    poll ? PCPPI_BELL_POLL : 0);
    268 }
    269 #endif /* NPCKBD > 0 */
    270