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