1*c7fb772bSthorpej /* $NetBSD: ms_hb.c,v 1.16 2021/08/07 16:19:00 thorpej Exp $ */
208f4daf2Stsutsui
308f4daf2Stsutsui /*-
408f4daf2Stsutsui * Copyright (c) 2001 Izumi Tsutsui. All rights reserved.
5b87210faStsutsui *
6b87210faStsutsui * Redistribution and use in source and binary forms, with or without
7b87210faStsutsui * modification, are permitted provided that the following conditions
8b87210faStsutsui * are met:
9b87210faStsutsui * 1. Redistributions of source code must retain the above copyright
10b87210faStsutsui * notice, this list of conditions and the following disclaimer.
11b87210faStsutsui * 2. Redistributions in binary form must reproduce the above copyright
12b87210faStsutsui * notice, this list of conditions and the following disclaimer in the
13b87210faStsutsui * documentation and/or other materials provided with the distribution.
14b87210faStsutsui *
15b87210faStsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16b87210faStsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17b87210faStsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18b87210faStsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19b87210faStsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20b87210faStsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21b87210faStsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22b87210faStsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23b87210faStsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24b87210faStsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25b87210faStsutsui */
26b87210faStsutsui
27b87210faStsutsui /*-
2808f4daf2Stsutsui * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
2908f4daf2Stsutsui *
3008f4daf2Stsutsui * Redistribution and use in source and binary forms, with or without
3108f4daf2Stsutsui * modification, are permitted provided that the following conditions
3208f4daf2Stsutsui * are met:
3308f4daf2Stsutsui * 1. Redistributions of source code must retain the above copyright
3408f4daf2Stsutsui * notice, this list of conditions and the following disclaimer.
3508f4daf2Stsutsui * 2. Redistributions in binary form must reproduce the above copyright
3608f4daf2Stsutsui * notice, this list of conditions and the following disclaimer in the
3708f4daf2Stsutsui * documentation and/or other materials provided with the distribution.
3808f4daf2Stsutsui * 3. The name of the author may not be used to endorse or promote products
3908f4daf2Stsutsui * derived from this software without specific prior written permission.
4008f4daf2Stsutsui *
4108f4daf2Stsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4208f4daf2Stsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4308f4daf2Stsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4408f4daf2Stsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4508f4daf2Stsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4608f4daf2Stsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4708f4daf2Stsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4808f4daf2Stsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4908f4daf2Stsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
5008f4daf2Stsutsui * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5108f4daf2Stsutsui */
5208f4daf2Stsutsui
53ed517291Slukem #include <sys/cdefs.h>
54*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: ms_hb.c,v 1.16 2021/08/07 16:19:00 thorpej Exp $");
55ed517291Slukem
5608f4daf2Stsutsui #include <sys/param.h>
5708f4daf2Stsutsui #include <sys/device.h>
5808f4daf2Stsutsui #include <sys/systm.h>
5908f4daf2Stsutsui
6008f4daf2Stsutsui #include <dev/wscons/wsconsio.h>
6108f4daf2Stsutsui #include <dev/wscons/wsmousevar.h>
6208f4daf2Stsutsui
6308f4daf2Stsutsui #include <machine/bus.h>
6408f4daf2Stsutsui #include <machine/cpu.h>
6508f4daf2Stsutsui
6608f4daf2Stsutsui #include <news68k/dev/hbvar.h>
6708f4daf2Stsutsui #include <news68k/dev/ms_hbreg.h>
6808f4daf2Stsutsui #include <news68k/dev/msvar.h>
6908f4daf2Stsutsui
7008f4daf2Stsutsui #include <news68k/news68k/isr.h>
7108f4daf2Stsutsui
7208f4daf2Stsutsui #define MS_SIZE 0x10 /* XXX */
7308f4daf2Stsutsui #define MS_IPL 5
7408f4daf2Stsutsui
75820a544dStsutsui static int ms_hb_match(device_t, cfdata_t, void *);
76820a544dStsutsui static void ms_hb_attach(device_t, device_t, void *);
7752b46dcfStsutsui static void ms_hb_init(struct ms_softc *);
7808f4daf2Stsutsui int ms_hb_intr(void *);
7908f4daf2Stsutsui
8052b46dcfStsutsui static int ms_hb_enable(void *);
8153524e44Schristos static int ms_hb_ioctl(void *, u_long, void *, int, struct lwp *);
8252b46dcfStsutsui static void ms_hb_disable(void *);
8308f4daf2Stsutsui
84820a544dStsutsui CFATTACH_DECL_NEW(ms_hb, sizeof(struct ms_softc),
85021b694dSthorpej ms_hb_match, ms_hb_attach, NULL, NULL);
8608f4daf2Stsutsui
8708f4daf2Stsutsui struct wsmouse_accessops ms_hb_accessops = {
8808f4daf2Stsutsui ms_hb_enable,
8908f4daf2Stsutsui ms_hb_ioctl,
9008f4daf2Stsutsui ms_hb_disable
9108f4daf2Stsutsui };
9208f4daf2Stsutsui
9352b46dcfStsutsui static int
ms_hb_match(device_t parent,cfdata_t cf,void * aux)94820a544dStsutsui ms_hb_match(device_t parent, cfdata_t cf, void *aux)
9508f4daf2Stsutsui {
9608f4daf2Stsutsui struct hb_attach_args *ha = aux;
9708f4daf2Stsutsui u_int addr;
9808f4daf2Stsutsui
9908f4daf2Stsutsui if (strcmp(ha->ha_name, "ms"))
10008f4daf2Stsutsui return 0;
10108f4daf2Stsutsui
10208f4daf2Stsutsui /* XXX no default address */
1035db9bf06Stsutsui if (ha->ha_address == (u_int)-1)
10408f4daf2Stsutsui return 0;
10508f4daf2Stsutsui
10643993578Stsutsui addr = ha->ha_address; /* XXX */
10708f4daf2Stsutsui
10808f4daf2Stsutsui if (badaddr((void *)addr, 1))
10908f4daf2Stsutsui return 0;
11008f4daf2Stsutsui
11108f4daf2Stsutsui return 1;
11208f4daf2Stsutsui }
11308f4daf2Stsutsui
11452b46dcfStsutsui static void
ms_hb_attach(device_t parent,device_t self,void * aux)115820a544dStsutsui ms_hb_attach(device_t parent, device_t self, void *aux)
11608f4daf2Stsutsui {
117820a544dStsutsui struct ms_softc *sc = device_private(self);
11808f4daf2Stsutsui struct hb_attach_args *ha = aux;
11908f4daf2Stsutsui bus_space_tag_t bt = ha->ha_bust;
12008f4daf2Stsutsui bus_space_handle_t bh;
12108f4daf2Stsutsui struct wsmousedev_attach_args wsa;
12208f4daf2Stsutsui int ipl;
12308f4daf2Stsutsui
124d062717cStsutsui sc->sc_dev = self;
12508f4daf2Stsutsui if (bus_space_map(bt, ha->ha_address, MS_SIZE, 0, &bh) != 0) {
126820a544dStsutsui aprint_error(": can't map device space\n");
12708f4daf2Stsutsui return;
12808f4daf2Stsutsui }
12908f4daf2Stsutsui
130820a544dStsutsui aprint_normal("\n");
13108f4daf2Stsutsui
13208f4daf2Stsutsui sc->sc_bt = bt;
13308f4daf2Stsutsui sc->sc_bh = bh;
13408f4daf2Stsutsui sc->sc_offset = MS_REG_DATA;
13508f4daf2Stsutsui ipl = ha->ha_ipl;
13608f4daf2Stsutsui if (ipl == -1)
13708f4daf2Stsutsui ipl = MS_IPL;
13808f4daf2Stsutsui
13908f4daf2Stsutsui ms_hb_init(sc);
14008f4daf2Stsutsui
1417d0e6302Stsutsui isrlink_autovec(ms_hb_intr, (void *)sc, ipl, IPL_TTY);
14208f4daf2Stsutsui
14308f4daf2Stsutsui wsa.accessops = &ms_hb_accessops;
14408f4daf2Stsutsui wsa.accesscookie = sc;
1452685996bSthorpej sc->sc_wsmousedev = config_found(self, &wsa, wsmousedevprint,
146*c7fb772bSthorpej CFARGS_NONE);
14708f4daf2Stsutsui }
14808f4daf2Stsutsui
14952b46dcfStsutsui static void
ms_hb_init(struct ms_softc * sc)15052b46dcfStsutsui ms_hb_init(struct ms_softc *sc)
15108f4daf2Stsutsui {
15208f4daf2Stsutsui bus_space_tag_t bt = sc->sc_bt;
15308f4daf2Stsutsui bus_space_handle_t bh = sc->sc_bh;
15408f4daf2Stsutsui
15508f4daf2Stsutsui bus_space_write_1(bt, bh, MS_REG_RESET, 0);
15608f4daf2Stsutsui bus_space_write_1(bt, bh, MS_REG_INTE, MS_INTE);
15708f4daf2Stsutsui }
15808f4daf2Stsutsui
15908f4daf2Stsutsui int
ms_hb_intr(void * v)16052b46dcfStsutsui ms_hb_intr(void *v)
16108f4daf2Stsutsui {
16208f4daf2Stsutsui struct ms_softc *sc = v;
16308f4daf2Stsutsui bus_space_tag_t bt = sc->sc_bt;
16408f4daf2Stsutsui bus_space_handle_t bh = sc->sc_bh;
16508f4daf2Stsutsui int handled = 0;
16608f4daf2Stsutsui
16708f4daf2Stsutsui while ((bus_space_read_1(bt, bh, MS_REG_STAT) & MSSTAT_RDY) != 0) {
16808f4daf2Stsutsui ms_intr(sc);
16908f4daf2Stsutsui handled = 1;
17008f4daf2Stsutsui }
17108f4daf2Stsutsui if (handled)
17208f4daf2Stsutsui bus_space_write_1(bt, bh, MS_REG_INTE, MS_INTE);
17308f4daf2Stsutsui
17408f4daf2Stsutsui return handled;
17508f4daf2Stsutsui }
17608f4daf2Stsutsui
17752b46dcfStsutsui static int
ms_hb_enable(void * v)17852b46dcfStsutsui ms_hb_enable(void *v)
17908f4daf2Stsutsui {
18008f4daf2Stsutsui struct ms_softc *sc = v;
18108f4daf2Stsutsui bus_space_tag_t bt = sc->sc_bt;
18208f4daf2Stsutsui bus_space_handle_t bh = sc->sc_bh;
18308f4daf2Stsutsui
18408f4daf2Stsutsui bus_space_write_1(bt, bh, MS_REG_INTE, MS_INTE);
18508f4daf2Stsutsui
18608f4daf2Stsutsui return 0;
18708f4daf2Stsutsui }
18808f4daf2Stsutsui
18952b46dcfStsutsui static void
ms_hb_disable(void * v)19052b46dcfStsutsui ms_hb_disable(void *v)
19108f4daf2Stsutsui {
19208f4daf2Stsutsui struct ms_softc *sc = v;
19308f4daf2Stsutsui bus_space_tag_t bt = sc->sc_bt;
19408f4daf2Stsutsui bus_space_handle_t bh = sc->sc_bh;
19508f4daf2Stsutsui
19608f4daf2Stsutsui bus_space_write_1(bt, bh, MS_REG_INTE, 0);
19708f4daf2Stsutsui }
19808f4daf2Stsutsui
19952b46dcfStsutsui static int
ms_hb_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)20053524e44Schristos ms_hb_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
20108f4daf2Stsutsui {
20252b46dcfStsutsui
20331144d99Satatat return EPASSTHROUGH;
20408f4daf2Stsutsui }
205