xref: /netbsd-src/sys/dev/ofisa/joy_ofisa.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /*	$NetBSD: joy_ofisa.c,v 1.20 2021/01/27 03:10:21 thorpej Exp $	*/
296aa1cecSthorpej 
396aa1cecSthorpej /*-
48a962f23Sjmcneill  * Copyright (c) 1996, 1998, 2008 The NetBSD Foundation, Inc.
596aa1cecSthorpej  * All rights reserved.
696aa1cecSthorpej  *
796aa1cecSthorpej  * This code is derived from software contributed to The NetBSD Foundation
896aa1cecSthorpej  * by Jason R. Thorpe.
996aa1cecSthorpej  *
1096aa1cecSthorpej  * Redistribution and use in source and binary forms, with or without
1196aa1cecSthorpej  * modification, are permitted provided that the following conditions
1296aa1cecSthorpej  * are met:
1396aa1cecSthorpej  * 1. Redistributions of source code must retain the above copyright
1496aa1cecSthorpej  *    notice, this list of conditions and the following disclaimer.
1596aa1cecSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1696aa1cecSthorpej  *    notice, this list of conditions and the following disclaimer in the
1796aa1cecSthorpej  *    documentation and/or other materials provided with the distribution.
1896aa1cecSthorpej  *
1996aa1cecSthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2096aa1cecSthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2196aa1cecSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2296aa1cecSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2396aa1cecSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2496aa1cecSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2596aa1cecSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2696aa1cecSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2796aa1cecSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2896aa1cecSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2996aa1cecSthorpej  * POSSIBILITY OF SUCH DAMAGE.
3096aa1cecSthorpej  */
3196aa1cecSthorpej 
32ab5d9d2bSlukem #include <sys/cdefs.h>
33*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: joy_ofisa.c,v 1.20 2021/01/27 03:10:21 thorpej Exp $");
34ab5d9d2bSlukem 
3596aa1cecSthorpej #include <sys/param.h>
3696aa1cecSthorpej #include <sys/systm.h>
3796aa1cecSthorpej #include <sys/kernel.h>
3896aa1cecSthorpej #include <sys/device.h>
3996aa1cecSthorpej 
40a2a38285Sad #include <sys/bus.h>
4196aa1cecSthorpej 
4296aa1cecSthorpej #include <dev/ofw/openfirm.h>
4396aa1cecSthorpej #include <dev/isa/isavar.h>
4496aa1cecSthorpej #include <dev/ofisa/ofisavar.h>
4596aa1cecSthorpej 
46d70864a8Spooka #include <dev/ic/joyvar.h>
4796aa1cecSthorpej 
4896aa1cecSthorpej #define	JOY_NPORTS	1	/* XXX should be in a header file */
4996aa1cecSthorpej 
508a962f23Sjmcneill struct joy_ofisa_softc {
518a962f23Sjmcneill 	struct joy_softc sc_joy;
528a962f23Sjmcneill 	kmutex_t sc_lock;
538a962f23Sjmcneill };
548a962f23Sjmcneill 
55701c2ae0Sxtraeme static int	joy_ofisa_match(device_t, cfdata_t, void *);
56701c2ae0Sxtraeme static void	joy_ofisa_attach(device_t, device_t, void *);
5796aa1cecSthorpej 
588a962f23Sjmcneill CFATTACH_DECL_NEW(joy_ofisa, sizeof(struct joy_ofisa_softc),
59c9b3657cSthorpej     joy_ofisa_match, joy_ofisa_attach, NULL, NULL);
6096aa1cecSthorpej 
611ebde598Sthorpej static const struct device_compatible_entry compat_data[] = {
621ebde598Sthorpej 	{ .compat = "pnpPNP,b02f" },	/* generic joystick */
6318f3098cSthorpej 	DEVICE_COMPAT_EOL
641ebde598Sthorpej };
651ebde598Sthorpej 
66701c2ae0Sxtraeme static int
joy_ofisa_match(device_t parent,cfdata_t match,void * aux)67701c2ae0Sxtraeme joy_ofisa_match(device_t parent, cfdata_t match, void *aux)
6896aa1cecSthorpej {
6996aa1cecSthorpej 	struct ofisa_attach_args *aa = aux;
7096aa1cecSthorpej 
71*6e54367aSthorpej 	return of_compatible_match(aa->oba.oba_phandle, compat_data);
7296aa1cecSthorpej }
7396aa1cecSthorpej 
74701c2ae0Sxtraeme static void
joy_ofisa_attach(device_t parent,device_t self,void * aux)75701c2ae0Sxtraeme joy_ofisa_attach(device_t parent, device_t self, void *aux)
7696aa1cecSthorpej {
778a962f23Sjmcneill 	struct joy_ofisa_softc *osc = device_private(self);
788a962f23Sjmcneill 	struct joy_softc *sc = &osc->sc_joy;
7996aa1cecSthorpej 	struct ofisa_attach_args *aa = aux;
8096aa1cecSthorpej 	struct ofisa_reg_desc reg;
8196aa1cecSthorpej 	int n;
8296aa1cecSthorpej 
8396aa1cecSthorpej 	/*
8496aa1cecSthorpej 	 * We're living on an OFW.  We have to ask the OFW what our
8596aa1cecSthorpej 	 * register property looks like.
8696aa1cecSthorpej 	 *
8796aa1cecSthorpej 	 * We expect:
8896aa1cecSthorpej 	 *
8996aa1cecSthorpej 	 *	1 i/o register region
9096aa1cecSthorpej 	 */
9196aa1cecSthorpej 
9296aa1cecSthorpej 	n = ofisa_reg_get(aa->oba.oba_phandle, &reg, 1);
9396aa1cecSthorpej 	if (n != 1) {
94701c2ae0Sxtraeme 		aprint_error(": error getting register data\n");
9596aa1cecSthorpej 		return;
9696aa1cecSthorpej 	}
9796aa1cecSthorpej 	if (reg.type != OFISA_REG_TYPE_IO) {
98701c2ae0Sxtraeme 		aprint_error(": register type not i/o\n");
9996aa1cecSthorpej 		return;
10096aa1cecSthorpej 	}
10196aa1cecSthorpej 	if (reg.len != JOY_NPORTS) {
102701c2ae0Sxtraeme 		aprint_error(": weird register size (%lu, expected %d)\n",
10396aa1cecSthorpej 		    (unsigned long)reg.len, JOY_NPORTS);
10496aa1cecSthorpej 		return;
10596aa1cecSthorpej 	}
10696aa1cecSthorpej 
10796aa1cecSthorpej 	sc->sc_iot = aa->iot;
108701c2ae0Sxtraeme 	sc->sc_dev = self;
10996aa1cecSthorpej 
11096aa1cecSthorpej 	if (bus_space_map(sc->sc_iot, reg.addr, reg.len, 0, &sc->sc_ioh)) {
111701c2ae0Sxtraeme 		aprint_error(": unable to map register space\n");
11296aa1cecSthorpej 		return;
11396aa1cecSthorpej 	}
11496aa1cecSthorpej 
115c035549eSchristos 	ofisa_print_model(NULL, aa->oba.oba_phandle);
11696aa1cecSthorpej 
1178a962f23Sjmcneill 	mutex_init(&osc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
1188a962f23Sjmcneill 	sc->sc_lock = &osc->sc_lock;
1198a962f23Sjmcneill 
12096aa1cecSthorpej 	joyattach(sc);
12196aa1cecSthorpej }
122