Home | History | Annotate | Line # | Download | only in isa
pcppi.c revision 1.17.22.3
      1  1.17.22.3      yamt /* $NetBSD: pcppi.c,v 1.17.22.3 2006/12/18 11:42:13 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.17.22.3      yamt __KERNEL_RCSID(0, "$NetBSD: pcppi.c,v 1.17.22.3 2006/12/18 11:42:13 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.1  drochner #include <machine/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.12     perry int	pcppi_match(struct device *, struct cfdata *, void *);
     60       1.15   xtraeme 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.15   xtraeme     pcppi_match, pcppi_isa_attach, NULL, NULL);
     64        1.1  drochner 
     65  1.17.22.2      yamt static int pcppisearch(device_t, cfdata_t, const int *, void *);
     66       1.12     perry static void pcppi_bell_stop(void*);
     67        1.1  drochner 
     68       1.16      cube #if NATTIMER > 0
     69       1.16      cube static void pcppi_attach_speaker(struct device *);
     70       1.16      cube #endif
     71       1.16      cube 
     72        1.1  drochner #define PCPPIPRI (PZERO - 1)
     73        1.1  drochner 
     74        1.1  drochner int
     75  1.17.22.2      yamt pcppi_match(struct device *parent, struct cfdata *match,
     76  1.17.22.1      yamt     void *aux)
     77        1.1  drochner {
     78        1.1  drochner 	struct isa_attach_args *ia = aux;
     79       1.16      cube 	bus_space_handle_t ppi_ioh;
     80       1.16      cube 	int have_ppi, rv;
     81        1.1  drochner 	u_int8_t v, nv;
     82        1.1  drochner 
     83        1.6   thorpej 	if (ISA_DIRECT_CONFIG(ia))
     84        1.6   thorpej 		return (0);
     85        1.6   thorpej 
     86        1.1  drochner 	/* If values are hardwired to something that they can't be, punt. */
     87       1.13     perry 	if (ia->ia_nio < 1 ||
     88       1.11  drochner 	    (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
     89        1.6   thorpej 	    ia->ia_io[0].ir_addr != IO_PPI))
     90        1.6   thorpej 		return (0);
     91        1.6   thorpej 
     92        1.6   thorpej 	if (ia->ia_niomem > 0 &&
     93       1.11  drochner 	    (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
     94        1.6   thorpej 		return (0);
     95        1.6   thorpej 
     96        1.6   thorpej 	if (ia->ia_nirq > 0 &&
     97       1.11  drochner 	    (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ))
     98        1.6   thorpej 		return (0);
     99        1.6   thorpej 
    100        1.6   thorpej 	if (ia->ia_ndrq > 0 &&
    101       1.11  drochner 	    (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
    102        1.1  drochner 		return (0);
    103        1.1  drochner 
    104        1.1  drochner 	rv = 0;
    105       1.16      cube 	have_ppi = 0;
    106        1.1  drochner 
    107        1.1  drochner 	if (bus_space_map(ia->ia_iot, IO_PPI, 1, 0, &ppi_ioh))
    108        1.1  drochner 		goto lose;
    109        1.1  drochner 	have_ppi = 1;
    110        1.1  drochner 
    111        1.1  drochner 	/*
    112        1.1  drochner 	 * Check for existence of PPI.  Realistically, this is either going to
    113        1.1  drochner 	 * be here or nothing is going to be here.
    114        1.1  drochner 	 *
    115        1.1  drochner 	 * We don't want to have any chance of changing speaker output (which
    116        1.1  drochner 	 * this test might, if it crashes in the middle, or something;
    117        1.1  drochner 	 * normally it's be to quick to produce anthing audible), but
    118        1.1  drochner 	 * many "combo chip" mock-PPI's don't seem to support the top bit
    119        1.1  drochner 	 * of Port B as a settable bit.  The bottom bit has to be settable,
    120        1.1  drochner 	 * since the speaker driver hardware still uses it.
    121        1.1  drochner 	 */
    122        1.1  drochner 	v = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    123        1.1  drochner 	bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v ^ 0x01);	/* XXX */
    124        1.1  drochner 	nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0);		/* XXX */
    125        1.1  drochner 	if (((nv ^ v) & 0x01) == 0x01)
    126        1.1  drochner 		rv = 1;
    127        1.1  drochner 	bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v);		/* 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) != 0x00) {
    130        1.1  drochner 		rv = 0;
    131        1.1  drochner 		goto lose;
    132        1.1  drochner 	}
    133        1.1  drochner 
    134        1.1  drochner 	/*
    135        1.1  drochner 	 * We assume that the programmable interval timer is there.
    136        1.1  drochner 	 */
    137        1.1  drochner 
    138        1.1  drochner lose:
    139        1.1  drochner 	if (have_ppi)
    140        1.1  drochner 		bus_space_unmap(ia->ia_iot, ppi_ioh, 1);
    141        1.1  drochner 	if (rv) {
    142        1.6   thorpej 		ia->ia_io[0].ir_addr = IO_PPI;
    143        1.6   thorpej 		ia->ia_io[0].ir_size = 1;
    144        1.6   thorpej 		ia->ia_nio = 1;
    145        1.6   thorpej 
    146        1.6   thorpej 		ia->ia_niomem = 0;
    147        1.6   thorpej 		ia->ia_nirq = 0;
    148        1.6   thorpej 		ia->ia_ndrq = 0;
    149        1.1  drochner 	}
    150        1.1  drochner 	return (rv);
    151        1.1  drochner }
    152        1.1  drochner 
    153        1.1  drochner void
    154  1.17.22.2      yamt pcppi_isa_attach(struct device *parent, struct device *self, void *aux)
    155        1.1  drochner {
    156       1.15   xtraeme         struct pcppi_softc *sc = (struct pcppi_softc *)self;
    157       1.15   xtraeme         struct isa_attach_args *ia = aux;
    158       1.15   xtraeme         bus_space_tag_t iot;
    159       1.15   xtraeme 
    160       1.15   xtraeme         sc->sc_iot = iot = ia->ia_iot;
    161       1.15   xtraeme 
    162       1.16      cube         if (bus_space_map(iot, IO_PPI, 1, 0, &sc->sc_ppi_ioh))
    163       1.15   xtraeme                 panic("pcppi_attach: couldn't map");
    164        1.1  drochner 
    165       1.15   xtraeme         printf("\n");
    166        1.4   thorpej 
    167       1.15   xtraeme         pcppi_attach(sc);
    168       1.15   xtraeme }
    169        1.1  drochner 
    170       1.15   xtraeme void
    171       1.15   xtraeme pcppi_attach(struct pcppi_softc *sc)
    172       1.15   xtraeme {
    173       1.15   xtraeme         struct pcppi_attach_args pa;
    174        1.1  drochner 
    175       1.15   xtraeme         callout_init(&sc->sc_bell_ch);
    176        1.1  drochner 
    177       1.15   xtraeme         sc->sc_bellactive = sc->sc_bellpitch = sc->sc_slp = 0;
    178        1.1  drochner 
    179        1.2   thorpej #if NPCKBD > 0
    180        1.2   thorpej 	/* Provide a beeper for the PC Keyboard, if there isn't one already. */
    181        1.2   thorpej 	pckbd_hookup_bell(pcppi_pckbd_bell, sc);
    182        1.2   thorpej #endif
    183       1.16      cube #if NATTIMER > 0
    184       1.16      cube 	config_defer((struct device *)sc, pcppi_attach_speaker);
    185       1.16      cube #endif
    186        1.2   thorpej 
    187        1.1  drochner 	pa.pa_cookie = sc;
    188  1.17.22.2      yamt 	config_search_loc(pcppisearch, &sc->sc_dv, "pcppi", NULL, &pa);
    189  1.17.22.2      yamt }
    190  1.17.22.2      yamt 
    191  1.17.22.2      yamt static int
    192  1.17.22.2      yamt pcppisearch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    193  1.17.22.2      yamt {
    194  1.17.22.2      yamt 
    195  1.17.22.2      yamt 	if (config_match(parent, cf, aux))
    196  1.17.22.2      yamt 		config_attach_loc(parent, cf, locs, aux, NULL);
    197  1.17.22.2      yamt 
    198  1.17.22.2      yamt 	return 0;
    199        1.1  drochner }
    200        1.1  drochner 
    201       1.16      cube #if NATTIMER > 0
    202       1.16      cube static void
    203       1.16      cube pcppi_attach_speaker(struct device *self)
    204       1.16      cube {
    205       1.16      cube 	struct pcppi_softc *sc = (struct pcppi_softc *)self;
    206       1.16      cube 
    207       1.16      cube 	if ((sc->sc_timer = attimer_attach_speaker()) == NULL)
    208       1.16      cube 		aprint_error("%s: could not find any available timer\n",
    209       1.16      cube 		    sc->sc_dv.dv_xname);
    210       1.16      cube 	else
    211       1.16      cube 		aprint_normal("%s: attached to %s\n", sc->sc_dv.dv_xname,
    212       1.16      cube 		    sc->sc_timer->sc_dev.dv_xname);
    213       1.16      cube }
    214       1.16      cube #endif
    215       1.16      cube 
    216        1.1  drochner void
    217       1.14   xtraeme pcppi_bell(pcppi_tag_t self, int pitch, int period, int slp)
    218        1.1  drochner {
    219        1.1  drochner 	struct pcppi_softc *sc = self;
    220       1.16      cube 	int s;
    221        1.1  drochner 
    222       1.16      cube 	s = spltty(); /* ??? */
    223        1.1  drochner 	if (sc->sc_bellactive) {
    224        1.2   thorpej 		if (sc->sc_timeout) {
    225        1.2   thorpej 			sc->sc_timeout = 0;
    226        1.4   thorpej 			callout_stop(&sc->sc_bell_ch);
    227        1.2   thorpej 		}
    228        1.1  drochner 		if (sc->sc_slp)
    229        1.1  drochner 			wakeup(pcppi_bell_stop);
    230        1.1  drochner 	}
    231        1.1  drochner 	if (pitch == 0 || period == 0) {
    232        1.1  drochner 		pcppi_bell_stop(sc);
    233        1.1  drochner 		sc->sc_bellpitch = 0;
    234       1.16      cube 		splx(s);
    235        1.1  drochner 		return;
    236        1.1  drochner 	}
    237        1.1  drochner 	if (!sc->sc_bellactive || sc->sc_bellpitch != pitch) {
    238       1.16      cube #if NATTIMER > 0
    239       1.16      cube 		if (sc->sc_timer != NULL)
    240       1.16      cube 			attimer_set_pitch(sc->sc_timer, pitch);
    241       1.16      cube #endif
    242        1.1  drochner 		/* enable speaker */
    243        1.1  drochner 		bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    244        1.1  drochner 			bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    245        1.1  drochner 			| PIT_SPKR);
    246        1.1  drochner 	}
    247        1.1  drochner 	sc->sc_bellpitch = pitch;
    248        1.1  drochner 
    249        1.1  drochner 	sc->sc_bellactive = 1;
    250        1.2   thorpej 	if (slp & PCPPI_BELL_POLL) {
    251        1.2   thorpej 		delay((period * 1000000) / hz);
    252        1.2   thorpej 		pcppi_bell_stop(sc);
    253        1.2   thorpej 	} else {
    254        1.2   thorpej 		sc->sc_timeout = 1;
    255        1.4   thorpej 		callout_reset(&sc->sc_bell_ch, period, pcppi_bell_stop, sc);
    256        1.2   thorpej 		if (slp & PCPPI_BELL_SLEEP) {
    257        1.2   thorpej 			sc->sc_slp = 1;
    258        1.2   thorpej 			tsleep(pcppi_bell_stop, PCPPIPRI | PCATCH, "bell", 0);
    259        1.2   thorpej 			sc->sc_slp = 0;
    260        1.2   thorpej 		}
    261        1.1  drochner 	}
    262       1.16      cube 	splx(s);
    263        1.1  drochner }
    264        1.1  drochner 
    265        1.1  drochner static void
    266       1.14   xtraeme pcppi_bell_stop(void *arg)
    267        1.1  drochner {
    268        1.1  drochner 	struct pcppi_softc *sc = arg;
    269        1.1  drochner 	int s;
    270        1.1  drochner 
    271        1.1  drochner 	s = spltty(); /* ??? */
    272        1.2   thorpej 	sc->sc_timeout = 0;
    273        1.2   thorpej 
    274        1.1  drochner 	/* disable bell */
    275        1.1  drochner 	bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
    276        1.1  drochner 			  bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
    277        1.1  drochner 			  & ~PIT_SPKR);
    278        1.1  drochner 	sc->sc_bellactive = 0;
    279        1.1  drochner 	if (sc->sc_slp)
    280        1.1  drochner 		wakeup(pcppi_bell_stop);
    281        1.1  drochner 	splx(s);
    282        1.1  drochner }
    283        1.2   thorpej 
    284        1.2   thorpej #if NPCKBD > 0
    285        1.2   thorpej void
    286  1.17.22.2      yamt pcppi_pckbd_bell(void *arg, u_int pitch, u_int period, u_int volume,
    287  1.17.22.1      yamt     int poll)
    288        1.2   thorpej {
    289        1.2   thorpej 
    290        1.2   thorpej 	/*
    291        1.2   thorpej 	 * Comes in as ms, goes out at ticks; volume ignored.
    292        1.2   thorpej 	 */
    293        1.3   thorpej 	pcppi_bell(arg, pitch, (period * hz) / 1000,
    294        1.3   thorpej 	    poll ? PCPPI_BELL_POLL : 0);
    295        1.2   thorpej }
    296        1.2   thorpej #endif /* NPCKBD > 0 */
    297