1/*	$NetBSD: joy_ofisa.c,v 1.20 2021/01/27 03:10:21 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 1996, 1998, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: joy_ofisa.c,v 1.20 2021/01/27 03:10:21 thorpej Exp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/device.h>
39
40#include <sys/bus.h>
41
42#include <dev/ofw/openfirm.h>
43#include <dev/isa/isavar.h>
44#include <dev/ofisa/ofisavar.h>
45
46#include <dev/ic/joyvar.h>
47
48#define	JOY_NPORTS	1	/* XXX should be in a header file */
49
50struct joy_ofisa_softc {
51	struct joy_softc sc_joy;
52	kmutex_t sc_lock;
53};
54
55static int	joy_ofisa_match(device_t, cfdata_t, void *);
56static void	joy_ofisa_attach(device_t, device_t, void *);
57
58CFATTACH_DECL_NEW(joy_ofisa, sizeof(struct joy_ofisa_softc),
59    joy_ofisa_match, joy_ofisa_attach, NULL, NULL);
60
61static const struct device_compatible_entry compat_data[] = {
62	{ .compat = "pnpPNP,b02f" },	/* generic joystick */
63	DEVICE_COMPAT_EOL
64};
65
66static int
67joy_ofisa_match(device_t parent, cfdata_t match, void *aux)
68{
69	struct ofisa_attach_args *aa = aux;
70
71	return of_compatible_match(aa->oba.oba_phandle, compat_data);
72}
73
74static void
75joy_ofisa_attach(device_t parent, device_t self, void *aux)
76{
77	struct joy_ofisa_softc *osc = device_private(self);
78	struct joy_softc *sc = &osc->sc_joy;
79	struct ofisa_attach_args *aa = aux;
80	struct ofisa_reg_desc reg;
81	int n;
82
83	/*
84	 * We're living on an OFW.  We have to ask the OFW what our
85	 * register property looks like.
86	 *
87	 * We expect:
88	 *
89	 *	1 i/o register region
90	 */
91
92	n = ofisa_reg_get(aa->oba.oba_phandle, &reg, 1);
93	if (n != 1) {
94		aprint_error(": error getting register data\n");
95		return;
96	}
97	if (reg.type != OFISA_REG_TYPE_IO) {
98		aprint_error(": register type not i/o\n");
99		return;
100	}
101	if (reg.len != JOY_NPORTS) {
102		aprint_error(": weird register size (%lu, expected %d)\n",
103		    (unsigned long)reg.len, JOY_NPORTS);
104		return;
105	}
106
107	sc->sc_iot = aa->iot;
108	sc->sc_dev = self;
109
110	if (bus_space_map(sc->sc_iot, reg.addr, reg.len, 0, &sc->sc_ioh)) {
111		aprint_error(": unable to map register space\n");
112		return;
113	}
114
115	ofisa_print_model(NULL, aa->oba.oba_phandle);
116
117	mutex_init(&osc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
118	sc->sc_lock = &osc->sc_lock;
119
120	joyattach(sc);
121}
122