1*c7fb772bSthorpej /* $NetBSD: kb_kbc.c,v 1.12 2021/08/07 16:19:00 thorpej Exp $ */
208f4daf2Stsutsui
3b87210faStsutsui /*-
4b87210faStsutsui * Copyright (c) 2001 Izumi Tsutsui. All rights reserved.
508f4daf2Stsutsui *
608f4daf2Stsutsui * Redistribution and use in source and binary forms, with or without
708f4daf2Stsutsui * modification, are permitted provided that the following conditions
808f4daf2Stsutsui * are met:
908f4daf2Stsutsui * 1. Redistributions of source code must retain the above copyright
1008f4daf2Stsutsui * notice, this list of conditions and the following disclaimer.
1108f4daf2Stsutsui * 2. Redistributions in binary form must reproduce the above copyright
1208f4daf2Stsutsui * notice, this list of conditions and the following disclaimer in the
1308f4daf2Stsutsui * documentation and/or other materials provided with the distribution.
1408f4daf2Stsutsui *
1508f4daf2Stsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1608f4daf2Stsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1708f4daf2Stsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1808f4daf2Stsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1908f4daf2Stsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2008f4daf2Stsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2108f4daf2Stsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2208f4daf2Stsutsui * 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.
2508f4daf2Stsutsui */
2608f4daf2Stsutsui
27ed517291Slukem #include <sys/cdefs.h>
28*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: kb_kbc.c,v 1.12 2021/08/07 16:19:00 thorpej Exp $");
29ed517291Slukem
3008f4daf2Stsutsui #include <sys/param.h>
3108f4daf2Stsutsui #include <sys/systm.h>
3208f4daf2Stsutsui #include <sys/device.h>
3308f4daf2Stsutsui
3408f4daf2Stsutsui #include <machine/bus.h>
3508f4daf2Stsutsui #include <machine/cpu.h>
3608f4daf2Stsutsui
3708f4daf2Stsutsui #include <dev/wscons/wsconsio.h>
3808f4daf2Stsutsui #include <dev/wscons/wskbdvar.h>
3908f4daf2Stsutsui #include <dev/wscons/wsksymdef.h>
4008f4daf2Stsutsui #include <dev/wscons/wsksymvar.h>
4108f4daf2Stsutsui
4208f4daf2Stsutsui
4308f4daf2Stsutsui #include <news68k/dev/kbcreg.h>
4408f4daf2Stsutsui #include <news68k/dev/kbcvar.h>
4508f4daf2Stsutsui #include <news68k/dev/kbvar.h>
4608f4daf2Stsutsui
4708f4daf2Stsutsui #include <news68k/news68k/isr.h>
4808f4daf2Stsutsui
49820a544dStsutsui static int kb_kbc_match(device_t, cfdata_t, void *);
50820a544dStsutsui static void kb_kbc_attach(device_t, device_t, void *);
5152b46dcfStsutsui static void kb_kbc_init(struct kb_softc *);
5208f4daf2Stsutsui int kb_kbc_intr(void *);
5308f4daf2Stsutsui int kb_kbc_cnattach(void);
5408f4daf2Stsutsui
55820a544dStsutsui CFATTACH_DECL_NEW(kb_kbc, sizeof(struct kb_softc),
56021b694dSthorpej kb_kbc_match, kb_kbc_attach, NULL, NULL);
5708f4daf2Stsutsui
5808f4daf2Stsutsui struct console_softc kb_kbc_conssc;
5908f4daf2Stsutsui
6052b46dcfStsutsui static int
kb_kbc_match(device_t parent,cfdata_t cf,void * aux)61820a544dStsutsui kb_kbc_match(device_t parent, cfdata_t cf, void *aux)
6208f4daf2Stsutsui {
6308f4daf2Stsutsui struct kbc_attach_args *ka = aux;
6408f4daf2Stsutsui
6508f4daf2Stsutsui if (strcmp(ka->ka_name, "kb"))
6608f4daf2Stsutsui return 0;
6708f4daf2Stsutsui
6808f4daf2Stsutsui return 1;
6908f4daf2Stsutsui }
7008f4daf2Stsutsui
7152b46dcfStsutsui static void
kb_kbc_attach(device_t parent,device_t self,void * aux)72820a544dStsutsui kb_kbc_attach(device_t parent, device_t self, void *aux)
7308f4daf2Stsutsui {
74820a544dStsutsui struct kb_softc *sc = device_private(self);
7508f4daf2Stsutsui struct kbc_attach_args *ka = aux;
7608f4daf2Stsutsui struct wskbddev_attach_args wsa;
7708f4daf2Stsutsui int ipl;
7808f4daf2Stsutsui
79d062717cStsutsui sc->sc_dev = self;
80820a544dStsutsui aprint_normal("\n");
8108f4daf2Stsutsui
8208f4daf2Stsutsui sc->sc_bt = ka->ka_bt;
8308f4daf2Stsutsui sc->sc_bh = ka->ka_bh;
8408f4daf2Stsutsui sc->sc_offset = KBC_KBREG_DATA;
8508f4daf2Stsutsui sc->sc_conssc = &kb_kbc_conssc;
8608f4daf2Stsutsui ipl = ka->ka_ipl;
8708f4daf2Stsutsui
8808f4daf2Stsutsui kb_kbc_init(sc);
8908f4daf2Stsutsui
907d0e6302Stsutsui isrlink_autovec(kb_kbc_intr, (void *)sc, ipl, IPL_TTY);
9108f4daf2Stsutsui
9208f4daf2Stsutsui wsa.console = kb_kbc_conssc.cs_isconsole;
9308f4daf2Stsutsui wsa.keymap = &kb_keymapdata;
9408f4daf2Stsutsui wsa.accessops = &kb_accessops;
9508f4daf2Stsutsui wsa.accesscookie = sc;
9608f4daf2Stsutsui
97*c7fb772bSthorpej sc->sc_wskbddev = config_found(self, &wsa, wskbddevprint, CFARGS_NONE);
9808f4daf2Stsutsui }
9908f4daf2Stsutsui
10052b46dcfStsutsui static void
kb_kbc_init(struct kb_softc * sc)10152b46dcfStsutsui kb_kbc_init(struct kb_softc *sc)
10208f4daf2Stsutsui {
10308f4daf2Stsutsui bus_space_tag_t bt = sc->sc_bt;
10408f4daf2Stsutsui bus_space_handle_t bh = sc->sc_bh;
10508f4daf2Stsutsui
10608f4daf2Stsutsui bus_space_write_1(bt, bh, KBC_KBREG_RESET, 0);
10708f4daf2Stsutsui bus_space_write_1(bt, bh, KBC_KBREG_INTE, KBC_INTE);
10808f4daf2Stsutsui }
10908f4daf2Stsutsui
11008f4daf2Stsutsui int
kb_kbc_intr(void * arg)11152b46dcfStsutsui kb_kbc_intr(void *arg)
11208f4daf2Stsutsui {
11308f4daf2Stsutsui struct kb_softc *sc = (struct kb_softc *)arg;
11408f4daf2Stsutsui struct console_softc *kb_conssc = sc->sc_conssc;
11508f4daf2Stsutsui bus_space_tag_t bt = sc->sc_bt;
11608f4daf2Stsutsui bus_space_handle_t bh = sc->sc_bh;
11708f4daf2Stsutsui int stat, handled = 0;
11808f4daf2Stsutsui
11908f4daf2Stsutsui kb_conssc->cs_nintr++;
12008f4daf2Stsutsui
12108f4daf2Stsutsui stat = bus_space_read_1(bt, bh, KBC_KBREG_STAT);
12208f4daf2Stsutsui if (stat & KBCSTAT_KBRDY) {
12308f4daf2Stsutsui kb_intr(sc);
12408f4daf2Stsutsui handled = 1;
12508f4daf2Stsutsui bus_space_write_1(bt, bh, KBC_KBREG_INTE, KBC_INTE);
12608f4daf2Stsutsui }
12708f4daf2Stsutsui
12808f4daf2Stsutsui return handled;
12908f4daf2Stsutsui }
13008f4daf2Stsutsui
13108f4daf2Stsutsui int
kb_kbc_cnattach(void)13252b46dcfStsutsui kb_kbc_cnattach(void)
13308f4daf2Stsutsui {
13408f4daf2Stsutsui
13508f4daf2Stsutsui kb_kbc_conssc.cs_isconsole = 1;
13608f4daf2Stsutsui return kb_cnattach(&kb_kbc_conssc);
13708f4daf2Stsutsui }
138