Home | History | Annotate | Line # | Download | only in isa
pcppi.c revision 1.42.2.1
      1  1.42.2.1  jdolecek /* $NetBSD: pcppi.c,v 1.42.2.1 2017/12/03 11:37:05 jdolecek 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.13     perry  *
      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.13     perry  *
     15      1.13     perry  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16      1.13     perry  * 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.13     perry  *
     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.42.2.1  jdolecek __KERNEL_RCSID(0, "$NetBSD: pcppi.c,v 1.42.2.1 2017/12/03 11:37:05 jdolecek Exp $");
     32      1.16      cube 
     33      1.16      cube #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.24        ad #include <sys/bus.h>
     43      1.36       mrg #include <sys/mutex.h>
     44      1.36       mrg #include <sys/condvar.h>
     45      1.38  jmcneill #include <sys/tty.h>
     46       1.1  drochner 
     47      1.16      cube #include <dev/ic/attimervar.h>
     48      1.16      cube 
     49       1.1  drochner #include <dev/isa/isareg.h>
     50       1.1  drochner #include <dev/isa/isavar.h>
     51       1.1  drochner #include <dev/isa/pcppireg.h>
     52       1.1  drochner #include <dev/isa/pcppivar.h>
     53       1.1  drochner 
     54       1.2   thorpej #include "pckbd.h"
     55       1.2   thorpej #if NPCKBD > 0
     56      1.10     bjh21 #include <dev/pckbport/pckbdvar.h>
     57       1.2   thorpej 
     58      1.12     perry void	pcppi_pckbd_bell(void *, u_int, u_int, u_int, int);
     59       1.2   thorpej #endif
     60       1.2   thorpej 
     61      1.30      cube int	pcppi_match(device_t, cfdata_t, void *);
     62      1.29    dyoung void	pcppi_isa_attach(device_t, device_t, void *);
     63      1.29    dyoung void	pcppi_childdet(device_t, device_t);
     64  1.42.2.1  jdolecek int	pcppi_rescan(device_t, const char *, const int *);
     65       1.1  drochner 
     66      1.35    dyoung CFATTACH_DECL3_NEW(pcppi, sizeof(struct pcppi_softc),
     67  1.42.2.1  jdolecek     pcppi_match, pcppi_isa_attach, pcppi_detach, NULL, pcppi_rescan,
     68  1.42.2.1  jdolecek     pcppi_childdet, DVF_DETACH_SHUTDOWN);
     69       1.1  drochner 
     70      1.19      cube static int pcppisearch(device_t, cfdata_t, const int *, void *);
     71      1.38  jmcneill static void pcppi_bell_stop(struct pcppi_softc *);
     72      1.38  jmcneill static void pcppi_bell_callout(void *);
     73       1.1  drochner 
     74      1.16      cube #if NATTIMER > 0
     75      1.29    dyoung static void pcppi_attach_speaker(device_t);
     76      1.34    dyoung static void pcppi_detach_speaker(struct pcppi_softc *);
     77      1.16      cube #endif
     78      1.16      cube 
     79       1.1  drochner int
     80      1.30      cube pcppi_match(device_t parent, cfdata_t match, void *aux)
     81       1.1  drochner {
     82       1.1  drochner 	struct isa_attach_args *ia = aux;
     83      1.16      cube 	bus_space_handle_t ppi_ioh;
     84      1.16      cube 	int have_ppi, rv;
     85       1.1  drochner 	u_int8_t v, nv;
     86       1.1  drochner 
     87       1.6   thorpej 	if (ISA_DIRECT_CONFIG(ia))
     88       1.6   thorpej 		return (0);
     89       1.6   thorpej 
     90       1.1  drochner 	/* If values are hardwired to something that they can't be, punt. */
     91      1.13     perry 	if (ia->ia_nio < 1 ||
     92      1.11  drochner 	    (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
     93       1.6   thorpej 	    ia->ia_io[0].ir_addr != IO_PPI))
     94       1.6   thorpej 		return (0);
     95       1.6   thorpej 
     96       1.6   thorpej 	if (ia->ia_niomem > 0 &&
     97      1.11  drochner 	    (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
     98       1.6   thorpej 		return (0);
     99       1.6   thorpej 
    100       1.6   thorpej 	if (ia->ia_nirq > 0 &&
    101      1.11  drochner 	    (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ))
    102       1.6   thorpej 		return (0);
    103       1.6   thorpej 
    104       1.6   thorpej 	if (ia->ia_ndrq > 0 &&
    105      1.11  drochner 	    (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
    106       1.1  drochner 		return (0);
    107       1.1  drochner 
    108       1.1  drochner 	rv = 0;
    109      1.16      cube 	have_ppi = 0;
    110       1.1  drochner 
    111       1.1  drochner 	if (bus_space_map(ia->ia_iot, IO_PPI, 1, 0, &ppi_ioh))
    112       1.1  drochner 		goto lose;
    113       1.1  drochner 	have_ppi = 1;
    114       1.1  drochner 
    115       1.1  drochner 	/*
    116       1.1  drochner 	 * Check for existence of PPI.  Realistically, this is either going to
    117       1.1  drochner 	 * be here or nothing is going to be here.
    118       1.1  drochner 	 *
    119       1.1  drochner 	 * We don't want to have any chance of changing speaker output (which
    120       1.1  drochner 	 * this test might, if it crashes in the middle, or something;
    121       1.1  drochner 	 * normally it's be to quick to produce anthing audible), but
    122       1.1  drochner 	 * many "combo chip" mock-PPI's don't seem to support the top bit
    123       1.1  drochner 	 * of Port B as a settable bit.  The bottom bit has to be settable,
    124       1.1  drochner 	 * since the speaker driver hardware still uses it.
    125       1.1  drochner 	 */
    126       1.1  drochner 	v = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    127       1.1  drochner 	bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v ^ 0x01);	/* XXX */
    128       1.1  drochner 	nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    129       1.1  drochner 	if (((nv ^ v) & 0x01) == 0x01)
    130       1.1  drochner 		rv = 1;
    131       1.1  drochner 	bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v);		/* XXX */
    132       1.1  drochner 	nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    133       1.1  drochner 	if (((nv ^ v) & 0x01) != 0x00) {
    134       1.1  drochner 		rv = 0;
    135       1.1  drochner 		goto lose;
    136       1.1  drochner 	}
    137       1.1  drochner 
    138       1.1  drochner 	/*
    139       1.1  drochner 	 * We assume that the programmable interval timer is there.
    140       1.1  drochner 	 */
    141       1.1  drochner 
    142       1.1  drochner lose:
    143       1.1  drochner 	if (have_ppi)
    144       1.1  drochner 		bus_space_unmap(ia->ia_iot, ppi_ioh, 1);
    145       1.1  drochner 	if (rv) {
    146       1.6   thorpej 		ia->ia_io[0].ir_addr = IO_PPI;
    147       1.6   thorpej 		ia->ia_io[0].ir_size = 1;
    148       1.6   thorpej 		ia->ia_nio = 1;
    149       1.6   thorpej 
    150       1.6   thorpej 		ia->ia_niomem = 0;
    151       1.6   thorpej 		ia->ia_nirq = 0;
    152       1.6   thorpej 		ia->ia_ndrq = 0;
    153       1.1  drochner 	}
    154       1.1  drochner 	return (rv);
    155       1.1  drochner }
    156       1.1  drochner 
    157       1.1  drochner void
    158      1.29    dyoung pcppi_isa_attach(device_t parent, device_t self, void *aux)
    159       1.1  drochner {
    160      1.29    dyoung         struct pcppi_softc *sc = device_private(self);
    161      1.15   xtraeme         struct isa_attach_args *ia = aux;
    162      1.15   xtraeme         bus_space_tag_t iot;
    163      1.15   xtraeme 
    164      1.30      cube 	sc->sc_dv = self;
    165      1.15   xtraeme         sc->sc_iot = iot = ia->ia_iot;
    166      1.15   xtraeme 
    167      1.26    dyoung         sc->sc_size = 1;
    168      1.26    dyoung         if (bus_space_map(iot, IO_PPI, sc->sc_size, 0, &sc->sc_ppi_ioh))
    169      1.15   xtraeme                 panic("pcppi_attach: couldn't map");
    170       1.1  drochner 
    171  1.42.2.1  jdolecek 	aprint_naive("\n");
    172      1.33        ad 	aprint_normal("\n");
    173      1.15   xtraeme         pcppi_attach(sc);
    174      1.15   xtraeme }
    175       1.1  drochner 
    176      1.29    dyoung void
    177      1.29    dyoung pcppi_childdet(device_t self, device_t child)
    178      1.29    dyoung {
    179      1.38  jmcneill 
    180      1.31    dyoung 	/* we hold no child references, so do nothing */
    181      1.29    dyoung }
    182      1.29    dyoung 
    183      1.26    dyoung int
    184      1.26    dyoung pcppi_detach(device_t self, int flags)
    185      1.26    dyoung {
    186      1.26    dyoung 	int rc;
    187      1.26    dyoung 	struct pcppi_softc *sc = device_private(self);
    188      1.26    dyoung 
    189      1.34    dyoung #if NATTIMER > 0
    190      1.34    dyoung 	pcppi_detach_speaker(sc);
    191      1.34    dyoung #endif
    192      1.34    dyoung 
    193      1.30      cube 	if ((rc = config_detach_children(sc->sc_dv, flags)) != 0)
    194      1.26    dyoung 		return rc;
    195      1.26    dyoung 
    196      1.27    dyoung 	pmf_device_deregister(self);
    197      1.26    dyoung 
    198      1.26    dyoung #if NPCKBD > 0
    199      1.26    dyoung 	pckbd_unhook_bell(pcppi_pckbd_bell, sc);
    200      1.26    dyoung #endif
    201      1.38  jmcneill 	mutex_spin_enter(&tty_lock);
    202      1.26    dyoung 	pcppi_bell_stop(sc);
    203      1.38  jmcneill 	mutex_spin_exit(&tty_lock);
    204      1.26    dyoung 
    205      1.38  jmcneill 	callout_halt(&sc->sc_bell_ch, NULL);
    206      1.26    dyoung 	callout_destroy(&sc->sc_bell_ch);
    207      1.38  jmcneill 
    208      1.38  jmcneill 	cv_destroy(&sc->sc_slp);
    209      1.38  jmcneill 
    210      1.26    dyoung 	bus_space_unmap(sc->sc_iot, sc->sc_ppi_ioh, sc->sc_size);
    211      1.36       mrg 
    212      1.26    dyoung 	return 0;
    213      1.26    dyoung }
    214      1.26    dyoung 
    215      1.15   xtraeme void
    216      1.15   xtraeme pcppi_attach(struct pcppi_softc *sc)
    217      1.15   xtraeme {
    218      1.30      cube 	device_t self = sc->sc_dv;
    219       1.1  drochner 
    220      1.38  jmcneill 	callout_init(&sc->sc_bell_ch, CALLOUT_MPSAFE);
    221      1.38  jmcneill 	callout_setfunc(&sc->sc_bell_ch, pcppi_bell_callout, sc);
    222      1.38  jmcneill 	cv_init(&sc->sc_slp, "bell");
    223       1.1  drochner 
    224      1.38  jmcneill         sc->sc_bellactive = sc->sc_bellpitch = 0;
    225      1.36       mrg 
    226       1.2   thorpej #if NPCKBD > 0
    227       1.2   thorpej 	/* Provide a beeper for the PC Keyboard, if there isn't one already. */
    228       1.2   thorpej 	pckbd_hookup_bell(pcppi_pckbd_bell, sc);
    229       1.2   thorpej #endif
    230      1.16      cube #if NATTIMER > 0
    231      1.30      cube 	config_defer(sc->sc_dv, pcppi_attach_speaker);
    232      1.16      cube #endif
    233      1.42    plunky 	if (!pmf_device_register(self, NULL, NULL))
    234      1.42    plunky 		aprint_error_dev(self, "couldn't establish power handler\n");
    235       1.2   thorpej 
    236  1.42.2.1  jdolecek 	pcppi_rescan(self, "pcppi", NULL);
    237  1.42.2.1  jdolecek }
    238  1.42.2.1  jdolecek 
    239  1.42.2.1  jdolecek int
    240  1.42.2.1  jdolecek pcppi_rescan(device_t self, const char *ifattr, const int *flags)
    241  1.42.2.1  jdolecek {
    242  1.42.2.1  jdolecek 	struct pcppi_softc *sc = device_private(self);
    243  1.42.2.1  jdolecek         struct pcppi_attach_args pa;
    244  1.42.2.1  jdolecek 
    245  1.42.2.1  jdolecek 	if (!ifattr_match(ifattr, "pcppi"))
    246  1.42.2.1  jdolecek 		return 0;
    247  1.42.2.1  jdolecek 
    248       1.1  drochner 	pa.pa_cookie = sc;
    249  1.42.2.1  jdolecek 	pa.pa_bell_func = pcppi_bell;
    250      1.30      cube 	config_search_loc(pcppisearch, sc->sc_dv, "pcppi", NULL, &pa);
    251  1.42.2.1  jdolecek 
    252  1.42.2.1  jdolecek 	return 0;
    253      1.19      cube }
    254      1.19      cube 
    255      1.19      cube static int
    256      1.19      cube pcppisearch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    257      1.19      cube {
    258      1.19      cube 
    259      1.19      cube 	if (config_match(parent, cf, aux))
    260      1.19      cube 		config_attach_loc(parent, cf, locs, aux, NULL);
    261      1.19      cube 
    262      1.19      cube 	return 0;
    263       1.1  drochner }
    264       1.1  drochner 
    265      1.16      cube #if NATTIMER > 0
    266      1.16      cube static void
    267      1.34    dyoung pcppi_detach_speaker(struct pcppi_softc *sc)
    268      1.34    dyoung {
    269      1.34    dyoung 	if (sc->sc_timer != NULL) {
    270      1.34    dyoung 		attimer_detach_speaker(sc->sc_timer);
    271      1.34    dyoung 		sc->sc_timer = NULL;
    272      1.34    dyoung 	}
    273      1.34    dyoung }
    274      1.34    dyoung 
    275      1.34    dyoung static void
    276      1.29    dyoung pcppi_attach_speaker(device_t self)
    277      1.16      cube {
    278      1.29    dyoung 	struct pcppi_softc *sc = device_private(self);
    279      1.16      cube 
    280      1.16      cube 	if ((sc->sc_timer = attimer_attach_speaker()) == NULL)
    281      1.29    dyoung 		aprint_error_dev(self, "could not find any available timer\n");
    282      1.29    dyoung 	else {
    283      1.30      cube 		aprint_normal_dev(sc->sc_timer, "attached to %s\n",
    284      1.29    dyoung 		    device_xname(self));
    285      1.29    dyoung 	}
    286      1.16      cube }
    287      1.16      cube #endif
    288      1.16      cube 
    289       1.1  drochner void
    290      1.14   xtraeme pcppi_bell(pcppi_tag_t self, int pitch, int period, int slp)
    291       1.1  drochner {
    292      1.38  jmcneill 
    293      1.38  jmcneill 	mutex_spin_enter(&tty_lock);
    294      1.38  jmcneill 	pcppi_bell_locked(self, pitch, period, slp);
    295      1.38  jmcneill 	mutex_spin_exit(&tty_lock);
    296      1.38  jmcneill }
    297      1.38  jmcneill 
    298      1.38  jmcneill void
    299      1.38  jmcneill pcppi_bell_locked(pcppi_tag_t self, int pitch, int period, int slp)
    300      1.38  jmcneill {
    301      1.32      cube 	struct pcppi_softc *sc = self;
    302       1.1  drochner 
    303       1.1  drochner 	if (sc->sc_bellactive) {
    304       1.2   thorpej 		if (sc->sc_timeout) {
    305       1.2   thorpej 			sc->sc_timeout = 0;
    306       1.4   thorpej 			callout_stop(&sc->sc_bell_ch);
    307       1.2   thorpej 		}
    308      1.38  jmcneill 		cv_broadcast(&sc->sc_slp);
    309       1.1  drochner 	}
    310       1.1  drochner 	if (pitch == 0 || period == 0) {
    311      1.38  jmcneill 		pcppi_bell_stop(sc);
    312       1.1  drochner 		sc->sc_bellpitch = 0;
    313       1.1  drochner 		return;
    314       1.1  drochner 	}
    315       1.1  drochner 	if (!sc->sc_bellactive || sc->sc_bellpitch != pitch) {
    316      1.16      cube #if NATTIMER > 0
    317      1.16      cube 		if (sc->sc_timer != NULL)
    318      1.16      cube 			attimer_set_pitch(sc->sc_timer, pitch);
    319      1.16      cube #endif
    320       1.1  drochner 		/* enable speaker */
    321       1.1  drochner 		bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    322       1.1  drochner 			bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    323       1.1  drochner 			| PIT_SPKR);
    324       1.1  drochner 	}
    325       1.1  drochner 	sc->sc_bellpitch = pitch;
    326       1.1  drochner 
    327       1.1  drochner 	sc->sc_bellactive = 1;
    328       1.2   thorpej 	if (slp & PCPPI_BELL_POLL) {
    329       1.2   thorpej 		delay((period * 1000000) / hz);
    330      1.38  jmcneill 		pcppi_bell_stop(sc);
    331       1.2   thorpej 	} else {
    332       1.2   thorpej 		sc->sc_timeout = 1;
    333      1.38  jmcneill 		callout_schedule(&sc->sc_bell_ch, period);
    334       1.2   thorpej 		if (slp & PCPPI_BELL_SLEEP) {
    335      1.38  jmcneill 			cv_wait_sig(&sc->sc_slp, &tty_lock);
    336       1.2   thorpej 		}
    337       1.1  drochner 	}
    338       1.1  drochner }
    339       1.1  drochner 
    340       1.1  drochner static void
    341      1.38  jmcneill pcppi_bell_callout(void *arg)
    342       1.1  drochner {
    343       1.1  drochner 	struct pcppi_softc *sc = arg;
    344       1.1  drochner 
    345      1.38  jmcneill 	mutex_spin_enter(&tty_lock);
    346      1.38  jmcneill 	if (sc->sc_timeout != 0) {
    347      1.38  jmcneill 		pcppi_bell_stop(sc);
    348      1.38  jmcneill 	}
    349      1.38  jmcneill 	mutex_spin_exit(&tty_lock);
    350      1.38  jmcneill }
    351      1.38  jmcneill 
    352      1.38  jmcneill static void
    353      1.38  jmcneill pcppi_bell_stop(struct pcppi_softc *sc)
    354      1.38  jmcneill {
    355      1.38  jmcneill 
    356       1.2   thorpej 	sc->sc_timeout = 0;
    357       1.2   thorpej 
    358       1.1  drochner 	/* disable bell */
    359       1.1  drochner 	bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    360       1.1  drochner 			  bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    361       1.1  drochner 			  & ~PIT_SPKR);
    362       1.1  drochner 	sc->sc_bellactive = 0;
    363      1.38  jmcneill 	cv_broadcast(&sc->sc_slp);
    364       1.1  drochner }
    365       1.2   thorpej 
    366       1.2   thorpej #if NPCKBD > 0
    367       1.2   thorpej void
    368      1.20  christos pcppi_pckbd_bell(void *arg, u_int pitch, u_int period, u_int volume,
    369      1.18  christos     int poll)
    370       1.2   thorpej {
    371       1.2   thorpej 
    372       1.2   thorpej 	/*
    373       1.2   thorpej 	 * Comes in as ms, goes out at ticks; volume ignored.
    374       1.2   thorpej 	 */
    375      1.41  drochner 	pcppi_bell_locked(arg, pitch, (period * hz) / 1000,
    376       1.3   thorpej 	    poll ? PCPPI_BELL_POLL : 0);
    377       1.2   thorpej }
    378       1.2   thorpej #endif /* NPCKBD > 0 */
    379