xref: /netbsd-src/sys/arch/news68k/dev/ms_hb.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: ms_hb.c,v 1.12 2008/03/29 05:48:33 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 Izumi Tsutsui.  All rights reserved.
5  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ms_hb.c,v 1.12 2008/03/29 05:48:33 tsutsui Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 
37 #include <dev/wscons/wsconsio.h>
38 #include <dev/wscons/wsmousevar.h>
39 
40 #include <machine/bus.h>
41 #include <machine/cpu.h>
42 
43 #include <news68k/dev/hbvar.h>
44 #include <news68k/dev/ms_hbreg.h>
45 #include <news68k/dev/msvar.h>
46 
47 #include <news68k/news68k/isr.h>
48 
49 #define MS_SIZE 0x10 /* XXX */
50 #define MS_IPL 5
51 
52 static int ms_hb_match(device_t, cfdata_t, void *);
53 static void ms_hb_attach(device_t, device_t, void *);
54 static void ms_hb_init(struct ms_softc *);
55 int ms_hb_intr(void *);
56 
57 static int ms_hb_enable(void *);
58 static int ms_hb_ioctl(void *, u_long, void *, int, struct lwp *);
59 static void ms_hb_disable(void *);
60 
61 CFATTACH_DECL_NEW(ms_hb, sizeof(struct ms_softc),
62     ms_hb_match, ms_hb_attach, NULL, NULL);
63 
64 struct wsmouse_accessops ms_hb_accessops = {
65 	ms_hb_enable,
66 	ms_hb_ioctl,
67 	ms_hb_disable
68 };
69 
70 static int
71 ms_hb_match(device_t parent, cfdata_t cf, void *aux)
72 {
73 	struct hb_attach_args *ha = aux;
74 	u_int addr;
75 
76 	if (strcmp(ha->ha_name, "ms"))
77 		return 0;
78 
79 	/* XXX no default address */
80 	if (ha->ha_address == (u_int)-1)
81 		return 0;
82 
83 	addr = IIOV(ha->ha_address); /* XXX */
84 
85 	if (badaddr((void *)addr, 1))
86 		return 0;
87 
88 	return 1;
89 }
90 
91 static void
92 ms_hb_attach(device_t parent, device_t self, void *aux)
93 {
94 	struct ms_softc *sc = device_private(self);
95 	struct hb_attach_args *ha = aux;
96 	bus_space_tag_t bt = ha->ha_bust;
97 	bus_space_handle_t bh;
98 	struct wsmousedev_attach_args wsa;
99 	int ipl;
100 
101 	sc->sc_dev = self;
102 	if (bus_space_map(bt, ha->ha_address, MS_SIZE, 0, &bh) != 0) {
103 		aprint_error(": can't map device space\n");
104 		return;
105 	}
106 
107 	aprint_normal("\n");
108 
109 	sc->sc_bt = bt;
110 	sc->sc_bh = bh;
111 	sc->sc_offset = MS_REG_DATA;
112 	ipl = ha->ha_ipl;
113 	if (ipl == -1)
114 		ipl = MS_IPL;
115 
116 	ms_hb_init(sc);
117 
118 	isrlink_autovec(ms_hb_intr, (void *)sc, ipl, IPL_TTY);
119 
120 	wsa.accessops = &ms_hb_accessops;
121 	wsa.accesscookie = sc;
122 	sc->sc_wsmousedev = config_found(self, &wsa, wsmousedevprint);
123 }
124 
125 static void
126 ms_hb_init(struct ms_softc *sc)
127 {
128 	bus_space_tag_t bt = sc->sc_bt;
129 	bus_space_handle_t bh = sc->sc_bh;
130 
131 	bus_space_write_1(bt, bh, MS_REG_RESET, 0);
132 	bus_space_write_1(bt, bh, MS_REG_INTE, MS_INTE);
133 }
134 
135 int
136 ms_hb_intr(void *v)
137 {
138 	struct ms_softc *sc = v;
139 	bus_space_tag_t bt = sc->sc_bt;
140 	bus_space_handle_t bh = sc->sc_bh;
141 	int handled = 0;
142 
143 	while ((bus_space_read_1(bt, bh, MS_REG_STAT) & MSSTAT_RDY) != 0) {
144 		ms_intr(sc);
145 		handled = 1;
146 	}
147 	if (handled)
148 		bus_space_write_1(bt, bh, MS_REG_INTE, MS_INTE);
149 
150 	return handled;
151 }
152 
153 static int
154 ms_hb_enable(void *v)
155 {
156 	struct ms_softc *sc = v;
157 	bus_space_tag_t bt = sc->sc_bt;
158 	bus_space_handle_t bh = sc->sc_bh;
159 
160 	bus_space_write_1(bt, bh, MS_REG_INTE, MS_INTE);
161 
162 	return 0;
163 }
164 
165 static void
166 ms_hb_disable(void *v)
167 {
168 	struct ms_softc *sc = v;
169 	bus_space_tag_t bt = sc->sc_bt;
170 	bus_space_handle_t bh = sc->sc_bh;
171 
172 	bus_space_write_1(bt, bh, MS_REG_INTE, 0);
173 }
174 
175 static int
176 ms_hb_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
177 {
178 
179 	return EPASSTHROUGH;
180 }
181