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