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