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